ArduPilot开源飞控之AP_Baro_SITL

news/2024/7/10 19:19:59 标签: 开源, Ardupilot

ArduPilot开源飞控之AP_Baro_SITL

  • 1. 源由
  • 2. back-end抽象类
  • 3. 方法实现
    • 3.1 AP_Baro_SITL
    • 3.2 _timer
    • 3.3 temperature_adjustment
    • 3.4 wind_pressure_correction
    • 3.5 update
  • 4. 参考资料

1. 源由

鉴于ArduPilot开源飞控之AP_Baro中涉及Sensor Driver有以下总线类型:

  1. I2C
  2. Serial UART
  3. CAN
  4. SITL //模拟传感器(暂时并列放在这里)

ArduPilot之开源代码Sensor Drivers设计的front-end / back-end分层设计思路,AP_Baro主要描述的是front-end。

为了AP_Baro代码研读的完整性,就继续简单的整理下下针对AP_Baro_SITL研读和理解。

2. back-end抽象类

AP_Baro_Backend驱动层需实现方法:

  • void update()
  • static AP_Baro_Backend *probe(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev)

注:通常来说使用ChibiOS的都有定时器,如果没有定时器,可以使用void accumulate(void)来实现传感器的数据定时获取。

class AP_Baro_Backend
{
public:
    AP_Baro_Backend(AP_Baro &baro);
    virtual ~AP_Baro_Backend(void) {};

    // each driver must provide an update method to copy accumulated
    // data to the frontend
    virtual void update() = 0;

    // accumulate function. This is used for backends that don't use a
    // timer, and need to be called regularly by the main code to
    // trigger them to read the sensor
    virtual void accumulate(void) {}

    void backend_update(uint8_t instance);

    //  Check that the baro valid by using a mean filter.
    // If the value further that filtrer_range from mean value, it is rejected.
    bool pressure_ok(float press);
    uint32_t get_error_count() const { return _error_count; }

#if AP_BARO_MSP_ENABLED
    virtual void handle_msp(const MSP::msp_baro_data_message_t &pkt) {}
#endif

#if AP_BARO_EXTERNALAHRS_ENABLED
    virtual void handle_external(const AP_ExternalAHRS::baro_data_message_t &pkt) {}
#endif

    /*
      device driver IDs. These are used to fill in the devtype field
      of the device ID, which shows up as BARO_DEVID* parameters to
      users.
     */
    enum DevTypes {
        DEVTYPE_BARO_SITL     = 0x01,
        DEVTYPE_BARO_BMP085   = 0x02,
        DEVTYPE_BARO_BMP280   = 0x03,
        DEVTYPE_BARO_BMP388   = 0x04,
        DEVTYPE_BARO_DPS280   = 0x05,
        DEVTYPE_BARO_DPS310   = 0x06,
        DEVTYPE_BARO_FBM320   = 0x07,
        DEVTYPE_BARO_ICM20789 = 0x08,
        DEVTYPE_BARO_KELLERLD = 0x09,
        DEVTYPE_BARO_LPS2XH   = 0x0A,
        DEVTYPE_BARO_MS5611   = 0x0B,
        DEVTYPE_BARO_SPL06    = 0x0C,
        DEVTYPE_BARO_UAVCAN   = 0x0D,
        DEVTYPE_BARO_MSP      = 0x0E,
        DEVTYPE_BARO_ICP101XX = 0x0F,
        DEVTYPE_BARO_ICP201XX = 0x10,
        DEVTYPE_BARO_MS5607   = 0x11,
        DEVTYPE_BARO_MS5837   = 0x12,
        DEVTYPE_BARO_MS5637   = 0x13,
        DEVTYPE_BARO_BMP390   = 0x14,
    };
    
protected:
    // reference to frontend object
    AP_Baro &_frontend;

    void _copy_to_frontend(uint8_t instance, float pressure, float temperature);

    // semaphore for access to shared frontend data
    HAL_Semaphore _sem;

    virtual void update_healthy_flag(uint8_t instance);

