/
restraining_bolt.ino
268 lines (218 loc) · 6.96 KB
/
restraining_bolt.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
/**
* \details
* This program is designed for and tested on Teensy 4.1 hardware. It may work on
* other variations of Arduino hardware if they have enough ram available.
*
* \see
* MAVLink for Dummies: https://api.ning.com/files/i*tFWQTF2R*7Mmw7hksAU-u9IABKNDO9apguOiSOCfvi2znk1tXhur0Bt00jTOldFvob-Sczg3*lDcgChG26QaHZpzEcISM5/MAVLINK_FOR_DUMMIESPart1_v.1.1.pdf
* Arduino with MAVLink https://github.com/tmaxxdd/arduino-with-mavlink
* MAVLink message documentation https://mavlink.io/en/messages/common.html
* TaskScheduler https://github.com/arkhipenko/TaskScheduler
* AdruinoLog https://github.com/thijse/Arduino-Log
* PWMServo https://github.com/PaulStoffregen/PWMServo
* TTSAutomate https://ttsautomate.com/
*
* \author Vincent Miceli
*
* \copyright 2020 Vincent Miceli.
*
* \license
* Licensed under the MIT License <http://opensource.org/licenses/MIT>.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
Library headers
*/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <Sd.h>
#include <SerialFlash.h>
#include <ArduinoLog.h>
#include <TaskScheduler.h>
/*
Local headers
*/
#include "LogHelper.h"
#include "Blinker.h"
#include "Configuration.h"
#include "EnumHelper.h"
#include "SerialMAVLinkReader.h"
#include "FileMAVLinkReader.h"
#include "MissionMonitor.h"
constexpr int FAILED_NO_SD = -1;
constexpr int FAILED_NO_TEST_FILE = -2;
constexpr int LOG_LEVEL = LOG_LEVEL_VERBOSE; // Log level
constexpr Stream* LOG_TARGET = &Serial; // Target USB serial port for log messages
constexpr auto CONFIG_FILE_NAME = "config.ini";
bool setupStatus = -1;
//Configuration file settings
Configuration* configuration;
// Audio player
AudioPlayer* audioPlayer;
//MAVLink event receiverand reader
MAVLinkEventReceiver* eventReceiver;
MAVLinkReader* mavlinkReader;
// Scheduler
Scheduler scheduler;
Task blinkTask;
Task readMAVLinkTask;
Task missionMonitorTask;
Task audioPlayerTask;
//Blinker
Blinker blinker;
/**
* @Brief
* Initialize logging, onboard LED, and start task scheduler
*
*/
void setup()
{
// Run audio player task
AudioMemory( 40 );
audioPlayer = new AudioPlayer();
audioPlayerTask.set( TASK_MILLISECOND * 250, TASK_FOREVER, &audioPlayerTick );
scheduler.addTask( audioPlayerTask );
audioPlayerTask.enable();
// Inialize onboard LED
pinMode( LED_BUILTIN, OUTPUT );
/// Serial debug logging setup
Serial.begin( 115200 );
Log.begin( LOG_LEVEL, LOG_TARGET, false );
Log.setSuffix( printNewline );
Log.trace( "" ); // Create a new line before starting timestamp
Log.setPrefix( printTimestamp );
// Check for SD Card
if ( !SD.begin( BUILTIN_SDCARD ) )
{
Log.error( "Could not read SD card" );
setupFailed( FAILED_NO_SD );
audioPlayer->play( NO_STORAGE_CARD_SOUND );
return;
}
else
{
Log.trace( "Found SD card" );
// Read configuration if it exists
configuration = new Configuration();
if ( !configuration->init( CONFIG_FILE_NAME ) )
{
Log.trace( "Using defaults, configuration file not found: %s", CONFIG_FILE_NAME );
}
else
{
Log.trace( "Loaded configuration file: %s", CONFIG_FILE_NAME );
}
// Setup the mavlink reader and monitor
eventReceiver = new MissionMonitor( configuration->getSecondsBeforeEmergencyStop(), (GPS_FIX_TYPE)configuration->getLowestGPSFixType(), audioPlayer );
if ( configuration->getTesting() == true )
{
if ( SD.exists( configuration->getTestFileName() ) )
{
Log.trace( "Using MAVLink test file: %s at %d milliseconds per message", configuration->getTestFileName(), configuration->getFileSpeedMilliseconds() );
Log.trace( "Restraining bolt starting...." );
audioPlayer->play( REPLAY_FROM_FILE_SOUND );
mavlinkReader = new FileMAVLinkReader( configuration->getTestFileName(), eventReceiver, configuration->getFileSpeedMilliseconds() );
}
else
{
Log.error( "Could not find test file: %s", configuration->getTestFileName() );
setupFailed( FAILED_NO_TEST_FILE );
audioPlayer->play( NO_TEST_FILE );
return;
}
}
else
{
Log.trace( "Using real time MAVLink over serial 1" );
Log.trace( "Restraining bolt starting...." );
mavlinkReader = new SerialMAVLinkReader( &Serial1, eventReceiver );
}
/**
* @brief
* Running a mission live or from file require to different mission timing solutions.
* File uses the recorded time in MAVLink packets while live uses local millis()
*/
eventReceiver->setMissionTimeCallback( []() {return mavlinkReader->getMissionTime(); } );
eventReceiver->setSendModeChangeCallback( []( ROVER_MODE roverMode ) { mavlinkReader->sendChangeMode(roverMode); } );
// Read from MAVLink task
readMAVLinkTask.set( TASK_MILLISECOND * 1, TASK_FOREVER, &mavlinkReaderTick );
scheduler.addTask( readMAVLinkTask );
readMAVLinkTask.enable();
// Run mission task
missionMonitorTask.set( TASK_MILLISECOND * 250, TASK_FOREVER, &eventReceiverTick );
scheduler.addTask( missionMonitorTask );
missionMonitorTask.enable();
setupSucceeded();
}
}
/**
* @brief
* Main program loop provides execution thread to scheduler
*/
void loop()
{
scheduler.execute();
}
/**
* @brief This method will blink the onboard LED with a pattern for a specific error code
* @param errorCode The code for the error that occured
*/
void setupFailed( int errorCode )
{
// Blink Task
blinkTask.set( TASK_MILLISECOND * 250, TASK_FOREVER, &blinkTick );
scheduler.addTask( blinkTask );
blinkTask.enable();
}
void setupSucceeded()
{
// Blink Task
blinkTask.set( TASK_MILLISECOND * 1000, TASK_FOREVER, &blinkTick );
scheduler.addTask( blinkTask );
blinkTask.enable();
}
/**
* @brief MAVLinkReader callback for scheduler
*/
void mavlinkReaderTick()
{
mavlinkReader->tick();
}
/**
* @brief EventReceiver callback for scheduler
*/
void eventReceiverTick()
{
eventReceiver->tick();
}
/**
* @brief Callback for normal LED blinking
*/
void blinkTick()
{
blinker.tick();
}
/**
* @brief Callback for audio queue
*/
void audioPlayerTick()
{
audioPlayer->tick();
}