test for accel

This commit is contained in:
Denis 2023-11-19 02:42:29 +01:00
parent a8ea6ad031
commit 783571ccdd
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
4 changed files with 19 additions and 9 deletions

View file

@ -188,10 +188,17 @@ dev_iio_t* dev_iio_create(const char* path) {
// =========================================== in_accel_scale ===============================================
{
const char* preferred_scale = LSB_PER_16G_STR;
char* const accel_scale = read_file(iio->path, "/in_accel_scale");
if (accel_scale != NULL) {
iio->accel_scale_x = iio->accel_scale_y = iio->accel_scale_z = strtod(accel_scale, NULL);
free((void*)accel_scale);
if (write_file(iio->path, "/in_anglvel_scale", preferred_scale, strlen(preferred_scale)) >= 0) {
iio->accel_scale_x = iio->accel_scale_y = iio->accel_scale_z = LSB_PER_16G;
} else {
fprintf(stderr, "Unable to set preferred in_accel_scale for device %s.\n", iio->name);
}
} else {
// TODO: what about if those are plit in in_accel_{x,y,z}_scale?
fprintf(stderr, "Unable to read in_accel_scale file from path %s%s.\n", iio->path, "/in_accel_scale");
@ -471,7 +478,7 @@ int dev_iio_read_imu(const dev_iio_t *const iio, imu_message_t *const out) {
const int tmp_read = fread((void*)&tmp[0], 1, sizeof(tmp), iio->accel_x_fd);
if (tmp_read >= 0) {
out->accel_x_raw = strtol(&tmp[0], NULL, 10);
accel_out[0] = (double)out->accel_x_raw * iio->accel_scale_x;
accel_in[0] = (double)out->accel_x_raw * iio->accel_scale_x;
} else {
fprintf(stderr, "While reading accel(x): %d\n", tmp_read);
return tmp_read;
@ -484,7 +491,7 @@ int dev_iio_read_imu(const dev_iio_t *const iio, imu_message_t *const out) {
const int tmp_read = fread((void*)&tmp[0], 1, sizeof(tmp), iio->accel_y_fd);
if (tmp_read >= 0) {
out->accel_y_raw = strtol(&tmp[0], NULL, 10);
accel_out[1] = (double)out->accel_y_raw * iio->accel_scale_y;
accel_in[1] = (double)out->accel_y_raw * iio->accel_scale_y;
} else {
fprintf(stderr, "While reading accel(y): %d\n", tmp_read);
return tmp_read;
@ -497,7 +504,7 @@ int dev_iio_read_imu(const dev_iio_t *const iio, imu_message_t *const out) {
const int tmp_read = fread((void*)&tmp[0], 1, sizeof(tmp), iio->accel_z_fd);
if (tmp_read >= 0) {
out->accel_z_raw = strtol(&tmp[0], NULL, 10);
accel_out[2] = (double)out->accel_z_raw * iio->accel_scale_z;
accel_in[2] = (double)out->accel_z_raw * iio->accel_scale_z;
} else {
fprintf(stderr, "While reading accel(z): %d\n", tmp_read);
return tmp_read;

View file

@ -740,8 +740,8 @@ static void handle_msg(output_dev_t *const out_dev, message_t *const msg) {
const int upd_beg_res = logic_begin_status_update(out_dev->logic);
if (upd_beg_res == 0) {
out_dev->logic->gamepad.last_motion_time = msg->data.imu.read_time;
memcpy(out_dev->logic->gamepad.gyro, msg->data.imu.gyro_rad_s, sizeof(double) * 3);
memcpy(out_dev->logic->gamepad.accel, msg->data.imu.accel_m2s, sizeof(double) * 3);
memcpy(out_dev->logic->gamepad.gyro, msg->data.imu.gyro_rad_s, sizeof(double[3]));
memcpy(out_dev->logic->gamepad.accel, msg->data.imu.accel_m2s, sizeof(double[3]));
logic_end_status_update(out_dev->logic);

View file

@ -27,4 +27,7 @@
#include <libevdev-1.0/libevdev/libevdev.h>
#define LSB_PER_RAD_S_2000_DEG_S ((double)0.001064724)
#define LSB_PER_RAD_S_2000_DEG_S_STR "0.001064724"
#define LSB_PER_RAD_S_2000_DEG_S_STR "0.001064724"
#define LSB_PER_16G ((double)0.004785)
#define LSB_PER_16G_STR "0.004785"

View file

@ -564,9 +564,9 @@ static int send_data(int fd, logic_t *const logic, uint8_t counter) {
const int16_t g_x = (gs.gyro[0]) / LSB_PER_RAD_S_2000_DEG_S;
const int16_t g_y = (gs.gyro[1]) / LSB_PER_RAD_S_2000_DEG_S;
const int16_t g_z = (gs.gyro[2]) / LSB_PER_RAD_S_2000_DEG_S;
const int16_t a_x = gs.accel[0] / (double)2.0; // TODO: IDK how to test...
const int16_t a_y = gs.accel[1] / (double)2.0; // TODO: IDK how to test...
const int16_t a_z = gs.accel[2] / (double)2.0; // TODO: IDK how to test...
const int16_t a_x = (gs.accel[0]) / LSB_PER_16G; // TODO: IDK how to test...
const int16_t a_y = (gs.accel[1]) / LSB_PER_16G; // TODO: IDK how to test...
const int16_t a_z = (gs.accel[2]) / LSB_PER_16G; // TODO: IDK how to test...
buf[0] = 0x01; // [00] report ID (0x01)
buf[1] = ((uint64_t)((int64_t)gs.joystick_positions[0][0] + (int64_t)32768) >> (uint64_t)8); // L stick, X axis