    // mean pressure for range filter
    float _mean_pressure; 
    // number of dropped samples. Not used for now, but can be usable to choose more reliable sensor
    uint32_t _error_count;

    // set bus ID of this instance, for BARO_DEVID parameters
    void set_bus_id(uint8_t instance, uint32_t id) {
        _frontend.sensors[instance].bus_id.set(int32_t(id));
    }
};

3. 方法实现

AP_Baro_SITL是一个模拟器件,其气压数据来源于模拟系统,对于模拟系统这里不展开,其传递参量的主要方式是全局变量_sitl->state.altitude

3.1 AP_Baro_SITL

实例初始化,注册一个定时回调函数。

AP_Baro_SITL::AP_Baro_SITL
 └──> <_sitl != nullptr>
    ├──> _instance = _frontend.register_sensor();
    ├──> <APM_BUILD_TYPE(APM_BUILD_ArduSub)>
    │   └──> _frontend.set_type(_instance, AP_Baro::BARO_TYPE_WATER);
    ├──> set_bus_id(_instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_SITL, 0, _instance, DEVTYPE_BARO_SITL));
    │
    │  /********************************************************************************
    │   * start periodic call back                                                     *
    │   ********************************************************************************/
    └──> hal.scheduler->register_timer_process(FUNCTOR_BIND(this, &AP_Baro_SITL::_timer, void));

3.2 _timer

定时模拟高度数据(这里不涉及温度的校准,但是做了一些模拟的噪音,比如:baro glitch/drift/noise/temperature/wind)。

AP_Baro_SITL::_timer
 │  /********************************************************************************
 │   * 100Hz                                                                        *
 │   ********************************************************************************/
 ├──> const uint32_t now = AP_HAL::millis();
 ├──> <(now - _last_sample_time) < 10>
 │   └──> return;
 │
 ├──> _last_sample_time = now;
 ├──> float sim_alt = _sitl->state.altitude;
 ├──> <_sitl->baro[_instance].disable>
 │   └──> return;  // barometer is disabled
 │
 │  /********************************************************************************
 │   * Update simulated altitude                                                    *
 │   ********************************************************************************/
 │  // Noise for simulated altitude
 ├──> sim_alt += _sitl->baro[_instance].drift * now * 0.001f;
 ├──> sim_alt += _sitl->baro[_instance].noise * rand_float();
 │
 │  // add baro glitch
 ├──> sim_alt += _sitl->baro[_instance].glitch;
 │
 │  // add delay
 ├──> uint32_t best_time_delta = 200;  // initialise large time representing buffer entry closest to current time - delay.
 ├──> uint8_t best_index = 0;  // initialise number representing the index of the entry in buffer closest to delay.
 │
 │  // storing data from sensor to buffer
 ├──> <now - _last_store_time >= 10>  // store data every 10 ms.
 │   ├──> _last_store_time = now;
 │   ├──> <_store_index > _buffer_length - 1>  
 │   │   └──> _store_index = 0;  // reset buffer index if index greater than size of buffer
 │   │
 │   │  // if freezed barometer, report altitude to last recorded altitude
 │   ├──> <_sitl->baro[_instance].freeze == 1>
 │   │   └──> sim_alt = _last_altitude;
 │   ├──> < else >
 │   │   └──> _last_altitude = sim_alt;
 │   │
 │   ├──> _buffer[_store_index].data = sim_alt;  // add data to current index
 │   ├──> _buffer[_store_index].time = _last_store_time;  // add time_stamp to current index
 │   └──> _store_index = _store_index + 1;  // increment index
 │
 │  // return delayed measurement
 ├──> const uint32_t delayed_time = now - _sitl->baro[_instance].delay;  // get time corresponding to delay
 │
 │  // find data corresponding to delayed time in buffer
 ├──> <for (uint8_t i = 0; i <= _buffer_length - 1; i++)>
 │   │  // find difference between delayed time and time stamp in buffer
 │   ├──> uint32_t time_delta = abs((int32_t)(delayed_time - _buffer[i].time));
 │   │  // if this difference is smaller than last delta, store this time
 │   └──> <time_delta < best_time_delta>
 │       ├──> best_index = i;
 │       └──> best_time_delta = time_delta;
 │
 ├──> <best_time_delta < 200>  // only output stored state if < 200 msec retrieval error
 │   └──> sim_alt = _buffer[best_index].data;
 │
 │  /********************************************************************************
 │   * Temperature adjust                                                           *
 │   ********************************************************************************/
 ├──> <!APM_BUILD_TYPE(APM_BUILD_ArduSub)>
 │   ├──> float sigma, delta, theta;
 │   ├──> AP_Baro::SimpleAtmosphere(sim_alt * 0.001f, sigma, delta, theta);
 │   ├──> float p = SSL_AIR_PRESSURE * delta;
 │   ├──> float T = KELVIN_TO_C(SSL_AIR_TEMPERATURE * theta);
 │   └──> temperature_adjustment(p, T);
 ├──> <else>
 │   ├──> float rho, delta, theta;
 │   ├──> AP_Baro::SimpleUnderWaterAtmosphere(-sim_alt * 0.001f, rho, delta, theta);
 │   ├──> float p = SSL_AIR_PRESSURE * delta;
 │   └──> float T = KELVIN_TO_C(SSL_AIR_TEMPERATURE * theta);
 │
 │  /********************************************************************************
 │   * add in correction for wind effects                                           *
 │   ********************************************************************************/
 ├──> p += wind_pressure_correction(_instance);
 │
 ├──> _recent_press = p;
 ├──> _recent_temp = T;
 └──> _has_sample = true;

