双足机器人简单步态生成
?讓機(jī)器人行走最簡單的方法是先得到一組步態(tài)曲線,即腿部每個關(guān)節(jié)隨時間運(yùn)動的角度值。可以在ADAMS或3D Max、Blender等軟件中建立好機(jī)構(gòu)/骨骼模型,設(shè)計出腳踝和髖關(guān)節(jié)的運(yùn)動曲線,然后進(jìn)行逆運(yùn)動學(xué)解算,測量每個關(guān)節(jié)在運(yùn)動過程中的轉(zhuǎn)角,最后將得到的曲線導(dǎo)出。拿到曲線數(shù)據(jù)后我們就可以用單片機(jī)讀取,然后發(fā)送給機(jī)器人的舵機(jī)去執(zhí)行運(yùn)行。這種方法的缺點是機(jī)器人只能按照固定的步態(tài)行走,不夠靈活,比如抬腳高度、步長等參數(shù)都是定死的,如果需要修改還得再使用別的軟件導(dǎo)出新的步態(tài)數(shù)據(jù)。
最簡單的腿部結(jié)構(gòu)如下圖所示,在髖關(guān)節(jié)、膝關(guān)節(jié)和踝關(guān)節(jié)各有一個轉(zhuǎn)動自由度,可以通過三角形余弦定理求得機(jī)構(gòu)的運(yùn)動學(xué)逆解。這種機(jī)器人只能在矢狀面上直線前進(jìn),不能轉(zhuǎn)彎。對行走過程進(jìn)行一定的簡化和假設(shè):
1. 髖關(guān)節(jié)始終保持恒定的高度(實際上會有微小的波動)
2. 機(jī)器人腳面始終平行于地面
為了確定每個關(guān)節(jié)的角度,需要設(shè)計行走過程中踝關(guān)節(jié)點的運(yùn)動軌跡。這里采用簡單的正弦曲線作為其軌跡(也可以采用樣條曲線、Bézier 曲線等),正弦曲線的幅值對應(yīng)抬腳最大高度。
?
在Python中實現(xiàn)導(dǎo)入雙腿模型文件,生成指定的步態(tài)數(shù)據(jù)后讓其循環(huán)運(yùn)動,就可以模擬機(jī)器人行走。代碼如下(很糙...只是實現(xiàn)了基本功能,細(xì)節(jié)還有待完善)
#!/usr/bin/env python import vtk import math from vtk.util.colors import * import numpy as np import timeactor = list() # the list of links filenames = ["link-1.stl","link-2.stl","link-3.stl","link-4.stl","link-5.stl","link-6.stl"] renWin = vtk.vtkRenderWindow()joint1 = vtk.vtkAssembly() joint2 = vtk.vtkAssembly() joint3 = vtk.vtkAssembly() joint4 = vtk.vtkAssembly() joint5 = vtk.vtkAssembly() joint6 = vtk.vtkAssembly()ThighLength = 100.0 ShankLength = 100.0 HipHeight = 180.0 FootLift = 10 StrideLength = 60 Subdivision = 20leg_joint = np.zeros(3, dtype=np.float) patterns = np.zeros((2*Subdivision ,6), dtype=np.float) _p = 0txt = vtk.vtkTextActor() distance = 0.0def Rad2Deg(rad):return rad * 180.0 / math.pidef FootHeight(x): return (HipHeight - FootLift * math.cos(abs(x * math.pi / StrideLength)))def LegIK(x, y):global leg_jointdist = math.sqrt(x**2 + y**2)leg_joint[0] = math.acos(dist / (2 * ShankLength)) + math.atan2(x, y)leg_joint[1] = math.pi - math.acos((ThighLength**2 + ShankLength**2 - dist**2) / (2 * ThighLength* ShankLength))leg_joint[2] = leg_joint[1] - leg_joint[0]def GenerateGait():global leg_jointglobal patterns# Move left leg forward. for i in range(Subdivision):x = (i - Subdivision/2) * (StrideLength / Subdivision)y = FootHeight(x) LegIK(x, y) patterns[i, :3] = Rad2Deg(leg_joint)# Move left leg backward. for i in range(Subdivision):x = (Subdivision/2 - i) * (StrideLength / Subdivision)y = HipHeightLegIK(x, y) patterns[i+Subdivision, :3] = Rad2Deg(leg_joint) # Build right leg from phase shift clone of left. for i in range(2*Subdivision):patterns[i, 3:] = -patterns[(i + Subdivision) % (2*Subdivision), :3]# Customize vtkInteractorStyleTrackballCamera class MyInteractor(vtk.vtkInteractorStyleTrackballCamera):def __init__(self,parent=None):self.AddObserver("CharEvent",self.OnCharEvent)self.AddObserver("KeyPressEvent",self.OnKeyPressEvent)def OnCharEvent(self,obj,event):passdef OnKeyPressEvent(self,obj,event):global _pglobal distance# Get the compound key strokes for the eventkey = self.GetInteractor().GetKeySym()GenerateGait()if(key == "Return"):# start animationjoint1.SetPosition(0, 0, HipHeight-ThighLength-ShankLength)joint4.SetPosition(0, 0, HipHeight-ThighLength-ShankLength)if (_p == 2*Subdivision):_p = 0joint1.SetOrientation(0, -patterns[_p][0], 0)joint2.SetOrientation(0, patterns[_p][1], 0)joint3.SetOrientation(0, -patterns[_p][2], 0)joint4.SetOrientation(0, patterns[_p][3], 0)joint5.SetOrientation(0, -patterns[_p][4], 0)joint6.SetOrientation(0, patterns[_p][5], 0)_p = _p + 1distance = distance + StrideLength/(2 * Subdivision * 1000.0)txt.SetInput("Distance: " + str(distance) + "m")renWin.Render()returndef CreateCoordinates():# create coordinate axes in the render windowaxes = vtk.vtkAxesActor() axes.SetTotalLength(40, 40, 40) # Set the total length of the axes in 3 dimensions # Set the type of the shaft to a cylinder:0, line:1, or user defined geometry. axes.SetShaftType(0) transform = vtk.vtkTransform() transform.Translate(0.0, 0.0, 200.0)axes.SetUserTransform(transform)axes.SetCylinderRadius(0.02) axes.GetXAxisCaptionActor2D().SetWidth(0.03) axes.GetYAxisCaptionActor2D().SetWidth(0.03) axes.GetZAxisCaptionActor2D().SetWidth(0.03) return axesdef CreateGround():# create plane sourceplane = vtk.vtkPlaneSource()plane.SetXResolution(20)plane.SetYResolution(20)plane.SetCenter(0,0,0)plane.SetNormal(0,0,1)# mappermapper = vtk.vtkPolyDataMapper()mapper.SetInputConnection(plane.GetOutputPort())# actoractor = vtk.vtkActor()actor.SetMapper(mapper)actor.GetProperty().SetRepresentationToWireframe()actor.GetProperty().SetColor(light_grey)transform = vtk.vtkTransform()transform.Scale(400,400,1)actor.SetUserTransform(transform)return actordef LoadSTL(filename):reader = vtk.vtkSTLReader()reader.SetFileName(filename)mapper = vtk.vtkPolyDataMapper() # maps polygonal data to graphics primitives mapper.SetInputConnection(reader.GetOutputPort())actor = vtk.vtkLODActor() actor.SetMapper(mapper)return actor # represents an entity in a rendered scenedef CreateScene():# Create a rendering window and rendererren = vtk.vtkRenderer()renWin.AddRenderer(ren)# Create a renderwindowinteractoriren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)style = MyInteractor()style.SetDefaultRenderer(ren)iren.SetInteractorStyle(style)for id, file in enumerate(filenames):actor.append(LoadSTL(file))r = vtk.vtkMath.Random(.4, 1.0)g = vtk.vtkMath.Random(.4, 1.0)b = vtk.vtkMath.Random(.4, 1.0)actor[id].GetProperty().SetDiffuseColor(r, g, b)actor[id].GetProperty().SetDiffuse(.8)actor[id].GetProperty().SetSpecular(.5)actor[id].GetProperty().SetSpecularColor(1.0,1.0,1.0)actor[id].GetProperty().SetSpecularPower(30.0)joint1.AddPart(actor[0])joint1.AddPart(joint2)joint2.AddPart(actor[1])joint2.AddPart(joint3)joint3.AddPart(actor[2])joint4.AddPart(actor[3])joint4.AddPart(joint5)joint5.AddPart(actor[4])joint5.AddPart(joint6)joint6.AddPart(actor[5])joint1.SetOrigin(0, 0, 200)joint4.SetOrigin(0, 0, 200)joint2.SetOrigin(0, 0, 100)joint5.SetOrigin(0, 0, 100)ren.AddActor(joint1)ren.AddActor(joint4)# Add coordinatesaxes = CreateCoordinates()ren.AddActor(axes)# Add groundground = CreateGround()ren.AddActor(ground)# create a text actortxt.SetInput("Distance: 0m")txtprop=txt.GetTextProperty()txtprop.SetFontFamilyToArial()txtprop.SetFontSize(18)txtprop.SetColor(1,1,1)txt.SetDisplayPosition(450,550)# assign actor to the renderer ren.AddActor(txt)# Set background colorren.SetBackground(.1, .1, .1)# Set window sizerenWin.SetSize(600, 600)# Enable user interface interactor iren.Initialize()iren.Start()if __name__ == "__main__":CreateScene() View Code按住回車鍵,一幀一幀播放動畫。最后的效果是這樣的:
?
?
參考:
https://github.com/Rhoban/IKWalk
Using Inverse Kinematics to Develop a Biped Robot Walking Gait C#
8 DOF Biped Robot using Dynamixel AX-12A Servos and Arduino
轉(zhuǎn)載于:https://www.cnblogs.com/21207-iHome/p/6052159.html
總結(jié)
以上是生活随笔為你收集整理的双足机器人简单步态生成的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webpack基础入门
- 下一篇: 运维人员日常工作(转自老男孩)