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
+5 -1
View File
@@ -12,6 +12,8 @@ from pyrobolearn.tools.interfaces.mouse_keyboard import MouseKeyboardInterface
sim = prl.simulators.Bullet()
# create mouse keyboard interface
# Note that to give the simulator `sim` to the interface can be optional especially if there is only one simulator.
# The interface will look in the memory to check the instantiated simulators and take the first one if it exists.
interface = MouseKeyboardInterface(sim)
# run interface
@@ -20,8 +22,10 @@ for _ in count():
interface.step()
# print pressed keys
if len(interface.key_pressed) > 0:
print("Keys that are pressed: {}".format(interface.key_pressed))
if len(interface.key_down) > 0:
print("Keys that are pressed: {}".format(interface.key_down))
print("Keys that are down: {}".format(interface.key_down))
# perform a step with the simulator
sim.step(sleep_time=sim.dt)
+14 -4
View File
@@ -1,23 +1,33 @@
#!/usr/bin/env python
"""Load the Playstation game controller interface
How to run:
```
$ python playstation.py --help # for help
$ python playstation.py --controller ps # to use any PS game controller (by default)
$ python playstation.py --controller ps3 # to use PS3 game controller
$ python playstation.py --controller ps4 # to use PS4 game controller
```
"""
import time
from itertools import count
import argparse
from pyrobolearn.tools.interfaces.controllers.playstation import PS3ControllerInterface, PS4ControllerInterface
from pyrobolearn.tools.interfaces.controllers.playstation import *
# create parser to select the game controller
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--controller', help='The Playstation game controller to use (ps3 or ps4)', type=str,
choices=['ps3', 'ps4'], default='ps4')
parser.add_argument('-c', '--controller', help='The Playstation game controller to use (ps, ps3, or ps4)', type=str,
choices=['ps', 'ps3', 'ps4'], default='ps')
args = parser.parse_args()
# load corresponding Playstation controller interface
if args.controller == 'ps':
controller = PSControllerInterface(verbose=False)
if args.controller == 'ps3':
controller = PS3ControllerInterface(verbose=True)
controller = PS3ControllerInterface(verbose=False)
elif args.controller == 'ps4':
controller = PS4ControllerInterface(verbose=False)
else:
+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)