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/radio.cpp
Views: 1798
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
rc().channel(CH_6)->set_default_dead_zone(0);
14
}
15
16
void Blimp::init_rc_in()
17
{
18
// the library gaurantees that these are non-nullptr:
19
channel_right = &rc().get_roll_channel();
20
channel_front = &rc().get_pitch_channel();
21
channel_up = &rc().get_throttle_channel();
22
channel_yaw = &rc().get_yaw_channel();
23
24
// set rc channel ranges
25
channel_right->set_angle(RC_SCALE);
26
channel_front->set_angle(RC_SCALE);
27
channel_yaw->set_angle(RC_SCALE);
28
channel_up->set_angle(RC_SCALE);
29
30
// set default dead zones
31
default_dead_zones();
32
33
// initialise throttle_zero flag
34
ap.throttle_zero = true;
35
}
36
37
// init_rc_out -- initialise motors
38
void Blimp::init_rc_out()
39
{
40
// enable aux servos to cope with multiple output channels per motor
41
AP::srv().enable_aux_servos();
42
43
// refresh auxiliary channel to function map
44
SRV_Channels::update_aux_servo_function();
45
}
46
47
48
// enable_motor_output() - enable and output lowest possible value to motors
49
void Blimp::enable_motor_output()
50
{
51
// enable motors
52
motors->output_min();
53
}
54
55
void Blimp::read_radio()
56
{
57
const uint32_t tnow_ms = millis();
58
59
if (rc().read_input()) {
60
ap.new_radio_frame = true;
61
62
set_throttle_and_failsafe(channel_up->get_radio_in());
63
set_throttle_zero_flag(channel_up->get_control_in());
64
65
const float dt = (tnow_ms - last_radio_update_ms)*1.0e-3f;
66
rc_throttle_control_in_filter.apply(channel_up->get_control_in(), dt);
67
last_radio_update_ms = tnow_ms;
68
return;
69
}
70
71
// No radio input this time
72
if (failsafe.radio) {
73
// already in failsafe!
74
return;
75
}
76
77
const uint32_t elapsed = tnow_ms - last_radio_update_ms;
78
// turn on throttle failsafe if no update from the RC Radio for 500ms or 2000ms if we are using RC_OVERRIDE
79
const uint32_t timeout = RC_Channels::has_active_overrides() ? FS_RADIO_RC_OVERRIDE_TIMEOUT_MS : FS_RADIO_TIMEOUT_MS;
80
if (elapsed < timeout) {
81
// not timed out yet
82
return;
83
}
84
if (!g.failsafe_throttle) {
85
// throttle failsafe not enabled
86
return;
87
}
88
if (!rc().has_ever_seen_rc_input() && !motors->armed()) {
89
// we only failsafe if we are armed OR we have ever seen an RC receiver
90
return;
91
}
92
93
// Nobody ever talks to us. Log an error and enter failsafe.
94
LOGGER_WRITE_ERROR(LogErrorSubsystem::RADIO, LogErrorCode::RADIO_LATE_FRAME);
95
set_failsafe_radio(true);
96
}
97
98
#define FS_COUNTER 3 // radio failsafe kicks in after 3 consecutive throttle values below failsafe_throttle_value
99
void Blimp::set_throttle_and_failsafe(uint16_t throttle_pwm)
100
{
101
// if failsafe not enabled pass through throttle and exit
102
if (g.failsafe_throttle == FS_THR_DISABLED) {
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