2021-03-18 00:12:54 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Blimp.h"
|
|
|
|
|
|
|
|
#define FORCE_VERSION_H_INCLUDE
|
|
|
|
#include "version.h"
|
|
|
|
#undef FORCE_VERSION_H_INCLUDE
|
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
2021-11-13 23:49:45 -04:00
|
|
|
#define SCHED_TASK(func, rate_hz, max_time_micros, priority) SCHED_TASK_CLASS(Blimp, &blimp, func, rate_hz, max_time_micros, priority)
|
2022-04-21 08:41:08 -03:00
|
|
|
#define FAST_TASK(func) FAST_TASK_CLASS(Blimp, &blimp, func)
|
2021-03-18 00:12:54 -03:00
|
|
|
|
|
|
|
/*
|
2022-04-21 08:41:08 -03:00
|
|
|
scheduler table - all tasks should be listed here.
|
2021-11-13 23:49:45 -04:00
|
|
|
|
|
|
|
All entries in this table must be ordered by priority.
|
|
|
|
|
|
|
|
This table is interleaved with the table in AP_Vehicle to determine
|
|
|
|
the order in which tasks are run. Convenience methods SCHED_TASK
|
|
|
|
and SCHED_TASK_CLASS are provided to build entries in this structure:
|
|
|
|
|
|
|
|
SCHED_TASK arguments:
|
|
|
|
- name of static function to call
|
|
|
|
- rate (in Hertz) at which the function should be called
|
|
|
|
- expected time (in MicroSeconds) that the function should take to run
|
|
|
|
- priority (0 through 255, lower number meaning higher priority)
|
|
|
|
|
|
|
|
SCHED_TASK_CLASS arguments:
|
|
|
|
- class name of method to be called
|
|
|
|
- instance on which to call the method
|
|
|
|
- method to call on that instance
|
|
|
|
- rate (in Hertz) at which the method should be called
|
|
|
|
- expected time (in MicroSeconds) that the method should take to run
|
|
|
|
- priority (0 through 255, lower number meaning higher priority)
|
|
|
|
|
2021-03-18 00:12:54 -03:00
|
|
|
*/
|
|
|
|
const AP_Scheduler::Task Blimp::scheduler_tasks[] = {
|
2022-04-21 08:41:08 -03:00
|
|
|
// update INS immediately to get current gyro data populated
|
|
|
|
FAST_TASK_CLASS(AP_InertialSensor, &blimp.ins, update),
|
|
|
|
// send outputs to the motors library immediately
|
|
|
|
FAST_TASK(motors_output),
|
2023-08-21 08:19:19 -03:00
|
|
|
// run EKF state estimator (expensive)
|
2022-04-21 08:41:08 -03:00
|
|
|
FAST_TASK(read_AHRS),
|
|
|
|
// Inertial Nav
|
|
|
|
FAST_TASK(read_inertia),
|
|
|
|
// check if ekf has reset target heading or position
|
|
|
|
FAST_TASK(check_ekf_reset),
|
|
|
|
// run the attitude controllers
|
|
|
|
FAST_TASK(update_flight_mode),
|
|
|
|
// update home from EKF if necessary
|
|
|
|
FAST_TASK(update_home_from_EKF),
|
|
|
|
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK(rc_loop, 100, 130, 3),
|
|
|
|
SCHED_TASK(throttle_loop, 50, 75, 6),
|
|
|
|
SCHED_TASK_CLASS(AP_GPS, &blimp.gps, update, 50, 200, 9),
|
|
|
|
SCHED_TASK(update_batt_compass, 10, 120, 12),
|
|
|
|
SCHED_TASK_CLASS(RC_Channels, (RC_Channels*)&blimp.g2.rc_channels, read_aux_all, 10, 50, 15),
|
|
|
|
SCHED_TASK(arm_motors_check, 10, 50, 18),
|
|
|
|
SCHED_TASK(update_altitude, 10, 100, 21),
|
|
|
|
SCHED_TASK(three_hz_loop, 3, 75, 24),
|
2023-06-06 05:05:07 -03:00
|
|
|
#if AP_SERVORELAYEVENTS_ENABLED
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK_CLASS(AP_ServoRelayEvents, &blimp.ServoRelayEvents, update_events, 50, 75, 27),
|
2023-06-06 05:05:07 -03:00
|
|
|
#endif
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK(full_rate_logging, 50, 50, 33),
|
2021-03-18 00:12:54 -03:00
|
|
|
#endif
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK_CLASS(AP_Notify, &blimp.notify, update, 50, 90, 36),
|
|
|
|
SCHED_TASK(one_hz_loop, 1, 100, 39),
|
|
|
|
SCHED_TASK(ekf_check, 10, 75, 42),
|
|
|
|
SCHED_TASK(check_vibration, 10, 50, 45),
|
|
|
|
SCHED_TASK(gpsglitch_check, 10, 50, 48),
|
|
|
|
SCHED_TASK_CLASS(GCS, (GCS*)&blimp._gcs, update_receive, 400, 180, 51),
|
|
|
|
SCHED_TASK_CLASS(GCS, (GCS*)&blimp._gcs, update_send, 400, 550, 54),
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK(ten_hz_logging_loop, 10, 350, 57),
|
|
|
|
SCHED_TASK(twentyfive_hz_logging, 25, 110, 60),
|
|
|
|
SCHED_TASK_CLASS(AP_Logger, &blimp.logger, periodic_tasks, 400, 300, 63),
|
2021-03-18 00:12:54 -03:00
|
|
|
#endif
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK_CLASS(AP_InertialSensor, &blimp.ins, periodic, 400, 50, 66),
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-11-13 23:49:45 -04:00
|
|
|
SCHED_TASK_CLASS(AP_Scheduler, &blimp.scheduler, update_logging, 0.1, 75, 69),
|
2024-01-09 23:37:03 -04:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
void Blimp::get_scheduler_tasks(const AP_Scheduler::Task *&tasks,
|
|
|
|
uint8_t &task_count,
|
|
|
|
uint32_t &log_bit)
|
|
|
|
{
|
|
|
|
tasks = &scheduler_tasks[0];
|
|
|
|
task_count = ARRAY_SIZE(scheduler_tasks);
|
|
|
|
log_bit = MASK_LOG_PM;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int8_t Blimp::_failsafe_priorities[4];
|
|
|
|
|
|
|
|
// rc_loops - reads user input from transmitter/receiver
|
|
|
|
// called at 100hz
|
|
|
|
void Blimp::rc_loop()
|
|
|
|
{
|
|
|
|
// Read radio and 3-position switch on radio
|
|
|
|
// -----------------------------------------
|
|
|
|
read_radio();
|
|
|
|
rc().read_mode_switch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// throttle_loop - should be run at 50 hz
|
|
|
|
// ---------------------------
|
|
|
|
void Blimp::throttle_loop()
|
|
|
|
{
|
|
|
|
// check auto_armed status
|
|
|
|
update_auto_armed();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update_batt_compass - read battery and compass
|
|
|
|
// should be called at 10hz
|
|
|
|
void Blimp::update_batt_compass(void)
|
|
|
|
{
|
|
|
|
// read battery before compass because it may be used for motor interference compensation
|
|
|
|
battery.read();
|
|
|
|
|
2021-07-27 22:54:59 -03:00
|
|
|
if (AP::compass().available()) {
|
2021-03-18 00:12:54 -03:00
|
|
|
// update compass with throttle value - used for compassmot
|
|
|
|
compass.set_voltage(battery.voltage());
|
|
|
|
compass.read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-03-18 00:12:54 -03:00
|
|
|
// Full rate logging of attitude, rate and pid loops
|
2021-06-23 23:26:56 -03:00
|
|
|
void Blimp::full_rate_logging()
|
2021-03-18 00:12:54 -03:00
|
|
|
{
|
2021-06-22 03:36:11 -03:00
|
|
|
if (should_log(MASK_LOG_ATTITUDE_FAST)) {
|
2021-03-18 00:12:54 -03:00
|
|
|
Log_Write_Attitude();
|
|
|
|
}
|
2021-06-18 01:40:01 -03:00
|
|
|
if (should_log(MASK_LOG_PID)) {
|
|
|
|
Log_Write_PIDs();
|
|
|
|
}
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ten_hz_logging_loop
|
|
|
|
// should be run at 10hz
|
|
|
|
void Blimp::ten_hz_logging_loop()
|
|
|
|
{
|
|
|
|
// log attitude data if we're not already logging at the higher rate
|
2021-06-22 03:36:11 -03:00
|
|
|
if (should_log(MASK_LOG_ATTITUDE_MED) && !should_log(MASK_LOG_ATTITUDE_FAST)) {
|
2021-03-18 00:12:54 -03:00
|
|
|
Log_Write_Attitude();
|
|
|
|
}
|
|
|
|
// log EKF attitude data
|
|
|
|
if (should_log(MASK_LOG_ATTITUDE_MED) || should_log(MASK_LOG_ATTITUDE_FAST)) {
|
|
|
|
Log_Write_EKF_POS();
|
|
|
|
}
|
|
|
|
if (should_log(MASK_LOG_RCIN)) {
|
|
|
|
logger.Write_RCIN();
|
2024-07-23 07:51:07 -03:00
|
|
|
#if AP_RSSI_ENABLED
|
2021-03-18 00:12:54 -03:00
|
|
|
if (rssi.enabled()) {
|
|
|
|
logger.Write_RSSI();
|
|
|
|
}
|
2024-07-23 07:51:07 -03:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
if (should_log(MASK_LOG_RCOUT)) {
|
|
|
|
logger.Write_RCOUT();
|
|
|
|
}
|
|
|
|
if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
|
2021-04-27 01:59:42 -03:00
|
|
|
AP::ins().Write_Vibration();
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// twentyfive_hz_logging - should be run at 25hz
|
|
|
|
void Blimp::twentyfive_hz_logging()
|
|
|
|
{
|
|
|
|
if (should_log(MASK_LOG_ATTITUDE_FAST)) {
|
|
|
|
Log_Write_EKF_POS();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_log(MASK_LOG_IMU)) {
|
2021-04-27 01:59:42 -03:00
|
|
|
AP::ins().Write_IMU();
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
}
|
2024-01-09 23:37:03 -04:00
|
|
|
#endif // HAL_LOGGING_ENABLED
|
2021-03-18 00:12:54 -03:00
|
|
|
|
|
|
|
// three_hz_loop - 3.3hz loop
|
|
|
|
void Blimp::three_hz_loop()
|
|
|
|
{
|
|
|
|
// check if we've lost contact with the ground station
|
|
|
|
failsafe_gcs_check();
|
|
|
|
}
|
|
|
|
|
|
|
|
// one_hz_loop - runs at 1Hz
|
|
|
|
void Blimp::one_hz_loop()
|
|
|
|
{
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-03-18 00:12:54 -03:00
|
|
|
if (should_log(MASK_LOG_ANY)) {
|
|
|
|
Log_Write_Data(LogDataID::AP_STATE, ap.value);
|
|
|
|
}
|
2024-01-09 23:37:03 -04:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
|
|
|
|
// update assigned functions and enable auxiliary servos
|
2024-11-09 03:24:59 -04:00
|
|
|
AP::srv().enable_aux_servos();
|
2021-03-18 00:12:54 -03:00
|
|
|
|
|
|
|
AP_Notify::flags.flying = !ap.land_complete;
|
2023-08-10 08:09:12 -03:00
|
|
|
|
|
|
|
blimp.pid_pos_yaw.set_notch_sample_rate(AP::scheduler().get_filtered_loop_rate_hz());
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Blimp::read_AHRS(void)
|
|
|
|
{
|
|
|
|
// we tell AHRS to skip INS update as we have already done it in fast_loop()
|
|
|
|
ahrs.update(true);
|
2021-06-22 03:36:11 -03:00
|
|
|
|
2021-06-18 01:40:01 -03:00
|
|
|
IGNORE_RETURN(ahrs.get_velocity_NED(vel_ned));
|
2023-07-27 02:03:08 -03:00
|
|
|
IGNORE_RETURN(ahrs.get_relative_position_NED_origin(pos_ned));
|
2021-06-18 01:40:01 -03:00
|
|
|
|
|
|
|
vel_yaw = ahrs.get_yaw_rate_earth();
|
|
|
|
Vector2f vel_xy_filtd = vel_xy_filter.apply({vel_ned.x, vel_ned.y});
|
|
|
|
vel_ned_filtd = {vel_xy_filtd.x, vel_xy_filtd.y, vel_z_filter.apply(vel_ned.z)};
|
|
|
|
vel_yaw_filtd = vel_yaw_filter.apply(vel_yaw);
|
2023-07-27 02:03:08 -03:00
|
|
|
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2023-07-27 02:03:08 -03:00
|
|
|
AP::logger().WriteStreaming("VNF", "TimeUS,X,XF,Y,YF,Z,ZF,Yaw,YawF,PX,PY,PZ,PYaw", "Qffffffffffff",
|
|
|
|
AP_HAL::micros64(),
|
|
|
|
vel_ned.x,
|
|
|
|
vel_ned_filtd.x,
|
|
|
|
vel_ned.y,
|
|
|
|
vel_ned_filtd.y,
|
|
|
|
vel_ned.z,
|
|
|
|
vel_ned_filtd.z,
|
|
|
|
vel_yaw,
|
|
|
|
vel_yaw_filtd,
|
|
|
|
pos_ned.x,
|
|
|
|
pos_ned.y,
|
|
|
|
pos_ned.z,
|
|
|
|
blimp.ahrs.get_yaw());
|
2024-01-09 23:37:03 -04:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read baro and log control tuning
|
|
|
|
void Blimp::update_altitude()
|
|
|
|
{
|
|
|
|
// read in baro altitude
|
|
|
|
read_barometer();
|
|
|
|
|
2024-01-09 23:37:03 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-03-18 00:12:54 -03:00
|
|
|
if (should_log(MASK_LOG_CTUN)) {
|
2024-02-12 20:26:08 -04:00
|
|
|
#if AP_INERTIALSENSOR_HARMONICNOTCH_ENABLED
|
2021-12-30 20:32:18 -04:00
|
|
|
AP::ins().write_notch_log_messages();
|
2024-02-12 20:26:08 -04:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
#if HAL_GYROFFT_ENABLED
|
|
|
|
gyro_fft.write_log_messages();
|
|
|
|
#endif
|
|
|
|
}
|
2024-01-09 23:37:03 -04:00
|
|
|
#endif
|
2021-03-18 00:12:54 -03:00
|
|
|
}
|
|
|
|
|
2021-06-18 01:40:01 -03:00
|
|
|
//Conversions are in 2D so that up remains up in world frame when the blimp is not exactly level.
|
2021-06-22 03:36:11 -03:00
|
|
|
void Blimp::rotate_BF_to_NE(Vector2f &vec)
|
2021-06-18 01:40:01 -03:00
|
|
|
{
|
2021-06-22 03:36:11 -03:00
|
|
|
float ne_x = vec.x*ahrs.cos_yaw() - vec.y*ahrs.sin_yaw();
|
|
|
|
float ne_y = vec.x*ahrs.sin_yaw() + vec.y*ahrs.cos_yaw();
|
|
|
|
vec.x = ne_x;
|
|
|
|
vec.y = ne_y;
|
2021-06-18 01:40:01 -03:00
|
|
|
}
|
|
|
|
|
2021-06-22 03:36:11 -03:00
|
|
|
void Blimp::rotate_NE_to_BF(Vector2f &vec)
|
2021-06-18 01:40:01 -03:00
|
|
|
{
|
2021-06-22 03:36:11 -03:00
|
|
|
float bf_x = vec.x*ahrs.cos_yaw() + vec.y*ahrs.sin_yaw();
|
|
|
|
float bf_y = -vec.x*ahrs.sin_yaw() + vec.y*ahrs.cos_yaw();
|
|
|
|
vec.x = bf_x;
|
|
|
|
vec.y = bf_y;
|
2021-03-18 00:12:54 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
constructor for main Blimp class
|
|
|
|
*/
|
|
|
|
Blimp::Blimp(void)
|
2024-01-09 23:37:03 -04:00
|
|
|
:
|
2021-03-18 00:12:54 -03:00
|
|
|
flight_modes(&g.flight_mode1),
|
|
|
|
control_mode(Mode::Number::MANUAL),
|
|
|
|
rc_throttle_control_in_filter(1.0f),
|
|
|
|
inertial_nav(ahrs),
|
|
|
|
param_loader(var_info),
|
|
|
|
flightmode(&mode_manual)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Blimp blimp;
|
|
|
|
AP_Vehicle& vehicle = blimp;
|
|
|
|
|
|
|
|
AP_HAL_MAIN_CALLBACKS(&blimp);
|