-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.py
149 lines (140 loc) · 4.33 KB
/
robot.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
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
from time import sleep
import RPi.GPIO as GPIO
import pigpio
import json
import threading
pi = pigpio.pi()
GPIO.setmode(GPIO.BCM)
pi.set_PWM_dutycycle(25, 255)
pi.set_servo_pulsewidth(14, 1500)
pi.set_servo_pulsewidth(15, 1500)
class Motor:
def __init__(self, pin1, pin2):
self.pin1 = pin1
self.pin2 = pin2
def Forward(self, speedfactor):
pi.set_PWM_dutycycle(self.pin1, round(speedfactor))
pi.set_PWM_dutycycle(self.pin2, 0)
def Backward(self, speedfactor):
pi.set_PWM_dutycycle(self.pin1, 0)
pi.set_PWM_dutycycle(self.pin2, round(speedfactor))
def Stop(self):
pi.set_PWM_dutycycle(self.pin1, 0)
pi.set_PWM_dutycycle(self.pin2, 0)
def Brake(self):
pi.set_PWM_dutycycle(self.pin1, 255)
pi.set_PWM_dutycycle(self.pin2, 255)
class L293D:
def __init__(self, motor1, motor2):
self.motor1 = motor1
self.motor2 = motor2
def Drive(self, factorx, factory):
if(factorx <= 0 and factorx > -0.5 and factory > 0):
self.LeftForward(factorx*2)
elif(factorx >= 0 and factorx < 0.5 and factory > 0):
self.RightForward(factorx*2)
elif(factorx <= 0 and factorx > -0.5 and factory < 0):
self.LeftBackward(factorx*2)
elif(factorx >= 0 and factorx < 0.5 and factory < 0):
self.RightBackward(factorx*2)
elif(factorx <= 0 and factory > 0):
self.LeftForwardFast(factorx)
elif(factorx >= 0 and factory > 0):
self.RightForwardFast(factorx)
elif(factorx <= 0 and factory < 0):
self.LeftBackwardFast(factorx)
elif(factorx >= 0 and factory < 0):
self.RightBackwardFast(factorx)
else:
self.Stop()
def Stop(self):
self.motor1.Stop()
self.motor2.Stop()
def LeftForward(self, factorx):
self.motor1.Forward(255)
self.motor2.Forward(255*abs((-factorx)-1))
def RightForward(self, factorx):
self.motor1.Forward(255*abs(factorx-1))
self.motor2.Forward(255)
def LeftBackward(self, factorx):
self.motor1.Backward(255)
self.motor2.Backward(255*abs((-factorx)-1))
def RightBackward(self, factorx):
self.motor1.Backward(255*abs(factorx-1))
self.motor2.Backward(255)
def LeftForwardFast(self, factorx):
self.motor1.Forward(255)
self.motor2.Backward(255*(abs(factorx)-0.5)*2)
def RightForwardFast(self, factorx):
self.motor1.Backward(255*(factorx-0.5)*2)
self.motor2.Forward(255)
def LeftBackwardFast(self, factorx):
self.motor1.Backward(255)
self.motor2.Forward(255*(abs(factorx)-0.5)*2)
def RightBackwardFast(self, factorx):
self.motor1.Forward(255*(abs(factorx)-0.5)*2)
self.motor2.Backward(255)
M1 = Motor(21, 16)
M2 = Motor(26, 13)
M3 = Motor(24, 23)
M4 = Motor(27, 17)
#just to make sure no gpio's have been left high
M1.Stop()
M2.Stop()
M3.Stop()
M4.Stop()
H1 = L293D(M1,M2)
H2 = L293D(M3,M4)
def is_json(myjson):
try:
json_object = json.loads(myjson)
except ValueError as e:
print("Not a valid json")
return False
return True
def processjson(message):
jsonmessage = message.payload.decode("utf-8")
if(is_json(jsonmessage)):
j = json.loads(jsonmessage)
yvalue = j['y']
xvalue = j['x']
H1.Drive(xvalue, yvalue)
H2.Drive(xvalue, yvalue)
if j['s'] != None:
servovalue = j['s']
pi.set_servo_pulsewidth(15, servovalue)
if j['k'] != None:
servovalue = j['k']
pi.set_servo_pulsewidth(14, servovalue)
def breakh():
print("nothing received anymore (1s). Executing EMERGENCY brake!")
H1.Stop()
H2.Stop()
t = threading.Timer(0.25, breakh)
def on_message(client, userdata, message):
try:
processjson(message)
print(message.payload.decode("utf-8"))
global t
t.cancel()
t = threading.Timer(0.25, breakh)
t.start()
except Error as e:
print(e)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected MQTT disconnection. Will auto-reconnect")
broker_address="raspberrypi"
print("creating new instance")
client = mqtt.Client("control1") #create new instance
client.on_message=on_message #attach function to callback
client.on_disconnect = on_disconnect
print("connecting to broker")
client.connect(broker_address,port=1883) #connect to broker
print("Subscribing to topic","robots/CameraRover")
client.subscribe("robots/CameraRover", qos=1)
print("Publishing message to topic","robots/CameraRover")
client.loop_forever() #start the loop
#sleep(360000) # wait