-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgo.py
131 lines (102 loc) · 3.02 KB
/
go.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
import signal
import socket
import select
from time import sleep
import adafruit_pca9685
import board
import busio
PORTSURGE = 0
STBDSURGE = 1
FWDHEAVE = 2
AFTHEAVE = 3
def pulse_to_duty(pulse_us):
return int(0xFFFF*(pulse_us/(1000000/hat.frequency)))
def shutdown(signal, frame):
stop_thrusters()
print('Stopping sockets...')
for s in conections:
s.close()
print('Stopping Pololu...')
hat.deinit()
print('Exiting...')
exit(0)
def stop_thrusters():
print('Stopping thrusters')
# Allow all accelerations and stop thrusters (4000 full back, 8000 full forward)
neutral_signal = 1500
hat.channels[PORTSURGE].duty_cycle = pulse_to_duty(neutral_signal)
hat.channels[STBDSURGE].duty_cycle = pulse_to_duty(neutral_signal)
hat.channels[FWDHEAVE].duty_cycle = pulse_to_duty(neutral_signal)
hat.channels[AFTHEAVE].duty_cycle = pulse_to_duty(neutral_signal)
def send_voltage(voltage):
for s in conections:
if s != incomingConnection:
s.sendall(str(voltage).encode())
# Subscribe shutdown
signal.signal(signal.SIGINT, shutdown)
print('Setting up socket...')
incomingConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connected = False
while not connected:
try:
incomingConnection.bind(('0.0.0.0', 50000))
connected = True
except:
sleep(1)
incomingConnection.listen(1);
conections = [incomingConnection]
print('Connecting to maestro...')
i2c = busio.I2C(board.SCL, board.SDA)
connected = False
while not connected:
try:
hat = adafruit_pca9685.PCA9685(i2c)
connected = True
except:
sleep(1)
hat.frequency = 400
stop_thrusters()
print('STEMbot is online')
data = []
while 1:
readable, writable, exceptional = select.select(conections, [], conections, 0)
for s in readable:
if s == incomingConnection:
conn, addr = incomingConnection.accept()
print("Connected to "+str(addr))
conections.append(conn)
else:
try:
data += s.recv(1024)
except:
conections.remove(s)
stop_thrusters()
print("Lost connection")
continue
if not data:
conections.remove(s)
stop_thrusters()
print("Lost connection")
continue
while len(data) >= 8:
portSurgeUs = data[0] * 256 + data[1]
stbdSurgeUs = data[2] * 256 + data[3]
fwdHeaveUs = data[4] * 256 + data[5]
aftHeaveUs = data[6] * 256 + data[7]
print(f"port surge: {portSurgeUs}")
print(f"stbd surge: {stbdSurgeUs}")
print(f"fwd heave: {fwdHeaveUs}")
print(f"aft heave: {aftHeaveUs}")
hat.channels[PORTSURGE].duty_cycle = pulse_to_duty(portSurgeUs)
hat.channels[STBDSURGE].duty_cycle = pulse_to_duty(stbdSurgeUs)
hat.channels[FWDHEAVE].duty_cycle = pulse_to_duty(fwdHeaveUs)
hat.channels[AFTHEAVE].duty_cycle = pulse_to_duty(aftHeaveUs)
#print(f"port surge: {hat.channels[PORTSURGE].duty_cycle}")
#print(f"stbd surge: {hat.channels[STBDSURGE].duty_cycle}")
#print(f"fwd heave: {hat.channels[FWDHEAVE].duty_cycle}")
#print(f"aft heave: {hat.channels[AFTHEAVE].duty_cycle}")
data = data[8:]
#Read Voltage
#voltage = 0
#send_voltage(voltage)
#sleep(0.001)