forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.cpp
252 lines (206 loc) · 6.13 KB
/
system.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
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
#include "Tracker.h"
// mission storage
static const StorageAccess wp_storage(StorageManager::StorageMission);
void Tracker::init_ardupilot()
{
// initialise stats module
stats.init();
BoardConfig.init();
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
can_mgr.init();
#endif
// initialise notify
notify.init();
AP_Notify::flags.pre_arm_check = true;
AP_Notify::flags.pre_arm_gps_check = true;
// initialise battery
battery.init();
// init baro before we start the GCS, so that the CLI baro test works
barometer.set_log_baro_bit(MASK_LOG_IMU);
barometer.init();
// setup telem slots with serial ports
gcs().setup_uarts();
#if LOGGING_ENABLED == ENABLED
log_init();
#endif
#ifdef ENABLE_SCRIPTING
scripting.init();
#endif // ENABLE_SCRIPTING
// initialise compass
AP::compass().set_log_bit(MASK_LOG_COMPASS);
AP::compass().init();
// GPS Initialization
gps.set_log_gps_bit(MASK_LOG_GPS);
gps.init(serial_manager);
ahrs.init();
ahrs.set_fly_forward(false);
ins.init(scheduler.get_loop_rate_hz());
ahrs.reset();
barometer.calibrate();
// initialise AP_Logger library
logger.setVehicle_Startup_Writer(FUNCTOR_BIND(&tracker, &Tracker::Log_Write_Vehicle_Startup_Messages, void));
// set serial ports non-blocking
serial_manager.set_blocking_writes_all(false);
// initialise rc channels including setting mode
rc().init();
// initialise servos
init_servos();
// use given start positions - useful for indoor testing, and
// while waiting for GPS lock
// sanity check location
if (fabsf(g.start_latitude) <= 90.0f && fabsf(g.start_longitude) <= 180.0f) {
current_loc.lat = g.start_latitude * 1.0e7f;
current_loc.lng = g.start_longitude * 1.0e7f;
} else {
gcs().send_text(MAV_SEVERITY_NOTICE, "Ignoring invalid START_LATITUDE or START_LONGITUDE parameter");
}
// see if EEPROM has a default location as well
if (current_loc.lat == 0 && current_loc.lng == 0) {
get_home_eeprom(current_loc);
}
gcs().send_text(MAV_SEVERITY_INFO,"Ready to track");
hal.scheduler->delay(1000); // Why????
Mode *newmode = mode_from_mode_num((Mode::Number)g.initial_mode.get());
if (newmode == nullptr) {
newmode = &mode_manual;
}
set_mode(*newmode, ModeReason::STARTUP);
if (g.startup_delay > 0) {
// arm servos with trim value to allow them to start up (required
// for some servos)
prepare_servos();
}
// disable safety if requested
BoardConfig.init_safety();
}
/*
fetch HOME from EEPROM
*/
bool Tracker::get_home_eeprom(struct Location &loc)
{
// Find out proper location in memory by using the start_byte position + the index
// --------------------------------------------------------------------------------
if (g.command_total.get() == 0) {
return false;
}
// read WP position
loc = {
int32_t(wp_storage.read_uint32(5)),
int32_t(wp_storage.read_uint32(9)),
int32_t(wp_storage.read_uint32(1)),
Location::AltFrame::ABSOLUTE
};
return true;
}
bool Tracker::set_home_eeprom(const Location &temp)
{
wp_storage.write_byte(0, 0);
wp_storage.write_uint32(1, temp.alt);
wp_storage.write_uint32(5, temp.lat);
wp_storage.write_uint32(9, temp.lng);
// Now have a home location in EEPROM
g.command_total.set_and_save(1); // At most 1 entry for HOME
return true;
}
bool Tracker::set_home(const Location &temp)
{
// check EKF origin has been set
Location ekf_origin;
if (ahrs.get_origin(ekf_origin)) {
if (!ahrs.set_home(temp)) {
return false;
}
}
if (!set_home_eeprom(temp)) {
return false;
}
current_loc = temp;
return true;
}
void Tracker::arm_servos()
{
hal.util->set_soft_armed(true);
logger.set_vehicle_armed(true);
}
void Tracker::disarm_servos()
{
hal.util->set_soft_armed(false);
logger.set_vehicle_armed(false);
}
/*
setup servos to trim value after initialising
*/
void Tracker::prepare_servos()
{
start_time_ms = AP_HAL::millis();
SRV_Channels::set_output_limit(SRV_Channel::k_tracker_yaw, SRV_Channel::Limit::TRIM);
SRV_Channels::set_output_limit(SRV_Channel::k_tracker_pitch, SRV_Channel::Limit::TRIM);
SRV_Channels::calc_pwm();
SRV_Channels::output_ch_all();
}
void Tracker::set_mode(Mode &newmode, const ModeReason reason)
{
if (mode == &newmode) {
// don't switch modes if we are already in the correct mode.
return;
}
mode = &newmode;
if (mode->requires_armed_servos()) {
arm_servos();
} else {
disarm_servos();
}
// log mode change
logger.Write_Mode((uint8_t)mode->number(), reason);
gcs().send_message(MSG_HEARTBEAT);
nav_status.bearing = ahrs.yaw_sensor * 0.01f;
}
bool Tracker::set_mode(const uint8_t new_mode, const ModeReason reason)
{
Mode *fred = nullptr;
switch ((Mode::Number)new_mode) {
case Mode::Number::INITIALISING:
return false;
case Mode::Number::AUTO:
fred = &mode_auto;
break;
case Mode::Number::MANUAL:
fred = &mode_manual;
break;
case Mode::Number::SCAN:
fred = &mode_scan;
break;
case Mode::Number::SERVOTEST:
fred = &mode_servotest;
break;
case Mode::Number::STOP:
fred = &mode_stop;
break;
case Mode::Number::GUIDED:
fred = &mode_guided;
break;
}
if (fred == nullptr) {
return false;
}
set_mode(*fred, reason);
return true;
}
/*
should we log a message type now?
*/
bool Tracker::should_log(uint32_t mask)
{
if (!logger.should_log(mask)) {
return false;
}
return true;
}
#include <AP_AdvancedFailsafe/AP_AdvancedFailsafe.h>
#include <AP_Avoidance/AP_Avoidance.h>
#include <AP_ADSB/AP_ADSB.h>
// dummy method to avoid linking AFS
bool AP_AdvancedFailsafe::gcs_terminate(bool should_terminate, const char *reason) {return false;}
AP_AdvancedFailsafe *AP::advancedfailsafe() { return nullptr; }
// dummy method to avoid linking AP_Avoidance
AP_Avoidance *AP::ap_avoidance() { return nullptr; }