forked from mavlink/c_uart_interface_example
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoffboard_setup.cpp
88 lines (66 loc) · 2.13 KB
/
offboard_setup.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
#include "offboard_setup.h"
#include "serial_port.h"
#include "system_ids.h"
// ------------------------------------------------------------------------------
// Start Off-Board Mode
// ------------------------------------------------------------------------------
void
start_offboard()
{
// ----------------------------------------------------------------------
// TOGGLE OFF-BOARD MODE
// ----------------------------------------------------------------------
// Sends the command to go off-board
int success = toggle_offboard(1.0);
// Check the command was written
if ( not success )
{
fprintf(stderr,"Error: off-board mode not set, could not write message\n");
throw EXIT_FAILURE;
}
}
// ------------------------------------------------------------------------------
// Stop Off-Board Mode
// ------------------------------------------------------------------------------
void
stop_offboard(void)
{
// ----------------------------------------------------------------------
// TOGGLE OFF-BOARD MODE
// ----------------------------------------------------------------------
// Sends the command to stop off-board
int success = toggle_offboard(0.0);
// Check the command was written
if ( not success )
{
fprintf(stderr,"Error: off-board mode not set, could not write message\n");
throw EXIT_FAILURE;
}
}
// ------------------------------------------------------------------------------
// Toggle Off-Board Mode
// ------------------------------------------------------------------------------
int
toggle_offboard(float sw)
{
// Prepare command for off-board mode
mavlink_command_long_t com;
/* This paramater is what sets the offboard mode
param1 > 0.5f = Enter offboard mode
param2 < 0.5f = Exit offboard mode
*/
com.param1 = sw;
com.target_system = sysid;
com.target_component = autopilot_compid;
com.command = MAV_CMD_NAV_GUIDED_ENABLE;
// Encode
mavlink_message_t message;
mavlink_msg_command_long_encode(sysid, compid, &message, &com);
// Send the message
int len = write_serial(message);
// Check if mode is successfully set
if(len > 0)
return 1;
else
return 0;
}