Pixhawk原生固件PX4之添外置传感器MPU6500

2023-05-16

欢迎交流~ 个人 Gitter 交流平台,点击直达:Gitter


成功的在Pixhawk上添加了一个自定义的传感器MPU6500.

Pixhawk飞控板上空余出一个SPI4接口

pe

pc

SPI

提示: 多出来的GPIO_EXT引脚可以作为片选引脚,再接一个传感器。

PX4驱动文件中已经写好了mpu6500的驱动程序,只是没有使用。整体与mpu6000.cpp类似,但是一些细节部分还未统一。总之PX4的传感器驱动部分一直在改。

这里是基于Firmware 1.5.2

修改位置

Firmware/src/drivers/boards/px4fmu-v2/board_config.h

/*Fantasy modified begins */
/* SPI4 off */
#define GPIO_SPI4_SCK_OFF   (GPIO_INPUT|GPIO_PULLDOWN|GPIO_PORTE|GPIO_PIN2)  // PE2
#define GPIO_SPI4_MISO_OFF  (GPIO_INPUT|GPIO_PULLDOWN|GPIO_PORTE|GPIO_PIN5)  // PE5
#define GPIO_SPI4_MOSI_OFF  (GPIO_INPUT|GPIO_PULLDOWN|GPIO_PORTE|GPIO_PIN6)  // PE6
/*Fantasy modified ends */

/* SPI4 chip selects off Fantasy add*/
#define GPIO_SPI_CS_EXT0_OFF        (GPIO_INPUT|GPIO_PULLDOWN|GPIO_SPEED_2MHz|GPIO_PORTE|GPIO_PIN4) // PE4
/*Fantasy modified ends */

Firmware/src/drivers/boards/px4fmu-v2/px4fmu2_init.c

