CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduSub/mode.cpp
Views: 1798
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 (!sub.control_check_barometer() && // maybe use ekf_alt_ok() instead?
99
flightmode->has_manual_throttle() &&
100
!new_flightmode->has_manual_throttle()) {
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
}
147
148
bool Sub::set_mode(const uint8_t new_mode, const ModeReason reason)
149
{
150
static_assert(sizeof(Mode::Number) == sizeof(new_mode), "The new mode can't be mapped to the vehicles mode number");
151
return sub.set_mode(static_cast<Mode::Number>(new_mode), reason);
152
}
153
154
// update_flight_mode - calls the appropriate attitude controllers based on flight mode
155
// called at 100hz or more
156
void Sub::update_flight_mode()
157
{
158
flightmode->run();
159
}
160
161
// exit_mode - high level call to organise cleanup as a flight mode is exited
162
void Sub::exit_mode(Mode *&old_flightmode, Mode *&new_flightmode){
163
#if HAL_MOUNT_ENABLED
164
camera_mount.set_mode_to_default();
165
#endif // HAL_MOUNT_ENABLED
166
}
167
168
// notify_flight_mode - sets notify object based on current flight mode. Only used for OreoLED notify device
169
void Sub::notify_flight_mode()
170
{
171
AP_Notify::flags.autopilot_mode = flightmode->is_autopilot();
172
AP_Notify::flags.flight_mode = (uint8_t)control_mode;
173
notify.set_flight_mode_str(flightmode->name4());
174
}
175
176
177
// get_pilot_desired_angle_rates - transform pilot's roll pitch and yaw input into a desired lean angle rates
178
// returns desired angle rates in centi-degrees-per-second
179
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)
180
{
181
float rate_limit;
182
Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
183
184
// apply circular limit to pitch and roll inputs
185
float total_in = norm(pitch_in, roll_in);
186
187
if (total_in > ROLL_PITCH_INPUT_MAX) {
188
float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
189
roll_in *= ratio;
190
pitch_in *= ratio;
191
}
192
193
// calculate roll, pitch rate requests
194
if (g.acro_expo <= 0) {
195
rate_bf_request.x = roll_in * g.acro_rp_p;
196
rate_bf_request.y = pitch_in * g.acro_rp_p;
197
} else {
198
// expo variables
199
float rp_in, rp_in3, rp_out;
200
201
// range check expo
202
if (g.acro_expo > 1.0f) {
203
g.acro_expo.set(1.0f);
204
}
205
206
// roll expo
207
rp_in = float(roll_in)/ROLL_PITCH_INPUT_MAX;
208
rp_in3 = rp_in*rp_in*rp_in;
209
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
210
rate_bf_request.x = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
211
212
// pitch expo
213
rp_in = float(pitch_in)/ROLL_PITCH_INPUT_MAX;
214
rp_in3 = rp_in*rp_in*rp_in;
215
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
216
rate_bf_request.y = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
217
}
218
219
// calculate yaw rate request
220
rate_bf_request.z = yaw_in * g.acro_yaw_p;
221
222
// calculate earth frame rate corrections to pull the vehicle back to level while in ACRO mode
223
224
if (g.acro_trainer != ACRO_TRAINER_DISABLED) {
225
// Calculate trainer mode earth frame rate command for roll
226
int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
227
rate_ef_level.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;
228
229
// Calculate trainer mode earth frame rate command for pitch
230
int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
231
rate_ef_level.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;
232
233
// Calculate trainer mode earth frame rate command for yaw
234
rate_ef_level.z = 0;
235
236
// Calculate angle limiting earth frame rate commands
237
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
238
if (roll_angle > sub.aparm.angle_max) {
239
rate_ef_level.x -= g.acro_balance_roll*(roll_angle-sub.aparm.angle_max);
240
} else if (roll_angle < -sub.aparm.angle_max) {
241
rate_ef_level.x -= g.acro_balance_roll*(roll_angle+sub.aparm.angle_max);
242
}
243
244
if (pitch_angle > sub.aparm.angle_max) {
245
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-sub.aparm.angle_max);
246
} else if (pitch_angle < -sub.aparm.angle_max) {
247
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+sub.aparm.angle_max);
248
}
249
}
250
251
// convert earth-frame level rates to body-frame level rates
252
attitude_control->euler_rate_to_ang_vel(attitude_control->get_attitude_target_quat(), rate_ef_level, rate_bf_level);
253
254
// combine earth frame rate corrections with rate requests
255
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
256
rate_bf_request.x += rate_bf_level.x;
257
rate_bf_request.y += rate_bf_level.y;
258
rate_bf_request.z += rate_bf_level.z;
259
} else {
260
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();
261
262
// Scale leveling rates by stick input
263
rate_bf_level = rate_bf_level*acro_level_mix;
264
265
// Calculate rate limit to prevent change of rate through inverted
266
rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
267
rate_bf_request.x += rate_bf_level.x;
268
rate_bf_request.x = constrain_float(rate_bf_request.x, -rate_limit, rate_limit);
269
270
// Calculate rate limit to prevent change of rate through inverted
271
rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
272
rate_bf_request.y += rate_bf_level.y;
273
rate_bf_request.y = constrain_float(rate_bf_request.y, -rate_limit, rate_limit);
274
275
// Calculate rate limit to prevent change of rate through inverted
276
rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
277
rate_bf_request.z += rate_bf_level.z;
278
rate_bf_request.z = constrain_float(rate_bf_request.z, -rate_limit, rate_limit);
279
}
280
}
281
282
// hand back rate request
283
roll_out = rate_bf_request.x;
284
pitch_out = rate_bf_request.y;
285
yaw_out = rate_bf_request.z;
286
}
287
288
289
bool Mode::set_mode(Mode::Number mode, ModeReason reason)
290
{
291
return sub.set_mode(mode, reason);
292
}
293
294
GCS_Sub &Mode::gcs()
295
{
296
return sub.gcs();
297
}
298
299