-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·173 lines (143 loc) · 7.44 KB
/
generate.py
File metadata and controls
executable file
·173 lines (143 loc) · 7.44 KB
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
#!/usr/bin/env python3
# Generate CommonConstants.h and CommonConstants.py to share registers between Pi and onboard Arduino
# Also copy the master Radio.h and TelemetryManager.h into the arduino file folders.
import os
import shutil
import mpy_cross
##
os.system("cp ArduinoFiles/Radio.h ArduinoFiles/RC_Full_Ground_Control/Radio.h")
os.system("cp ArduinoFiles/Radio.h ArduinoFiles/RC_Plane_Onboard_Control/Radio.h")
os.system("cp ArduinoFiles/DataManager.h ArduinoFiles/RC_Full_Ground_Control/DataManager.h")
os.system("cp ArduinoFiles/DataManager.h ArduinoFiles/RC_Plane_Onboard_Control/DataManager.h")
##
hFile = "ArduinoFiles/RC_Plane_Onboard_Control/CommonConstants.h"
pyFile = "GroundControl/CommonConstants.py"
# All registers in the form: (register number,register name,[comments])
registers = [
# Ground to Plane registers:
(0,"setAilerons"),
(1,"setElevator"),
(2,"setRudder"),
(3,"setThrottle"),
(4,"setDropDoor"),
(5,"setAutopilotState"),
(6,"requestTelemetry"),
# Plane to Ground registers:
(64,"reportedDropDoorState"),
(65,"onboardLoopSpeed","Calculated hectomicroseconds (0.1ms units) each loop takes on average."),
(66,"reportedControlState"),
(67,"onboardRSSI","Received signal strength (as a positive value, not negative)"),
(68,"currentBattVoltage","Voltage of battery (raw reading - processing to occur on PI)"),
(69,"onboardCorruptedMessages"),
(70,"onboardError",""), # See below in program for error codes
(71,"onboardPacketReceiveRate","Number of packets received since last request"),
(72,"accelerometerX_HB","High byte"), (73,"accelerometerX_LB","Low byte"),
(74,"accelerometerY_HB"), (75,"accelerometerY_LB"),
(76,"accelerometerZ_HB"), (77,"accelerometerZ_LB"),
(78,"gyroX_HB"), (79,"gyroX_LB"),
(80,"gyroY_HB"), (81,"gyroY_LB"),
(82,"gyroZ_HB"), (83,"gyroZ_LB"),
# Ground Arduino to PI registers:
(128,"groundLoopSpeed","Milliseconds each loop takes on average."),
(129,"groundRSSI"),
(130,"groundRadioStarted","0 for success, 1 for failure"),
(131,"aileronTrimPos"),
(132,"elevatorTrimPos"),
(133,"rudderTrimPos"),
(134,"groundPacketSendRate","Incremented only when transmitted packet is >0 bytes."),
# Test Channels:
(192,"testChannel1"),
(193,"testChannel2"),
(194,"testChannel3"),
(195,"testChannel4"),
(255,"DO_NOT_USE"),
]
registersPrefix = "reg_"
# Ground to Air radio link:
controlCentreFrequency = 458.4875E6 # 458.4875 MHz, IR2030 pg69 (version of April 2021)
groundToAirTxPower = 20 # 100mW (20dBm), legal maximum for this band. (2dBm = 1.5849mW, 10dBm = 10mW, 20dBm = 100mW).
controlChannelNumber = 23 # Can be anywhere from 1-40
controlChannelSpacing = 25E3 # 25kHz
groundToAirBandwidth = 125E3 # 125kHz, aka 5 channels wide. Supported values: 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3, 41.7E3, 62.5E3, 125E3, 250E3, and 500E3
groundToAirFrequency = controlCentreFrequency + (controlChannelNumber * controlChannelSpacing)
print(f"groundToAirFrequency: {groundToAirFrequency}Hz, groundToAirBandwidth: {groundToAirBandwidth}:Hz (channel {controlChannelNumber}), groundToAirTxPower: {groundToAirTxPower}dBm ({round(10**(groundToAirTxPower/10),2)}mW)")
# Air to Ground radio link:
telemetryCentreFrequency = 434.04E6 - 25E3 # 434.04 Mhz - 25kHz channel spacing, IR2030 pg69 (version of April 2021)
airToGroundTxPower = 10 # 10mW (10dBm), legal maximum for this band. (2dBm = 1.5849mW, 10dBm = 10mW, 20dBm = 100mW).
telemetryChannelNumber = 17 # Can be anywhere from 1-30
telemetryChannelSpacing = 25E3
airToGroundBandwidth = 125E3 # 125kHz, aka 5 channels wide. Supported values: 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3, 41.7E3, 62.5E3, 125E3, 250E3, and 500E3
airToGroundFrequency = telemetryCentreFrequency + (telemetryChannelNumber * telemetryChannelSpacing)
print(f"airToGroundFrequency: {airToGroundFrequency}Hz, airToGroundBandwidth: {airToGroundBandwidth}:Hz (channel {telemetryChannelNumber}), airToGroundTxPower: {airToGroundTxPower}dBm ({round(10**(groundToAirTxPower/10),2)}mW)")
# Shared constants in the form: (constant name,constant value,[comments])
constants = [
("batteryVoltageReadingOffset",550),
("maxRadioMessageLength",32,"Bytes"),
("unlockDoorSignal",100,"Signal to send from ground to unlock door."),
("lockDoorSignal",200,"Signal to send from ground to lock door."),
("groundToAirFrequency",groundToAirFrequency),
("groundToAirBandwidth",groundToAirBandwidth),
("airToGroundFrequency",airToGroundFrequency),
("airToGroundBandwidth",airToGroundBandwidth),
("groundToAirTxPower",groundToAirTxPower),
("airToGroundTxPower",airToGroundTxPower),
("airTelemetryInterval",500,"Milliseconds between requests for telemetry from plane."),
("groundTelemetryInterval",250,"Milliseconds between sending Ground-Pi telemetry."),
# Error message constants:
("conflictingDropDoorMessageERR",0,"Two messages sent to the drop door were both different"),
("conflictingControlStateMessageERR",1,"Two messages sent to change the control state were both different"),
]
with open(hFile,"w") as h_file:
h_file.write("// This is an autogenerated file, see generate.py for edits. //\n\n")
for reg in registers:
writeString = "#define "+registersPrefix+str(reg[1])+" "+str(reg[0])
if len(reg) == 3: # i.e. if comment exists
writeString += " // "+reg[2]
h_file.write(writeString+"\n")
h_file.write("\n")
for const in constants:
if(abs(const[1]) >= 32767):
numType = "long"
else:
numType = "int"
writeString = "const "+numType+" "+str(const[0])+" = "+str(const[1])+";"
if len(const) == 3: # i.e. if comment exists
writeString += " // "+const[2]
h_file.write(writeString+"\n")
with open(pyFile,"w") as py_file:
py_file.write("## This is an autogenerated file, see generate.py for edits. ##\n\n")
py_file.write("registers = {\n")
for reg in registers:
writeString = ' "'+str(reg[1])+'": '+str(reg[0])+','
if len(reg) == 3: # i.e. if comment exists
writeString += " # "+reg[2]
py_file.write(writeString+"\n")
py_file.write("}\n\n")
py_file.write("commonConstants = {\n")
for const in constants:
writeString = ' "'+str(const[0])+'": '+str(const[1])+','
if len(reg) == 3: # i.e. if comment exists
writeString += " # "+const[2]
py_file.write(writeString+"\n")
py_file.write("}\n")
# Copy files into the ESP8266 folder
try:
shutil.rmtree("GroundControl/ESP8266")
except FileNotFoundError:
pass
os.mkdir("GroundControl/ESP8266")
shutil.copy("GroundControl/CommonConstants.py","GroundControl/ESP8266/")
shutil.copy("GroundControl/serial.py","GroundControl/ESP8266/")
shutil.copy("GroundControl/webServer.py","GroundControl/ESP8266/")
manageDataCode = open("GroundControl/manageData.py").read()
for const in constants:
manageDataCode = manageDataCode.replace(f"_{const[0]}",str(const[1]))
for reg in registers:
manageDataCode = manageDataCode.replace(f"_{reg[1]}",str(reg[0]))
with open("GroundControl/ESP8266/manageData.py", "w") as f:
f.write(manageDataCode)
for file in os.listdir("GroundControl/ESP8266"):
mpy_cross.run(f"GroundControl/ESP8266/{file}")
os.system("rm GroundControl/ESP8266/*.py")
shutil.copytree("GroundControl/static","GroundControl/ESP8266/static")
os.system("cp ArduinoFiles/RC_Plane_Onboard_Control/CommonConstants.h ArduinoFiles/RC_Full_Ground_Control/CommonConstants.h")