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/Blimp/RC_Channel_Blimp.cpp
Views: 1798
1
#include "Blimp.h"
2
3
#include "RC_Channel_Blimp.h"
4
5
6
// defining these two macros and including the RC_Channels_VarInfo header defines the parameter information common to all vehicle types
7
#define RC_CHANNELS_SUBCLASS RC_Channels_Blimp
8
#define RC_CHANNEL_SUBCLASS RC_Channel_Blimp
9
10
#include <RC_Channel/RC_Channels_VarInfo.h>
11
12
int8_t RC_Channels_Blimp::flight_mode_channel_number() const
13
{
14
return blimp.g.flight_mode_chan.get();
15
}
16
17
void RC_Channel_Blimp::mode_switch_changed(modeswitch_pos_t new_pos)
18
{
19
if (new_pos < 0 || (uint8_t)new_pos > blimp.num_flight_modes) {
20
// should not have been called
21
return;
22
}
23
24
if (!blimp.set_mode((Mode::Number)blimp.flight_modes[new_pos].get(), ModeReason::RC_COMMAND)) {
25
// alert user to mode change failure
26
if (blimp.ap.initialised) {
27
AP_Notify::events.user_mode_change_failed = 1;
28
}
29
return;
30
}
31
32
// play a tone
33
// alert user to mode change (except if autopilot is just starting up)
34
if (blimp.ap.initialised) {
35
AP_Notify::events.user_mode_change = 1;
36
}
37
}
38
39
bool RC_Channels_Blimp::in_rc_failsafe() const
40
{
41
return blimp.failsafe.radio;
42
}
43
44
bool RC_Channels_Blimp::has_valid_input() const
45
{
46
if (blimp.failsafe.radio) {
47
return false;
48
}
49
if (blimp.failsafe.radio_counter != 0) {
50
return false;
51
}
52
return true;
53
}
54
55
RC_Channel * RC_Channels_Blimp::get_arming_channel(void) const
56
{
57
return blimp.channel_yaw;
58
}
59
60
// init_aux_switch_function - initialize aux functions
61
void RC_Channel_Blimp::init_aux_function(const AUX_FUNC ch_option, const AuxSwitchPos ch_flag)
62
{
63
// init channel options
64
switch (ch_option) {
65
// the following functions do not need to be initialised:
66
case AUX_FUNC::MANUAL:
67
break;
68
default:
69
RC_Channel::init_aux_function(ch_option, ch_flag);
70
break;
71
}
72
}
73
74
// do_aux_function_change_mode - change mode based on an aux switch
75
// being moved
76
void RC_Channel_Blimp::do_aux_function_change_mode(const Mode::Number mode,
77
const AuxSwitchPos ch_flag)
78
{
79
switch (ch_flag) {
80
case AuxSwitchPos::HIGH: {
81
// engage mode (if not possible we remain in current flight mode)
82
const bool success = blimp.set_mode(mode, ModeReason::AUX_FUNCTION);
83
if (blimp.ap.initialised) {
84
if (success) {
85
AP_Notify::events.user_mode_change = 1;
86
} else {
87
AP_Notify::events.user_mode_change_failed = 1;
88
}
89
}
90
break;
91
}
92
default:
93
// return to flight mode switch's flight mode if we are currently
94
// in this mode
95
if (blimp.control_mode == mode) {
96
rc().reset_mode_switch();
97
}
98
}
99
}
100
101
// do_aux_function - implement the function invoked by auxiliary switches
102
bool RC_Channel_Blimp::do_aux_function(const AuxFuncTrigger &trigger)
103
{
104
const AUX_FUNC &ch_option = trigger.func;
105
const AuxSwitchPos &ch_flag = trigger.pos;
106
107
switch (ch_option) {
108
109
case AUX_FUNC::SAVE_TRIM:
110
if ((ch_flag == AuxSwitchPos::HIGH) &&
111
(blimp.control_mode <= Mode::Number::MANUAL) &&
112
(blimp.channel_up->get_control_in() == 0)) {
113
blimp.save_trim();
114
}
115
break;
116
117
case AUX_FUNC::LOITER:
118
do_aux_function_change_mode(Mode::Number::LOITER, ch_flag);
119
break;
120
121
case AUX_FUNC::MANUAL:
122
do_aux_function_change_mode(Mode::Number::MANUAL, ch_flag);
123
break;
124
125
default:
126
return RC_Channel::do_aux_function(trigger);
127
}
128
return true;
129
}
130
131
// save_trim - adds roll and pitch trims from the radio to ahrs
132
void Blimp::save_trim()
133
{
134
// save roll and pitch trim
135
float roll_trim = ToRad((float)channel_right->get_control_in()*0.01f);
136
float pitch_trim = ToRad((float)channel_front->get_control_in()*0.01f);
137
ahrs.add_trim(roll_trim, pitch_trim);
138
LOGGER_WRITE_EVENT(LogEvent::SAVE_TRIM);
139
gcs().send_text(MAV_SEVERITY_INFO, "Trim saved");
140
}
141
142
// auto_trim - slightly adjusts the ahrs.roll_trim and ahrs.pitch_trim towards the current stick positions
143
// meant to be called continuously while the pilot attempts to keep the blimp level
144
void Blimp::auto_trim_cancel()
145
{
146
auto_trim_counter = 0;
147
AP_Notify::flags.save_trim = false;
148
gcs().send_text(MAV_SEVERITY_INFO, "AutoTrim cancelled");
149
}
150
151
void Blimp::auto_trim()
152
{
153
if (auto_trim_counter > 0) {
154
if (blimp.flightmode != &blimp.mode_manual ||
155
!blimp.motors->armed()) {
156
auto_trim_cancel();
157
return;
158
}
159
160
// flash the leds
161
AP_Notify::flags.save_trim = true;
162
163
if (!auto_trim_started) {
164
if (ap.land_complete) {
165
// haven't taken off yet
166
return;
167
}
168
auto_trim_started = true;
169
}
170
171
if (ap.land_complete) {
172
// landed again.
173
auto_trim_cancel();
174
return;
175
}
176
177
auto_trim_counter--;
178
179
// calculate roll trim adjustment
180
float roll_trim_adjustment = ToRad((float)channel_right->get_control_in() / 4000.0f);
181
182
// calculate pitch trim adjustment
183
float pitch_trim_adjustment = ToRad((float)channel_front->get_control_in() / 4000.0f);
184
185
// add trim to ahrs object
186
// save to eeprom on last iteration
187
ahrs.add_trim(roll_trim_adjustment, pitch_trim_adjustment, (auto_trim_counter == 0));
188
189
// on last iteration restore leds and accel gains to normal
190
if (auto_trim_counter == 0) {
191
AP_Notify::flags.save_trim = false;
192
gcs().send_text(MAV_SEVERITY_INFO, "AutoTrim: Trims saved");
193
}
194
}
195
}
196
197