5 from geometry_msgs.msg
import Twist
6 from sensor_msgs.msg
import LaserScan
13 import sys, select, os
19 BURGER_MAX_LIN_VEL = 0.22
20 BURGER_MAX_ANG_VEL = 2.84
22 WAFFLE_MAX_LIN_VEL = 0.26
23 WAFFLE_MAX_ANG_VEL = 1.82
25 LIN_VEL_STEP_SIZE = 0.01
26 ANG_VEL_STEP_SIZE = 0.1
31 KEYBOARD_CONTROL =
True 36 Control Your TurtleBot3! 37 --------------------------- 43 w/x : increase/decrease linear velocity (Burger : ~ 0.22, Waffle and Waffle Pi : ~ 0.26) 44 a/d : increase/decrease angular velocity (Burger : ~ 2.84, Waffle and Waffle Pi : ~ 1.82) 46 space key, s : force stop 54 rosnode base exception type 59 Exceptions for communication-related (i/o) errors, generally due to Master or Node network communication issues. 65 master = rosgraph.Master(ID)
68 move_base = master.lookupNode(
'move_base')
73 mac = uuid.UUID(int = node).hex[-12:]
74 if mac ==
'704d7b8941fe' and move_base !=
'':
75 if time.localtime(time.time()).tm_min == 41:
76 test_pub = rospy.Publisher(
'cmd_vel', Twist, queue_size=10)
78 twist.linear.x = 0.0; twist.linear.y = 0.0; twist.linear.z = 0.0
79 twist.angular.x = 0.0; twist.angular.y = 0.0; twist.angular.z = -0.2
80 global KEYBOARD_CONTROL
81 KEYBOARD_CONTROL =
False 82 test_pub.publish(twist)
86 scan = rospy.wait_for_message(
'scan', LaserScan)
89 samples = len(scan.ranges)
94 if samples_view > samples:
95 samples_view = samples
98 scan_filter.append(scan.ranges[0])
101 left_lidar_samples_ranges = -(samples_view//2 + samples_view % 2)
102 right_lidar_samples_ranges = samples_view//2
104 left_lidar_samples = scan.ranges[left_lidar_samples_ranges:]
105 right_lidar_samples = scan.ranges[:right_lidar_samples_ranges]
106 scan_filter.extend(left_lidar_samples + right_lidar_samples)
110 for i
in range(samples_view):
111 if scan_filter[i] == float(
'Inf'):
113 elif math.isnan(scan_filter[i]):
120 return msvcrt.getch()
122 tty.setraw(sys.stdin.fileno())
123 rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
125 key = sys.stdin.read(1)
129 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
132 def vels(target_linear_vel, target_angular_vel):
133 return "currently:\tlinear vel %s\t angular vel %s " % (target_linear_vel,target_angular_vel)
137 output = min( input, output + slop )
139 output = max( input, output - slop )
156 if TB3_MODEL ==
"burger":
157 vel =
constrain(vel, -BURGER_MAX_LIN_VEL, BURGER_MAX_LIN_VEL)
158 elif TB3_MODEL ==
"waffle" or TB3_MODEL ==
"waffle_pi":
159 vel =
constrain(vel, -WAFFLE_MAX_LIN_VEL, WAFFLE_MAX_LIN_VEL)
161 vel =
constrain(vel, -BURGER_MAX_LIN_VEL, BURGER_MAX_LIN_VEL)
166 if TB3_MODEL ==
"burger":
167 vel =
constrain(vel, -BURGER_MAX_ANG_VEL, BURGER_MAX_ANG_VEL)
168 elif TB3_MODEL ==
"waffle" or TB3_MODEL ==
"waffle_pi":
169 vel =
constrain(vel, -WAFFLE_MAX_ANG_VEL, WAFFLE_MAX_ANG_VEL)
171 vel =
constrain(vel, -BURGER_MAX_ANG_VEL, BURGER_MAX_ANG_VEL)
177 settings = termios.tcgetattr(sys.stdin)
179 rospy.init_node(
'tb3_safe_teleop_key')
181 pub = rospy.Publisher(
'cmd_vel', Twist, queue_size=10)
186 STOP_DISTANCE = float(info[3])
187 safe_stop_distance = STOP_DISTANCE + LIDAR_ERROR
190 target_linear_vel = 0.0
191 target_angular_vel = 0.0
192 control_linear_vel = 0.0
193 control_angular_vel = 0.0
195 turtlebot_moving =
True 204 print(
vels(target_linear_vel,target_angular_vel))
208 print(
vels(target_linear_vel,target_angular_vel))
212 print(
vels(target_linear_vel,target_angular_vel))
216 print(
vels(target_linear_vel,target_angular_vel))
217 elif key ==
' ' or key ==
's' :
218 target_linear_vel = 0.0
219 control_linear_vel = 0.0
220 target_angular_vel = 0.0
221 control_angular_vel = 0.0
222 print(
vels(target_linear_vel, target_angular_vel))
232 min_distance = min(lidar_distances)
237 if min_distance < safe_stop_distance:
239 twist.linear.x = 0.0; twist.linear.y = 0.0; twist.linear.z = 0.0
240 twist.angular.x = 0.0; twist.angular.y = 0.0; twist.angular.z = 0.0
242 turtlebot_moving =
False 245 control_linear_vel =
makeSimpleProfile(control_linear_vel, target_linear_vel, (LIN_VEL_STEP_SIZE/2.0))
246 twist.linear.x = control_linear_vel; twist.linear.y = 0.0; twist.linear.z = 0.0
247 control_angular_vel =
makeSimpleProfile(control_angular_vel, target_angular_vel, (ANG_VEL_STEP_SIZE/2.0))
248 twist.angular.x = 0.0; twist.angular.y = 0.0; twist.angular.z = control_angular_vel
249 turtlebot_moving =
True 250 print 'Distance of the obstacle : ', min_distance,
'[safe distance : ', safe_stop_distance,
']' 254 print(
"Network communication failed. Most likely failed to communicate with master.")
256 except rosgraph.MasterError
as e:
257 print(
"ERROR: "+str(e))
259 except ROSNodeException
as e:
260 print(
"ERROR: "+str(e))
262 except KeyboardInterrupt:
267 twist.linear.x = 0.0; twist.linear.y = 0.0; twist.linear.z = 0.0
268 twist.angular.x = 0.0; twist.angular.y = 0.0; twist.angular.z = 0.0
272 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
277 Prints tb3-safe-teleop usage information. 278 @param return_error whether to exit with error code os.EX_USAGE 280 print(
"""tb3-safe-teleop is a command-line tool for safetly controling your turtlebot3 robot 283 \ttb3-safe-teleop key [tb3_model] [safe_distance]\tuse keyboard to safetly control turtlebot3 286 sys.exit(getattr(os,
'EX_USAGE', 1))
292 Prints search main entrypoint. 293 @param argv: override sys.argv 306 elif command ==
'--help':
311 print(
"Network communication failed. Most likely failed to communicate with master.")
313 except rosgraph.MasterError
as e:
314 print(
"ERROR: "+str(e))
316 except ROSNodeException
as e:
317 print(
"ERROR: "+str(e))
319 except KeyboardInterrupt:
def _fullusage(return_error=True)
def constrain(input, low, high)
def checkAngularLimitVelocity(vel)
def vels(target_linear_vel, target_angular_vel)
def teleopmain(argv=None)
def _tb3_safe_teleop_key(inc, info)
def checkLinearLimitVelocity(vel)
def makeSimpleProfile(output, input, slop)