mirror of
https://github.com/okalachev/flix.git
synced 2025-07-29 12:28:59 +00:00
Implement RC fail-safe
This commit is contained in:
parent
2fdad7bdb6
commit
ec832d4e37
@ -52,6 +52,7 @@ float thrustTarget;
|
|||||||
|
|
||||||
void control() {
|
void control() {
|
||||||
interpretRC();
|
interpretRC();
|
||||||
|
failsafe();
|
||||||
if (mode == STAB) {
|
if (mode == STAB) {
|
||||||
controlAttitude();
|
controlAttitude();
|
||||||
controlRate();
|
controlRate();
|
||||||
|
22
flix/failsafe.ino
Normal file
22
flix/failsafe.ino
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
|
||||||
|
// Repository: https://github.com/okalachev/flix
|
||||||
|
|
||||||
|
// Fail-safe for RC loss
|
||||||
|
|
||||||
|
#define RC_LOSS_TIMEOUT 0.2
|
||||||
|
#define DESCEND_TIME 3.0 // time to descend from full throttle to zero
|
||||||
|
|
||||||
|
void failsafe() {
|
||||||
|
if (t - controlsTime > RC_LOSS_TIMEOUT) {
|
||||||
|
descend();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void descend() {
|
||||||
|
// Smooth descend on RC lost
|
||||||
|
mode = STAB;
|
||||||
|
controls[RC_CHANNEL_ROLL] = 0;
|
||||||
|
controls[RC_CHANNEL_PITCH] = 0;
|
||||||
|
controls[RC_CHANNEL_YAW] = 0;
|
||||||
|
controls[RC_CHANNEL_THROTTLE] -= dt / DESCEND_TIME;
|
||||||
|
}
|
@ -28,6 +28,7 @@ float dt; // time delta from previous step, s
|
|||||||
float loopFreq; // loop frequency, Hz
|
float loopFreq; // loop frequency, Hz
|
||||||
int16_t channels[RC_CHANNELS]; // raw rc channels
|
int16_t channels[RC_CHANNELS]; // raw rc channels
|
||||||
float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle)
|
float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle)
|
||||||
|
float controlsTime; // time of the last controls update
|
||||||
Vector gyro; // gyroscope data
|
Vector gyro; // gyroscope data
|
||||||
Vector acc; // accelerometer data, m/s/s
|
Vector acc; // accelerometer data, m/s/s
|
||||||
Vector rates; // filtered angular rates, rad/s
|
Vector rates; // filtered angular rates, rad/s
|
||||||
|
@ -93,6 +93,7 @@ void handleMavlink(const void *_msg) {
|
|||||||
controls[RC_CHANNEL_YAW] = manualControl.r / 1000.0f * MAVLINK_CONTROL_SCALE;
|
controls[RC_CHANNEL_YAW] = manualControl.r / 1000.0f * MAVLINK_CONTROL_SCALE;
|
||||||
controls[RC_CHANNEL_MODE] = 1; // STAB mode
|
controls[RC_CHANNEL_MODE] = 1; // STAB mode
|
||||||
controls[RC_CHANNEL_ARMED] = 1; // armed
|
controls[RC_CHANNEL_ARMED] = 1; // armed
|
||||||
|
controlsTime = t;
|
||||||
|
|
||||||
if (abs(controls[RC_CHANNEL_YAW]) < MAVLINK_CONTROL_YAW_DEAD_ZONE) controls[RC_CHANNEL_YAW] = 0;
|
if (abs(controls[RC_CHANNEL_YAW]) < MAVLINK_CONTROL_YAW_DEAD_ZONE) controls[RC_CHANNEL_YAW] = 0;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ void readRC() {
|
|||||||
SBUSData data = RC.data();
|
SBUSData data = RC.data();
|
||||||
memcpy(channels, data.ch, sizeof(channels)); // copy channels data
|
memcpy(channels, data.ch, sizeof(channels)); // copy channels data
|
||||||
normalizeRC();
|
normalizeRC();
|
||||||
|
controlsTime = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ float loopFreq;
|
|||||||
float motors[4];
|
float motors[4];
|
||||||
int16_t channels[16]; // raw rc channels
|
int16_t channels[16]; // raw rc channels
|
||||||
float controls[RC_CHANNELS];
|
float controls[RC_CHANNELS];
|
||||||
|
float controlsTime;
|
||||||
Vector acc;
|
Vector acc;
|
||||||
Vector gyro;
|
Vector gyro;
|
||||||
Vector rates;
|
Vector rates;
|
||||||
@ -48,6 +49,8 @@ void sendMavlink();
|
|||||||
void sendMessage(const void *msg);
|
void sendMessage(const void *msg);
|
||||||
void receiveMavlink();
|
void receiveMavlink();
|
||||||
void handleMavlink(const void *_msg);
|
void handleMavlink(const void *_msg);
|
||||||
|
void failsafe();
|
||||||
|
void descend();
|
||||||
inline Quaternion FLU2FRD(const Quaternion &q);
|
inline Quaternion FLU2FRD(const Quaternion &q);
|
||||||
|
|
||||||
// mocks
|
// mocks
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "log.ino"
|
#include "log.ino"
|
||||||
#include "cli.ino"
|
#include "cli.ino"
|
||||||
#include "mavlink.ino"
|
#include "mavlink.ino"
|
||||||
|
#include "failsafe.ino"
|
||||||
#include "lpf.h"
|
#include "lpf.h"
|
||||||
|
|
||||||
using ignition::math::Vector3d;
|
using ignition::math::Vector3d;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user