This demo is about the simulation of a crawling spider robot with six legs, where we control the motion of the legs with 18 actuators.
Finally, note how we used ChFunction objects to build some basic loop motions for the actuators, in prescribed rotation mode.
import os
import math
import pychrono as chrono
import pychrono.postprocess as postprocess
import pychrono.irrlicht as chronoirr
def __init__(self):
super().__init__()
self.bodylist = []
def Initialize(self, mark1, mark2):
body1 = mark1.GetBody()
body2 = mark2.GetBody()
self.bodylist.append([body1, body2])
frame = mark1.GetAbsFrame()
super().Initialize(body1, body2, frame)
def Set_rot_funct(self, rotfun):
super().SetAngleFunction(rotfun)
print ("Load a model exported by SolidWorks")
chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.05)
chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.05)
parts = chrono.ImportSolidWorksSystem('./spider_robot')
for ib in parts:
mysystem.Add(ib)
bbody = mysystem.SearchBody('Part3^SPIDER_ROBOT-1')
bbody.SetBodyFixed(False)
b1base = mysystem.SearchBody('M-410iB-300 -1/ArmBase-1')
b1turret = mysystem.SearchBody('M-410iB-300 -1/M-410iB-300-02-1')
b1bicept = mysystem.SearchBody('M-410iB-300 -1/M-410iB-300-03-1')
b1forearm = mysystem.SearchBody('M-410iB-300 -1/M-410iB-300-06-1')
m1_1B = b1base. SearchMarker('marker_M1_B')
m1_1A = b1turret. SearchMarker('marker_M1_A')
m1_2B = b1turret. SearchMarker('marker_M2_B')
m1_2A = b1bicept. SearchMarker('marker_M2_A')
m1_3B = b1bicept. SearchMarker('marker_M3_B')
m1_3A = b1forearm.SearchMarker('marker_M3_A')
b2base = mysystem.SearchBody('M-410iB-300 -2/ArmBase-1')
b2turret = mysystem.SearchBody('M-410iB-300 -2/M-410iB-300-02-1')
b2bicept = mysystem.SearchBody('M-410iB-300 -2/M-410iB-300-03-1')
b2forearm = mysystem.SearchBody('M-410iB-300 -2/M-410iB-300-06-1')
m2_1B = b2base. SearchMarker('marker_M1_B')
m2_1A = b2turret. SearchMarker('marker_M1_A')
m2_2B = b2turret. SearchMarker('marker_M2_B')
m2_2A = b2bicept. SearchMarker('marker_M2_A')
m2_3B = b2bicept. SearchMarker('marker_M3_B')
m2_3A = b2forearm.SearchMarker('marker_M3_A')
b3base = mysystem.SearchBody('M-410iB-300 -3/ArmBase-1')
b3turret = mysystem.SearchBody('M-410iB-300 -3/M-410iB-300-02-1')
b3bicept = mysystem.SearchBody('M-410iB-300 -3/M-410iB-300-03-1')
b3forearm = mysystem.SearchBody('M-410iB-300 -3/M-410iB-300-06-1')
m3_1B = b3base. SearchMarker('marker_M1_B')
m3_1A = b3turret. SearchMarker('marker_M1_A')
m3_2B = b3turret. SearchMarker('marker_M2_B')
m3_2A = b3bicept. SearchMarker('marker_M2_A')
m3_3B = b3bicept. SearchMarker('marker_M3_B')
m3_3A = b3forearm.SearchMarker('marker_M3_A')
b7base = mysystem.SearchBody('M-410iB-300 -7/ArmBase-1')
b7turret = mysystem.SearchBody('M-410iB-300 -7/M-410iB-300-02-1')
b7bicept = mysystem.SearchBody('M-410iB-300 -7/M-410iB-300-03-1')
b7forearm = mysystem.SearchBody('M-410iB-300 -7/M-410iB-300-06-1')
m7_1B = b7base. SearchMarker('marker_M1_B')
m7_1A = b7turret. SearchMarker('marker_M1_A')
m7_2B = b7turret. SearchMarker('marker_M2_B')
m7_2A = b7bicept. SearchMarker('marker_M2_A')
m7_3B = b7bicept. SearchMarker('marker_M3_B')
m7_3A = b7forearm.SearchMarker('marker_M3_A')
b8base = mysystem.SearchBody('M-410iB-300 -8/ArmBase-1')
b8turret = mysystem.SearchBody('M-410iB-300 -8/M-410iB-300-02-1')
b8bicept = mysystem.SearchBody('M-410iB-300 -8/M-410iB-300-03-1')
b8forearm = mysystem.SearchBody('M-410iB-300 -8/M-410iB-300-06-1')
m8_1B = b8base. SearchMarker('marker_M1_B')
m8_1A = b8turret. SearchMarker('marker_M1_A')
m8_2B = b8turret. SearchMarker('marker_M2_B')
m8_2A = b8bicept. SearchMarker('marker_M2_A')
m8_3B = b8bicept. SearchMarker('marker_M3_B')
m8_3A = b8forearm.SearchMarker('marker_M3_A')
b9base = mysystem.SearchBody('M-410iB-300 -9/ArmBase-1')
b9turret = mysystem.SearchBody('M-410iB-300 -9/M-410iB-300-02-1')
b9bicept = mysystem.SearchBody('M-410iB-300 -9/M-410iB-300-03-1')
b9forearm = mysystem.SearchBody('M-410iB-300 -9/M-410iB-300-06-1')
m9_1B = b9base. SearchMarker('marker_M1_B')
m9_1A = b9turret. SearchMarker('marker_M1_A')
m9_2B = b9turret. SearchMarker('marker_M2_B')
m9_2A = b9bicept. SearchMarker('marker_M2_A')
m9_3B = b9bicept. SearchMarker('marker_M3_B')
m9_3A = b9forearm.SearchMarker('marker_M3_A')
period = 2
mfunc_swingSa.Set_fa(mfunc_sineS)
mfunc_swingSa.Set_window_length(period)
mfunc_swingSa.Set_window_start(0)
mfunc_swingSb.Set_fa(mfunc_sineS)
mfunc_swingSb.Set_window_length(period)
mfunc_swingSb.Set_window_start(period/2.0)
mfunc_swingDb.Set_fa(mfunc_sineD)
mfunc_swingDb.Set_window_length(period)
mfunc_swingDb.Set_window_start(period/2.0)
mfunc_swingDa.Set_fa(mfunc_sineD)
mfunc_swingDa.Set_window_length(period)
mfunc_swingDa.Set_window_start(0)
mfunc_sigma.Set_amp(-0.2)
mfunc_sigma.Set_end(0.5)
mfunc_sigmb.Set_amp(0.2)
mfunc_sigmb.Set_end(0.5)
mfunc_seq.InsertFunct(mfunc_sigma, 0.5, 1, True)
mfunc_seq.InsertFunct(mfunc_const, 1.0, 1, True)
mfunc_seq.InsertFunct(mfunc_sigmb, 0.5, 1, True)
mfunc_updownA.Set_fa(mfunc_seq)
mfunc_updownA.Set_window_length(period)
mfunc_updownB.Set_fa(mfunc_seq)
mfunc_updownB.Set_window_length(period)
mfunc_updownB.Set_window_phase(period/2.0)
motor1_1 = SpiderRobotMotor()
motor1_1.Initialize(m1_1A, m1_1B)
motor1_1.Set_rot_funct(mfunc_swingSa)
mysystem.Add(motor1_1)
motor1_2 = SpiderRobotMotor()
motor1_2.Initialize(m1_2A, m1_2B)
motor1_2.Set_rot_funct(mfunc_updownA)
mysystem.Add(motor1_2)
motor1_3 = SpiderRobotMotor()
motor1_3.Initialize(m1_3A, m1_3B)
motor1_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor1_3)
motor2_1 = SpiderRobotMotor()
motor2_1.Initialize(m2_1A, m2_1B)
motor2_1.Set_rot_funct(mfunc_swingSb)
mysystem.Add(motor2_1)
motor2_2 = SpiderRobotMotor()
motor2_2.Initialize(m2_2A, m2_2B)
motor2_2.Set_rot_funct(mfunc_updownB)
mysystem.Add(motor2_2)
motor2_3 = SpiderRobotMotor()
motor2_3.Initialize(m2_3A, m2_3B)
motor2_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor2_3)
motor3_1 = SpiderRobotMotor()
motor3_1.Initialize(m3_1A, m3_1B)
motor3_1.Set_rot_funct(mfunc_swingSa)
mysystem.Add(motor3_1)
motor3_2 = SpiderRobotMotor()
motor3_2.Initialize(m3_2A, m3_2B)
motor3_2.Set_rot_funct(mfunc_updownA)
mysystem.Add(motor3_2)
motor3_3 = SpiderRobotMotor()
motor3_3.Initialize(m3_3A, m3_3B)
motor3_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor3_3)
motor9_1 = SpiderRobotMotor()
motor9_1.Initialize(m9_1A, m9_1B)
motor9_1.Set_rot_funct(mfunc_swingDb)
mysystem.Add(motor9_1)
motor9_2 = SpiderRobotMotor()
motor9_2.Initialize(m9_2A, m9_2B)
motor9_2.Set_rot_funct(mfunc_updownB)
mysystem.Add(motor9_2)
motor9_3 = SpiderRobotMotor()
motor9_3.Initialize(m9_3A, m9_3B)
motor9_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor9_3)
motor8_1 = SpiderRobotMotor()
motor8_1.Initialize(m8_1A, m8_1B)
motor8_1.Set_rot_funct(mfunc_swingDa)
mysystem.Add(motor8_1)
motor8_2 = SpiderRobotMotor()
motor8_2.Initialize(m8_2A, m8_2B)
motor8_2.Set_rot_funct(mfunc_updownA)
mysystem.Add(motor8_2)
motor8_3 = SpiderRobotMotor()
motor8_3.Initialize(m8_3A, m8_3B)
motor8_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor8_3)
motor7_1 = SpiderRobotMotor()
motor7_1.Initialize(m7_1A, m7_1B)
motor7_1.Set_rot_funct(mfunc_swingDb)
mysystem.Add(motor7_1)
motor7_2 = SpiderRobotMotor()
motor7_2.Initialize(m7_2A, m7_2B)
motor7_2.Set_rot_funct(mfunc_updownB)
mysystem.Add(motor7_2)
motor7_3 = SpiderRobotMotor()
motor7_3.Initialize(m7_3A, m7_3B)
motor7_3.Set_rot_funct(mfunc_const)
mysystem.Add(motor7_3)
mfloor.SetBodyFixed(True)
mfloor.GetCollisionModel().ClearModel()
mymat.SetRestitution(0.0)
mfloor.GetCollisionModel().AddBox(mymat, 10,0.5,10, chrono.ChVectorD(0,0.8,0))
mfloor.GetCollisionModel().BuildModel()
mfloor.SetCollide(True)
mysystem.Add(mfloor)
mfloorshape.GetBoxGeometry().Size = chrono.ChVectorD(10,0.5,10)
mfloorshape.GetBoxGeometry().Pos = chrono.ChVectorD(0,0.8,0)
mfloor.AddAsset(mfloorshape)
mfloorcolor = chrono.ChColorAsset(0.2,0.2,0.2)
mfloor.AddAsset(mfloorcolor)
myapplication = chronoirr.ChIrrApp(mysystem, 'Test', chronoirr.dimension2du(1280,720))
myapplication.AddTypicalSky('./data/skybox/')
myapplication.AddTypicalCamera(chronoirr.vector3df(2.8,2.6,2.8),chronoirr.vector3df(0.0,2.6,0.0))
myapplication.AddTypicalLights()
myapplication.AddLightWithShadow(chronoirr.vector3df(10,20,10),chronoirr.vector3df(0,2.6,0), 10 ,10,40, 60, 512)
myapplication.AssetBindAll()
myapplication.AssetUpdateAll()
myapplication.AddShadowAll()
solver.SetMaxIterations(200)
mysystem.SetSolver(solver)
myapplication.SetTimestep(0.001)
while(myapplication.GetDevice().run()):
myapplication.BeginScene()
myapplication.DrawAll()
myapplication.DoStep()
myapplication.EndScene()