update game controllers and keyboard interfaces + example with quadcopter and wheeled robot

This commit is contained in:
Brian Delhaisse
2019-06-21 03:26:36 +02:00
parent f512d42d31
commit c356c062bb
18 changed files with 1475 additions and 164 deletions
+40 -20
View File
@@ -3,13 +3,25 @@
how to run:
```
$ python quadcopter_controller.py --help # for help
$ python quadcopter_controller.py --controller xbox # to use Xbox game controller
$ python quadcopter_controller.py --controller ps3 # to use PS3 game controller
$ python quadcopter_controller.py --help # for help
$ python quadcopter_controller.py --controller keyboard # to use the keyboard
$ python quadcopter_controller.py --controller xbox # to use Xbox game controller
$ python quadcopter_controller.py --controller ps # to use PS game controller
```
Mapping of the keyboard interface:
- `top arrow`: move forward
- `bottom arrow`: move backward
- `left arrow`: move sideways to the left
- `right arrow`: move sideways to the right
- `ctrl + top arrow`: ascend
- `ctrl + bottom arrow`: descend
- `ctrl + left arrow`: turn to the right
- `ctrl + right arrow`: turn to the left
- `space`: switch between first-person and third-person view
"""
import numpy as np
# import numpy as np
from itertools import count
import argparse
@@ -18,18 +30,26 @@ import pyrobolearn as prl
# create parser to select the game controller
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--controller', help='the game controller to use', type=str,
choices=['xbox', 'ps3'], default='ps3')
parser.add_argument('-c', '--controller', help='the controller to use', type=str,
choices=['keyboard', 'xbox', 'ps'], default='keyboard')
args = parser.parse_args()
# load corresponding interface
# if args.controller == 'xbox':
# from pyrobolearn.tools.interfaces.controllers.xbox import Xbox360ControllerInterface as Controller
# elif args.controller == 'ps3':
# from pyrobolearn.tools.interfaces.controllers.playstation import PS3ControllerInterface as Controller
# else:
# raise NotImplementedError("Unknown game controller")
if args.controller == 'keyboard': # keyboard interface
from pyrobolearn.tools.interfaces.mouse_keyboard.mousekeyboard import MouseKeyboardInterface as Controller
from pyrobolearn.tools.bridges.mouse_keyboard.bridge_mousekeyboard_quadcopter \
import BridgeMouseKeyboardQuadcopter as Bridge
elif args.controller == 'xbox': # Xbox interface
from pyrobolearn.tools.interfaces.controllers.xbox import XboxControllerInterface as Controller
from pyrobolearn.tools.bridges.controllers.robots.bridge_controller_quadcopter import BridgeControllerQuadcopter \
as Bridge
elif args.controller == 'ps': # PS interface
from pyrobolearn.tools.interfaces.controllers.playstation import PSControllerInterface as Controller
from pyrobolearn.tools.bridges.controllers.robots.bridge_controller_quadcopter import BridgeControllerQuadcopter \
as Bridge
else:
raise NotImplementedError("Unknown game controller")
# create simulator
@@ -42,17 +62,17 @@ world = prl.worlds.BasicWorld(sim)
robot = prl.robots.Quadcopter(sim, position=[0., 0., 2.])
world.load_robot(robot)
# load interface
# controller = Controller()
# load interface that accepts input events
controller = Controller()
# load bridge that connects the interface/controller with the quadcopter
# The bridge is the one that maps the input events from the interface to commands sent to the quadcopter
bridge = Bridge(quadcopter=robot, interface=controller)
# run simulator
for t in count():
# robot.hover()
# robot.set_propeller_velocities(velocity)
robot.move([1., 1., 1.])
# follow quadcopter (seen from behind)
world.follow(robot, distance=2, yaw=-np.pi / 2)
# perform a step with the bridge and interface
bridge.step(update_interface=True)
# perform one step in the world
world.step(sleep_dt=1. / 240)