Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduSub/mode.cpp
9374 views
1
#include "Sub.h"
2
3
/*
4
constructor for Mode object
5
*/
6
Mode::Mode(void) :
7
g(sub.g),
8
g2(sub.g2),
9
inertial_nav(sub.inertial_nav),
10
ahrs(sub.ahrs),
11
motors(sub.motors),
12
channel_roll(sub.channel_roll),
13
channel_pitch(sub.channel_pitch),
14
channel_throttle(sub.channel_throttle),
15
channel_yaw(sub.channel_yaw),
16
channel_forward(sub.channel_forward),
17
channel_lateral(sub.channel_lateral),
18
position_control(&sub.pos_control),
19
attitude_control(&sub.attitude_control),
20
G_Dt(sub.G_Dt)
21
{ };
22
23
// return the static controller object corresponding to supplied mode
24
Mode *Sub::mode_from_mode_num(const Mode::Number mode)
25
{
26
Mode *ret = nullptr;
27
28
switch (mode) {
29
case Mode::Number::MANUAL:
30
ret = &mode_manual;
31
break;
32
case Mode::Number::STABILIZE:
33
ret = &mode_stabilize;
34
break;
35
case Mode::Number::ACRO:
36
ret = &mode_acro;
37
break;
38
case Mode::Number::ALT_HOLD:
39
ret = &mode_althold;
40
break;
41
case Mode::Number::SURFTRAK:
42
ret = &mode_surftrak;
43
break;
44
case Mode::Number::POSHOLD:
45
ret = &mode_poshold;
46
break;
47
case Mode::Number::AUTO:
48
ret = &mode_auto;
49
break;
50
case Mode::Number::GUIDED:
51
ret = &mode_guided;
52
break;
53
case Mode::Number::CIRCLE:
54
ret = &mode_circle;
55
break;
56
case Mode::Number::SURFACE:
57
ret = &mode_surface;
58
break;
59
case Mode::Number::MOTOR_DETECT:
60
ret = &mode_motordetect;
61
break;
62
default:
63
break;
64
}
65
66
return ret;
67
}
68
69
70
// set_mode - change flight mode and perform any necessary initialisation
71
// optional force parameter used to force the flight mode change (used only first time mode is set)
72
// returns true if mode was successfully set
73
// Some modes can always be set successfully but the return state of other flight modes should be checked and the caller should deal with failures appropriately
74
bool Sub::set_mode(Mode::Number mode, ModeReason reason)
75
{
76
77
// return immediately if we are already in the desired mode
78
if (mode == control_mode) {
79
control_mode_reason = reason;
80
return true;
81
}
82
83
Mode *new_flightmode = mode_from_mode_num((Mode::Number)mode);
84
if (new_flightmode == nullptr) {
85
notify_no_such_mode((uint8_t)mode);
86
return false;
87
}
88
89
if (new_flightmode->requires_GPS() &&
90
!sub.position_ok()) {
91
gcs().send_text(MAV_SEVERITY_WARNING, "Mode change failed: %s requires position", new_flightmode->name());
92
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
93
return false;
94
}
95
96
// check for valid altitude if old mode did not require it but new one does
97
// we only want to stop changing modes if it could make things worse
98
if (!flightmode->requires_altitude() &&
99
new_flightmode->requires_altitude() &&
100
!sub.control_check_barometer()) { // maybe use ekf_alt_ok() instead?
101
gcs().send_text(MAV_SEVERITY_WARNING, "Mode change failed: %s need alt estimate", new_flightmode->name());
102
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
103
return false;
104
}
105
106
if (!new_flightmode->init(false)) {
107
gcs().send_text(MAV_SEVERITY_WARNING,"Flight mode change failed %s", new_flightmode->name());
108
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
109
return false;
110
}
111
112
// perform any cleanup required by previous flight mode
113
exit_mode(flightmode, new_flightmode);
114
115
// store previous flight mode
116
prev_control_mode = control_mode;
117
118
// update flight mode
119
flightmode = new_flightmode;
120
control_mode = mode;
121
control_mode_reason = reason;
122
#if HAL_LOGGING_ENABLED
123
logger.Write_Mode((uint8_t)control_mode, reason);
124
#endif
125
gcs().send_message(MSG_HEARTBEAT);
126
127
// update notify object
128
notify_flight_mode();
129
130
// return success
131
return true;
132
}
133
134
// exit_mode - high level call to organise cleanup as a flight mode is exited
135
void Sub::exit_mode(Mode::Number old_control_mode, Mode::Number new_control_mode)
136
{
137
// stop mission when we leave auto mode
138
if (old_control_mode == Mode::Number::AUTO) {
139
if (mission.state() == AP_Mission::MISSION_RUNNING) {
140
mission.stop();
141
}
142
#if HAL_MOUNT_ENABLED
143
camera_mount.set_mode_to_default();
144
#endif // HAL_MOUNT_ENABLED
145
}
146
motors.set_max_throttle(1.0f);
147
}
148
149
bool Sub::set_mode(const uint8_t new_mode, const ModeReason reason)
150
{
151
static_assert(sizeof(Mode::Number) == sizeof(new_mode), "The new mode can't be mapped to the vehicles mode number");
152
return sub.set_mode(static_cast<Mode::Number>(new_mode), reason);
153
}
154
155
// update_flight_mode - calls the appropriate attitude controllers based on flight mode
156
// called at 100hz or more
157
void Sub::update_flight_mode()
158
{
159
flightmode->run();
160
}
161
162
// exit_mode - high level call to organise cleanup as a flight mode is exited
163
void Sub::exit_mode(Mode *&old_flightmode, Mode *&new_flightmode){
164
#if HAL_MOUNT_ENABLED
165
camera_mount.set_mode_to_default();
166
#endif // HAL_MOUNT_ENABLED
167
motors.set_max_throttle(1.0f);
168
}
169
170
// notify_flight_mode - sets notify object based on current flight mode. Only used for OreoLED notify device
171
void Sub::notify_flight_mode()
172
{
173
AP_Notify::flags.autopilot_mode = flightmode->is_autopilot();
174
AP_Notify::flags.flight_mode = (uint8_t)control_mode;
175
notify.set_flight_mode_str(flightmode->name4());
176
}
177
178
179
// get_pilot_desired_angle_rates - transform pilot's roll pitch and yaw input into a desired lean angle rates
180
// returns desired angle rates in centi-degrees-per-second
181
void Mode::get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out)
182
{
183
float rate_limit;
184
Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
185
186
// apply circular limit to pitch and roll inputs
187
float total_in = norm(pitch_in, roll_in);
188
189
if (total_in > ROLL_PITCH_INPUT_MAX) {
190
float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
191
roll_in *= ratio;
192
pitch_in *= ratio;
193
}
194
195
// calculate roll, pitch rate requests
196
if (g.acro_expo <= 0) {
197
rate_bf_request.x = roll_in * g.acro_rp_p;
198
rate_bf_request.y = pitch_in * g.acro_rp_p;
199
} else {
200
// expo variables
201
float rp_in, rp_in3, rp_out;
202
203
// range check expo
204
if (g.acro_expo > 1.0f) {
205
g.acro_expo.set(1.0f);
206
}
207
208
// roll expo
209
rp_in = float(roll_in)/ROLL_PITCH_INPUT_MAX;
210
rp_in3 = rp_in*rp_in*rp_in;
211
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
212
rate_bf_request.x = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
213
214
// pitch expo
215
rp_in = float(pitch_in)/ROLL_PITCH_INPUT_MAX;
216
rp_in3 = rp_in*rp_in*rp_in;
217
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
218
rate_bf_request.y = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
219
}
220
221
// calculate yaw rate request
222
rate_bf_request.z = yaw_in * g.acro_yaw_p;
223
224
// calculate earth frame rate corrections to pull the vehicle back to level while in ACRO mode
225
226
if (g.acro_trainer != ACRO_TRAINER_DISABLED) {
227
// Calculate trainer mode earth frame rate command for roll
228
int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
229
rate_ef_level.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;
230
231
// Calculate trainer mode earth frame rate command for pitch
232
int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
233
rate_ef_level.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;
234
235
// Calculate trainer mode earth frame rate command for yaw
236
rate_ef_level.z = 0;
237
238
// Calculate angle limiting earth frame rate commands
239
const float angle_max_cd = attitude_control->lean_angle_max_cd();
240
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
241
if (roll_angle > angle_max_cd) {
242
rate_ef_level.x -= g.acro_balance_roll*(roll_angle-angle_max_cd);
243
} else if (roll_angle < -angle_max_cd) {
244
rate_ef_level.x -= g.acro_balance_roll*(roll_angle+angle_max_cd);
245
}
246
247
if (pitch_angle > angle_max_cd) {
248
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-angle_max_cd);
249
} else if (pitch_angle < -angle_max_cd) {
250
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+angle_max_cd);
251
}
252
}
253
254
// convert earth-frame level rates to body-frame level rates
255
attitude_control->euler_derivative_to_body(attitude_control->get_attitude_target_quat(), rate_ef_level, rate_bf_level);
256
257
// combine earth frame rate corrections with rate requests
258
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
259
rate_bf_request.x += rate_bf_level.x;
260
rate_bf_request.y += rate_bf_level.y;
261
rate_bf_request.z += rate_bf_level.z;
262
} else {
263
float acro_level_mix = constrain_float(1-MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0, 0, 1)*ahrs.cos_pitch();
264
265
// Scale leveling rates by stick input
266
rate_bf_level = rate_bf_level*acro_level_mix;
267
268
// Calculate rate limit to prevent change of rate through inverted
269
rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
270
rate_bf_request.x += rate_bf_level.x;
271
rate_bf_request.x = constrain_float(rate_bf_request.x, -rate_limit, rate_limit);
272
273
// Calculate rate limit to prevent change of rate through inverted
274
rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
275
rate_bf_request.y += rate_bf_level.y;
276
rate_bf_request.y = constrain_float(rate_bf_request.y, -rate_limit, rate_limit);
277
278
// Calculate rate limit to prevent change of rate through inverted
279
rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
280
rate_bf_request.z += rate_bf_level.z;
281
rate_bf_request.z = constrain_float(rate_bf_request.z, -rate_limit, rate_limit);
282
}
283
}
284
285
// hand back rate request
286
roll_out = rate_bf_request.x;
287
pitch_out = rate_bf_request.y;
288
yaw_out = rate_bf_request.z;
289
}
290
291
292
bool Mode::set_mode(Mode::Number mode, ModeReason reason)
293
{
294
return sub.set_mode(mode, reason);
295
}
296
297
GCS_Sub &Mode::gcs()
298
{
299
return sub.gcs();
300
}
301
302