Changes between Version 32 and Version 33 of Tutorials


Ignore:
Timestamp:
Mar 31, 2016, 8:12:17 AM (10 years ago)
Author:
fma
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Tutorials

    v32 v33  
    5050
    5151    def _createActuatorPool(self):
    52         driver = VPython3Dof(settings.LEGS_INDEX, settings.LEGS_GEOMETRY, settings.LEGS_ORIGIN, settings.LEGS_SERVOS_MAPPING)
     52        driver = VPython3Dof(settings.LEGS_INDEX, settings.LEGS_GEOMETRY, settings.LEGS_ORIGIN, settings.LEGS_ACTUATORS_MAPPING)
    5353        pool = ActuatorPool(driver)
    5454
     
    5656
    5757            # Create joints actuators
    58             num = settings.LEGS_SERVOS_MAPPING[leg.index]['coxa']
     58            num = settings.LEGS_ACTUATORS_MAPPING[leg.index]['coxa']
    5959            actuator = Actuator(leg.coxa, num)
    6060            pool.add(actuator)
    6161
    62             num = settings.LEGS_SERVOS_MAPPING[leg.index]['femur']
     62            num = settings.LEGS_ACTUATORS_MAPPING[leg.index]['femur']
    6363            actuator = Actuator(leg.femur, num)
    6464            pool.add(actuator)
    6565
    66             num = settings.LEGS_SERVOS_MAPPING[leg.index]['tibia']
     66            num = settings.LEGS_ACTUATORS_MAPPING[leg.index]['tibia']
    6767            actuator = Actuator(leg.tibia, num)
    6868            pool.add(actuator)
     
    137137config.initNeutral(LEGS_INDEX, LEGS_NEUTRAL)
    138138
    139 # Legs / servos mapping
    140 LEGS_SERVOS_MAPPING = {
     139# Legs / actuators mapping
     140LEGS_ACTUATORS_MAPPING = {
    141141    'RF': {'coxa':  0, 'femur':  1, 'tibia':  2},
    142142    'RM': {'coxa':  4, 'femur':  5, 'tibia':  6},
     
    150150Some of these values seem obvious, some don't; we'll discuss all this later.
    151151
    152 We need to create our remote control, in order to drive our simulated robot. for this example, let's use an old gamepad, a [http://www.thrustmaster.com/en_IN/products/firestorm-dual-analog-3 Thrustmaster Firestorm Dual Analog 3]. As I own such gamepad, I already added support in Py4bot.
    153 
    154 Back in '''{{{hexapod.py}}}''':
    155 
    156 {{{
    157 #!python
    158 
    159 class Gamepad(RemoteControl):
    160     def _createFrontend(self):
    161         return FrontendFactory().create("thrustmaster", path="/dev/input/by-id/usb-Mega_World_Thrustmaster_dual_analog_3.2-event-joystick")
    162 
    163     def _buildComponents(self):
    164         self._addComponent(Button, command=GaitSequencer().walkStop, key="button_000")
    165         self._addComponent(Button, command=GaitSequencer().walkStep, key="button_003")
    166 
    167         self._addComponent(Button, command=GaitSequencer().selectPrevGait, key="button_009", trigger="hold")
    168         self._addComponent(Button, command=GaitSequencer().selectNextGait, key="button_008", trigger="hold")
    169 
    170         self._addComponent(Joystick, command=GaitSequencer().walk, keys=("analog_02", "analog_03", "analog_00"), mapper=MapperWalk())
    171 
    172         self._addComponent(Button, command=self.robot.incBodyPosition, key="button_004", mapper=MapperSetValue(dz=+5))
    173         self._addComponent(Button, command=self.robot.incBodyPosition, key="button_005", mapper=MapperSetValue(dz=-5))
    174 
    175         self._addComponent(Analog, command=self.robot.setBodyExtraPosition, key="analog_01", modifier="button_006", mapper=MapperSet('z'))
    176 }}}
    177 
    178152Now, let's create our robot. Still in '''{{{hexapod.py}}}''', add:
    179153
     
    192166#!python
    193167
    194     def addGait(gaitClass, gaitName):
    195         gait = gaitClass(gaitName, settings.GAIT_LEGS_GROUPS[gaitName], settings.GAIT_PARAMS[gaitName])
    196         GaitManager().add(gait)
    197 
    198     addGait(GaitTripod, "tripod")
    199     addGait(GaitTetrapod, "tetrapod")
    200     addGait(GaitRiple, "riple")
    201     addGait(GaitWave, "metachronal")
    202     addGait(GaitWave, "wave")
     168    gait = GaitRiple("riple", settings.GAIT_LEGS_GROUPS, settings.GAIT_PARAMS)
     169    GaitManager().add(gait)
    203170}}}
    204171
     
    209176
    210177# Gaits
    211 GAIT_LEGS_GROUPS = {
    212     'tripod':      (('RM', 'LF', 'LR'), ('RF', 'LM', 'RR')),
    213     'tetrapod':    (('RR', 'LM'), ('RF', 'LR'), ('RM', 'LF')),
    214     'riple':       (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',)),
    215     'metachronal': (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',)),
    216     'wave':        (('RR',), ('RM',), ('RF',), ('LR',), ('LM',), ('LF',))
    217 }
     178GAIT_LEGS_GROUPS = (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',))
    218179
    219180GAIT_PARAMS = {
    220     'tripod': {'length': 40., 'angle': 10., 'height': 40., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    221     'tetrapod': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    222     'riple': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 300.},
    223     'metachronal': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    224     'wave': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.}
    225 }
    226 }}}
    227 
    228 Final steps are: instanciate the gamepad, launch the gait sequencer, and run the robot!
    229 
    230 {{{
    231 #!python
    232 
    233     remote = Gamepad(robot)
     181    'length': 40.,
     182    'angle': 10.,
     183    'height': 30.,
     184    'minLength': 4.,
     185    'minAngle': 2.,
     186    'speedMin':  50.,
     187    'speedMax': 300.
     188}
     189}}}
     190
     191Final steps are: launch the gait sequencer, move a little bit the body position, ask the robot to start walking, and run the robot main loop:
     192
     193{{{
     194#!python
    234195
    235196    GaitSequencer().start()
    236     remote.start()
    237197
    238198    robot.setBodyPosition(z=-30)
    239     GaitManager().select("riple")
    240 
    241     robot.mainLoop()
    242 
    243     remote.stop()
    244     remote.join()
     199
     200    GaitSequencer().walk(0.75, 90., 0., 1)
     201
     202    robot.mainLoop(5)
     203
     204    GaitSequencer().walkStop()
     205    time.sleep(5)
     206
    245207    GaitSequencer().stop()
    246208    GaitSequencer().join()
     
    251213}}}
    252214
    253 You should then see:
     215You should then see the robot walking for a few seconds:
    254216
    255217[[Image(tutorial_1.png, nolink)]]
    256 
    257 ''TODO: need to put a 3DoF sceenshot!''
    258218
    259219=== Settings ===
     
    341301#!python
    342302
    343 LEGS_SERVOS_MAPPING = {
     303LEGS_ACTUATORS_MAPPING = {
    344304    'RF': {'coxa':  0, 'femur':  1, 'tibia':  2},
    345305    'RM': {'coxa':  4, 'femur':  5, 'tibia':  6},
     
    351311}}}
    352312
    353 {{{LEGS_SERVOS_MAPPING}}} dict contains a table to map all joints to servos nums.
    354 
    355 {{{
    356 #!python
    357 
    358 GAIT_LEGS_GROUPS = {
    359     'tripod':      (('RM', 'LF', 'LR'), ('RF', 'LM', 'RR')),
    360     'tetrapod':    (('RR', 'LM'), ('RF', 'LR'), ('RM', 'LF')),
    361     'riple':       (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',)),
    362     'metachronal': (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',)),
    363     'wave':        (('RR',), ('RM',), ('RF',), ('LR',), ('LM',), ('LF',))
    364 }
    365 }}}
    366 
    367 {{{GAIT_LEGS_GROUPS}}} dict contains the legs grouped together and controlled at the same time during the gait usage.
    368 
    369 For example, the '''tripod''' gait is made of 2 groups of 3 legs.
     313{{{LEGS_ACTUATORS_MAPPING}}} dict contains a table to map all joints to actuators nums.
     314
     315{{{
     316#!python
     317
     318GAIT_LEGS_GROUPS = (('RR',), ('LM',), ('RF',), ('LR',), ('RM',), ('LF',))
     319}}}
     320
     321{{{GAIT_LEGS_GROUPS}}} contains the legs grouped together and controlled at the same time during the gait usage.
     322
     323In this example, the '''riple''' gait is made of 6 groups of 1 legs.
    370324
    371325Note that we need to define sequences, so don't forget the comma to define a tuple with a unique element.
     
    375329
    376330GAIT_PARAMS = {
    377     'tripod': {'length': 40., 'angle': 10., 'height': 40., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    378     'tetrapod': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    379     'riple': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 300.},
    380     'metachronal': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.},
    381     'wave': {'length': 40., 'angle': 10., 'height': 30., 'minLength': 4., 'minAngle': 2., 'speedMin':  50., 'speedMax': 200.}
    382 }
    383 }}}
    384 
    385 {{{GAIT_PARAMS}}} dict contains some additional gaits params:
     331    'length': 40.,
     332    'angle': 10.,
     333    'height': 30.,
     334    'minLength': 4.,
     335    'minAngle': 2.,
     336    'speedMin':  50.,
     337    'speedMax': 300.
     338}
     339}}}
     340
     341{{{GAIT_PARAMS}}} contains some additional gaits params:
    386342
    387343* {{{length}}} is the distance each leg will move for an entire cycle when the robot translate;
     
    396352
    397353''Files for this tutorial are available in [source:/py4bot/examples/tutorial_2/]''
     354
     355In this tutorial, we are just going to add a remote control in order to drive our tutorial 1 simulated robot.
     356
     357Add the following code between the {{{Hexapod}} class, and the {{{main()}}} function:
     358
     359
     360{{{
     361#!python
     362
     363class Gamepad(RemoteControl):
     364    def _createFrontend(self):
     365        return FrontendFactory().create("thrustmaster", path="/dev/input/by-id/usb-Mega_World_Thrustmaster_dual_analog_3.2-event-joystick")
     366
     367    def _buildComponents(self):
     368        self._addComponent(Button, command=GaitSequencer().walkStop, key="button_000")
     369        self._addComponent(Button, command=GaitSequencer().walkStep, key="button_003")
     370
     371        self._addComponent(Button, command=GaitSequencer().selectPrevGait, key="button_009", trigger="hold")
     372        self._addComponent(Button, command=GaitSequencer().selectNextGait, key="button_008", trigger="hold")
     373
     374        self._addComponent(Joystick, command=GaitSequencer().walk, keys=("analog_02", "analog_03", "analog_00"), mapper=MapperWalk())
     375
     376        self._addComponent(Button, command=self.robot.incBodyPosition, key="button_004", mapper=MapperSetValue(dz=+5))
     377        self._addComponent(Button, command=self.robot.incBodyPosition, key="button_005", mapper=MapperSetValue(dz=-5))
     378
     379        self._addComponent(Analog, command=self.robot.setBodyExtraPosition, key="analog_01", modifier="button_006", mapper=MapperSet('z'))
     380}}}
     381
     382Then, modify the {{{main.py}}} function as following:
     383
     384{{{
     385#!python
     386
     387def main():
     388    robot = Hexapod()
     389
     390    robot = Hexapod()
     391
     392    gait = GaitRiple("riple", settings.GAIT_LEGS_GROUPS, settings.GAIT_PARAMS)
     393    GaitManager().add(gait)
     394
     395    remote = Gamepad(robot)
     396
     397    GaitSequencer().start()
     398    remote.start()
     399
     400    robot.setBodyPosition(z=-30)
     401
     402    robot.mainLoop()
     403
     404    remote.stop()
     405    remote.join()
     406    GaitSequencer().stop()
     407    GaitSequencer().join()
     408}}}
     409
     410== Tutorial 3 ==
     411
     412''Files for this tutorial are available in [source:/py4bot/examples/tutorial_3/]''
    398413
    399414Ok, a simulated robot is cool, but a real one is much more fun!
     
    494509See the [[FAQ#Howtocalibrateservosparameters|FAQ]] to fine tune servos calibration.
    495510
     511
     512
     513
     514
     515
     516We need to create our remote control, in order to drive our simulated robot. for this example, let's use an old gamepad, a [http://www.thrustmaster.com/en_IN/products/firestorm-dual-analog-3 Thrustmaster Firestorm Dual Analog 3]. As I own such gamepad, I already added support in Py4bot.
     517
     518Back in '''{{{hexapod.py}}}''':
     519
     520{{{
     521#!python
     522
     523class Gamepad(RemoteControl):
     524    def _createFrontend(self):
     525        return FrontendFactory().create("thrustmaster", path="/dev/input/by-id/usb-Mega_World_Thrustmaster_dual_analog_3.2-event-joystick")
     526
     527    def _buildComponents(self):
     528        self._addComponent(Button, command=GaitSequencer().walkStop, key="button_000")
     529        self._addComponent(Button, command=GaitSequencer().walkStep, key="button_003")
     530
     531        self._addComponent(Button, command=GaitSequencer().selectPrevGait, key="button_009", trigger="hold")
     532        self._addComponent(Button, command=GaitSequencer().selectNextGait, key="button_008", trigger="hold")
     533
     534        self._addComponent(Joystick, command=GaitSequencer().walk, keys=("analog_02", "analog_03", "analog_00"), mapper=MapperWalk())
     535
     536        self._addComponent(Button, command=self.robot.incBodyPosition, key="button_004", mapper=MapperSetValue(dz=+5))
     537        self._addComponent(Button, command=self.robot.incBodyPosition, key="button_005", mapper=MapperSetValue(dz=-5))
     538
     539        self._addComponent(Analog, command=self.robot.setBodyExtraPosition, key="analog_01", modifier="button_006", mapper=MapperSet('z'))
     540}}}
     541
     542
    496543== Conclusion ==
    497544