3.3 temperature_adjustment

温度模拟修正。

AP_Baro_SITL::temperature_adjustment
 ├──> const float tsec = AP_HAL::millis() * 0.001f;
 ├──> const float T_sensor = T + AP::sitl()->temp_board_offset;
 ├──> const float tconst = AP::sitl()->temp_tconst;
 ├──> <tsec < 23 * tconst> // time which past the equation below equals T_sensor within approx. 1E-9
 │   ├──> const float T0 = AP::sitl()->temp_start;
 │   └──> T = T_sensor - (T_sensor - T0) * expf(-tsec / tconst);
 ├──> < else >
 │   └──> T = T_sensor;
 ├──> const float baro_factor = AP::sitl()->temp_baro_factor;
 ├──> const float Tzero = 30.0f;  // start baro adjustment at 30C
 └──> <is_positive(baro_factor)>
     │  // this produces a pressure change with temperature that
     │  // closely matches what has been observed with a ICM-20789
     │  // barometer. A typical factor is 1.2.
     └──> p -= powf(MAX(T - Tzero, 0), baro_factor);

3.4 wind_pressure_correction

风力压强修正。

AP_Baro_SITL::wind_pressure_correction
 ├──> const auto &bp = AP::sitl()->baro[instance];
 │
 │  // correct for static pressure position errors
 ├──> const Vector3f &airspeed_vec_bf = AP::sitl()->state.velocity_air_bf;
 │
 ├──> float error = 0.0;
 ├──> const float sqx = sq(airspeed_vec_bf.x);
 ├──> const float sqy = sq(airspeed_vec_bf.y);
 ├──> const float sqz = sq(airspeed_vec_bf.z);
 │
 │  // error for x
 ├──> <is_positive(airspeed_vec_bf.x)>
 │   └──> error += bp.wcof_xp * sqx;
 ├──> < else >
 │   └──> error += bp.wcof_xn * sqx;
 │
 │  // error for y
 ├──> <is_positive(airspeed_vec_bf.y)>
 │   └──> error += bp.wcof_yp * sqy;
 ├──> < else >
 │   └──> error += bp.wcof_yn * sqy;
 │
 │  // error for z
 ├──> <is_positive(airspeed_vec_bf.z)>
 │   └──> error += bp.wcof_zp * sqz;
 ├──> < else >
 │   └──> error += bp.wcof_zn * sqz;
 │
 └──> return error * 0.5 * SSL_AIR_DENSITY * AP::baro().get_air_density_ratio();

3.5 update

front-end / back-end数据更新。

