-
Notifications
You must be signed in to change notification settings - Fork 0
/
roomba_wasd_control.py
90 lines (73 loc) · 2.12 KB
/
roomba_wasd_control.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
import readchar
import rospy
from std_msgs.msg import Int8
## Motor Stuff
# import pigpio
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT, initial=False)
GPIO.setup(24, GPIO.OUT, initial=False)## Motor Stuff
GPIO.setup(26, GPIO.OUT, initial=False)
# definition of H-Bridge IC pins
pinDirection = 25 # physical 22
pinPWM = 8 # physical 24
pinDisable = 7 # physical 26
# initialization of GPIO pins
pi.set_mode(pinDirection, pigpio.OUTPUT)
pi.set_mode(pinPWM, pigpio.OUTPUT)
pi.set_mode(pinDisable, pigpio.OUTPUT)
def driveMotorForwards():
pi.write(pinDirection, False)
pi.write(pinPWM, False)
pi.write(pinDisable, True)
def driveMotorBackwards():
pi.write(pinDirection, True)
pi.write(pinPWM, False)
pi.write(pinDisable, True)
def stopMotor():
pi.write(pinDirection, True)
pi.write(pinPWM, False)
pi.write(pinDisable, False)
def talker():
pub = rospy.Publisher('pet_roomba3000', Int8, queue_size=10)
rospy.init_node('RoombaWASD', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
key = readchar.readkey()
key = key.lower()
if key in ('wasdujn'):
if key == 'w': # Forward
print "w"
pub.publish(1)
elif key == 'a': # Left
print "a"
pub.publish(4)
elif key == 'd': # Right
print "d"
pub.publish(5)
elif key == 's': # stand still
print "s"
pub.publish(6)
elif key == 'u':
print "u"
driveMotorForwards()
pub.publish(8)
elif key == 'j':
print "j"
driveMotorBackwards()
pub.publish(9)
elif key == 'n':
print "n"
stopMotor()
pub.publish(10)
elif key == 'p':
print "stop"
break
rate.sleep()
GPIO.cleanup()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass