eefocus_4023189 发表于 2024-12-3 09:25:23

飞凌嵌入式ElfBoard ELF 1板卡-运动追踪之编写程序

编程步骤
(一)打开设备
#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;

                        gyro_y_adc = databuf;

                        gyro_z_adc = databuf;

                        accel_x_adc = databuf;

                        accel_y_adc = databuf;

                        accel_z_adc = databuf;

                        temp_adc = databuf;把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;因为角速度、加速度和温度这些都不能用常量表示,所以需要给他们转换成浮点量,并做相应数学计算,以转换成我们可读的数据。
(五)关闭设备
close(fd);详细代码
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;

        unsigned char data;

        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;

                        gyro_y_adc = databuf;

                        gyro_z_adc = databuf;

                        accel_x_adc = databuf;

                        accel_y_adc = databuf;

                        accel_z_adc = databuf;

                        temp_adc = databuf;



                        /* ????? */

                        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;

}

页: [1]
查看完整版本: 飞凌嵌入式ElfBoard ELF 1板卡-运动追踪之编写程序

Baidu
map