The problem i am facing is to try and calculate the x and y position of a robot with dead reckoning.
Reading from the encoders and getting proper rotations of the wheels of my robot works. The robot has 3 wheels, where 2 of the 3 can be controlled like in this picture:
Calculating the distance from these wheel rotations and from those distances the heading of the robot is tested and works properly. Here is the code for the heading:
void MotorHandler::UpdateHeading(Position& currentPosition, double leftMotorDistance, double rightMotorDistance)
{
currentPosition.heading = (double)((int)(((rightMotorDistance - leftMotorDistance) / (2 * PI * AXLE_LENGTH)) * FULL_ROTATION_DEGREES) % 360);
}
Where axle_length is the length between both wheels and full_rotation_degree is 360.
But after this, when i try to calculate the x and y of the robot at any given moment i get weird results. Here is the code for calculating the x and y:
void MotorHandler::CalculateCurrentPosition(Position& currentPosition)
{
double leftMotorDistance = (motors[0]->GetRotation()/FULL_ROTATION_PULSES) * WHEEL_CIRCUMFERENCE;
double rightMotorDistance = (motors[1]->GetRotation()/FULL_ROTATION_PULSES) * WHEEL_CIRCUMFERENCE;
double headingToRadians = (currentPosition.heading * PI) / 180;
if (fabs(leftMotorDistance - rightMotorDistance) < 1.0e-6)
{
currentPosition.X = leftMotorDistance * cos(headingToRadians);
currentPosition.Y = rightMotorDistance * sin(headingToRadians);
}
else
{
double wd = (rightMotorDistance - leftMotorDistance) / AXLE_LENGTH;
double expr = (AXLE_LENGTH * (rightMotorDistance + leftMotorDistance)) / (2 * (rightMotorDistance - leftMotorDistance));
currentPosition.X = expr * sin(wd + headingToRadians) - expr * sin(headingToRadians);
currentPosition.Y = -expr * cos(wd + headingToRadians) + expr * cos(headingToRadians);
UpdateHeading(currentPosition, leftMotorDistance, rightMotorDistance);
}
}
Where the starting x, y and heading are 0. As said, calculating the heading of the robot works properly, which means that the distances of the wheels also are calculated correctly and works properly.
Links used:
https://www.cs.princeton.edu/courses/archive/fall11/cos495/COS495-Lecture5-Odometry.pdf
http://rossum.sourceforge.net/papers/DiffSteer/
Calculate position of differential drive robot
Ty!