-
Notifications
You must be signed in to change notification settings - Fork 2
/
teleop.py
107 lines (92 loc) · 3.31 KB
/
teleop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
from pynput import keyboard
import qi
import sys
from time import sleep
class Pepper:
session = None
motion_service = None
auton_service = None
tts = None
def __init__(self, ip, port):
self.session = qi.Session()
try:
print("Connecting to Pepper at {ip}:{port}".format(ip=ip, port=port))
self.session.connect("tcp://{ip}:{port}".format(ip=ip, port=str(port)))
print("Connected!")
except RuntimeError:
print("Unable to connect to Pepper.")
sys.exit(1)
self.motion_service = self.session.service("ALMotion")
self.auton_service = self.session.service("ALAutonomousMoves")
self.tts = self.session.service("ALTextToSpeech")
def move_forward(self, speed):
print("Moving")
self.motion_service.moveToward(speed, 0, 0)
def turn_around(self, speed):
print("Turning")
self.motion_service.moveToward(0, 0, speed)
def stop_moving(self):
print("Stopping")
self.motion_service.stopMove()
def disable_collision_protection(self):
print("Disabling collision protection")
self.motion_service.setExternalCollisionProtectionEnabled("All", False)
def sleep(self, duration):
sleep(duration)
self.stop_moving()
def speak(self, text):
self.tts.say(text)
def enable_collision_protection(self):
print("Enabling collision protection")
self.auton_service.setBackgroundStrategy("backToNeutral")
self.motion_service.setExternalCollisionProtectionEnabled("All", True)
def on_keypress(self, key):
try:
print("alphanumeric key {0} pressed".format(key.char))
if key.char == "w":
self.move_forward(1)
self.sleep(3)
elif key.char == "s":
self.move_forward(-1)
self.sleep(3)
elif key.char == "a":
self.turn_around(1)
self.sleep(1.3)
elif key.char == "d":
self.turn_around(-1)
self.sleep(1.3)
elif key.char == "1":
self.speak("Hi")
elif key.char == "2":
self.speak("Bye")
except AttributeError:
print("special key {0} pressed".format(key))
def start_teleop(self):
print("Disabling collision protection and starting teleop.")
print(
"W: forward, S: backward, A: turn left, D: turn right, 1: say hi, 2: say bye"
)
print("Press Ctrl+C to exit.")
self.disable_collision_protection()
listener = keyboard.Listener(on_press=self.on_keypress)
listener.start()
try:
while True:
pass
except KeyboardInterrupt:
listener.stop()
self.enable_collision_protection()
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="teleop.py",
description="Teleoperation script for Pepper Robot.",
)
parser.add_argument("--ip", type=str, required=True, help="Pepper's IP address")
parser.add_argument(
"-p", "--port", type=int, default=9559, help="Pepper's port number"
)
args = parser.parse_args()
robot = Pepper(args.ip, args.port)
robot.start_teleop()