/
roomba_v1.py
202 lines (169 loc) · 5 KB
/
roomba_v1.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Testing drive code with the pyCreate2 library
from pycreate2 import Create2
import time
from sshkeyboard import listen_keyboard
from multiprocessing import Process
import rospy
from std_msgs.msg import Int16
def rightEncoderPub():
pub = rospy.Publisher('RightEncoder', Int16, queue_size=10)
rospy.init_node('rightEncoderPublisher', anonymous=True)
rate = rospy.Rate(10) #10hz
while not rospy.is_shutdown():
try:
pub.publish(bot.get_sensors().encoder_counts_right )
# lpub.publish(1)
rate.sleep()
# lpub.sleep()
except rospy.ROSInterruptException:
pass
def LeftEncoderPub():
# topic is right encoder
pub = rospy.Publisher('leftEncoder', Int16, queue_size=10)
# node name is right encoder publisher
rospy.init_node('leftEncoderPublisher', anonymous=True)
rate = rospy.Rate(10) #10hz
while not rospy.is_shutdown():
try:
pub.publish(bot.get_sensors().encoder_counts_left )
# lpub.publish(1)
rate.sleep()
# lpub.sleep()
except rospy.ROSInterruptException:
pass
port = "/dev/ttyUSB1"
bot = Create2(port, 19200) # (port, baudrate)
# Start the Create2
bot.start()
#bot.dire
procs = []
# print(name)
#proc = Process(target=rightEncoderPub)
#procs.append(proc)
#proc.start()
#
#proc = Process(target=LeftEncoderPub)
#procs.append(proc)
#proc.start()
# Put the bot into safe mode for driving
bot.safe()
VELOCITYCHANGE = 100
ROTATIONCHANGE = 100
class KeyboardHandler:
def __init__(self, bot):
self.callbackKeyUp = False
self.callbackKeyDown = False
self.callbackKeyLeft = False
self.callbackKeyRight = False
self.bot = bot
def press(self, key):
if key == 'up':
self.callbackKeyUp = True
elif key == 'down':
self.callbackKeyDown = True
elif key == 'left':
self.callbackKeyLeft = True
elif key == 'right':
self.callbackKeyRight = True
else:
return
self.handle_movement()
def release(self, key):
if key == 'up':
self.callbackKeyUp = False
elif key == 'down':
self.callbackKeyDown = False
elif key == 'left':
self.callbackKeyLeft = False
elif key == 'right':
self.callbackKeyRight = False
else:
return
self.handle_movement()
def handle_movement(self):
velocity = 0
velocity += VELOCITYCHANGE if self.callbackKeyUp is True else 0
velocity -= VELOCITYCHANGE if self.callbackKeyDown is True else 0
rotation = 0
rotation += ROTATIONCHANGE if self.callbackKeyLeft is True else 0
rotation -= ROTATIONCHANGE if self.callbackKeyRight is True else 0
# compute left and right wheel velocities
vr = int(velocity + (rotation/2))
vl = int(velocity - (rotation/2))
print(vl, vr)
self.bot.drive_direct(vr, vl)
keyboardHandler = KeyboardHandler(bot)
#listen_keyboard(
# on_press=keyboardHandler.press,
# on_release=keyboardHandler.release,
# until="space",
#)
while True:
bot.drive_direct(100, -100)
time.sleep(.5)
bot.drive_stop()
time.sleep(.5)
print(bot.get_sensors().encoder_counts_right )
time.sleep(.5)
bot.drive_direct(-100,100)
time.sleep(.5)
bot.drive_stop()
time.sleep(.5)
print(bot.get_sensors().encoder_counts_right )
time.sleep(.5)
# complete the processes
for proc in procs:
proc.join()
bot.close()
# Main drive loop
#try:
# while True:
# if (keyboard.is_pressed('z')):
# bot.drive_stop()
# break
#
# if (keyboard.is_pressed('space')):
# bot.drive_stop()
#
# elif (keyboard.is_pressed('UP')):
# if (keyboard.is_pressed('RIGHT')):
# bot.drive_direct(0, SPEEDR)
# elif (keyboard.is_pressed('LEFT')):
# bot.drive_direct(SPEEL, 0)
# else:
# bot.drive_direct(SPEEDL, SPEEDR)
#
# elif (keyboard.is_pressed('DOWN')):
# dafdslfajpressed('RIGHT')):
# bot.drive_direct(-SPEEDL, 0)
# elif (keyboard.is_pressed('LEFT')):
# bot.drive_direct(0, -SPEEDR)
# else:
# bot.drive_direct(-SPEEDL, -SPEEDR)
#
# elif (keyboard.is_pressed('RIGHT')):
# bot.drive_direct(-SPEEDL, SPEEDR)
# elif (keyboard.is_pressed('LEFT')):
# bot.drive_direct(SPEEDL, -SPEEDR)
#
#
# elif (keyboard.is_pressed('=')):
# if (SPEEDL < 500):
# SPEEDL += 10
# SPEEDR += 10
# elif (keyboard.is_pressed('-')):
# if (SPEEDR > 0):
# SPEEDL -= 10
# SPEEDR -= 10
#
# time.sleep(0.02)
#
#except KeyboardInterrupt:
# print("\nShutting down...\n")
#finally:
# try:
# bot.close()
# exit(0)
# except:
# print("Serial close failed")
# exit(0)