2021-12-02 03:10 PM
This is the configuration of accelerometer:
// Enable Block Data Update
lis2dh12_block_data_update_set(&dev_ctx, PROPERTY_DISABLE);
// Set Output Data Rate
lis2dh12_data_rate_set(&dev_ctx, LIS2DH12_ODR_100Hz);
// Set full scale
lis2dh12_full_scale_set(&dev_ctx, LIS2DH12_2g);
// Enable temperature sensor
lis2dh12_temperature_meas_set(&dev_ctx, LIS2DH12_TEMP_DISABLE);
// Set device in continuous mode
lis2dh12_operating_mode_set(&dev_ctx, LIS2DH12_HR_12bit);
// Enable and configure high-pass filter
lis2dh12_high_pass_mode_set(&dev_ctx, LIS2DH12_NORMAL_WITH_RST);
lis2dh12_high_pass_bandwidth_set(&dev_ctx, LIS2DH12_AGGRESSIVE);
lis2dh12_high_pass_on_outputs_set(&dev_ctx, 0);
lis2dh12_high_pass_int_conf_set(&dev_ctx, LIS2DH12_ON_INT1_GEN);
// Reset filter
uint8_t filterRef;
lis2dh12_filter_reference_get(&dev_ctx, &filterRef);
// Configure interrupts
lis2dh12_ctrl_reg6_t int_polarity = {0};
int_polarity.int_polarity = 0; // 0: active-high; 1: active-low
lis2dh12_pin_int2_config_set(&dev_ctx, &int_polarity);
I ´m using this formulas (i obtein degrees from -90 to 90):
result_x = 57.2957786 * (atan2( accel_values.x,sqrt(accel_values.y * accel_values.y + accel_values.z * accel_values.z)));
result_y = 57.2957786 * (atan2( accel_values.y,sqrt(accel_values.x * accel_values.x + accel_values.z * accel_values.z)));
Someone could help me or say what is the way to transform the values to degrees 360º?
Thank you.
Solved! Go to Solution.
2021-12-03 02:48 AM
Good morning, that's correct since the atan2 function returns values from -90deg to 90deg.
Here from Stackoverflow:
A simple solution that catches all cases.
degrees = (degrees + 360) % 360; // +360 for implementations where mod returns negative numbers
Explanation
Positive: 1 to 180
If you mod any positive number between 1 and 180 by 360, you will get the exact same number you put in. Mod here just ensures these positive numbers are returned as the same value.
Negative: -180 to -1
Using mod here will return values in the range of 180 and 359 degrees.
Special cases: 0 and 360
Using mod means that 0 is returned, making this a safe 0-359 degrees solution.
https://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
2021-12-03 02:48 AM
Good morning, that's correct since the atan2 function returns values from -90deg to 90deg.
Here from Stackoverflow:
A simple solution that catches all cases.
degrees = (degrees + 360) % 360; // +360 for implementations where mod returns negative numbers
Explanation
Positive: 1 to 180
If you mod any positive number between 1 and 180 by 360, you will get the exact same number you put in. Mod here just ensures these positive numbers are returned as the same value.
Negative: -180 to -1
Using mod here will return values in the range of 180 and 359 degrees.
Special cases: 0 and 360
Using mod means that 0 is returned, making this a safe 0-359 degrees solution.
https://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
2021-12-03 03:48 AM
Hi DSull, thank you for your fast answer.
I have read the answers in "stackoverflow" and try them, 3 points:
Any ideas?
2021-12-03 06:15 AM
I understand you have a problem with coordinate systems. You want to convert your sensor data in cartesian coordinate system to spherical coordinate system. If the coordinate system on the site below matches what you have in mind, it will not be a problem to write the C code.