Skip to content

Commit e2231e9

Browse files
authored
Add files via upload
1 parent d1fcbaf commit e2231e9

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

scripts/teleop

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env python
2+
3+
import rospy
4+
5+
from geometry_msgs.msg import Twist
6+
7+
import sys, select, termios, tty
8+
9+
msg = """
10+
Control Your Udacity Bot!
11+
---------------------------
12+
Moving around:
13+
u i o
14+
j k l
15+
m , .
16+
17+
q/z : increase/decrease max speeds by 10%
18+
w/x : increase/decrease only linear speed by 10%
19+
e/c : increase/decrease only angular speed by 10%
20+
space key, k : force stop
21+
anything else : stop smoothly
22+
23+
CTRL-C to quit
24+
"""
25+
26+
moveBindings = {
27+
'i':(1,0),
28+
'o':(1,-1),
29+
'j':(0,1),
30+
'l':(0,-1),
31+
'u':(1,1),
32+
',':(-1,0),
33+
'.':(-1,1),
34+
'm':(-1,-1),
35+
}
36+
37+
speedBindings={
38+
'q':(1.1,1.1),
39+
'z':(.9,.9),
40+
'w':(1.1,1),
41+
'x':(.9,1),
42+
'e':(1,1.1),
43+
'c':(1,.9),
44+
}
45+
46+
def getKey():
47+
tty.setraw(sys.stdin.fileno())
48+
rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
49+
if rlist:
50+
key = sys.stdin.read(1)
51+
else:
52+
key = ''
53+
54+
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
55+
return key
56+
57+
speed = .2
58+
turn = 1
59+
60+
def vels(speed,turn):
61+
return "currently:\tspeed %s\tturn %s " % (speed,turn)
62+
63+
if __name__=="__main__":
64+
settings = termios.tcgetattr(sys.stdin)
65+
66+
rospy.init_node('teleop')
67+
pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)
68+
69+
x = 0
70+
th = 0
71+
status = 0
72+
count = 0
73+
acc = 0.1
74+
target_speed = 0
75+
target_turn = 0
76+
control_speed = 0
77+
control_turn = 0
78+
try:
79+
print(msg)
80+
print(vels(speed,turn))
81+
while(1):
82+
key = getKey()
83+
if key in moveBindings.keys():
84+
x = moveBindings[key][0]
85+
th = moveBindings[key][1]
86+
count = 0
87+
elif key in speedBindings.keys():
88+
speed = speed * speedBindings[key][0]
89+
turn = turn * speedBindings[key][1]
90+
count = 0
91+
92+
print(vels(speed,turn))
93+
if (status == 14):
94+
print(msg)
95+
status = (status + 1) % 15
96+
elif key == ' ' or key == 'k' :
97+
x = 0
98+
th = 0
99+
control_speed = 0
100+
control_turn = 0
101+
else:
102+
count = count + 1
103+
if count > 4:
104+
x = 0
105+
th = 0
106+
if (key == '\x03'):
107+
break
108+
109+
target_speed = speed * x
110+
target_turn = turn * th
111+
112+
if target_speed > control_speed:
113+
control_speed = min( target_speed, control_speed + 0.02 )
114+
elif target_speed < control_speed:
115+
control_speed = max( target_speed, control_speed - 0.02 )
116+
else:
117+
control_speed = target_speed
118+
119+
if target_turn > control_turn:
120+
control_turn = min( target_turn, control_turn + 0.1 )
121+
elif target_turn < control_turn:
122+
control_turn = max( target_turn, control_turn - 0.1 )
123+
else:
124+
control_turn = target_turn
125+
126+
twist = Twist()
127+
twist.linear.x = control_speed; twist.linear.y = 0; twist.linear.z = 0
128+
twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = control_turn
129+
pub.publish(twist)
130+
131+
except Exception as e:
132+
print(e)
133+
134+
finally:
135+
twist = Twist()
136+
twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
137+
twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
138+
pub.publish(twist)
139+
140+
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
141+

0 commit comments

Comments
 (0)