Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Blimp/radio.cpp
Views: 1798
#include "Blimp.h"123// Function that will read the radio data, limit servos and trigger a failsafe4// ----------------------------------------------------------------------------56void Blimp::default_dead_zones()7{8channel_right->set_default_dead_zone(20);9channel_front->set_default_dead_zone(20);10channel_up->set_default_dead_zone(30);11channel_yaw->set_default_dead_zone(20);12rc().channel(CH_6)->set_default_dead_zone(0);13}1415void Blimp::init_rc_in()16{17// the library gaurantees that these are non-nullptr:18channel_right = &rc().get_roll_channel();19channel_front = &rc().get_pitch_channel();20channel_up = &rc().get_throttle_channel();21channel_yaw = &rc().get_yaw_channel();2223// set rc channel ranges24channel_right->set_angle(RC_SCALE);25channel_front->set_angle(RC_SCALE);26channel_yaw->set_angle(RC_SCALE);27channel_up->set_angle(RC_SCALE);2829// set default dead zones30default_dead_zones();3132// initialise throttle_zero flag33ap.throttle_zero = true;34}3536// init_rc_out -- initialise motors37void Blimp::init_rc_out()38{39// enable aux servos to cope with multiple output channels per motor40AP::srv().enable_aux_servos();4142// refresh auxiliary channel to function map43SRV_Channels::update_aux_servo_function();44}454647// enable_motor_output() - enable and output lowest possible value to motors48void Blimp::enable_motor_output()49{50// enable motors51motors->output_min();52}5354void Blimp::read_radio()55{56const uint32_t tnow_ms = millis();5758if (rc().read_input()) {59ap.new_radio_frame = true;6061set_throttle_and_failsafe(channel_up->get_radio_in());62set_throttle_zero_flag(channel_up->get_control_in());6364const float dt = (tnow_ms - last_radio_update_ms)*1.0e-3f;65rc_throttle_control_in_filter.apply(channel_up->get_control_in(), dt);66last_radio_update_ms = tnow_ms;67return;68}6970// No radio input this time71if (failsafe.radio) {72// already in failsafe!73return;74}7576const uint32_t elapsed = tnow_ms - last_radio_update_ms;77// turn on throttle failsafe if no update from the RC Radio for 500ms or 2000ms if we are using RC_OVERRIDE78const uint32_t timeout = RC_Channels::has_active_overrides() ? FS_RADIO_RC_OVERRIDE_TIMEOUT_MS : FS_RADIO_TIMEOUT_MS;79if (elapsed < timeout) {80// not timed out yet81return;82}83if (!g.failsafe_throttle) {84// throttle failsafe not enabled85return;86}87if (!rc().has_ever_seen_rc_input() && !motors->armed()) {88// we only failsafe if we are armed OR we have ever seen an RC receiver89return;90}9192// Nobody ever talks to us. Log an error and enter failsafe.93LOGGER_WRITE_ERROR(LogErrorSubsystem::RADIO, LogErrorCode::RADIO_LATE_FRAME);94set_failsafe_radio(true);95}9697#define FS_COUNTER 3 // radio failsafe kicks in after 3 consecutive throttle values below failsafe_throttle_value98void Blimp::set_throttle_and_failsafe(uint16_t throttle_pwm)99{100// if failsafe not enabled pass through throttle and exit101if (g.failsafe_throttle == FS_THR_DISABLED) {102return;103}104105//check for low throttle value106if (throttle_pwm < (uint16_t)g.failsafe_throttle_value) {107108// if we are already in failsafe or motors not armed pass through throttle and exit109if (failsafe.radio || !(rc().has_ever_seen_rc_input() || motors->armed())) {110return;111}112113// check for 3 low throttle values114// Note: we do not pass through the low throttle until 3 low throttle values are received115failsafe.radio_counter++;116if ( failsafe.radio_counter >= FS_COUNTER ) {117failsafe.radio_counter = FS_COUNTER; // check to ensure we don't overflow the counter118set_failsafe_radio(true);119}120} else {121// we have a good throttle so reduce failsafe counter122failsafe.radio_counter--;123if ( failsafe.radio_counter <= 0 ) {124failsafe.radio_counter = 0; // check to ensure we don't underflow the counter125126// disengage failsafe after three (nearly) consecutive valid throttle values127if (failsafe.radio) {128set_failsafe_radio(false);129}130}131// pass through throttle132}133}134135#define THROTTLE_ZERO_DEBOUNCE_TIME_MS 400136// set_throttle_zero_flag - set throttle_zero flag from debounced throttle control137// throttle_zero is used to determine if the pilot intends to shut down the motors138// Basically, this signals when we are not flying. We are either on the ground139// or the pilot has shut down the vehicle in the air and it is free-floating140void Blimp::set_throttle_zero_flag(int16_t throttle_control)141{142static uint32_t last_nonzero_throttle_ms = 0;143uint32_t tnow_ms = millis();144145// if not using throttle interlock and non-zero throttle and not E-stopped,146// or using motor interlock and it's enabled, then motors are running,147// and we are flying. Immediately set as non-zero148if (throttle_control > 0) {149last_nonzero_throttle_ms = tnow_ms;150ap.throttle_zero = false;151} else if (tnow_ms - last_nonzero_throttle_ms > THROTTLE_ZERO_DEBOUNCE_TIME_MS) {152ap.throttle_zero = true;153}154//TODO: This may not be needed155}156157158