Last active
March 6, 2025 08:36
-
-
Save GOROman/f91f2c01adb6cfb192563e62c9c851fb to your computer and use it in GitHub Desktop.
#がいためIMU を Arduino で動かす SPRESENSE向けマルチIMU Add-onボード [CXD5602PWBIMU1J]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Spresense Multi-IMU[CXD5602PWBIMU1J] Sample for Arduino IDE | |
// | |
// #がいためIMU | |
// by @GOROman | |
#include <stdio.h> | |
#include <sys/ioctl.h> | |
#include <time.h> | |
#include <inttypes.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <poll.h> | |
#include <errno.h> | |
// PWBIMU | |
#include <nuttx/sensors/cxd5602pwbimu.h> | |
extern "C" int board_cxd5602pwbimu_initialize(int); | |
// IMU設定 | |
#define IMU_RATE (60) // Hz | |
#define IMU_ADRANGE (4) // G | |
#define IMU_GDRANGE (500) // dps | |
#define IMU_FIFO (1) // FIFO | |
static int fd; | |
void setup() { | |
int ret = 0; | |
// ↓これがないと動かん | |
ret = board_cxd5602pwbimu_initialize(5); | |
if (ret < 0) | |
{ | |
printf("ERROR: Failed to initialize CXD5602PWBIMU.\n"); | |
return; | |
} | |
fd = open("/dev/imu0", O_RDONLY); | |
if (fd < 0) { | |
printf("ERROR: Could not open /dev/imu0\n"); | |
return; | |
} | |
ret = ioctl(fd, SNIOC_SSAMPRATE, IMU_RATE); | |
if (ret) { | |
printf("ERROR: Set sampling rate failed. %d\n", errno); | |
return; | |
} | |
// レンジ設定 | |
cxd5602pwbimu_range_t range; | |
range.accel = IMU_ADRANGE; | |
range.gyro = IMU_GDRANGE; | |
ret = ioctl(fd, SNIOC_SDRANGE, (unsigned long)(uintptr_t)&range); | |
if (ret) { | |
printf("ERROR: Set dynamic range failed. %d\n", errno); | |
return; | |
} | |
// FIFO指定 | |
ret = ioctl(fd, SNIOC_SFIFOTHRESH, IMU_FIFO); | |
if (ret) { | |
printf("ERROR: Set FIFO failed. %d\n", errno); | |
return; | |
} | |
ret = ioctl(fd, SNIOC_ENABLE, 1); | |
if (ret) { | |
printf("ERROR: Enable failed. %d\n", errno); | |
return; | |
} | |
//delay(500); | |
// ToDo 空読みしてバッファのゴミを捨てる | |
printf("#timestamp,temp,ax,ay,az,gx,gy,gz\n"); | |
} | |
void loop() { | |
struct pollfd fds[1]; | |
fds[0].fd = fd; | |
fds[0].events = POLLIN; | |
// ポーリング | |
int ret = poll(fds, 1, 1000); | |
if (ret > 0) { | |
// データ取得 | |
cxd5602pwbimu_data_t data; | |
ret = read(fd, &data, sizeof(data)); | |
if (ret == sizeof(data)) { | |
float timestamp = data.timestamp / 19200000.0f; | |
printf("%4.2F,%4.2F,%F,%F,%F,%F,%F,%F\n", timestamp, data.temp, data.ax, data.ay, data.az, data.gx, data.gy, data.gz); | |
} | |
} | |
} |
Author
GOROman
commented
Mar 3, 2025

参考にさせていただいたもの
https://qiita.com/SaChiKaKK/items/50c550782b13fe43061e
これをexternしてユーザーが呼ぶ必要があるのは、まずいのではw
extern "C" int board_cxd5602pwbimu_initialize(int);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment