Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Blimp/mode.cpp
9460 views
1
#include "Blimp.h"
2
3
/*
4
* High level calls to set and update flight modes logic for individual
5
* flight modes is in control_acro.cpp, control_stabilize.cpp, etc
6
*/
7
8
/*
9
constructor for Mode object
10
*/
11
Mode::Mode(void) :
12
g(blimp.g),
13
g2(blimp.g2),
14
inertial_nav(blimp.inertial_nav),
15
ahrs(blimp.ahrs),
16
motors(blimp.motors),
17
loiter(blimp.loiter),
18
channel_right(blimp.channel_right),
19
channel_front(blimp.channel_front),
20
channel_up(blimp.channel_up),
21
channel_yaw(blimp.channel_yaw),
22
G_Dt(blimp.G_Dt)
23
{ };
24
25
// return the static controller object corresponding to supplied mode
26
Mode *Blimp::mode_from_mode_num(const Mode::Number mode)
27
{
28
Mode *ret = nullptr;
29
30
switch (mode) {
31
case Mode::Number::LAND:
32
ret = &mode_land;
33
break;
34
case Mode::Number::MANUAL:
35
ret = &mode_manual;
36
break;
37
case Mode::Number::VELOCITY:
38
ret = &mode_velocity;
39
break;
40
case Mode::Number::LOITER:
41
ret = &mode_loiter;
42
break;
43
case Mode::Number::RTL:
44
ret = &mode_rtl;
45
break;
46
default:
47
break;
48
}
49
50
return ret;
51
}
52
53
54
// set_mode - change flight mode and perform any necessary initialisation
55
// optional force parameter used to force the flight mode change (used only first time mode is set)
56
// returns true if mode was successfully set
57
// 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
58
bool Blimp::set_mode(Mode::Number mode, ModeReason reason)
59
{
60
61
// return immediately if we are already in the desired mode
62
if (mode == control_mode) {
63
control_mode_reason = reason;
64
return true;
65
}
66
67
Mode *new_flightmode = mode_from_mode_num((Mode::Number)mode);
68
if (new_flightmode == nullptr) {
69
notify_no_such_mode((uint8_t)mode);
70
return false;
71
}
72
73
bool ignore_checks = !motors->armed(); // allow switching to any mode if disarmed. We rely on the arming check to perform
74
75
if (!ignore_checks &&
76
new_flightmode->requires_GPS() &&
77
!blimp.position_ok()) {
78
gcs().send_text(MAV_SEVERITY_WARNING, "Mode change failed: %s requires position", new_flightmode->name());
79
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
80
return false;
81
}
82
83
// check for valid altitude if old mode did not require it but new one does
84
// we only want to stop changing modes if it could make things worse
85
if (!ignore_checks &&
86
!blimp.ekf_alt_ok() &&
87
flightmode->has_manual_throttle() &&
88
!new_flightmode->has_manual_throttle()) {
89
gcs().send_text(MAV_SEVERITY_WARNING, "Mode change failed: %s need alt estimate", new_flightmode->name());
90
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
91
return false;
92
}
93
94
if (!new_flightmode->init(ignore_checks)) {
95
gcs().send_text(MAV_SEVERITY_WARNING,"Flight mode change failed %s", new_flightmode->name());
96
LOGGER_WRITE_ERROR(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
97
return false;
98
}
99
100
// perform any cleanup required by previous flight mode
101
exit_mode(flightmode, new_flightmode);
102
103
// update flight mode
104
flightmode = new_flightmode;
105
control_mode = mode;
106
control_mode_reason = reason;
107
#if HAL_LOGGING_ENABLED
108
logger.Write_Mode((uint8_t)control_mode, reason);
109
#endif
110
gcs().send_message(MSG_HEARTBEAT);
111
112
// update notify object
113
notify_flight_mode();
114
115
// return success
116
return true;
117
}
118
119
bool Blimp::set_mode(const uint8_t new_mode, const ModeReason reason)
120
{
121
static_assert(sizeof(Mode::Number) == sizeof(new_mode), "The new mode can't be mapped to the vehicles mode number");
122
#ifdef DISALLOW_GCS_MODE_CHANGE_DURING_RC_FAILSAFE
123
if (reason == ModeReason::GCS_COMMAND && blimp.failsafe.radio) {
124
// don't allow mode changes while in radio failsafe
125
return false;
126
}
127
#endif
128
return blimp.set_mode(static_cast<Mode::Number>(new_mode), reason);
129
}
130
131
// update_flight_mode - calls the appropriate attitude controllers based on flight mode
132
// called at 100hz or more
133
void Blimp::update_flight_mode()
134
{
135
flightmode->run();
136
}
137
138
// exit_mode - high level call to organise cleanup as a flight mode is exited
139
void Blimp::exit_mode(Mode *&old_flightmode,
140
Mode *&new_flightmode) {}
141
142
// notify_flight_mode - sets notify object based on current flight mode. Only used for OreoLED notify device
143
void Blimp::notify_flight_mode()
144
{
145
AP_Notify::flags.autopilot_mode = flightmode->is_autopilot();
146
AP_Notify::flags.flight_mode = (uint8_t)control_mode;
147
notify.set_flight_mode_str(flightmode->name4());
148
}
149
150
void Mode::update_navigation()
151
{
152
// run autopilot to make high level decisions about control modes
153
run_autopilot();
154
}
155
156
// returns desired thrust/acceleration
157
void Mode::get_pilot_input(Vector3f &pilot, float &yaw)
158
{
159
// throttle failsafe check
160
if (blimp.failsafe.radio || !rc().has_ever_seen_rc_input()) {
161
pilot.y = 0;
162
pilot.x = 0;
163
pilot.z = 0;
164
yaw = 0;
165
return;
166
}
167
// fetch pilot inputs
168
pilot.y = channel_right->get_control_in() / float(RC_SCALE);
169
pilot.x = channel_front->get_control_in() / float(RC_SCALE);
170
//TODO: need to make this channel_up instead, and then have it .negative. before being sent to pilot.z -> this is "throttle" channel, so higher = up.
171
pilot.z = -channel_up->get_control_in() / float(RC_SCALE);
172
yaw = channel_yaw->get_control_in() / float(RC_SCALE);
173
}
174
175
bool Mode::is_disarmed_or_landed() const
176
{
177
if (!motors->armed() || !blimp.ap.auto_armed || blimp.ap.land_complete) {
178
return true;
179
}
180
return false;
181
}
182
183
bool Mode::set_mode(Mode::Number mode, ModeReason reason)
184
{
185
return blimp.set_mode(mode, reason);
186
}
187
188
GCS_Blimp &Mode::gcs()
189
{
190
return blimp.gcs();
191
}
192
193