Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Blimp/radio.cpp
9388 views
1
#include "Blimp.h"
2
3
4
// Function that will read the radio data, limit servos and trigger a failsafe
5
// ----------------------------------------------------------------------------
6
7
void Blimp::default_dead_zones()
8
{
9
channel_right->set_default_dead_zone(20);
10
channel_front->set_default_dead_zone(20);
11
channel_up->set_default_dead_zone(30);
12
channel_yaw->set_default_dead_zone(20);
13
}
14
15
void Blimp::init_rc_in()
16
{
17
// the library guarantees that these are non-nullptr:
18
channel_right = &rc().get_roll_channel();
19
channel_front = &rc().get_pitch_channel();
20
channel_up = &rc().get_throttle_channel();
21
channel_yaw = &rc().get_yaw_channel();
22
23
// set rc channel ranges
24
channel_right->set_angle(RC_SCALE);
25
channel_front->set_angle(RC_SCALE);
26
channel_yaw->set_angle(RC_SCALE);
27
channel_up->set_angle(RC_SCALE);
28
29
// set default dead zones
30
default_dead_zones();
31
32
// initialise throttle_zero flag
33
ap.throttle_zero = true;
34
}
35
36
// init_rc_out -- initialise motors
37
void Blimp::init_rc_out()
38
{
39
// enable aux servos to cope with multiple output channels per motor
40
AP::srv().enable_aux_servos();
41
42
// refresh auxiliary channel to function map
43
SRV_Channels::update_aux_servo_function();
44
}
45
46
47
// enable_motor_output() - enable and output lowest possible value to motors
48
void Blimp::enable_motor_output()
49
{
50
// enable motors
51
motors->output_min();
52
}
53
54
void Blimp::read_radio()
55
{
56
const uint32_t tnow_ms = millis();
57
58
if (rc().read_input()) {
59
ap.new_radio_frame = true;
60
61
set_throttle_and_failsafe(channel_up->get_radio_in());
62
set_throttle_zero_flag(channel_up->get_control_in());
63
64
const float dt = (tnow_ms - last_radio_update_ms)*1.0e-3f;
65
rc_throttle_control_in_filter.apply(channel_up->get_control_in(), dt);
66
last_radio_update_ms = tnow_ms;
67
return;
68
}
69
70
// No radio input this time
71
if (failsafe.radio) {
72
// already in failsafe!
73
return;
74
}
75
76
const uint32_t elapsed = tnow_ms - last_radio_update_ms;
77
// turn on throttle failsafe if no update from the RC Radio for RC_FS_TIMEOUT seconds or 1000ms if we are using RC_OVERRIDE
78
const uint32_t timeout = RC_Channels::has_active_overrides() ? FS_RADIO_RC_OVERRIDE_TIMEOUT_MS : rc().get_fs_timeout_ms();
79
if (elapsed < timeout) {
80
// not timed out yet
81
return;
82
}
83
if (!g.failsafe_throttle) {
84
// throttle failsafe not enabled
85
return;
86
}
87
if (!rc().has_ever_seen_rc_input() && !motors->armed()) {
88
// we only failsafe if we are armed OR we have ever seen an RC receiver
89
return;
90
}
91
92
// Nobody ever talks to us. Log an error and enter failsafe.
93
LOGGER_WRITE_ERROR(LogErrorSubsystem::RADIO, LogErrorCode::RADIO_LATE_FRAME);
94
set_failsafe_radio(true);
95
}
96
97
#define FS_COUNTER 3 // radio failsafe kicks in after 3 consecutive throttle values below failsafe_throttle_value
98
void Blimp::set_throttle_and_failsafe(uint16_t throttle_pwm)
99
{
100
// if failsafe not enabled pass through throttle and exit
101
if (g.failsafe_throttle == FS_THR_DISABLED) {
102
set_failsafe_radio(false);
103
return;
104
}
105
106
//check for low throttle value
107
if (throttle_pwm < (uint16_t)g.failsafe_throttle_value) {
108
109
// if we are already in failsafe or motors not armed pass through throttle and exit
110
if (failsafe.radio || !(rc().has_ever_seen_rc_input() || motors->armed())) {
111
return;
112
}
113
114
// check for 3 low throttle values
115
// Note: we do not pass through the low throttle until 3 low throttle values are received
116
failsafe.radio_counter++;
117
if ( failsafe.radio_counter >= FS_COUNTER ) {
118
failsafe.radio_counter = FS_COUNTER; // check to ensure we don't overflow the counter
119
set_failsafe_radio(true);
120
}
121
} else {
122
// we have a good throttle so reduce failsafe counter
123
failsafe.radio_counter--;
124
if ( failsafe.radio_counter <= 0 ) {
125
failsafe.radio_counter = 0; // check to ensure we don't underflow the counter
126
127
// disengage failsafe after three (nearly) consecutive valid throttle values
128
if (failsafe.radio) {
129
set_failsafe_radio(false);
130
}
131
}
132
// pass through throttle
133
}
134
}
135
136
#define THROTTLE_ZERO_DEBOUNCE_TIME_MS 400
137
// set_throttle_zero_flag - set throttle_zero flag from debounced throttle control
138
// throttle_zero is used to determine if the pilot intends to shut down the motors
139
// Basically, this signals when we are not flying. We are either on the ground
140
// or the pilot has shut down the vehicle in the air and it is free-floating
141
void Blimp::set_throttle_zero_flag(int16_t throttle_control)
142
{
143
static uint32_t last_nonzero_throttle_ms = 0;
144
uint32_t tnow_ms = millis();
145
146
// if not using throttle interlock and non-zero throttle and not E-stopped,
147
// or using motor interlock and it's enabled, then motors are running,
148
// and we are flying. Immediately set as non-zero
149
if (throttle_control > 0) {
150
last_nonzero_throttle_ms = tnow_ms;
151
ap.throttle_zero = false;
152
} else if (tnow_ms - last_nonzero_throttle_ms > THROTTLE_ZERO_DEBOUNCE_TIME_MS) {
153
ap.throttle_zero = true;
154
}
155
//TODO: This may not be needed
156
}
157
158