-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArduPilotMegaMAV1.cc
567 lines (493 loc) · 15.8 KB
/
ArduPilotMegaMAV1.cc
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*=====================================================================
QGroundControl Open Source Ground Control Station
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
(c) 2013 APMPLANNER PROJECT <http://www.ardupliot.com>
This file is part of the QGROUNDCONTROL project
QGROUNDCONTROL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QGROUNDCONTROL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
/**
* @file
* @brief Implementation of class ArduPilotMegaMAV Uas Object
* @author Lorenz Meier
* @author Bill Bonney
*/
#include "ArduPilotMegaMAV1.h"
#include "QsLog.h"
#include "GAudioOutput.h"
#include "LinkManager1.h"
#ifndef MAVLINK_MSG_ID_MOUNT_CONFIGURE
#include "ardupilotmega/mavlink_msg_mount_configure.h"
#endif
#ifndef MAVLINK_MSG_ID_MOUNT_CONTROL
#include "ardupilotmega/mavlink_msg_mount_control.h"
#endif
#include <QString>
#include <QDir>
#include <QDesktopServices>
#include <QSettings>
CustomMode::CustomMode()
{
}
CustomMode::CustomMode(int aMode)
{
m_mode = aMode;
}
int CustomMode::modeAsInt()
{
return m_mode;
}
QString CustomMode::operator <<(int aMode)
{
return QString::number(aMode);
}
QString CustomMode::colorForMode(int aMode)
{
const uint numberOfKmlColors = 16;
const QString kmlColors[] = {"FFFF00FF"
, "FF00FF00"
, "FFFF0000"
, "FFFF2323"
, "FFFFCE00"
, "FF00CEFF"
, "FF009900"
, "FF33FFCC"
, "FF0000FF"
, "FFFFAAAA"
, "FFABABAB"
, "FF99FF33"
, "FF66CC99"
, "FFCC3300"
, "FF0066FF"};
if ((sizeof(kmlColors)/sizeof(const char*)) > aMode*numberOfKmlColors ){
QLOG_ERROR() << "ColorForMode: not enough colors, so wrapping to 1st color";
aMode -= numberOfKmlColors;
}
return kmlColors[aMode];
}
ApmPlane::ApmPlane(planeMode aMode) : CustomMode(aMode)
{
}
ApmPlane::planeMode ApmPlane::mode()
{
return static_cast<ApmPlane::planeMode>(m_mode);
}
QString ApmPlane::stringForMode(int aMode)
{
switch(static_cast<planeMode>(aMode)) {
case MANUAL:
return "Manual";
break;
case CIRCLE:
return "Circle";
break;
case STABILIZE:
return "Stabilize";
break;
case TRAINING:
return "Training";
break;
case FLY_BY_WIRE_A:
return "FBW A";
break;
case FLY_BY_WIRE_B:
return "FBW B";
break;
case AUTO:
return "Auto";
break;
case RTL:
return "RTL";
break;
case LOITER:
return "Loiter";
break;
case GUIDED:
return "Guided";
break;
case INITIALIZING:
return "Initializing";
break;
case ACRO:
return "Acro";
break;
case CRUISE:
return "Cruise";
break;
case AUTOTUNE:
return "Auto Tune";
break;
case RESERVED_9:
case RESERVED_13:
case RESERVED_14:
return "Reserved";
default:
return "Undefined: " + QString::number(aMode);
}
}
ApmCopter::ApmCopter(copterMode aMode) : CustomMode(aMode)
{
}
ApmCopter::copterMode ApmCopter::mode()
{
return static_cast<ApmCopter::copterMode>(m_mode);
}
QString ApmCopter::stringForMode(int aMode) {
switch(static_cast<copterMode>(aMode)) {
case STABILIZE:
return "Stabilize";
break;
case ACRO:
return "Acro";
break;
case ALT_HOLD:
return "Alt Hold";
break;
case AUTO:
return "Auto";
break;
case GUIDED:
return "Guided";
break;
case LOITER:
return "Loiter";
break;
case RTL:
return "RTL";
break;
case CIRCLE:
return "Circle";
break;
case POSITION:
return "Reserved"; // Marked as reserved as not supported since AC3.2
break;
case LAND:
return "Land";
break;
case OF_LOITER:
return "OF Loiter";
break;
case DRIFT:
return "Drift";
break;
case SPORT:
return "Sport";
break;
case RESERVED_12:
return "Reserved";
break;
case POS_HOLD:
return "Position Hold";
break;
case AUTOTUNE:
return "Autotune";
break;
case FLIP:
return "Flip";
break;
default:
return "Undefined";
}
}
ApmRover::ApmRover(roverMode aMode) : CustomMode(aMode)
{
}
ApmRover::roverMode ApmRover::mode()
{
return static_cast<ApmRover::roverMode>(m_mode);
}
QString ApmRover::stringForMode(int aMode) {
switch(static_cast<roverMode>(aMode)) {
case MANUAL:
return "Manual";
break;
case LEARNING:
return "Learning";
break;
case STEERING:
return "Steering";
break;
case HOLD:
return "Hold";
break;
case AUTO:
return "Auto";
break;
case RTL:
return "RTL";
break;
case GUIDED:
return "Guided";
break;
case INITIALIZING:
return "Initializing";
break;
case RESERVED_1:
case RESERVED_5:
case RESERVED_6:
case RESERVED_7:
case RESERVED_8:
case RESERVED_9:
case RESERVED_12:
case RESERVED_13:
case RESERVED_14:
return "Reserved";
default:
return "Undefined";
}
}
ArduPilotMegaMAV::ArduPilotMegaMAV(MAVLinkProtocol* mavlink, int id) :
UAS(mavlink, id)//,
// place other initializers here
{
//This does not seem to work. Manually request each stream type at a specified rate.
// Ask for all streams at 4 Hz
//enableAllDataTransmission(4);
txReqTimer = new QTimer(this);
connect(txReqTimer,SIGNAL(timeout()),this,SLOT(RequestAllDataStreams()));
QTimer::singleShot(5000,this,SLOT(RequestAllDataStreams())); //Send an initial TX request in 5 seconds.
txReqTimer->start(10000); //Resend the TX requests every 10 seconds.
connect(this,SIGNAL(connected()),this,SLOT(uasConnected()));
connect(this,SIGNAL(disconnected()),this,SLOT(uasDisconnected()));
connect(this, SIGNAL(textMessageReceived(int,int,int,QString)),
this, SLOT(textMessageReceived(int,int,int,QString)));
connect(this, SIGNAL(heartbeatTimeout(bool,uint)),
this, SLOT(heartbeatTimeout(bool,uint)));
}
void ArduPilotMegaMAV::RequestAllDataStreams()
{
QLOG_TRACE() << "APM:RequestAllDataRates";
QSettings settings;
settings.sync();
settings.beginGroup("DATA_RATES");
enableExtendedSystemStatusTransmission(settings.value("EXT_SYS_STATUS",2).toInt());
enablePositionTransmission(settings.value("POSITION",3).toInt());
enableExtra1Transmission(settings.value("EXTRA1",10).toInt());
enableExtra2Transmission(settings.value("EXTRA2",10).toInt());
enableExtra3Transmission(settings.value("EXTRA3",2).toInt());
enableRawSensorDataTransmission(settings.value("RAW_SENSOR_DATA",2).toInt());
enableRCChannelDataTransmission(settings.value("RC_CHANNEL_DATA",2).toInt());
settings.endGroup();
}
void ArduPilotMegaMAV::uasConnected()
{
QLOG_INFO() << "ArduPilotMegaMAV APM Connected";
QTimer::singleShot(500,this,SLOT(RequestAllDataStreams())); //Send an initial TX request in 0.5 seconds.
createNewMAVLinkLog(type);
LinkManager::instance()->startLogging();
}
void ArduPilotMegaMAV::uasDisconnected()
{
QLOG_INFO() << "ArduPilotMegaMAV APM disconnected";
//if (mavlink)
// {
// mavlink->stopLogging();
// }
}
void ArduPilotMegaMAV::createNewMAVLinkLog(uint8_t type)
{
QString subDir;
// This creates a log in subdir based on the vehicle
// first detected. When connecting to multiple vehicles
// it will not log message based on each specific.
switch(type) {
case MAV_TYPE_TRICOPTER:
subDir = "/tricopter/";
break;
case MAV_TYPE_QUADROTOR:
subDir = "/quadcopter/";
break;
case MAV_TYPE_HEXAROTOR:
subDir = "/hexcopter/";
break;
case MAV_TYPE_OCTOROTOR:
subDir = "/octocopter/";
break;
case MAV_TYPE_GROUND_ROVER:
subDir = "/rover/";
break;
case MAV_TYPE_HELICOPTER:
subDir = "/helicopter/";
break;
case MAV_TYPE_FIXED_WING:
subDir = "/plane/";
break;
default:
subDir = "/";
}
LinkManager::instance()->setLogSubDirectory(subDir);
}
/**
* This function is called by MAVLink once a complete, uncorrupted (CRC check valid)
* mavlink packet is received.
*
* @param link Hardware link the message came from (e.g. /dev/ttyUSB0 or UDP port).
* messages can be sent back to the system via this link
* @param message MAVLink message, as received from the MAVLink protocol stack
*/
void ArduPilotMegaMAV::receiveMessage(LinkInterface* link, mavlink_message_t message)
{
// Let UAS handle the default message set
//qDebug() << "Message type:" << message.sysid << message.msgid;
UAS::receiveMessage(link, message);
if (message.sysid == uasId) {
// Handle your special messages
switch (message.msgid) {
case MAVLINK_MSG_ID_HEARTBEAT:
{
//QLOG_DEBUG() << "ARDUPILOT RECEIVED HEARTBEAT";
break;
}
case MAVLINK_MSG_ID_STATUSTEXT:
{
QByteArray b;
b.resize(MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN+1);
mavlink_msg_statustext_get_text(&message, b.data());
// Ensure NUL-termination
b[b.length()-1] = '\0';
QString text = QString(b);
int severity = mavlink_msg_statustext_get_severity(&message);
QLOG_INFO() << "STATUS TEXT:" << severity << ":" << text;
if (text.startsWith("ArduCopter") || text.startsWith("ArduPlane")
|| text.startsWith("ArduRover")) {
QLOG_DEBUG() << "APM Version String detected:" << text;
emit versionDetected(text);
}
} break;
default:
//QLOG_DEBUG() << "\nARDUPILOT RECEIVED MESSAGE WITH ID" << message.msgid;
break;
}
}
}
void ArduPilotMegaMAV::setMountConfigure(unsigned char mode, bool stabilize_roll,bool stabilize_pitch,bool stabilize_yaw)
{
//Only supported by APM
mavlink_message_t msg;
mavlink_msg_mount_configure_pack(255,1,&msg,this->uasId,1,mode,stabilize_roll,stabilize_pitch,stabilize_yaw);
sendMessage(msg);
}
void ArduPilotMegaMAV::setMountControl(double pa,double pb,double pc,bool islatlong)
{
mavlink_message_t msg;
if (islatlong)
{
mavlink_msg_mount_control_pack(255,1,&msg,this->uasId,1,pa*10000000.0,pb*10000000.0,pc*10000000.0,0);
}
else
{
mavlink_msg_mount_control_pack(255,1,&msg,this->uasId,1,pa,pb,pc,0);
}
sendMessage(msg);
}
void ArduPilotMegaMAV::armSystem()
{
QLOG_INFO() << "APM ARM System";
mavlink_message_t msg;
mavlink_msg_command_long_pack(getSystemId(),
getComponentId(),
&msg,
getUASID(), // uint8_t target_system,
MAV_COMP_ID_SYSTEM_CONTROL, // uint8_t target_component
MAV_CMD_COMPONENT_ARM_DISARM, // uint16_t command,
1, // uint8_t confirmation,
1.0, // float param1,
0, // float param2,
0, // float param3,
0, // float param4,
0, // float param5,
0, // float param6,
0 // float param7)
);
sendMessage(msg);
}
void ArduPilotMegaMAV::disarmSystem()
{
QLOG_INFO() << "APM DISARM System";
mavlink_message_t msg;
mavlink_msg_command_long_pack(getSystemId(),
getComponentId(),
&msg,
getUASID(), // uint8_t target_system,
MAV_COMP_ID_SYSTEM_CONTROL, // uint8_t target_component
MAV_CMD_COMPONENT_ARM_DISARM, // uint16_t command,
1, // uint8_t confirmation,
0.0, // float param1,
0, // float param2,
0, // float param3,
0, // float param4,
0, // float param5,
0, // float param6,
0 // float param7)
);
sendMessage(msg);
}
QString ArduPilotMegaMAV::getCustomModeText()
{
QLOG_DEBUG() << "APM: getCustomModeText()";
QString customModeString;
if (isFixedWing()){
customModeString = ApmPlane::stringForMode(custom_mode);
} else if (isMultirotor()){
customModeString = ApmCopter::stringForMode(custom_mode);
} else if (isGroundRover()){
customModeString = ApmRover::stringForMode(custom_mode);
} else if (getSystemType() == MAV_TYPE_ANTENNA_TRACKER ){
customModeString = tr("Ant Tracker");
} else {
QLOG_WARN() << "APM: Unsupported System Type " << getSystemType();
customModeString = tr("APM UNKOWN");
}
return customModeString;
}
QString ArduPilotMegaMAV::getCustomModeAudioText()
{
QLOG_DEBUG() << "APM: getCustomModeAudioText()";
QString returnString = tr("and mode is ");
return returnString + getCustomModeText();
}
void ArduPilotMegaMAV::textMessageReceived(int /*uasid*/, int /*componentid*/, int severity, QString text)
{
QLOG_DEBUG() << "APM: Text Message rx'd" << text;
if (text.startsWith("PreArm:")) {
// Speak the PreArm warning
QString audioString = "Pre-arm check:" + text.remove("PreArm:");
GAudioOutput::instance()->say(audioString, severity);
} else if (text.startsWith("Arm:")){
QString audioString = "Please press and hold safety switch";
GAudioOutput::instance()->say(audioString, severity);
}
}
void ArduPilotMegaMAV::heartbeatTimeout(bool timeout, unsigned int /*ms*/)
{
if(timeout == false) {
QLOG_DEBUG() << "Send requestall data streams when heartbeat restarts";
// Request data, as this means we have reconnected
// Send an request in .5 seconds.
QTimer::singleShot(500,this,SLOT(RequestAllDataStreams()));
}
}
void ArduPilotMegaMAV::playCustomModeChangedAudioMessage()
{
QString phrase;
phrase = "Mode changed to " + getCustomModeText() + " for system " + QString::number(getUASID());
QLOG_DEBUG() << "APM say:" << phrase;
GAudioOutput::instance()->say(phrase.toLower());
}
void ArduPilotMegaMAV::playArmStateChangedAudioMessage(bool armedState)
{
QString armedPhrase("disarmed");
if (armedState){
armedPhrase = "armed";
}
QLOG_DEBUG() << "APM say:" << armedPhrase;
GAudioOutput::instance()->say(QString("system %1 is %2").arg(QString::number(getUASID()),armedPhrase));
}