key2thrust_angle.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from std_msgs.msg import Float32
5 
6 import math
7 import numpy
8 import sys
9 import termios
10 
11 instructions = """
12 Reading from the keyboard and Publishing Thrust Angles!
13 ---------------------------
14 Change Thrust Angle clockwise: h
15 Change Thrust Angle counter-clockwise: ;
16 
17 r/v : increase/decrease thruster angle change speed by 10%
18 
19 CTRL-C to quit
20 """
21 
22 moveBindings = {
23  'h': -1,
24  ';': 1,
25  }
26 
27 speedBindings = {
28  'r': 1,
29  'v': -1,
30  }
31 
32 
33 # Implement getch function, which reads in 1 char from user input.
34 # Purposely different from teleop_twist_keyboard.py b/c of
35 # issues outputting to stdout when they both read from the input.
36 # Reference https://gist.github.com/houtianze/9e623a90bb836aedadc3abea54cf6747
37 
38 def __gen_ch_getter(echo):
39 
40  def __fun():
41  fd = sys.stdin.fileno()
42  oldattr = termios.tcgetattr(fd)
43  newattr = oldattr[:]
44  try:
45  if echo:
46  # disable ctrl character printing, otherwise,
47  # backspace will be printed as "^?"
48  lflag = ~(termios.ICANON | termios.ECHOCTL)
49  else:
50  lflag = ~(termios.ICANON | termios.ECHO)
51  newattr[3] &= lflag
52  termios.tcsetattr(fd, termios.TCSADRAIN, newattr)
53  ch = sys.stdin.read(1)
54  if echo and ord(ch) == 127: # backspace
55  # emulate backspace erasing
56  # https://stackoverflow.com/a/47962872/404271
57  sys.stdout.write('\b \b')
58  finally:
59  termios.tcsetattr(fd, termios.TCSADRAIN, oldattr)
60  return ch
61  return __fun
62 
63 
64 if __name__ == "__main__":
65  # Setup getch function
66  getch = __gen_ch_getter(False)
67 
68  # Setup ros publishers and node
69  left_pub = rospy.Publisher('left_thrust_angle', Float32, queue_size=1)
70  right_pub = rospy.Publisher('right_thrust_angle', Float32, queue_size=1)
71  rospy.init_node('key2thrust_angle')
72 
73  # Initialize current angle and angle speed
74  thrust_angle_speed = 0.1
75  max_angle = rospy.get_param("~max_angle", math.pi / 2)
76  curr_angle = 0
77  num_prints = 0
78 
79  try:
80  # Output instructions
81  print(instructions)
82  print('Max Angle: {}'.format(max_angle))
83  while(1):
84  # Read in pressed key
85  key = getch()
86 
87  if key in moveBindings.keys():
88  # Increment angle, but clip it between [-max_angle, max_angle]
89  curr_angle += thrust_angle_speed * moveBindings[key]
90  curr_angle = numpy.clip(curr_angle,
91  -max_angle, max_angle).item()
92 
93  elif key in speedBindings.keys():
94  # Increment/decrement speed of angle change and print it
95  thrust_angle_speed += (speedBindings[key] * 0.1 *
96  thrust_angle_speed)
97  print('currently:\t'
98  'thruster angle speed {} '.format(thrust_angle_speed))
99 
100  # Reprint instructions after 14 speed updates
101  if (num_prints == 14):
102  print(instructions)
103  num_prints = (num_prints + 1) % 15
104 
105  elif key == '\x03':
106  break
107 
108  # Publish thrust angle
109  angle_msg = Float32()
110  angle_msg.data = curr_angle
111  left_pub.publish(angle_msg)
112  right_pub.publish(angle_msg)
113 
114  except Exception as e:
115  print(e)
116 
117  finally:
118  # Send 0 angle command at end
119  angle_msg = Float32()
120  angle_msg.data = 0
121  left_pub.publish(angle_msg)
122  right_pub.publish(angle_msg)
def __gen_ch_getter(echo)


vrx_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Thu May 7 2020 03:54:56