forked from bitdog-io/restraining_bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissionMonitor.cpp
More file actions
282 lines (223 loc) · 7.51 KB
/
Copy pathMissionMonitor.cpp
File metadata and controls
282 lines (223 loc) · 7.51 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
//
//
//
#include <ctime>
#include "MissionMonitor.h"
#include "EnumHelper.h"
#include "ArduinoLog.h"
#include "AudioPlayer.h"
MissionMonitor::MissionMonitor( uint32_t secondsBeforeEmergencyStop, GPS_FIX_TYPE lowestGpsFixTpye, AudioPlayer* audioPlayer )
{
_secondsBeforeEmergencyStop = secondsBeforeEmergencyStop;
_lowestGpsFixTpye = lowestGpsFixTpye;
_audioPlayer = audioPlayer;
}
void MissionMonitor::onHeatbeat( mavlink_heartbeat_t mavlink_heartbeat )
{
_lastHeartbeatTimeMilliseconds = getMissionTime();
// Check for state change
if ( mavlink_heartbeat.type == (uint8_t)MAV_TYPE_GROUND_ROVER )
{
if ( mavlink_heartbeat.custom_mode != (uint32_t)_roverMode )
{
ROVER_MODE roverMode = (ROVER_MODE)mavlink_heartbeat.custom_mode;
MAV_MODE_FLAG mavModeFlag = (MAV_MODE_FLAG)mavlink_heartbeat.base_mode;
Log.trace( "Rover mode changed from %s to %s ", EnumHelper::convert( _roverMode ), EnumHelper::convert( roverMode ) );
play( roverMode );
_mavModeFlag = mavModeFlag;
_roverMode = roverMode;
// The drive mode changed, restart everything
start();
}
}
if ( _firstHeartbeat == false )
{
_firstHeartbeat = true;
_audioPlayer->play( MAVLINK_GOOD_SOUND );
}
}
void MissionMonitor::onMissionItemReached( mavlink_mission_item_reached_t mavlink_mission_item_reached )
{
Log.trace( "Destination reached: %d", mavlink_mission_item_reached.seq );
_lastProgressMadeTimeMilliseconds = getMissionTime();
}
void MissionMonitor::onNavControllerOutput( mavlink_nav_controller_output_t mavlink_nav_controller )
{
bool progressMade = false;
uint32_t missionTime = getMissionTime();
if ( _lastDistanceToWaypoint == -1 )
{
Log.trace( "Distance to new waypoint is %d", mavlink_nav_controller.wp_dist );
progressMade = true;
}
else if ( _lastDistanceToWaypoint == mavlink_nav_controller.wp_dist )
{
//Log.trace( "Distance to waypoint is %d Mission Time: %d", mavlink_nav_controller.wp_dist,getMissionTime() );
// if the last report was going in the wrong direction, don't capture time as progress being made
if ( !_wrongDirection )
progressMade = true;
}
else if ( _lastDistanceToWaypoint < mavlink_nav_controller.wp_dist )
{
// We are making negative progress toward waypoint
Log.trace( "Distance to waypoint is %d and growing for %d milliseconds", mavlink_nav_controller.wp_dist, missionTime - _lastProgressMadeTimeMilliseconds );
_wrongDirection = true;
_wrongDirectionCount += 1;
}
else
{
Log.trace( "Distance to waypoint is %d and closing Mission Time: %d", mavlink_nav_controller.wp_dist, getMissionTime() );
progressMade = true;
}
_lastDistanceToWaypoint = mavlink_nav_controller.wp_dist;
if ( progressMade )
{
_lastProgressMadeTimeMilliseconds = missionTime;
_wrongDirection = false;
_wrongDirectionCount = 0;
}
}
void MissionMonitor::onMissionCurrent( mavlink_mission_current_t mavlink_mission_current )
{
if ( _currentWaypointSequenceId != mavlink_mission_current.seq )
{
Log.trace( "New destination: %d", mavlink_mission_current.seq );
_currentWaypointSequenceId = mavlink_mission_current.seq;
_lastDistanceToWaypoint = -1;
_lastProgressMadeTimeMilliseconds = getMissionTime();
}
}
void MissionMonitor::onGPSRawInt( mavlink_gps_raw_int_t mavlink_gps_raw_int )
{
_gps1FixType = (GPS_FIX_TYPE)mavlink_gps_raw_int.fix_type;
}
void MissionMonitor::onGPS2Raw( mavlink_gps2_raw_t mavlink_gps2_raw )
{
_gps2FixType = (GPS_FIX_TYPE)mavlink_gps2_raw.fix_type;
}
void MissionMonitor::tick()
{
evaluateMission();
if ( !_firstTick )
{
_firstTick = true;
_audioPlayer->play( READY_SOUND );
}
}
/**
* @brief
* This method will monitor the current state of the flight controller. When the flight controller
* is in AUTO mode, the logic will ensure progress is being made between waypoints. If it detects a problem, this
* method will call fail mission.
*/
void MissionMonitor::evaluateMission()
{
uint32_t missionTime = getMissionTime();
uint32_t timeDifference = missionTime - _lastProgressMadeTimeMilliseconds;
uint8_t maxGPSFixType = max( _gps1FixType, _gps2FixType );
// Check if the rover is in auto mode
bool isAutoMode = _roverMode == ROVER_MODE_AUTO;
// Check if the rover is in hold mode
bool isHoldMode = _roverMode == ROVER_MODE_HOLD;
// MAVLink is lost if the last heartbeat was received over _secondsBeforeEmergencyStop seconds ago
bool mavlinkLost = _firstHeartbeat && missionTime - _lastHeartbeatTimeMilliseconds >= (_secondsBeforeEmergencyStop * 1000);
// GPS is lost if the fix type drops below _lowestGpsFixTpye
bool gpsLost = maxGPSFixType < _lowestGpsFixTpye;
// The rover is no longer making progress if the last time it closed the distance to the next waypoint was over _secondsBeforeEmergencyStop seconds
bool noProgress = _lastProgressMadeTimeMilliseconds != 0 && timeDifference >= (_secondsBeforeEmergencyStop * 1000);
if ( !_isFailed )
{
if ( mavlinkLost )
{
// We haven't heard from the flight controller for some time, we can't continue
Log.trace( "MAVLink lost" );
failMission();
_firstHeartbeat = false; // Start looking for first heartbeat again
_audioPlayer->play( MAVLINK_BAD_SOUND );
}
if ( isAutoMode )
{
if ( gpsLost )
{
// Put the rover in hold mode
// If the rover does go into hold mode all of the progress counters will be reset by the start() function
sendModeChange( ROVER_MODE_HOLD );
Log.trace( "GPS lost, current fix type: %d", maxGPSFixType );
_audioPlayer->play( GPS_SIGNAL_LOW_SOUND );
}
else if ( noProgress )
{
// We haven't made progress in the correct direction for some time, stop the rover
Log.trace( "Last progress time: %d ", round( missionTime - _lastHeartbeatTimeMilliseconds ) );
failMission();
}
if ( _wrongDirectionCount == 2 )
{
// wrong direction detected twice, play sound and bump count to avoid play this sound again, its the only thing this count is being used for
_wrongDirectionCount += 1;
_audioPlayer->play( WRONG_DIRECTION_SOUND );
}
}
else if ( isHoldMode && !gpsLost )
{
// We are in hold mode and gps single is good now
// Put the rover into auto mode after a gps signal lost
sendModeChange( ROVER_MODE_AUTO );
}
}
}
void MissionMonitor::failMission()
{
Log.trace( "*************** SHUTDOWN *********************************************" );
_isFailed = true;
_servoRelay.powerRelayOff();
_servoRelay.alarmRelayOn();
_audioPlayer->play( EMERGENCY_STOP_SOUND );
Log.trace( "**********************************************************************" );
}
void MissionMonitor::start()
{
_lastProgressMadeTimeMilliseconds = 0;
_isFailed = false;
_wrongDirection = false;
_wrongDirectionCount = 0;
_servoRelay.powerRelayOn();
_servoRelay.alarmRelayOff();
}
void MissionMonitor::play( ROVER_MODE roverMode )
{
switch ( roverMode )
{
case ROVER_MODE_MANUAL:
_audioPlayer->play( MANUAL_MODE_SOUND );
break;
case ROVER_MODE_ACRO:
_audioPlayer->play( ACRO_MODE_SOUND );
break;
case ROVER_MODE_STEERING:
_audioPlayer->play( STEERING_MODE_SOUND );
break;
case ROVER_MODE_HOLD:
_audioPlayer->play( HOLD_MODE_SOUND );
break;
case ROVER_MODE_LOITER:
_audioPlayer->play( LOITER_MODE_SOUND );
break;
case ROVER_MODE_AUTO:
_audioPlayer->play( AUTO_MODE_SOUND );
break;
case ROVER_MODE_RTL:
_audioPlayer->play( RTL_MODE_SOUND );
break;
case ROVER_MODE_SMART_RTL:
_audioPlayer->play( SRTL_MODE_SOUND );
break;
case ROVER_MODE_GUIDED:
_audioPlayer->play( GUIDED_MODE_SOUND );
break;
case ROVER_MODE_INITIALIZING:
break;
case ROVER_MODE_ENUM_END:
break;
}
}