Problems scaling millimeters to degrees in code

Hello, I am using C++ PROS and I am making a function that takes in millimeters and outputs degrees so that I can simply plug in a number of millimeters that I want the robot to run for and have it convert that to a unit that the computer can use. Currently, my code looks like this:

int num = mm * (360 /*d*/ / (82.55/*w*/ * 0.8 /*g*/));

mm = millimeters that the robot is to drive for

d = number of degrees in a rotation (360)

w = the diameter of our wheels (82.55)

g = The evaluation of our gear ratio, 48/60 (0.8)

When I ran the above segment, it would consistently move the robot about four times as far as I had told it to. I have mostly fixed this with the following:

return num / 4;

Even though the problem as been solved, I am really just curious what caused it. I have combed through my formula and I couldn’t see anything that caused this.

Excuse me if this is not the right place to post this.

1 Like

In your post you are moving a distance with a circle and only referencing the diameter without using pi.

If a wheel turns 360 degrees, it will move the robot 1 circumference which is the diameter times pi.

Let’s imagine a 35 mm diameter wheel. its circumference is 35 * pi meaning it travels 35 * pi every 360 degrees. Next, we factor in the drive ratio. If a motor on a 48 is driving a wheel on a 60 then the wheel will rotate 0.8 degrees for every 1 degree of rotation by the motor. So, we get 35 * 0.8 * pi. This is the distance per turn, which is 360 degrees, so we divide by 360 to get mm per degree.

Giving a final equation of (35 * 0.8 * pi) / 360
d = diameter, m = target distance, r = (motor gear teeth / wheel gear teeth)
The universal equation: (d * r * pi) / 360.

To get degrees per mm just go 1 / equation.
So, equation for degrees given millimeters would be: m / ((d * r * pi) / 360)

Also please don’t put the integrated comments to signify each variable as they make the code quite difficult to read. Especially in this scenario as the / used in comments is also the divide symbol in C++.

The best options would be comments on the line above of or inline after the code.

Hope this helps :slight_smile:

I just did the math with my chassis and it looks about right (207 mm per rotation), but I will test it next time I can get to my robot. Thank you very much!!