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/Blimp.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#include "Blimp.h"1617#define FORCE_VERSION_H_INCLUDE18#include "version.h"19#undef FORCE_VERSION_H_INCLUDE2021const AP_HAL::HAL& hal = AP_HAL::get_HAL();2223#define SCHED_TASK(func, rate_hz, max_time_micros, priority) SCHED_TASK_CLASS(Blimp, &blimp, func, rate_hz, max_time_micros, priority)24#define FAST_TASK(func) FAST_TASK_CLASS(Blimp, &blimp, func)2526/*27scheduler table - all tasks should be listed here.2829All entries in this table must be ordered by priority.3031This table is interleaved with the table in AP_Vehicle to determine32the order in which tasks are run. Convenience methods SCHED_TASK33and SCHED_TASK_CLASS are provided to build entries in this structure:3435SCHED_TASK arguments:36- name of static function to call37- rate (in Hertz) at which the function should be called38- expected time (in MicroSeconds) that the function should take to run39- priority (0 through 255, lower number meaning higher priority)4041SCHED_TASK_CLASS arguments:42- class name of method to be called43- instance on which to call the method44- method to call on that instance45- rate (in Hertz) at which the method should be called46- expected time (in MicroSeconds) that the method should take to run47- priority (0 through 255, lower number meaning higher priority)4849*/50const AP_Scheduler::Task Blimp::scheduler_tasks[] = {51// update INS immediately to get current gyro data populated52FAST_TASK_CLASS(AP_InertialSensor, &blimp.ins, update),53// send outputs to the motors library immediately54FAST_TASK(motors_output),55// run EKF state estimator (expensive)56FAST_TASK(read_AHRS),57// Inertial Nav58FAST_TASK(read_inertia),59// check if ekf has reset target heading or position60FAST_TASK(check_ekf_reset),61// run the attitude controllers62FAST_TASK(update_flight_mode),63// update home from EKF if necessary64FAST_TASK(update_home_from_EKF),6566SCHED_TASK(rc_loop, 100, 130, 3),67SCHED_TASK(throttle_loop, 50, 75, 6),68SCHED_TASK_CLASS(AP_GPS, &blimp.gps, update, 50, 200, 9),69SCHED_TASK(update_batt_compass, 10, 120, 12),70SCHED_TASK_CLASS(RC_Channels, (RC_Channels*)&blimp.g2.rc_channels, read_aux_all, 10, 50, 15),71SCHED_TASK(arm_motors_check, 10, 50, 18),72SCHED_TASK(update_altitude, 10, 100, 21),73SCHED_TASK(three_hz_loop, 3, 75, 24),74#if AP_SERVORELAYEVENTS_ENABLED75SCHED_TASK_CLASS(AP_ServoRelayEvents, &blimp.ServoRelayEvents, update_events, 50, 75, 27),76#endif77#if HAL_LOGGING_ENABLED78SCHED_TASK(full_rate_logging, 50, 50, 33),79#endif80SCHED_TASK_CLASS(AP_Notify, &blimp.notify, update, 50, 90, 36),81SCHED_TASK(one_hz_loop, 1, 100, 39),82SCHED_TASK(ekf_check, 10, 75, 42),83SCHED_TASK(check_vibration, 10, 50, 45),84SCHED_TASK(gpsglitch_check, 10, 50, 48),85SCHED_TASK_CLASS(GCS, (GCS*)&blimp._gcs, update_receive, 400, 180, 51),86SCHED_TASK_CLASS(GCS, (GCS*)&blimp._gcs, update_send, 400, 550, 54),87#if HAL_LOGGING_ENABLED88SCHED_TASK(ten_hz_logging_loop, 10, 350, 57),89SCHED_TASK(twentyfive_hz_logging, 25, 110, 60),90SCHED_TASK_CLASS(AP_Logger, &blimp.logger, periodic_tasks, 400, 300, 63),91#endif92SCHED_TASK_CLASS(AP_InertialSensor, &blimp.ins, periodic, 400, 50, 66),93#if HAL_LOGGING_ENABLED94SCHED_TASK_CLASS(AP_Scheduler, &blimp.scheduler, update_logging, 0.1, 75, 69),95#endif96};9798void Blimp::get_scheduler_tasks(const AP_Scheduler::Task *&tasks,99uint8_t &task_count,100uint32_t &log_bit)101{102tasks = &scheduler_tasks[0];103task_count = ARRAY_SIZE(scheduler_tasks);104log_bit = MASK_LOG_PM;105}106107constexpr int8_t Blimp::_failsafe_priorities[4];108109// rc_loops - reads user input from transmitter/receiver110// called at 100hz111void Blimp::rc_loop()112{113// Read radio and 3-position switch on radio114// -----------------------------------------115read_radio();116rc().read_mode_switch();117}118119// throttle_loop - should be run at 50 hz120// ---------------------------121void Blimp::throttle_loop()122{123// check auto_armed status124update_auto_armed();125}126127// update_batt_compass - read battery and compass128// should be called at 10hz129void Blimp::update_batt_compass(void)130{131// read battery before compass because it may be used for motor interference compensation132battery.read();133134if (AP::compass().available()) {135// update compass with throttle value - used for compassmot136compass.set_voltage(battery.voltage());137compass.read();138}139}140141#if HAL_LOGGING_ENABLED142// Full rate logging of attitude, rate and pid loops143void Blimp::full_rate_logging()144{145if (should_log(MASK_LOG_ATTITUDE_FAST)) {146Log_Write_Attitude();147}148if (should_log(MASK_LOG_PID)) {149Log_Write_PIDs();150}151}152153// ten_hz_logging_loop154// should be run at 10hz155void Blimp::ten_hz_logging_loop()156{157// log attitude data if we're not already logging at the higher rate158if (should_log(MASK_LOG_ATTITUDE_MED) && !should_log(MASK_LOG_ATTITUDE_FAST)) {159Log_Write_Attitude();160}161// log EKF attitude data162if (should_log(MASK_LOG_ATTITUDE_MED) || should_log(MASK_LOG_ATTITUDE_FAST)) {163Log_Write_EKF_POS();164}165if (should_log(MASK_LOG_RCIN)) {166logger.Write_RCIN();167#if AP_RSSI_ENABLED168if (rssi.enabled()) {169logger.Write_RSSI();170}171#endif172}173if (should_log(MASK_LOG_RCOUT)) {174logger.Write_RCOUT();175}176if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {177AP::ins().Write_Vibration();178}179}180181182// twentyfive_hz_logging - should be run at 25hz183void Blimp::twentyfive_hz_logging()184{185if (should_log(MASK_LOG_ATTITUDE_FAST)) {186Log_Write_EKF_POS();187}188189if (should_log(MASK_LOG_IMU)) {190AP::ins().Write_IMU();191}192}193#endif // HAL_LOGGING_ENABLED194195// three_hz_loop - 3.3hz loop196void Blimp::three_hz_loop()197{198// check if we've lost contact with the ground station199failsafe_gcs_check();200}201202// one_hz_loop - runs at 1Hz203void Blimp::one_hz_loop()204{205#if HAL_LOGGING_ENABLED206if (should_log(MASK_LOG_ANY)) {207Log_Write_Data(LogDataID::AP_STATE, ap.value);208}209#endif210211// update assigned functions and enable auxiliary servos212AP::srv().enable_aux_servos();213214AP_Notify::flags.flying = !ap.land_complete;215216blimp.pid_pos_yaw.set_notch_sample_rate(AP::scheduler().get_filtered_loop_rate_hz());217}218219void Blimp::read_AHRS(void)220{221// we tell AHRS to skip INS update as we have already done it in fast_loop()222ahrs.update(true);223224IGNORE_RETURN(ahrs.get_velocity_NED(vel_ned));225IGNORE_RETURN(ahrs.get_relative_position_NED_origin(pos_ned));226227vel_yaw = ahrs.get_yaw_rate_earth();228Vector2f vel_xy_filtd = vel_xy_filter.apply({vel_ned.x, vel_ned.y});229vel_ned_filtd = {vel_xy_filtd.x, vel_xy_filtd.y, vel_z_filter.apply(vel_ned.z)};230vel_yaw_filtd = vel_yaw_filter.apply(vel_yaw);231232#if HAL_LOGGING_ENABLED233AP::logger().WriteStreaming("VNF", "TimeUS,X,XF,Y,YF,Z,ZF,Yaw,YawF,PX,PY,PZ,PYaw", "Qffffffffffff",234AP_HAL::micros64(),235vel_ned.x,236vel_ned_filtd.x,237vel_ned.y,238vel_ned_filtd.y,239vel_ned.z,240vel_ned_filtd.z,241vel_yaw,242vel_yaw_filtd,243pos_ned.x,244pos_ned.y,245pos_ned.z,246blimp.ahrs.get_yaw());247#endif248}249250// read baro and log control tuning251void Blimp::update_altitude()252{253// read in baro altitude254read_barometer();255256#if HAL_LOGGING_ENABLED257if (should_log(MASK_LOG_CTUN)) {258#if AP_INERTIALSENSOR_HARMONICNOTCH_ENABLED259AP::ins().write_notch_log_messages();260#endif261#if HAL_GYROFFT_ENABLED262gyro_fft.write_log_messages();263#endif264}265#endif266}267268//Conversions are in 2D so that up remains up in world frame when the blimp is not exactly level.269void Blimp::rotate_BF_to_NE(Vector2f &vec)270{271float ne_x = vec.x*ahrs.cos_yaw() - vec.y*ahrs.sin_yaw();272float ne_y = vec.x*ahrs.sin_yaw() + vec.y*ahrs.cos_yaw();273vec.x = ne_x;274vec.y = ne_y;275}276277void Blimp::rotate_NE_to_BF(Vector2f &vec)278{279float bf_x = vec.x*ahrs.cos_yaw() + vec.y*ahrs.sin_yaw();280float bf_y = -vec.x*ahrs.sin_yaw() + vec.y*ahrs.cos_yaw();281vec.x = bf_x;282vec.y = bf_y;283284}285286/*287constructor for main Blimp class288*/289Blimp::Blimp(void)290:291flight_modes(&g.flight_mode1),292control_mode(Mode::Number::MANUAL),293rc_throttle_control_in_filter(1.0f),294inertial_nav(ahrs),295param_loader(var_info),296flightmode(&mode_manual)297{298}299300Blimp blimp;301AP_Vehicle& vehicle = blimp;302303AP_HAL_MAIN_CALLBACKS(&blimp);304305306