Hello. An absolute encoder on a rotary axis gives values in range -2147483648 to 2147483648. When value is > 2147483648 , the encoder value is overflows to -2147483648. When value is <-2147483648, the encoder value overflows to 2147483648 I need to create a function in C that converts the encoder value into a modulo 360 rotary axis value. The initial encoder value is 0 and it changes in positive when encoder rotates in positive direction and it changes in negative when encoder rotates in negative direction. One rotation is equal to 3600 encoder value units.
For positive input encoderValue it is ok. But for negative input of
encoder value i do not have correct results
Can someone help?
Here is my code:
#include <stdio.h>
#include <inttypes.h>
int mapEncoderToRotaryAxis(int64_t encoderValue) {
// Define the number of encoder value units in one rotation
int encoderUnitsPerRotation = 3600;
// Calculate the mapped output value in modulo 360 degrees
int64_t deltaEncoder = encoderValue;
while (deltaEncoder < 0) {
deltaEncoder += encoderUnitsPerRotation;
}
int mappedValue = (int)(deltaEncoder % encoderUnitsPerRotation);
return mappedValue;
}
int main() {
int64_t encoderValue;
printf("Enter an encoder value between -2147483648 and 2147483648: ");
scanf("%" PRId64, &encoderValue);
int rotaryAxisValue = mapEncoderToRotaryAxis(encoderValue);
printf("Mapped value in modulo 360 rotary axis: %d degrees\n", rotaryAxisValue);
return 0;
}
For positive input encoderValue it is ok. But for negative input of
encoder value i do not have correct results
Can someone help?
Here is my code:
#include <stdio.h>
#include <inttypes.h>
int mapEncoderToRotaryAxis(int64_t encoderValue) {
// Define the number of encoder value units in one rotation
int encoderUnitsPerRotation = 3600;
// Calculate the mapped output value in modulo 360 degrees
int64_t deltaEncoder = encoderValue;
while (deltaEncoder < 0) {
deltaEncoder += encoderUnitsPerRotation;
}
int mappedValue = (int)(deltaEncoder % encoderUnitsPerRotation);
return mappedValue;
}
int main() {
int64_t encoderValue;
printf("Enter an encoder value between -2147483648 and 2147483648: ");
scanf("%" PRId64, &encoderValue);
int rotaryAxisValue = mapEncoderToRotaryAxis(encoderValue);
printf("Mapped value in modulo 360 rotary axis: %d degrees\n", rotaryAxisValue);
return 0;
}