-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto.py
114 lines (100 loc) · 2.55 KB
/
auto.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
import RPi.GPIO as gpio
from time import sleep
from motion import *
from retrieve_reading import *
import VL53L0X
import sys
import paho.mqtt.client as mqtt
# MQTT constants
MQTT_HOST = "178.128.97.253"
MQTT_PORT = 1883
MQTT_TIMEOUT = 60
MQTT_TOPIC_SUBSCRIBE = "/bg/fc/robotarm/cmds"
# pinout definitions
M_TL_DIR = 8
M_TL_PUL = 10
M_TL_EN = 12
M_FB_DIR = 11
M_FB_PUL = 13
M_FB_EN = 15
M_TR_DIR = 19
M_TR_PUL = 21
M_TR_EN = 23
SW_FB = 24
SW_TL = 26
SW_TR = 22
PIN = {
"FB": {
"A": {
"DIR": M_FB_DIR,
"PUL": M_FB_PUL,
"EN": M_FB_EN,
"LIM": SW_FB
}
},
"TELESCOPE": {
"L": {
"DIR": M_TL_DIR,
"PUL": M_TL_PUL,
"EN": M_TL_EN,
"LIM": SW_TL
},
"R": {
"DIR": M_TR_DIR,
"PUL": M_TR_PUL,
"EN": M_TR_EN,
"LIM": SW_TR
}
}
}
trigger = False
currPos = 0
def main():
gpio.setmode(gpio.BOARD)
for mode in PIN:
for direction in PIN[mode]:
for pinout in PIN[mode][direction]:
if pinout == "LIM":
gpio.setup(PIN[mode][direction][pinout], gpio.IN, pull_up_down=gpio.PUD_UP)
else:
gpio.setup(PIN[mode][direction][pinout], gpio.OUT)
def auto(dist):
global currPos
main()
pinSetup(PIN)
# sensorObject = retrieve_reading(VL53L0X.VL53L0X())
moveFB(currPos,dist)
sleep(1)
currPos = moveTele(dist)
sleep(1)
gpio.cleanup()
def home():
main()
pinSetup(PIN)
calibrate_sequence = [PIN["TELESCOPE"]["L"], PIN["TELESCOPE"]["R"], PIN["FB"]["A"]]
for sequence in calibate_sequence:
calibrate(sequence)
gpio.cleanup()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC_SUBSCRIBE)
def on_message(client, userdata, msg):
payload = json.loads(msg.payload)
print(payload["position"])
if payload["name"] == "arm1":
if payload["position"] == 0:
home()
elif payload["position"] == 1:
d = 3*2*payload["position"]+149.13*(2*payload["position"]-1)+149.13/2
auto(1.38)
elif payload["position"] == 2:
auto(1.68)
try:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_HOST, MQTT_PORT)
client.loop_forever()
finally:
gpio.cleanup()
client.loop_stop()