Skip to main content
Visitor II
December 2, 2021
Solved

Good night. I have a LIS2DH12TR over an ATSAML21E17B. I´m reading the values of axis (x,y,x) transforming the values to degres (from -90º to 90º) but I need degrees from 0 to 360. I have tested a lot of formulas but nothing. Any idea? Ty.

  • December 2, 2021
  • 3 replies
  • 1032 views

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.

    This topic has been closed for replies.
    Best answer by DSull.3

    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

    3 replies

    DSull.3Answer
    Visitor II
    December 3, 2021

    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

    JaAuthor
    Visitor II
    December 3, 2021

    Hi DSull, thank you for your fast answer.

    I have read the answers in "stackoverflow" and try them, 3 points:

    • If I rotate over x axis, the values for this axis are corrects. Ok.
    • If I rotate over y axis, the values for this axis are corrects. Ok.
    • When I´m rotating over x axis only, I expect that the values for y axis have the same value allways because I´m rotating over x only but when z changes from positive to negative, the value of y axis changes from 0 to 180. The same happen with the other axis. Fail.

    Any ideas?

    Graduate II
    December 3, 2021

    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.

    https://keisan.casio.com/exec/system/1359533867