-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffoFly_Receiver.cpp
236 lines (224 loc) · 6.58 KB
/
AffoFly_Receiver.cpp
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
#include "Arduino.h"
#include "types.h"
#include "config.h"
#include "def.h"
#include "LED.h"
#include "Buzzer.h"
#include "Battery.h"
#include "Sonar.h"
#include "Output.h"
#include "EEPROM.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"
#include "AffoFly_Receiver.h"
static const uint32_t radioReceiveInterval = 5000; //200hz
uint32_t previousRadioReceiveTime = 0;
static const uint32_t generateOutputInterval = 10000; //100hz
uint32_t previousGenerateOutputTime = 0;
uint32_t previousSignalTime = 0;
static const uint32_t signalTimeout = 500000; // 0.5s
uint16_t packageCount = 0;
#ifdef DEBUG
uint32_t loopCount = 0;
#endif
static const uint32_t checkPackageCountInterval = 1000000; //1hz
uint32_t previousCheckPackageCountTime = 0;
uint32_t token = 0;
uint8_t channel = 0;
ControlData controlData;
TxBindData txBindData;
RF24 radio(NRF_CE_PIN, NRF_CSN_PIN);
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
EEPROM_ensureValid();
#ifdef DEBUG
EEPROM_dumpAll();
#endif
LED_init();
// Turn both LEDs on until next pattern kicks in
LEDFlashPattern pattern = systemStarted;
LED_start(pattern);
LED_flash(micros());
pinMode(BIND_PIN, INPUT_PULLUP);
if (digitalRead(BIND_PIN) == LOW) {
#ifdef DEBUG
Serial.println("Binding...");
#endif
radioBind();
} else {
token = EEPROM_readToken();
channel = EEPROM_readChannel();
if (token == 0 || channel == 0) {
LEDFlashPattern pattern = radioBindingRequired;
LED_start(pattern);
LED_flash(micros());
#ifdef DEBUG
Serial.println("Binding required");
#endif
while (1) {
delay(600000);
}
} else {
radioInit();
resetData();
Output_init();
#ifdef V_BAT
Battery_init();
#endif
}
}
delay(100);
}
void loop() {
uint32_t currentTime = micros();
receiveData(currentTime);
checkPackageCount(currentTime);
LED_flash(currentTime);
generateOutput(currentTime);
#ifdef V_BAT
Battery_read(currentTime);
#endif
#ifdef SONAR
Sonar_read(currentTime);
#endif
#ifdef DEBUG
loopCount++;
#endif
}
void radioInit() {
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setAutoAck(false);
radio.setChannel(channel);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, RADIO_PIPE);
radio.startListening();
#ifdef DEBUG
Serial.print(F("Channel: ")); Serial.println(channel);
printf_begin();
radio.printDetails();
#endif
}
void receiveData(uint32_t currentTime) {
ControlData tempData;
if (radio.available()) {
radio.read(&tempData, sizeof(ControlData));
#ifdef DEBUG
Serial.print("ROM Token: "); Serial.print(token); Serial.print(" ");
Serial.print("RADIO Token: "); Serial.print(tempData.Token); Serial.print(" ");
Serial.println();
#endif
if (tempData.Token == token) {
controlData = tempData;
previousSignalTime = currentTime;
packageCount++;
#ifdef DEBUG
Serial.print("THR: "); Serial.print(controlData.Throttle); Serial.print(" ");
Serial.print("YAW: "); Serial.print(controlData.Yaw); Serial.print(" ");
Serial.print("PIT: "); Serial.print(controlData.Pitch); Serial.print(" ");
Serial.print("ROL: "); Serial.print(controlData.Roll); Serial.print(" ");
Serial.print("AUX1: "); Serial.print(controlData.Aux1); Serial.print(" ");
Serial.print("AUX2: "); Serial.print(controlData.Aux2); Serial.print(" ");
Serial.print("AUX3: "); Serial.print(controlData.Aux3); Serial.print(" ");
Serial.print("AUX4: "); Serial.print(controlData.Aux4); Serial.print(" ");
Serial.print("AUX5: "); Serial.print(controlData.Aux5); Serial.print(" ");
Serial.print("AUX6: "); Serial.print(controlData.Aux6); Serial.print(" ");
Serial.print("SWD1: "); Serial.print(controlData.Swd1); Serial.print(" ");
Serial.print("SWD2: "); Serial.print(controlData.Swd2); Serial.print(" ");
Serial.println();
#endif
}
}
}
void generateOutput(uint32_t currentTime) {
if (currentTime - previousGenerateOutputTime >= generateOutputInterval) {
previousGenerateOutputTime = currentTime;
Output_data(controlData);
}
}
void resetData() {
controlData.Throttle = 1000;
controlData.Yaw = 1500;
controlData.Pitch = 1500;
controlData.Roll = 1500;
}
void checkPackageCount(uint32_t currentTime) {
if (currentTime - previousCheckPackageCountTime >= checkPackageCountInterval) {
previousCheckPackageCountTime = currentTime;
#ifdef DEBUG
Serial.print("Package count: "); Serial.print(packageCount); Serial.print(" ");
Serial.print("Loop count: "); Serial.println(loopCount);
loopCount = 0;
#endif
LEDFlashPattern pattern;
if (packageCount >= SIGNAL_HIGH_PACKAGE_COUNT) {
pattern = signalHigh;
} else if (packageCount >= SIGNAL_MEDIUM_PACKAGE_COUNT) {
pattern = signalMedium;
} else if (packageCount > 0) {
pattern = signalLow;
} else {
pattern = signalNone;
resetData();
#ifdef DEBUG
Serial.println("Lost signal, control data has been reset.");
#endif
}
LED_start(pattern);
packageCount = 0;
}
}
void radioBind() {
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.enableAckPayload();
radio.openReadingPipe(1, RADIO_PIPE);
radio.startListening();
delay(100);
radio.stopListening();
bool bound = false;
while (1) {
if (!bound) {
TxBindData txBindData;
for (uint8_t i = RADIO_CHANNEL_LOWER_BOUNDARY; i <= RADIO_CHANNEL_UPPER_BOUNDARY; i++) {
radio.setChannel(i);
radio.startListening();
radio.writeAckPayload(1, RX_IDENTIFIER, 10);
delay(10);
while (radio.available()) {
radio.read(&txBindData, sizeof(TxBindData));
Serial.print("id: "); Serial.print(txBindData.TxIdentifier); Serial.print(" ");
if (strcmp(txBindData.TxIdentifier, TX_IDENTIFIER) == 0) {
Serial.print("token: "); Serial.print(txBindData.Token); Serial.println();
radio.writeAckPayload(1, RX_IDENTIFIER, strlen(RX_IDENTIFIER));
EEPROM_writeToken(txBindData.Token);
EEPROM_writeChannel(i);
channel = i;
bound = true;
break;
}
}
radio.stopListening();
#ifdef DEBUG
Serial.print(".");
#endif
}
LEDFlashPattern pattern = radioSearching;
LED_start(pattern);
#ifdef DEBUG
Serial.println();
#endif
} else {
LEDFlashPattern pattern = radioBound;
LED_start(pattern);
#ifdef DEBUG
Serial.print("Bound on channel: "); Serial.println(channel);
#endif
}
}
}