Access IMU data

Hi,

I am trying to save the drone data in a file. Using the available Hello example which deals partly with telemetry I can access most of the data.
For example To get the GPS latitude in a variable. The following code allows to do it:

struct tlm_consumer *consumer;
consumer = tlm_consumer_new();
double val;
struct timespec timestamp;

int res = tlm_consumer_reg(consumer,
					  &val,
					  "sensors_gps.location.latitude",
					  TLM_TYPE_FLOAT64,
					  sizeof(double),
					  1,
					  &timestamp);

tlm_consumer_get_sample(consumer, NULL, TLM_LATEST);

On the other hand the IMU data are provided in an array of 10. If I proceed in the same way and that I understand the function well this code should work but it is not the case:

struct tlm_consumer *consumer;
consumer = tlm_consumer_new();
float val[10];
struct timespec timestamp;

int res = tlm_consumer_reg(consumer,
					  val,
					  "sensors_imu.gyro[10].x",
					  TLM_TYPE_FLOAT32,
					  sizeof(float),
					  10,
					  &timestamp);

tlm_consumer_get_sample(consumer, NULL, TLM_LATEST);

Note that I also tried using tlm_consumer_reg_struct_ptr and TLM_REG_FIELD_SCALAR_ARRAY_EX just like in the example but it doesn’t work either.

Do you have any idea how to do it?

Thibaut

Hi @thgral,

To get “gyro” data you need to use tlm_consumer_reg_struct_ptr. First, define your struct :

// IMU GYRO
static const struct tlm_reg_field sensors_tlm_imu_vec3_fields[] = {
	TLM_REG_FIELD_SCALAR(union sensor_vector, x, TLM_TYPE_FLOAT32),
};

static const struct tlm_reg_struct sensors_tlm_imu_gyro_reg_struct =
	TLM_REG_STRUCT("gyro", sensors_tlm_imu_vec3_fields);

static const struct tlm_reg_field sensors_tlm_imu_fields[] = {
	TLM_REG_FIELD_STRUCT_ARRAY(struct sensor_imu_data,
				   gyro,
				   &sensors_tlm_imu_gyro_reg_struct),
};

static const struct tlm_reg_struct sensors_tlm_reg_imu =
	TLM_REG_STRUCT(NULL, sensors_tlm_imu_fields);

and you can use these structs to get “sensors_imu” data (to get gyro[0…9].x → 10 samples) :

union sensor_vector {
	struct {
		float x;
	};
};

struct sensor_imu_data {
	/* Gyroscope data in SI unit (rad/s) */
	union sensor_vector gyro[10];
};

// in main
struct sensor_imu_data imu;

// ... init consumer ...
tlm_consumer_reg_struct_ptr(consumer,
			&imu,
			"sensors_imu",
			&sensors_tlm_reg_imu);

//... get_samples...

Axel

1 Like

Thank you very much @Axelm.
I also discovered that we can directly access a single imu data without reading the whole table. To do so, we just have to indicate in the name of the variable the value we want. For example if we want to get only the first gyro value :

int res = tlm_consumer_reg(consumer,
					  &variable,
					  "sensors_imu.sensors_imu[0].x",
					  TLM_TYPE_FLOAT32,
					  sizeof(double),
					  1,
					  &timestamp);

Yes, exactly. You can also register an array of values:

	const struct tlm_consumer_reg_entry sensors_array[] = {
         {.ptr = &var1,
		 .name = "sensors_imu.gyro[0].x",
		 .type = TLM_TYPE_FLOAT32,
		 .size = sizeof(float),
		 .count = 1,
		 .timestamp = &timestamp},
         {.ptr = &var2,
		 .name = "sensors_imu.gyro[1].x",
		 .type = TLM_TYPE_FLOAT32,
		 .size = sizeof(float),
		 .count = 1,
		 .timestamp = &timestamp},
         {.ptr = &var3,
		 .name = "sensors_imu.gyro[2].x",
		 .type = TLM_TYPE_FLOAT32,
		 .size = sizeof(float),
		 .count = 1,
		 .timestamp = &timestamp},
         { ...
              };
	};

	tlm_consumer_reg_array(consumer,
				     sensors_array,
				     ARRAY_SIZE(sensors_array));
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.