Skip to content

Commit c3ab298

Browse files
authored
Fix interpretation of IMU data
1 parent 38c0bc8 commit c3ab298

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/board_controller/openbci/galea_v4.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,12 @@ void GaleaV4::read_thread ()
466466
// aux, 5 times smaller sampling rate
467467
if (((int)b[0 + offset]) % 5 == 0)
468468
{
469-
double accel_scale = (double)(0.002 / (pow (2, 4)));
470-
double gyro_scale = (double)(0.002 / (pow (2, 4))); // to be confirmed
471-
double magnetometer_scale = (double)(0.002 / (pow (2, 4))); // to be confirmed
469+
double accel_scale = (double)(8.0 / static_cast<double> (pow (2, 16) - 1));
470+
double gyro_scale = (double)(1000.0 / static_cast<double> (pow (2, 16) - 1));
471+
double magnetometer_scale_xy =
472+
(double)(2.6 / static_cast<double> (pow (2, 13) - 1));
473+
double magnetometer_scale_z =
474+
(double)(5.0 / static_cast<double> (pow (2, 15) - 1));
472475
aux_package[board_descr["auxiliary"]["package_num_channel"].get<int> ()] =
473476
(double)b[0 + offset];
474477
uint16_t temperature = 0;
@@ -501,25 +504,28 @@ void GaleaV4::read_thread ()
501504
timestamp_device;
502505
// accel
503506
aux_package[board_descr["auxiliary"]["accel_channels"][0].get<int> ()] =
504-
accel_scale * cast_16bit_to_int32 (b + 96 + offset);
507+
accel_scale * (double)cast_16bit_to_int32_swap_order (b + 96 + offset);
505508
aux_package[board_descr["auxiliary"]["accel_channels"][1].get<int> ()] =
506-
accel_scale * cast_16bit_to_int32 (b + 98 + offset);
509+
accel_scale * (double)cast_16bit_to_int32_swap_order (b + 98 + offset);
507510
aux_package[board_descr["auxiliary"]["accel_channels"][2].get<int> ()] =
508-
accel_scale * cast_16bit_to_int32 (b + 100 + offset);
511+
accel_scale * (double)cast_16bit_to_int32_swap_order (b + 100 + offset);
509512
// gyro
510513
aux_package[board_descr["auxiliary"]["gyro_channels"][0].get<int> ()] =
511-
gyro_scale * cast_16bit_to_int32 (b + 102 + offset);
514+
gyro_scale * (double)cast_16bit_to_int32_swap_order (b + 102 + offset);
512515
aux_package[board_descr["auxiliary"]["gyro_channels"][1].get<int> ()] =
513-
gyro_scale * cast_16bit_to_int32 (b + 104 + offset);
516+
gyro_scale * (double)cast_16bit_to_int32_swap_order (b + 104 + offset);
514517
aux_package[board_descr["auxiliary"]["gyro_channels"][2].get<int> ()] =
515-
gyro_scale * cast_16bit_to_int32 (b + 106 + offset);
518+
gyro_scale * (double)cast_16bit_to_int32_swap_order (b + 106 + offset);
516519
// magnetometer
517520
aux_package[board_descr["auxiliary"]["magnetometer_channels"][0].get<int> ()] =
518-
magnetometer_scale * cast_16bit_to_int32 (b + 108 + offset);
521+
magnetometer_scale_xy *
522+
(double)cast_13bit_to_int32_swap_order (b + 108 + offset);
519523
aux_package[board_descr["auxiliary"]["magnetometer_channels"][1].get<int> ()] =
520-
magnetometer_scale * cast_16bit_to_int32 (b + 110 + offset);
524+
magnetometer_scale_xy *
525+
(double)cast_13bit_to_int32_swap_order (b + 110 + offset);
521526
aux_package[board_descr["auxiliary"]["magnetometer_channels"][2].get<int> ()] =
522-
magnetometer_scale * cast_16bit_to_int32 (b + 112 + offset);
527+
magnetometer_scale_z *
528+
(double)cast_15bit_to_int32_swap_order (b + 112 + offset);
523529

524530
push_package (aux_package, (int)BrainFlowPresets::AUXILIARY_PRESET);
525531
}

src/utils/inc/custom_cast.h

+36
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ inline int32_t cast_16bit_to_int32_swap_order (unsigned char *byte_array)
4646
return cast_16bit_to_int32 (swapped);
4747
}
4848

49+
inline int32_t cast_13bit_to_int32 (unsigned char *byte_array)
50+
{
51+
int prefix = 0;
52+
if (byte_array[0] > 0x0F)
53+
{
54+
prefix = 0xFFFFE000;
55+
}
56+
return prefix | (byte_array[0] << 8) | byte_array[1];
57+
}
58+
59+
inline int32_t cast_13bit_to_int32_swap_order (unsigned char *byte_array)
60+
{
61+
unsigned char swapped[2];
62+
swapped[0] = byte_array[1];
63+
swapped[1] = byte_array[0];
64+
return cast_13bit_to_int32 (swapped);
65+
}
66+
67+
inline int32_t cast_15bit_to_int32 (unsigned char *byte_array)
68+
{
69+
int prefix = 0;
70+
if (byte_array[0] > 0x3F)
71+
{
72+
prefix = 0xFFFF8000;
73+
}
74+
return prefix | (byte_array[0] << 8) | byte_array[1];
75+
}
76+
77+
inline int32_t cast_15bit_to_int32_swap_order (unsigned char *byte_array)
78+
{
79+
unsigned char swapped[2];
80+
swapped[0] = byte_array[1];
81+
swapped[1] = byte_array[0];
82+
return cast_15bit_to_int32 (swapped);
83+
}
84+
4985
inline void uchar_to_bits (unsigned char c, unsigned char *bits)
5086
{
5187
for (int i = sizeof (unsigned char) * 8; i; c >>= 1)

0 commit comments

Comments
 (0)