超级版主
- 积分
- 4672
- 金钱
- 4672
- 注册时间
- 2019-5-8
- 在线时间
- 1224 小时
|
1)实验平台:正点原子阿尔法Linux开发板
2) 章节摘自【正点原子】《I.MX6U嵌入式Qt开发指南 V1.0》
3)购买链接:https://detail.tmall.com/item.htm?id=609033604451
4)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/arm-linux/zdyz-i.mx6ull.html
5)正点原子官方B站:https://space.bilibili.com/394620890
6)正点原子阿尔法Linux交流群:1027879335
第二十三章 ICM20608
本章是ICM20608实验,本章与上一章类似,都是获取传感器的数据,ICM20608是的一款六轴MEMS传感器,包括三轴加速度和三轴陀螺仪。因为本章与上一章类似,本章只提供获取ICM20608的接口,不再重复设计界面程序。
更多ICM20608的信息请看【正点原子】I.MX6U用户快速体验V1.x.pdf的第3.21小节。
23.1 资源简介
在正点原子I.MX6ULL ALPHA开发板底板上有一个6轴MEMS传感器,也就是在底板上晶振旁边的传感器,采用的是spi接口。(注意:I.MX6ULL MINI开发板没有这个传器)。下图为I.MX6ULL ALPHA开发板的6轴MEMS传感器原理图。
开发板底板实物图。
23.2 应用接口
本实验提供ICM20608的Qt访问出厂系统ICM20608驱动设备文件/dev/icm20608的接口。接口类Icm20608根据【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.x.pdf编写第六十二章 Linux SPI 驱动实验编写,想要理解原理请看【正点原子】I.MX6U 嵌入式Linux驱动开发指南,第六十二章,已经写的很详细。对于Qt访问底层驱动接口,我们需要对驱动有一点了解,否则学习起来是比较难的!建议读者全方面学习一下正点原子的I.MX6U配套教程。
源码在路径为Qt/3/09_spi_icm20608_sensor。
“icm20608.h”,头文件如下。
- /******************************************************************
- Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
- * @projectName sensor
- * @brief icm20608.h
- * [url=home.php?mod=space&uid=90321]@Author[/url] Deng Zhimao
- * [url=home.php?mod=space&uid=55957]@EMAIL[/url] <a href="mailto:1252699831@qq.com">1252699831@qq.com</a>
- * [url=home.php?mod=space&uid=28414]@net[/url] <a href="www.openedv.com" target="_blank">www.openedv.com</a>
- * @date 2020-07-10
- *******************************************************************/
- 1 #ifndef ICM20608_H
- 2 #define ICM20608_H
- 3
- 4 #include <QObject>
- 5 #include <QTimer>
- 6
- 7 class Icm20608 : public QObject
- 8 {
- 9 Q_OBJECT
- 10
- 11 public:
- 12 explicit Icm20608(QObject *parent = 0);
- 13 ~Icm20608();
- 14
- 15 Q_INVOKABLE void setCapture(bool str);
- 16
- 17 private:
- 18 QTimer *timer;
- 19 QString gxdata;
- 20 QString gydata;
- 21 QString gzdata;
- 22 QString axdata;
- 23 QString aydata;
- 24 QString azdata;
- 25 QString tempdata;
- 26
- 27 QString gxData();
- 28 QString gyData();
- 29 QString gzData();
- 30 QString axData();
- 31 QString ayData();
- 32 QString azData();
- 33 QString tempData();
- 34
- 35 void icm20608ReadData();
- 36
- 37 Q_PROPERTY(QString gxData READ gxData NOTIFY icm20608DataChanged)
- 38 Q_PROPERTY(QString gyData READ gyData NOTIFY icm20608DataChanged)
- 39 Q_PROPERTY(QString gzData READ gzData NOTIFY icm20608DataChanged)
- 40 Q_PROPERTY(QString axData READ axData NOTIFY icm20608DataChanged)
- 41 Q_PROPERTY(QString ayData READ ayData NOTIFY icm20608DataChanged)
- 42 Q_PROPERTY(QString azData READ azData NOTIFY icm20608DataChanged)
- 43 Q_PROPERTY(QString tempData READ tempData NOTIFY icm20608DataChanged)
- 44
- 45 public slots:
- 46 void timer_timeout();
- 47
- 48
- 49 signals:
- 50 void icm20608DataChanged();
- 51 };
- 52
- 53 #endif // ICM20608_H
复制代码
“icm20608.h”,源文件如下。
- /******************************************************************
- Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
- * @projectName sensor
- * @brief icm20608.cpp
- * @author Deng Zhimao
- * @email <a href="mailto:1252699831@qq.com">1252699831@qq.com</a>
- * @net <a href="www.openedv.com" target="_blank">www.openedv.com</a>
- * @date 2020-07-10
- *******************************************************************/
- 1 #include "icm20608.h"
- 2 #include <stdio.h>
- 3 #include <string.h>
- 4 #include <sys/types.h>
- 5 #include <sys/stat.h>
- 6 #include <fcntl.h>
- 7 #include <unistd.h>
- 8 #include <QDebug>
- 9
- 10 Icm20608::Icm20608(QObject *parent) : QObject (parent)
- 11 {
- 12 timer = new QTimer();
- 13 #if __arm__
- 14 system("insmod /home/root/driver/icm20608/icm20608.ko");
- 15 #endif
- 16 connect(timer,SIGNAL(timeout()),this,SLOT(timer_timeout()));
- 17 }
- 18
- 19 Icm20608::~Icm20608()
- 20 {
- 21
- 22 }
- 23
- 24 void Icm20608::icm20608ReadData()
- 25 {
- 26 int fd;
- 27 char const *filename = "/dev/icm20608";
- 28 signed int databuf[7];
- 29 unsigned char data[14];
- 30 signed int gyro_x_adc, gyro_y_adc, gyro_z_adc;
- 31 signed int accel_x_adc, accel_y_adc, accel_z_adc;
- 32 signed int temp_adc;
- 33
- 34 float gyro_x_act, gyro_y_act, gyro_z_act;
- 35 float accel_x_act, accel_y_act, accel_z_act;
- 36 float temp_act;
- 37
- 38 int ret = 0;
- 39
- 40 fd = open(filename, O_RDWR);
- 41 if(fd < 0) {
- 42 printf("can't open file %s\r\n", filename);
- 43 return;
- 44 }
- 45
- 46 ret = read(fd, databuf, sizeof(databuf));
- 47 /* 若读取成功 */
- 48 if (ret == 0) {
- 49 gyro_x_adc = databuf[0];
- 50 gyro_y_adc = databuf[1];
- 51 gyro_z_adc = databuf[2];
- 52 accel_x_adc = databuf[3];
- 53 accel_y_adc = databuf[4];
- 54 accel_z_adc = databuf[5];
- 55 temp_adc = databuf[6];
- 56
- 57 /* 实际值 */
- 58 gyro_x_act = (float)(gyro_x_adc) / 16.4;
- 59 gyro_y_act = (float)(gyro_y_adc) / 16.4;
- 60 gyro_z_act = (float)(gyro_z_adc) / 16.4;
- 61 accel_x_act = (float)(accel_x_adc) / 2048;
- 62 accel_y_act = (float)(accel_y_adc) / 2048;
- 63 accel_z_act = (float)(accel_z_adc) / 2048;
- 64 temp_act = ((float)(temp_adc) - 25 ) / 326.8 + 25;
- 65
- 66 gxdata = QString::number(gyro_x_act, 'f', 2);
- 67 gydata = QString::number(gyro_y_act, 'f', 2);
- 68 gzdata = QString::number(gyro_z_act, 'f', 2);
- 69 axdata = QString::number(accel_x_act, 'f', 2);
- 70 aydata = QString::number(accel_y_act, 'f', 2);
- 71 azdata = QString::number(accel_z_act, 'f', 2);
- 72 tempdata = QString::number(temp_act, 'f', 2);
- 73 }
- 74 close(fd);
- 75 emit icm20608DataChanged();
- 76 }
- 77
- 78 QString Icm20608::gxData()
- 79 {
- 80 return gxdata;
- 81 }
- 82
- 83 QString Icm20608::gyData()
- 84 {
- 85 return gydata;
- 86 }
- 87
- 88 QString Icm20608::gzData()
- 89 {
- 90 return gzdata;
- 91 }
- 92
- 93 QString Icm20608::axData()
- 94 {
- 95 return axdata;
- 96 }
- 97
- 98 QString Icm20608::ayData()
- 99 {
- 100 return aydata;
- 101 }
- 102
- 103 QString Icm20608::azData()
- 104 {
- 105 return azdata;
- 106 }
- 107
- 108 QString Icm20608::tempData()
- 109 {
- 110 return tempdata;
- 111 }
- 112
- 113
- 114 void Icm20608::timer_timeout()
- 115 {
- 116 icm20608ReadData();
- 117 }
- 118
- 119 void Icm20608::setCapture(bool str)
- 120 {
- 121 if(str)
- 122 timer->start(500);
- 123 else
- 124 timer->stop();
- 125 }
复制代码
|
|