• ArduPilot开源飞控之AP_Baro_ExternalAHRS


    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。

    为了更好的从整体理解气压计这个传感器的嵌入式应用,这里深入到back-end驱动层,针对基于串行协议的气压计设备,进行一个研读和理解。

    2. back-end抽象类

    AP_Baro_Backend驱动层需实现方法:

    • void update()
    • static AP_Baro_Backend *probe(AP_Baro &baro, AP_HAL::OwnPtr 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))
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    3. 方法实现

    由于气压数据来自串行总线,因此,其逻辑与MSP、CAN协议类似,相对简单,没有校准等复杂物理公式。

    3.1 AP_Baro_ExternalAHRS

    实例初始化。

    AP_Baro_ExternalAHRS::AP_Baro_ExternalAHRS
     ├──> instance = _frontend.register_sensor()
     └──> set_bus_id(instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_SERIAL,port,0,0))
    
    • 1
    • 2
    • 3

    3.2 update

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

    AP_Baro_ExternalAHRS::update
     └──> 
         ├──> WITH_SEMAPHORE(_sem)
         ├──> _copy_to_frontend(instance, sum_pressure/count, sum_temp/count)
         ├──> sum_pressure = sum_temp = 0
         └──> count = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.3 handle_external

    处理串行协议中气压和温度数据。

    AP_Baro_ExternalAHRS::handle_external
     ├──> 
     │   └──> return // not for us
     ├──> WITH_SEMAPHORE(_sem)
     ├──> sum_pressure += pkt.pressure_pa
     ├──> sum_temp += pkt.temperature
     └──> count++
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.4 Serial port

    串行口驱动数据接收。

    AP_Vehicle::setup
     └──> AP_ExternalAHRS::init
         └──> AP_ExternalAHRS_VectorNav::AP_ExternalAHRS_VectorNav
             └──> AP_ExternalAHRS_VectorNav::update_thread  //thread_create
                 └──> AP_ExternalAHRS_VectorNav::check_uart
                     ├──> AP_ExternalAHRS_VectorNav::process_packet1
                     │   └──> AP_Baro::handle_external
                     │       └──> AP_Baro_ExternalAHRS::handle_external
                     └──> AP_ExternalAHRS_VectorNav::process_packet_VN_100
                         └──> AP_Baro::handle_external
                             └──> AP_Baro_ExternalAHRS::handle_external
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4. 参考资料

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

  • 相关阅读:
    校园安防监控系统升级改造方案:如何实现设备利旧上云与AI视频识别感知?
    C语言 Cortex-A7核 IIC实验
    VM系列振弦采集模块信号检测与采样
    上周热点回顾(1.29-2.4)
    关于动态规划算法
    深入理解强化学习——强化学习的例子
    cesium 雷达扫描 (扫描线)
    Spring Boot Actuator(eureka xstream deserialization RCE)
    spring框架概述及快速入门
    【计算理论】复杂性类coNP
  • 原文地址:https://blog.csdn.net/lida2003/article/details/133761868