-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteControlCode.ino
More file actions
342 lines (277 loc) · 10.2 KB
/
RemoteControlCode.ino
File metadata and controls
342 lines (277 loc) · 10.2 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
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
//Dependencies
#include <Wire.h>
#include <string>
#include <sstream>
#include "HT_SSD1306Wire.h"
#include "LoRaWan_APP.h"
#include "Arduino.h"
//For LED
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
//Constants
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
#define JOYSTICK1_NEUTRAL 3500
#define JOYSTICK1_HIGH 1500
#define JOYSTICK2_NEUTRAL 3500
#define JOYSTICK2_LOW 1500
//Global Variables
static SSD1306Wire display(0x3c, 500000, SDA, SCL, GEOMETRY_128_64, GPIO10); // addr, freq, SDA, SCL, resolution, rst
// These messages will prevent bogus messages from being interpretted
const uint8_t CONTROLLERMSG_CODE[8] = "UK RC ";
const uint8_t BOATMSG_CODE[8] = "UK BOAT";
// This struct contains commands for the boat
union controllerMsg {
struct {
uint8_t secretCode[8];
bool isGoingForward;
bool isGoingBackward;
bool isSteeringLeft;
bool isSteeringRight;
bool killswitch;
} status;
uint8_t str[13];
};
union boatMsg {
struct {
uint8_t secretCode[8];
int16_t lastRSSI;
int16_t steeringPosition;
float latitude;
float longitude;
float speed;
} status;
uint8_t str[24];
} inboundMsg;
void OnTxDone( void );
void OnTxTimeout( void );
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
void OnRxTimeout( void );
int signalStrength = 0;
uint32_t signalTime = 0;
int displaySelector = 0;
char displayString[32];
// Functions declaration
typedef void (*DisplayFunc)(void);
//Event handlers
static RadioEvents_t RadioEvents = {};
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
controllerMsg updateStatusVals() {
controllerMsg outboundMsg = {0};
memcpy(outboundMsg.status.secretCode, CONTROLLERMSG_CODE, sizeof(CONTROLLERMSG_CODE));
// Update throttle commands
int reading = analogRead(ADC1);
if (reading > JOYSTICK1_NEUTRAL) {
outboundMsg.status.isGoingForward = false;
outboundMsg.status.isGoingBackward = false;
} else if (reading > JOYSTICK1_HIGH) {
outboundMsg.status.isGoingForward = false;
outboundMsg.status.isGoingBackward = true;
} else {
outboundMsg.status.isGoingForward = true;
outboundMsg.status.isGoingBackward = false;
}
// Update steering commands
reading = analogRead(ADC2);
if (reading > JOYSTICK2_NEUTRAL) {
outboundMsg.status.isSteeringRight = false;
outboundMsg.status.isSteeringLeft = false;
} else if (reading > JOYSTICK2_LOW) {
outboundMsg.status.isSteeringRight = false;
outboundMsg.status.isSteeringLeft = true;
} else {
outboundMsg.status.isSteeringRight = true;
outboundMsg.status.isSteeringLeft = false;
}
// Update killswitch
outboundMsg.status.killswitch = digitalRead(GPIO3);
return outboundMsg;
}
// ===== SCREENDISPLAY.INO =====
void displayTeamName() {
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawStringMaxWidth(0, 0, 128, "Team 10:Bluegrass Boataneers");
}
void displayThrottle() {
int throtReading = analogRead(ADC1);
if (throtReading > JOYSTICK1_NEUTRAL) {
snprintf(displayString, sizeof(displayString), "MID: %d", throtReading);
} else if (throtReading > JOYSTICK1_HIGH) {
snprintf(displayString, sizeof(displayString), "LOW: %d", throtReading);
} else {
snprintf(displayString, sizeof(displayString), "HIGH: %d", throtReading);
}
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "Throttle");
display.drawString(0, 20, displayString);
}
void displaySteering() {
int steerReading = analogRead(ADC2);
if (steerReading > JOYSTICK2_NEUTRAL) {
snprintf(displayString, sizeof(displayString), "MID: %d", steerReading);
} else if (steerReading > JOYSTICK2_LOW) {
snprintf(displayString, sizeof(displayString), "LEFT: %d", steerReading);
} else {
snprintf(displayString, sizeof(displayString), "RIGHT: %d", steerReading);
}
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "Steering");
display.drawString(0, 20, displayString);
}
void displayKillSwitch() {
if (digitalRead(GPIO3)) {
snprintf(displayString, sizeof(displayString), "Killswitch ON");
} else {
snprintf(displayString, sizeof(displayString), "Killswitch OFF");
}
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
}
void displayConnectionStrength() {
if (millis() - signalTime < 1000) {
snprintf(displayString, sizeof(displayString), "boat RSSI: %d", inboundMsg.status.lastRSSI);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
snprintf(displayString, sizeof(displayString), "controller RSSI: %d", signalStrength);
display.drawString(0, 20, displayString);
} else {
snprintf(displayString, sizeof(displayString), "Timed out: %d", millis() - signalTime);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
}
}
void displayPosition() {
snprintf(displayString, sizeof(displayString), "lat: %f.3", inboundMsg.status.latitude);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
snprintf(displayString, sizeof(displayString), "lng: %f.3", inboundMsg.status.latitude);
display.drawString(0, 20, displayString);
}
void displaySpeed() {
snprintf(displayString, sizeof(displayString), "speed (mph): %f.3", inboundMsg.status.speed);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
}
void displaySteeringPosition() {
snprintf(displayString, sizeof(displayString), "steerPos: %d", inboundMsg.status.steeringPosition);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, displayString);
}
void VextON(void) {
pinMode(Vext,OUTPUT);
digitalWrite(Vext, LOW);
}
void VextOFF(void) {//Vext default OFF
pinMode(Vext,OUTPUT);
digitalWrite(Vext, HIGH);
}
void writeDisplay(int select) {
select %= 8;
DisplayFunc outputs[] = {displayTeamName, displayThrottle, displaySteering, displayKillSwitch, displayConnectionStrength, displayPosition, displaySpeed, displaySteeringPosition};
// Clear the display
display.clear();
// Draw the current display method
outputs[select]();
display.display();
}
// ===== LORAHANDLING.INO =====
void LoRaSetup() {
RadioEvents.RxTimeout = onRxTimeout;
RadioEvents.RxDone = OnRxDone;
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init(&RadioEvents);
Radio.SetChannel(RF_FREQUENCY);
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
}
void onRxTimeout() {
Serial.println("Rx Timeout");
}
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
signalStrength = rssi;
signalTime = millis();
Serial.println("Received");
if (size == sizeof(boatMsg)) {
boatMsg temp;
memcpy(temp.str, payload, sizeof(boatMsg));
if (memcmp(temp.status.secretCode, BOATMSG_CODE, sizeof(BOATMSG_CODE)) == 0) {
memcpy(inboundMsg.str, payload, sizeof(inboundMsg));
Serial.printf("Copied %s, rssi: %d, pos: %d, lat: %.2f, lon: %.2f, speed: %.2f \n",
inboundMsg.status.secretCode, inboundMsg.status.lastRSSI, inboundMsg.status.steeringPosition,
inboundMsg.status.latitude, inboundMsg.status.longitude, inboundMsg.status.speed);
// Serial.println("Copied.");
}
}
}
void OnTxDone() {
// Serial.printf("Copied %s, fwd: %d, rev: %d, left: %d, right: %d, kill: %d \n",
// outboundMsg.status.secretCode, outboundMsg.status.isGoingForward, outboundMsg.status.isGoingBackward,
// outboundMsg.status.isSteeringLeft, outboundMsg.status.isSteeringRight, outboundMsg.status.killswitch);
Serial.println("Tx Done");
}
void OnTxTimeout() {
Serial.println("Tx Timeout");
}
//Setup function
void setup() {
Serial.begin(115200);
Radio.Init(&RadioEvents);
Radio.SetChannel(RF_FREQUENCY);
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, true, 0, 0, LORA_IQ_INVERSION_ON, 3000);
//Serial.println();
//VextON();
//delay(100);
// Initialize the display
display.init();
display.setFont(ArialMT_Plain_10);
// Read button press
pinMode(GPIO1, INPUT_PULLUP);
pinMode(GPIO3, INPUT_PULLDOWN);
}
void loop() {
// Serial.printf("Status %s, fwd: %d, rev: %d, left: %d, right: %d, kill: %d \n",
// outboundMsg.status.secretCode, outboundMsg.status.isGoingForward, outboundMsg.status.isGoingBackward,
// outboundMsg.status.isSteeringLeft, outboundMsg.status.isSteeringRight, outboundMsg.status.killswitch);
static uint32_t debounceTimer = 0;
// If button pressed, switch display.
if (!digitalRead(GPIO1) && (millis() - debounceTimer > 500)) {
debounceTimer = millis();
displaySelector++;
}
// Loops through the displays
writeDisplay(displaySelector);
// Listen for boat signals
Radio.Rx(100);
delay(100);
Radio.IrqProcess();
// Package up myRC data and send it to the other module
Radio.Send(updateStatusVals().str, sizeof(controllerMsg));
delay(100);
Radio.IrqProcess();
}