Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
keyboard_teleop.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 """
4  Control of the robot with keyboad.
5  Based on https://github.com/ros-teleop
6 """
7 
8 from __future__ import print_function
9 import rospy
10 from mf_robot_simulator.msg import Command, CartesianCommand
11 import sys, select, termios, tty
12 
13 msg = """
14 ---------------------------
15 Moving around:
16  u i o p
17  j k l m
18 Cartesian command:
19  r t y
20  f g h
21 anything else : stop
22 q/z : increase/decrease max speeds by 10%
23 CTRL-C to quit
24 """
25 
26 moveKeys = ['i', 'j', 'k', 'l', 'p', 'm', 'u', 'o', 'r', 't', 'y',
27  'f', 'g', 'h']
28 
29 speedBindings={
30  'q':(1.1,1.1),
31  'z':(.9,.9),
32  'w':(1.1,1),
33  'x':(.9,1),
34  'e':(1,1.1),
35  'c':(1,.9),
36  }
37 
38 TIMEOUT = -1 # to signal timeout on key input
39 
40 def getKey():
41  tty.setraw(sys.stdin.fileno())
42  rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
43 
44  key = None
45 
46  if rlist:
47  key = sys.stdin.read(1)
48  termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
49  else:
50  key = TIMEOUT
51 
52  return key
53 
54 
55 def vels(speed):
56  return "currently:\tspeed %s " % (speed)
57 
58 
59 if __name__=="__main__":
60  settings = termios.tcgetattr(sys.stdin)
61 
62  rospy.init_node('keyboard_teleop')
63  pub = rospy.Publisher('command', Command, queue_size = 1)
64  cart_pub = rospy.Publisher('cart_command', CartesianCommand, queue_size = 1)
65 
66  speed = rospy.get_param("~init_speed", 0.5)
67  init_cmd_vx = rospy.get_param("~init_cmd_vx", 1.0)
68  init_cmd_vy = rospy.get_param("~init_cmd_vy", 1.0)
69  init_cmd_vz = rospy.get_param("~init_cmd_vz", 1.0)
70  speed = rospy.get_param("~init_speed", 0.5)
71  speed = rospy.get_param("~init_speed", 0.5)
72  inc_delta = rospy.get_param("~inc_delta", 0.1) # increment for deltas
73  inc_P = rospy.get_param("~inc_P", 0.1) # increment for P
74  delta_r = 0
75  delta_e = 0
76  n = 0
77  P = 0
78 
79  v_x = 0.0 # current cartesian speed
80  v_y = 0.0
81  v_z = 0.0
82  cmd_vx = init_cmd_vx # cartesian command speed
83  cmd_vy = init_cmd_vy
84  cmd_vz = init_cmd_vz
85 
86 
87  try:
88  print(msg)
89  print(vels(speed))
90  while(1):
91  key = getKey()
92 
93  if key in moveKeys:
94  b_print = True
95 
96  if key == 'u':
97  delta_e += inc_delta
98  elif key == 'o':
99  delta_e -= inc_delta
100  elif key == 'j':
101  delta_r -= inc_delta
102  elif key == 'l':
103  delta_r += inc_delta
104  elif key == 'p':
105  P -= inc_P
106  elif key == 'm':
107  P += inc_P
108  else:
109  b_print = False
110 
111  if key == 'i':
112  n = speed
113  elif key == 'k':
114  n = -speed
115  elif key == 'y':
116  v_x = cmd_vx
117  elif key == 'r':
118  v_x = -cmd_vx
119  elif key == 'f':
120  v_y = cmd_vy
121  elif key == 'h':
122  v_y = -cmd_vy
123  elif key == 'g':
124  v_z = cmd_vz
125  elif key == 't':
126  v_z = -cmd_vz
127  else:
128  n = 0.0
129  v_x = 0.0
130  v_y = 0.0
131  v_z = 0.0
132 
133  if b_print:
134  print("n={:.1f} ; delta_r={:.2f} ; delta_e={:.2f} ; P={:.3f}".format(
135  n, delta_r, delta_e, P))
136 
137  elif key in speedBindings.keys():
138  speed = speed * speedBindings[key][0]
139  v_x = v_x * speedBindings[key][0]
140  v_y = v_y * speedBindings[key][0]
141  v_z = v_z * speedBindings[key][0]
142  n = speed
143 
144  print(vels(speed))
145  elif key == TIMEOUT:
146  v_x = 0.0
147  v_y = 0.0
148  v_z = 0.0
149  else:
150  n = 0.0
151 
152  if (key == '\x03'):
153  break
154 
155  command = Command()
156  command.n = n
157  command.delta_r = delta_r
158  command.delta_e = delta_e
159  command.P = P
160  pub.publish(command)
161 
162  cart_command = CartesianCommand()
163  cart_command.v_x = v_x
164  cart_command.v_y = v_y
165  cart_command.v_z = v_z
166  cart_pub.publish(cart_command)
167 
168  except Exception as e:
169  print(e)
170 
171  finally:
172  command = Command()
173  pub.publish(command)
174 
175  termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)