|
编程步骤
(一)打开设备
- #define ICM20607_DEV "/dev/icm20607"
- fd = open(ICM20607_DEV, O_RDWR);
- if(fd < 0) {
- printf("can't open file %s\r\n", ICM20607_DEV);
- return -1;
- }
复制代码 宏定义板卡上的传感器节点为/dev/icm20607;
使用open()打开传感器,如果错误返回-1;
(二)读取数据
- ret = read(fd, databuf, sizeof(databuf));
复制代码 使用read将设备中的数据读取出来放入datebuf中。
(三)定义数组成员
- gyro_x_adc = databuf[0];
- gyro_y_adc = databuf[1];
- gyro_z_adc = databuf[2];
- accel_x_adc = databuf[3];
- accel_y_adc = databuf[4];
- accel_z_adc = databuf[5];
- temp_adc = databuf[6];
复制代码 把x、y、z轴的角速度,x、y、z轴的加速度和温度一共7个元素填入数组内,这些成员是从设备中读取出来的数据。
(四)数据类型转换
- gyro_x_act = (float)(gyro_x_adc) / 16.4;
- gyro_y_act = (float)(gyro_y_adc) / 16.4;
- gyro_z_act = (float)(gyro_z_adc) / 16.4;
- accel_x_act = (float)(accel_x_adc) / 2048;
- accel_y_act = (float)(accel_y_adc) / 2048;
- accel_z_act = (float)(accel_z_adc) / 2048;
- temp_act = ((float)(temp_adc) - 25 ) / 326.8 + 25;
复制代码 因为角速度、加速度和温度这些都不能用常量表示,所以需要给他们转换成浮点量,并做相应数学计算,以转换成我们可读的数据。
(五)关闭设备
详细代码
elf1_cmd_icm20607:
- elf1_cmd_icm20607:
- #include "stdio.h"
- #include "unistd.h"
- #include "sys/types.h"
- #include "sys/stat.h"
- #include "sys/ioctl.h"
- #include "fcntl.h"
- #include "stdlib.h"
- #include "string.h"
- #include <poll.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <fcntl.h>
- #define ICM20607_DEV "/dev/icm20607"
- int main(int argc, char *argv[])
- {
- int fd;
- signed int databuf[7];
- unsigned char data[14];
- signed int gyro_x_adc, gyro_y_adc, gyro_z_adc;
- signed int accel_x_adc, accel_y_adc, accel_z_adc;
- signed int temp_adc;
-
- float gyro_x_act, gyro_y_act, gyro_z_act;
- float accel_x_act, accel_y_act, accel_z_act;
- float temp_act;
-
- int ret = 0;
-
- fd = open(ICM20607_DEV, O_RDWR);
- if(fd < 0) {
- printf("can't open file %s\r\n", ICM20607_DEV);
- return -1;
- }
-
- while (1) {
- ret = read(fd, databuf, sizeof(databuf));
- if(ret == 0) { /* ?????? */
- gyro_x_adc = databuf[0];
- gyro_y_adc = databuf[1];
- gyro_z_adc = databuf[2];
- accel_x_adc = databuf[3];
- accel_y_adc = databuf[4];
- accel_z_adc = databuf[5];
- temp_adc = databuf[6];
-
- /* ????? */
- gyro_x_act = (float)(gyro_x_adc) / 16.4;
- gyro_y_act = (float)(gyro_y_adc) / 16.4;
- gyro_z_act = (float)(gyro_z_adc) / 16.4;
- accel_x_act = (float)(accel_x_adc) / 2048;
- accel_y_act = (float)(accel_y_adc) / 2048;
- accel_z_act = (float)(accel_z_adc) / 2048;
- temp_act = ((float)(temp_adc) - 25 ) / 326.8 + 25;
-
-
- printf("\r\n");
- printf("raw value:\r\n");
- printf("gx = %d, gy = %d, gz = %d\r\n", gyro_x_adc, gyro_y_adc, gyro_z_adc);
- printf("ax = %d, ay = %d, az = %d\r\n", accel_x_adc, accel_y_adc, accel_z_adc);
- printf("temp = %d\r\n", temp_adc);
- printf("\r\n");
- printf("act value:\r\n");
- printf("act gx = %.2f度/S, act gy = %.2f度/S, act gz = %.2f度/S\r\n", gyro_x_act, gyro_y_act, gyro_z_act);
- printf("act ax = %.2fg, act ay = %.2fg, act az = %.2fg\r\n", accel_x_act, accel_y_act, accel_z_act);
- printf("act temp = %.2f摄氏度\r\n", temp_act);
- }
- sleep(1); /*1s */
- }
- close(fd);
- return 0;
- }
复制代码
|
|