-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyTrim.py
256 lines (240 loc) · 6.56 KB
/
pyTrim.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python3
# Script for controlling Pololi G2 rPi-HAT from ProSim v1 generic TCP-driver
# Script written by Mathias Nilsen <m(a)thias.no>
# Licence? Use if you want, but if you make it better or it is handy for your
# project, please tell me. :-)
#
import socket
import sys
import threading
import time
import re
from dual_g2_hpmd_rpi import motors, MAX_SPEED
# This is the only thing you need to edit here..
SERVER = '10.0.10.120'
PORT = 8091
global cs
global flapspos
global trimpos
global trimspeed
global trimmotor
global cmd
global kill
global speedbrake
trimpos = "Not initialized"
trimmotor = "Not initialized"
trimspeed = "NA"
flapspos = "Not initialized"
cmd = "Not initialized"
speedbrake = "Not initialized"
connerrors = 0
kill = 1
def connection(SERVER, PORT):
global cs
global trimpos
global flapspos
global trimmotor
global cmd
global connerrors
global kill
global speedbrake
while True:
try:
# Initialize socket to SERVER
cs = "\033[1;33;40mInitializing..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30) # 30
try:
cs = "\033[1;33;40mTrying to connect to %s:%s" % (SERVER, PORT)
s.connect((SERVER, PORT))
try:
#s.send(b'filter G_PED_ELEV_TRIM')
#s.send(b'filter N_TRIM_MOTOR_VALUE')
#s.send(b'filter B_FLAP')
#s.send(b'filter B_PITCH_CMD')
#s.send(b'B_SPEED_BRAKE')
cs = "\033[1;30;42mConnected to %s:%s" % (SERVER, PORT)
line = b''
while True:
if trimmotor != "Not initialized":
kill = 0
msg, ancmsg, flags, addr = s.recvmsg(8148)
d = msg.decode('utf-8').rstrip()
#part = s.recv(1)
#if part != b'\n':
#line = line + part
#elif part == b'\n':
#d = line.decode('utf-8')
#line = b''
if d.startswith("G_PED_ELEV_TRIM"):
spl = d.split("= ", 1)
trimpos = spl[1]
elif d.startswith("N_TRIM_MOTOR_VALUE"):
spl = re.split(" = |\r", d)
if spl[1] == "1":
trimmotor = "Up"
elif spl[1] == "-1":
trimmotor = "Down"
else:
trimmotor = "Brake"
elif d.startswith("B_FLAP_"):
spl = re.split(' = |_', d)
if "1" in spl[3]:
flapspos = spl[2]
elif d.startswith("B_SPEED_BRAKE_DEPLOY = 1"):
speedbrake = "DEPLOY"
elif d.startswith("B_SPEED_BRAKE_RESTOW = 1"):
speedbrake = "RESTOW"
elif d.startswith("B_PITCH_CMD"):
spl = re.split(" = |\r", d)
if spl[1] == "1":
cmd = "Engaged"
else:
cmd = "Disengaged"
except socket.timeout:
kill = 1
cs = "\033[1;37;41mNothing received in a while. Reconnecting. "
except socket.error as msg:
kill = 1
cs = "\033[1;37;41mCould not connect to ProSIM on %s:%s -> %s. Retrying.." % (SERVER, PORT, msg)
time.sleep(.5)
finally:
connerrors += 1
s.close()
def settrimspeed():
global trimspeed
while True:
if flapspos == "Not initialized":
trimspeed = "NA"
elif cmd is "Disengaged" and flapspos is "0":
trimspeed = 113
elif cmd is "Disengaged" and flapspos is not "0":
trimspeed = 175
elif cmd is "Engaged" and flapspos is "0":
trimspeed = 90
elif cmd is "Engaged" and flapspos is not "0":
trimspeed = 135
else:
kill = 1
time.sleep(.2)
def motorcontrol():
global motorstatus
global motorspeed
motorstatus = "Disabled"
motorspeed = 0
while True:
if kill == 1:
motors.motor1.disable()
motorstatus = "Disabled"
time.sleep(1)
elif (trimspeed == 113 or trimspeed == 175 or trimspeed == 90 or trimspeed == 135):
motors.motor1.setSpeed(0)
motorspeed = 0
motors.motor1.enable()
motorstatus = "Enabled"
if trimmotor is "Up":
if trimspeed is not 480: # Give some boost if not max speed
motors.motor1.setSpeed(480)
motorspeed = 480
if trimspeed == 175:
time.sleep(.25)
elif trimspeed == 135:
time.sleep(.2)
elif trimspeed == 113:
time.sleep(.15)
elif trimspeed == 90:
time.sleep(.15)
else:
time.sleep(.15)
while trimmotor is "Up":
motorspeed = trimspeed
motors.motor1.setSpeed(motorspeed)
time.sleep(.01)
motors.motor1.setSpeed(-480)
if trimspeed == 175:
time.sleep(.23)
elif trimspeed == 90:
time.sleep(.12)
else:
time.sleep(.15)
motors.motor1.setSpeed(0)
elif trimmotor is "Down":
if trimspeed is not 480: # Give some boost if not max speed
motors.motor1.setSpeed(-480)
motorspeed = -480
if trimspeed == 175:
time.sleep(.25)
elif trimspeed == 135:
time.sleep(.2)
elif trimspeed == 113:
time.sleep(.15)
elif trimspeed == 90:
time.sleep(.15)
else:
time.sleep(.15)
while trimmotor is "Down":
motorspeed = trimspeed * -1
motors.motor1.setSpeed(motorspeed)
time.sleep(.01)
motors.motor1.setSpeed(480)
if trimspeed == 175:
time.sleep(.22)
elif trimspeed == 90:
time.sleep(.12)
else:
time.sleep(.15)
motors.motor1.setSpeed(0)
else:
motors.motor1.setSpeed(0)
motorspeed = 0
time.sleep(.1)
else:
motors.motor1.disable()
motorstatus = "Disabled"
time.sleep(.1)
def speedbrakecontrol():
global speedbrake
time.sleep(1)
motors.motor2.enable()
motors.motor2.setSpeed(0)
speedbrake = "Brake"
while True:
if speedbrake == "DEPLOY":
#motors.motor2.enable()
motors.motor2.setSpeed(-300)
time.sleep(.9)
motors.motor2.setSpeed(0)
#motors.motor2.disable()
speedbrake = "Brake"
elif speedbrake == "RESTOW":
#motors.motor2.enable()
motors.motor2.setSpeed(300)
time.sleep(.9)
motors.motor2.setSpeed(0)
#motors.motor2.disable()
speedbrake = "Brake"
time.sleep(.2)
def status():
while True:
print(chr(27) + "[2J")
print("\033[0;0H", end="")
print(" - pyTrim - ")
print("Connection status: " +cs)
print("\033[0;37;40m", end="")
print("Connection errors: " +str(connerrors), end="\r\n\n\n")
print("Trim position: \t\t" +str(trimpos))
print("Trim motor (from sim): \t" +trimmotor)
print("Trim speed: \t\t" +str(trimspeed), end="\r\n\n")
print("Flaps position: \t" +str(flapspos))
print("Autopilot: \t\t" +cmd, end="\r\n\n\n")
print("Actual motorstatus: \t" +motorstatus)
print("Actual motorspeed: \t" +str(motorspeed))
print("Killswitch: \t\t" +str(kill), end="\r\n\n")
print("Speedbrake: \t\t" +speedbrake)
print("\033[0;37;40m", end="")
time.sleep(.5)
threading.Thread(target=connection, args=(SERVER,PORT)).start()
threading.Thread(target=settrimspeed).start()
threading.Thread(target=motorcontrol).start()
threading.Thread(target=status).start()
threading.Thread(target=speedbrakecontrol).start()