-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllerSubscriber.py
162 lines (137 loc) · 3.47 KB
/
controllerSubscriber.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
#!/usr/bin/env python3.6
import rospy
import rospy
from std_msgs.msg import String,Int32,Int32MultiArray,MultiArrayLayout,MultiArrayDimension, Int16MultiArray
#rosroverxbox is the name of the package, and IntList is the name of the .msg file
from cleaningbot.msg import IntList
import RPi.GPIO as GPIO
from time import sleep
def init():
global in1
global in2
global en
global in5
global in6
global en3
global speed
global speed3
global in3
global in4
global en2
global in7
global in8
global en4
global speed2
global speed4
#.
in1 = 23
in2 = 24
en = 18
#.
in3 = 20
in4 = 16
en2 = 21
#.
in5 = 13
in6 = 19
en3 = 26
#.
in7 = 4
in8 = 17
en4 = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.setup(en,GPIO.OUT)
GPIO.output(in1,GPIO.LOW)
GPIO.output(in2,GPIO.LOW)
GPIO.setmode(GPIO.BCM)
GPIO.setup(in3,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)
GPIO.setup(en2,GPIO.OUT)
GPIO.output(in3,GPIO.LOW)
GPIO.output(in4,GPIO.LOW)
GPIO.setmode(GPIO.BCM)
GPIO.setup(in5,GPIO.OUT)
GPIO.setup(in6,GPIO.OUT)
GPIO.setup(en3,GPIO.OUT)
GPIO.output(in5,GPIO.LOW)
GPIO.output(in6,GPIO.LOW)
GPIO.setmode(GPIO.BCM)
GPIO.setup(in7,GPIO.OUT)
GPIO.setup(in8,GPIO.OUT)
GPIO.setup(en4,GPIO.OUT)
GPIO.output(in7,GPIO.LOW)
GPIO.output(in8,GPIO.LOW)
speed = GPIO.PWM(en,1000)
speed2 = GPIO.PWM(en2,1000)
speed3 = GPIO.PWM(en3,1000)
speed4 = GPIO.PWM(en4,1000)
#default speed
speed.start(75)
speed2.start(75)
speed3.start(75)
speed4.start(75)
def stop(x1,x2):
GPIO.output(x1,GPIO.LOW)
GPIO.output(x2,GPIO.LOW)
def callback0(data):
#runs everytime button changes
arr = data.drive
print(".")
#set speed to 75 percent
speed.ChangeDutyCycle(75)
speed2.ChangeDutyCycle(75)
speed3.ChangeDutyCycle(75)
speed4.ChangeDutyCycle(75)
GPIO.output(in1,GPIO.LOW)
GPIO.output(in2,GPIO.LOW)
GPIO.output(in3,GPIO.LOW)
GPIO.output(in4,GPIO.LOW)
GPIO.output(in5,GPIO.LOW)
GPIO.output(in6,GPIO.LOW)
GPIO.output(in7,GPIO.LOW)
GPIO.output(in8,GPIO.LOW)
#3,4 and 5,6 are a pair
#1,2 and 7,8 are a pair
if arr[0] == 1:
#move forward
GPIO.output(in2,GPIO.HIGH)
GPIO.output(in4,GPIO.HIGH)
GPIO.output(in6,GPIO.HIGH)
GPIO.output(in8,GPIO.HIGH)
elif arr[0] == 2:
#move backwards
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in3,GPIO.HIGH)
GPIO.output(in5,GPIO.HIGH)
GPIO.output(in7,GPIO.HIGH)
elif arr[1] == 1:
#turn right
GPIO.output(in2,GPIO.HIGH)
GPIO.output(in3,GPIO.HIGH)
GPIO.output(in5,GPIO.HIGH)
GPIO.output(in7,GPIO.HIGH)
elif arr[1] == 2:
#turn left
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in4,GPIO.HIGH)
GPIO.output(in6,GPIO.HIGH)
GPIO.output(in8,GPIO.HIGH)
elif arr[0] == 0 or arr[1] == 0:
#stop
stop(in1,in2)
stop(in3,in4)
stop(in5,in6)
stop(in7,in8)
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber('driver', IntList, callback0)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
try:
init()
listener()
except rospy.ROSInterruptException:
GPIO.cleanup()