__EXPORT int nsh_archinitialize(void)
{
  ...
    spi4 = px4_spibus_initialize(4);

    /* Default SPI4 to 1MHz and de-assert the known chip selects. */
    SPI_SETFREQUENCY(spi4, 10000000);
    SPI_SETBITS(spi4, 8);
    SPI_SETMODE(spi4, SPIDEV_MODE3);
    /* Fantasy modified begins */
    // SPI_SELECT(spi4, PX4_SPIDEV_EXT0, false);
    // SPI_SELECT(spi4, PX4_SPIDEV_EXT1, false);
    SPI_SELECT(spi4, PX4_SPIDEV_EXT_MPU, false);
    // 此处不改也可,只是为了强调SPI_SELECT()这个函数的作用
    /* Fantasy modified ends */

Firmware/src/drivers/mpu6500/mpu6500.cpp

int
mpu6500_main(int argc, char *argv[])
{
    /* Fantasy modified begins*/
    //bool external_bus = false;
    bool external_bus = true; //因为这里使用的是外部SPI
    /* Fantasy modified ends*/
  ...

Firmware/ROMFS/px4fmu_common/init.d/rc.sensors

#!nsh
#
# Standard startup script for PX4FMU v1, v2, v3, v4 onboard sensor drivers.
#
...
    else
        # FMUv2
        if mpu6000 start
        then
        fi

        if mpu9250 start
        then
        fi

        if l3gd20 start
        then
        fi

        if lsm303d start
        then
        fi
    fi
fi

Firmware/cmake/configs/nuttx_px4fmu-v2_default.cmake

...
    drivers/pwm_input
    drivers/camera_trigger
    drivers/bst
    #drivers/snapdragon_rc_pwm
    drivers/lis3mdl
    drivers/mpu6500
...

随便在某宝买了一款MPU6500的传感器模块

mpu6500

接上就好使

spiconnec

实际测试

test

确定好使。

总结与疑问:

  • 配置好端口驱动注册到系统就可以采用基本的文件操作指令了
  • 之前听说传感器都是采用的中断方式读取,这里笔者并没有接MPU6500传感器模块的中断引脚。
/* 
 * A simple data read app
 */
#include <px4_config.h>
#include <px4_tasks.h>
#include <px4_posix.h>
#include <unistd.h>
#include <stdio.h>
#include <nuttx/config.h>
#include <drivers/drv_hrt.h>
#include <sys/types.h>
#include <stdint.h>
#include <sys/time.h>
#include <px4_time.h>
#include <string.h>
#include <nuttx/rtc.h>

#include <stdio.h>
#include <errno.h>
#include <uORB/uORB.h>

#include <uORB/topics/sensor_accel.h>
#include <uORB/topics/sensor_gyro.h>



__EXPORT int fantasy_test_main(int argc, char *argv[]);

int fantasy_test_main(int argc, char *argv[])
{
    PX4_INFO("Hello Sky!");

    /* subscribe to sensor_combined topic */
    int accel_sub_fd = orb_subscribe(ORB_ID(sensor_accel_mpu6500));
    int  gyro_sub_fd = orb_subscribe(ORB_ID(sensor_gyro_mpu6500));
    /* limit the update rate to 10 Hz */
    orb_set_interval(accel_sub_fd, 100);
    orb_set_interval(gyro_sub_fd,100);

    /* one could wait for multiple topics with this technique, just using one here */
    px4_pollfd_struct_t fds[] = {
        { .fd = accel_sub_fd,   .events = POLLIN },
        { .fd = gyro_sub_fd,    .events = POLLIN },

    };

    int error_counter = 0;

    for (int i = 0; i < 10; i++) {
        /* wait for sensor update of 1 file descriptor for 1000 ms (1 second) */
        int poll_ret = px4_poll(fds, 2, 1000);

        /* handle the poll result */
        if (poll_ret == 0) {
            /* this means none of our providers is giving us data */
            PX4_ERR("Got no data within a second");

        } else if (poll_ret < 0) {
            /* this is seriously bad - should be an emergency */
            if (error_counter < 10 || error_counter % 50 == 0) {
                /* use a counter to prevent flooding (and slowing us down) */
                PX4_ERR("ERROR return value from poll(): %d", poll_ret);
            }

            error_counter++;

        } else {

            if (fds[0].revents & POLLIN) {
                /* obtained data for the first file descriptor */
                struct sensor_accel_s acc_raw;
                /* copy sensors raw data into local buffer */
                orb_copy(ORB_ID(sensor_accel_mpu6500), accel_sub_fd, &acc_raw);
                PX4_INFO("Accelerometer:\t %8.4f \t%8.4f \t%8.4f ",
                     (double)acc_raw.x_raw,
                     (double)acc_raw.y_raw,
                     (double)acc_raw.z_raw);
            }


            if (fds[1].revents & POLLIN) {
                /* obtained data for the first file descriptor */
                struct sensor_gyro_s gyro_raw;
                /* copy sensors raw data into local buffer */
                orb_copy(ORB_ID(sensor_gyro_mpu6500), gyro_sub_fd, &gyro_raw);
                PX4_INFO("Gyroscope:\t %8.4f \t%8.4f \t%8.4f ",
                     (double)gyro_raw.x_raw,
                     (double)gyro_raw.y_raw,
                     (double)gyro_raw.z_raw);
            }

        }
    }

    PX4_INFO("exiting");

    return 0;
}

接下来在QGC添加自定义消息。


                                          By Fantasy

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pixhawk原生固件PX4之添外置传感器MPU6500 的相关文章

随机推荐

  • C++编译报错fmt未定义的引用

    对 fmt v5 internal basic data POWERS OF 10 64 未定义的引用 1 最简单的方法 xff1a 把代码中printf的输出全部换成std cout或者其他的 2 安装fmt包 git clone htt
  • PyG/torch_geometric的一些坑

    安装PyG span class token keyword import span os span class token keyword import span torch os span class token punctuation
  • Pixhawk官网飞行模式介绍

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a Flight Mode 飞行模式 原文地址 http dev px4 io concept flight modes html 飞行模式定义了系统在任何给定时间的状态
  • PX4中文维基汉化项目启动

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 敬启者 xff1a 打算进行PX4官网的汉化工作 GitBook 与官网的方式相同 xff0c 我们也是将网站以GitBook的方式呈现给大家 汉化后的版本先点点点点
  • Windows / Ubuntu操作系统下Pixhawk原生固件PX4的编译方法

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 更新于2017 3 13 FAQ 本文说明针对 PX4 Firmware 1 6 0 问题 1 xff1a 找不到python jinja2模块 CMake Erro
  • Sublime Text中文乱码的解决方法

    Sublime Text Sublime Text这款代码编译器相当不错 xff0c 自带高亮显示 xff0c 界面清新 但是Sublime Text默认是不支持中文显示的 xff0c 这种中文乱码的行为万万是不能够接受的 这里简单介绍一下
  • 自制Pixhawk飞控板烧写BootLoader教程

    对于自己制作的飞控板 xff0c 通过USB连接电脑之后 xff0c 开始电脑是无法检测到飞控板的端口存在的 检测不到端口 xff0c 就不能用控制台给飞控板烧写固件 xff0c 就不能用QGroundControl xff0c 就不能进行
  • Pixhawk原生固件PX4之常用函数解读

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a PX4Firmware 经常有人将Pixhawk PX4 APM还有ArduPilot弄混 这里首先还是简要说明一下 xff1a Pixhawk是飞控硬件平台 xff
  • Pixhawk原生固件PX4之添加uORB主题

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 本说明针对 Firmware v1 5 4 1 添加流程说明 1 在Firmware msg下新建uORB的成员变量 xff0c eg xxx msg 2 在Firm
  • Pixhawk原生固件PX4之SITL软件在环仿真

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 故事开始之前 xff0c 先按照笔者的这一篇博客在Ubuntu上完成固件的编译 jMAVSim仿真 jMAVSim仿真不需要任何配置 xff0c 直接输入指令即可 s
  • Pixhawk原生固件PX4之串口读取信息

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 这篇博客纯粹出于对FreeApe这位先行者贡献的复现 xff0c 也是本人一直想要进行的一项操作 在此还是做一下记录 时代在改变 xff0c 代码在更新 xff0c
  • Effective Modern C++ 条款21 比起直接使用new,更偏爱使用std::make_unique和std::make_shared

    比起直接使用new xff0c 更偏爱使用std make unique和std make shared 让我们从std make unique和std make shared之间的比较开始讲起吧 std make shared是C 43
  • Pixhawk原生固件PX4之串口添加读取传感器实现

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 本博客承接前一篇 xff0c 对FreeApe的串口添加超声波传感器博文后半部分进行学习 为什么叫前奏呢 xff0c 因为用了伪传感器 xff0c 把单片机用串口发送
  • Pixhawk原生固件PX4之MAVLink协议解析

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a PX4 对Mavlink 协议提供了良好的原生支持 该协议既可以用于地面站 Ground ControlStation GCS 对无人机 UAV 的控制 xff0c
  • Pixhawk原生固件PX4之TAKEOFF的启动流程

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 以TAKEOFF为例说明PX4中一个飞行模式的启动流程 众所周知由遥控器或者地面站决定Main state作为用户期望到达的飞行模式然后有commander进行条件判
  • Pixhawk原生固件PX4之驱动ID

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 驱动ID PX4使用驱动ID将独立传感器贯穿于整个系统 这些ID存储于配置参数中 xff0c 用于匹配传感器校正值 xff0c 以及决定哪些传感器被记录到log中 传
  • Pixhawk原生固件PX4之SPI驱动注册过程

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 一切事出有因 xff0c 为了添加一个自定义SPI总线连接的传感器 xff0c 首先要弄清楚一个SPI设备的注册过程 xff0c 大致涉及以下的一些文件 接下来就该以
  • Pixhawk原生固件PX4之MPU6000驱动分析

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 要想自己添加一个传感器的话 xff0c 最好先搞明白已有的传感器的工作过程 这里记录一下PX4中MPU6000加速度计陀螺仪的解读过程 xff0c 从mpu6000
  • Pixhawk原生固件PX4之日期时间的确定

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 偶然注意到PX4日志中老是出现类似于2000 01 01 00 00 00这种日期 有兴趣的可以搜索一下千年虫问题 xff0c 于是结合代码进行了一波分析 最后定位到
  • Pixhawk原生固件PX4之添外置传感器MPU6500

    欢迎交流 个人 Gitter 交流平台 xff0c 点击直达 xff1a 成功的在Pixhawk上添加了一个自定义的传感器MPU6500 Pixhawk飞控板上空余出一个SPI4接口 提示 xff1a 多出来的GPIO EXT引脚可以作为片