AP_Baro_SITL::update
 ├──> <!_has_sample>
 │   └──> return;
 ├──> WITH_SEMAPHORE(_sem);
 ├──> _copy_to_frontend(_instance, _recent_press, _recent_temp);
 └──> _has_sample = false;

4. 参考资料

【1】ArduPilot开源飞控系统之简单介绍
【2】ArduPilot之开源代码Task介绍
【3】ArduPilot飞控启动&运行过程简介
【4】ArduPilot之开源代码Library&Sketches设计
【5】ArduPilot之开源代码Sensor Drivers设计


http://www.niftyadmin.cn/n/5080914.html

相关文章

c++视觉处理-----cv::findContours函数和图像进行去噪、平滑、边缘检测和轮廓检测,动态检测图形

cv::findContours cv::findContours 是OpenCV中用于查找图像中对象轮廓的函数。轮廓是对象的边界&#xff0c;通常用于对象检测、分割和形状分析。cv::findContours 函数的基本用法如下&#xff1a; cv::findContours(image, contours, hierarchy, mode, method, offset cv:…

Vuex基础使用存取值+异步请求后台

目录 一、Vuex简介 1.1 定义 1.2 Vuex关键概念 1.3 使用Vuex的优势 1.4 Vuex中各个js文件的用途 1.5 Vuex各组件 1.5.1 图解 1.5.2 详解 1.6 变量传值的演变形式 二、Vuex获取值 2.1 安装 2.2 菜单栏 2.3 模块 2.4 引用 三、Vuex改变值 四、Vuex异步&请求后台…

清华智谱AI大模型ChatGLM-Pro申请开通教程

清华智谱AI大模型ChatGLM-Pro申请开通教程 ChatGLM系列模型&#xff0c;包括ChatGLM-130B和ChatGLM-6B模型&#xff0c;支持相对复杂的自然语言指令&#xff0c;并且能够解决困难的推理类问题。其中&#xff0c;ChatGLM-6B模型吸引了全球超过 160 万人下载安装&#xff0c;该模…

气象台卫星监测vr交互教学增强学生的学习兴趣和动力

对地观测是以地球为研究对象&#xff0c;依托卫星、飞船等光电仪器&#xff0c;进行各种探测活动&#xff0c;其核心是遥感技术&#xff0c;因此为了让遥感专业学员能提前熟悉对地观测规则、流程、方法及注意事项&#xff0c;借助VR虚拟现实制作的三维仿真场景&#xff0c;能让…

嵌入式Linux裸机开发(七)UART串口、IIC、SPI通信

系列文章目录 文章目录 系列文章目录前言UART串口通信介绍UART配置 IIC介绍I.MX6U 的 I2C SPI介绍I.MX6U ECSPI 结语 前言 大概学完这三种通信后&#xff0c;之后就先去学系统移植&#xff0c;其他的先暂时放下 UART串口通信 介绍 串口全称叫做串行接口&#xff0c;通常也叫…

工业读写器如何选型?

随着工业自动化的迅速发展&#xff0c;库存管理、生产流程、质量管理等传统工作人工工作也逐渐由各种智能设备来替代管理。RFID技术作为非接触式数据传输的通信方式&#xff0c;也常常应用在工业场合之中。具体工业RFID读写器如何选型&#xff0c;有哪些选择要点呢?ANDEAWELL国…

Pytorvh之Vision Transformer图像分类

文章目录 前言一、Transformer1.Transformer概览2.Self-Attention3.Multi-head Attention4.Position-wise Feed-Forward Networks(位置前馈网络)5.残差连接和层归一化6.Positional Encodings(位置编码) 二、Vision Transformer1.Vision Transformer概览2.Embedding层结构&#…

Pytorch之SwinTransformer图像分类

文章目录 前言一、Swin Transformer1.Swin Transformer概览2.Patch Partition3.Patch Merging4.W-MSA5.SW-MSA(滑动窗口多头注意力机制)6.Relative Position bias(相对位置偏移)7.网络结构&#x1f947;Swin Transformer Block&#x1f948;Architecture 二、网络实现1.构建Eff…