Skip to content

Instantly share code, notes, and snippets.

@GOROman
Last active March 6, 2025 08:36
Show Gist options
  • Save GOROman/f91f2c01adb6cfb192563e62c9c851fb to your computer and use it in GitHub Desktop.
Save GOROman/f91f2c01adb6cfb192563e62c9c851fb to your computer and use it in GitHub Desktop.
#がいためIMU を Arduino で動かす SPRESENSE向けマルチIMU Add-onボード [CXD5602PWBIMU1J]
// 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);
}
}
}
@GOROman
Copy link
Author

GOROman commented Mar 3, 2025

timestamp,temp,ax,ay,az,gx,gy,gz
0.33,33.93,-3.011784,4.816074,8.351231,0.116137,-0.562403,0.033414
0.35,33.93,-2.417288,4.626781,8.032255,0.071058,-0.337508,0.022280
0.37,33.94,-2.164863,4.402452,7.950056,0.020741,-0.126543,0.024627
0.38,33.93,-2.223323,4.405577,8.007706,-0.015018,-0.097223,0.040852
0.40,33.92,-2.315368,4.505774,8.119014,-0.021957,-0.076775,0.004358
0.42,33.94,-3.137459,4.520225,8.580765,0.020912,-0.064899,-0.013044
0.43,33.93,-3.596541,4.446510,8.750338,0.161372,-0.249943,0.059044
0.45,33.93,-3.358593,4.739544,8.387946,0.208131,-0.415035,0.222680
0.46,33.94,-2.416405,4.720460,7.937698,0.139508,-0.447042,0.293826
0.48,33.93,-1.708674,4.741129,7.770493,-0.029699,-0.326419,0.199817
0.49,33.93,-1.556814,4.622583,7.942013,-0.122732,-0.201148,0.085569
0.51,33.94,-1.848096,4.609468,8.399862,-0.117049,-0.167079,0.008333
0.53,33.94,-2.148170,4.572726,8.609358,-0.000808,-0.107989,-0.057255
0.54,33.94,-2.626677,4.763606,8.555855,0.089707,0.011833,-0.029932
0.56,33.94,-2.294027,4.758095,8.078823,0.060156,0.025836,-0.014603
0.58,33.93,-2.275369,4.656123,8.063630,-0.029173,0.075388,-0.067727
0.60,33.93,-2.354237,4.531997,8.246021,-0.038535,0.057073,-0.106667
0.61,33.92,-2.478798,4.547624,8.224124,-0.017507,-0.033713,-0.103964
0.63,33.94,-2.578885,4.595068,8.293842,-0.006515,-0.085014,-0.060892

@GOROman
Copy link
Author

GOROman commented Mar 3, 2025

image

@GOROman
Copy link
Author

GOROman commented Mar 3, 2025

参考にさせていただいたもの
https://qiita.com/SaChiKaKK/items/50c550782b13fe43061e

@GOROman
Copy link
Author

GOROman commented Mar 3, 2025

これを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