forked from waymond91/APM2.5-6_Custom_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APM2.5-6_Custom_Firmware.ino
366 lines (313 loc) · 7.34 KB
/
APM2.5-6_Custom_Firmware.ino
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <SPI.h>
#include "MPU6000.h"
#include "APM_PPM.h"
#include "APM2RCOutput.h"
#include "COBS.h"
//LED output pins
const int ledBlue = 25;
const int ledYellow = 26;
const int ledRed = 27;
//SPI Bus SlaveSelect pins
const int imuSelect = 53;
const int pressureSelect = 40;
/*
_______________DataFlash(AT45DB161-MU) Pinout_______________
PJ1 = MOSI_DF = 14
PJ2 = SCK_DF = NA
PA6 = CS_DF = 28
PG0 = RST_DEF = 41
I assume these pins go to some SPI hardware...
*/
/*
_______________Magnetometer(HMC5883L) Pinout________________
SCL = PD0 = 21
SDA = PD1 = 20
This seems like the defualt bus
*/
/*
_______________Servo PPM Encoder Pinout______________________
Definitely a lot more going on in the circuit than I orignally thought.
ATMEGA32U4 ATMEGA2560
PPM_PD1 PA2
PPM_PD0 PA1
PPM_PC2 => Throws switch for whole system
This pin is on the ATMEGA32U4
This will throw the connections for pins
RX0-1 ____PPM_TX (Normally Open)
ATMEGA2560_PE0 (RX?)_____|____3DR_RX (Normally Closed)
TX0-O ____PPM_RX (Normally Open)
ATMEGA2560_PE1 (TX?)_____|____3DR_TX (Normally Closed)
*/
APM_PPM rxin;
/*
_______________Extra IO_______________________________________
IO1 = PF0 = ADC0
IO2 = PF1 = ADC1
IO3 = PF2 = ADC2
IO4 = PF3 = ADC3
IO5 = PF4 = ADC4
IO6 = PF5 = ADC5
IO7 = PF6 = ADC6
IO8 = PF8 = ADC7
IO9 = PK0 = ADC8
*/
const int io_count = 9;
const int io_pins[9] = {A0, A1, A2, A3, A4, A5, A6, A7, A8};
APM2RCOutput output;
//SPI Bus SlaveSelect pins
MPU6000 imu;
float accelVec[3];
float gyroVec[3];
#define X 0
#define Y 1
#define Z 2
void setup () {
Serial.begin(115200);
pinMode(ledBlue, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledRed, OUTPUT);
digitalWrite(ledBlue, HIGH);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledRed, HIGH);
pinMode(imuSelect, OUTPUT);
pinMode(pressureSelect, OUTPUT);
digitalWrite(imuSelect, HIGH);
digitalWrite(pressureSelect, HIGH);
imu.initialize();
output.init();
for (int i = 0; i < 8; i++) {
output.enable_ch(i);
}
for (int i = 0; i < 8; i++) {
pinMode(A0 + i, INPUT);
}
rxin.initialize();
}
class Rate {
public:
Rate(uint32_t target_us);
void tick();
bool tick_async();
private:
uint32_t target_us;
uint32_t last_us;
};
Rate::Rate(uint32_t target_us) : target_us(target_us), last_us(0) {}
void Rate::tick() {
//Serial.println("");
//Serial.println(last_us);
digitalWrite(ledBlue, 1); // Turn off blue when sleeping
while ((micros() - last_us) < target_us) {
delay(0);
}
//Serial.println(micros() - last_us);
last_us = micros();
digitalWrite(ledBlue, 0); // Turn on blue when in action
}
bool Rate::tick_async() {
// Return true if tick expired
if ((micros() - last_us) < target_us) {
return false;
}
last_us = micros();
return true;
}
Rate loop_rate(10000);
Rate loop_1s(1000000);
class SerialServo {
public:
SerialServo();
void update_from_serial();
void update_output();
bool is_enabled();
private:
uint32_t last_update;
bool enabled;
};
SerialServo::SerialServo() : last_update(0), enabled(false) {}
bool SerialServo::is_enabled() {
return enabled;
}
void SerialServo::update_from_serial() {
int ch = Serial.read();
if (ch == -1) {
return;
}
uint8_t buf[32];
size_t buflen = 0;
size_t max_skip = 20;
bool has_packet = false;
uint8_t buf_head[4];
bool first_loop = true;
while(max_skip > 0) {
if (! first_loop) {
// Prevent the first byte read above from overwritten
ch = Serial.read();
}
first_loop = false;
if (ch == -1) {
max_skip -= 1;
continue;
}
if (buflen < 4) {
buf_head[0] = buf_head[1];
buf_head[1] = buf_head[2];
buf_head[2] = buf_head[3];
buf_head[3] = ch;
if (buf_head[1] == 'O' && buf_head[2] == 'U' && buf_head[3] == 'T') {
memcpy(buf, buf_head, 4);
buflen = 4;
}
continue;
}
buf[buflen] = ch;
buflen ++;
if (ch == 0) {
has_packet = true;
break;
}
if (buflen >= 32) {
return;
}
}
if (! has_packet) {
return;
}
uint8_t buf2[32];
size_t len2 = COBSDecode(buf, buflen, buf2, 32);
if (len2 <= 0) {
// Bad parse
return;
}
uint16_t * outputs = (uint16_t *)(buf2 + 3);
for (int i = 0; i < 8; i++) {
if (outputs[i] > 3000) {
// Shortcut to disable servo
output.disable_ch(i);
} else {
output.enable_ch(i);
output.write(i, outputs[i]);
}
}
enabled = true;
last_update = millis();
}
void SerialServo::update_output() {
// Disable if timeout
if (millis() - last_update > 100) {
for (int i = 0; i < 8; i++) {
output.disable_ch(i);
}
enabled = false;
}
}
SerialServo ss;
class PowerModule {
public:
PowerModule();
void update();
private:
uint8_t osr;
uint8_t loop_count;
uint32_t volt_sum;
uint32_t cur_sum;
uint32_t pwr_sum;
};
PowerModule::PowerModule() :
osr(32),
loop_count(0),
volt_sum(0),
cur_sum(0),
pwr_sum(0) {
pinMode(A12, INPUT);
pinMode(A13, INPUT);
}
void PowerModule::update() {
// Current: ADC12 INA169 R_L = 100K R_S = 5e-4
// Current 1A at batt = 0.05V at input = around 10 at input
// Voltage: ADC13 1V at batt = 0.1V at input = around 20 at input
uint16_t cur = analogRead(A12);
uint16_t volt = analogRead(A13);
if (cur > cur_sum) {
cur_sum = cur;
}
volt_sum += volt;
uint32_t energy = cur * volt;
pwr_sum += energy;
loop_count += 1;
if (loop_count >= osr) {
uint8_t buf[32], buf2[32];
buf[0] = 'P';
buf[1] = 'W';
buf[2] = 'R';
buf[3] = loop_count;
// Peak current in LSB
*(uint32_t*)(buf + 4) = cur_sum;
// Voltage in sum-LSB
// To be divided by osr in upper level
*(uint32_t*)(buf + 8) = volt_sum;
// Energy used in sum-LSB
*(uint32_t*)(buf + 12) = pwr_sum;
size_t buflen = COBSEncode(buf, 16, buf2, 32);
Serial.write(buf2, buflen);
loop_count = 0;
cur_sum = 0;
volt_sum = 0;
pwr_sum = 0;
}
}
PowerModule pm;
void loop() {
loop_spi_print();
loop_ppm_print();
loop_adc_print();
pm.update();
ss.update_from_serial();
ss.update_output();
digitalWrite(ledYellow, !ss.is_enabled());
loop_rate.tick();
}
void loop_adc_print () {
uint8_t buf[32], buf2[32];
buf[0] = 'A';
buf[1] = 'D';
buf[2] = 'C';
for (int i = 0; i < 8; i++) {
*(uint16_t *)(buf + 3 + 2 * i) = analogRead(A0 + i);
}
size_t buflen = COBSEncode((uint8_t *)buf, 19, buf2, 32);
Serial.write(buf2, buflen);
}
void loop_ppm_print () {
uint8_t buf[32], buf2[32];
buf[0] = 'P';
buf[1] = 'P';
buf[2] = 'M';
buf[3] = rxin.newData();
rxin.read((uint16_t *)(buf+4));
size_t buflen = COBSEncode((uint8_t *) buf, 4 + (numChannel * 2), buf2, 32);
Serial.write(buf2, buflen);
}
void loop_spi_print () {
int16_t dofs[6];
dofs[0] = imu.readWord(0x3B);
dofs[1] = imu.readWord(0x3D);
dofs[2] = imu.readWord(0x3F);
dofs[3] = imu.readWord(0x43);
dofs[4] = imu.readWord(0x45);
dofs[5] = imu.readWord(0x47);
uint8_t buf[32], buf2[32];
buf[0] = 'I';
buf[1] = 'M';
buf[2] = 'U';
memcpy(buf+3, dofs, 12);
size_t buflen = COBSEncode((uint8_t *) buf, 15, buf2, 32);
Serial.write(buf2, buflen);
}
byte spi_read(byte read_command) {
digitalWrite(imuSelect, LOW);
SPI.transfer(read_command);
byte miso_data = SPI.transfer(0);
digitalWrite(imuSelect, HIGH);
return miso_data;
}