Base Turning Code

I’ve coded my base motors into individual motor groups, and I’ve been trying to make a turnForDegrees function to have the robot turn a certain amount of degrees to the left. At first, I tried using the “radius” of the rectangular base to find the distance needed to move on each side, but it is not very accurate. Does anyone have any experience that could help me with this?

A great tool to use for this kind of thing is the inertial sensor which can tell you a lot of information about your robot’s movement. For this scenario, a command that would be useful is inertialSensor.rotation() (use the name of your inertial sensor) which outputs the angle that the robot has turned since the rotation was last reset. Using this sensor, you can write your own functions to turn certain amounts.

Some things to keep in mind are the calibration time that comes with using the inertial sensor (simply using the command inertialSensor.calibrate() and waiting ~2 seconds at the start of your program will cover this) and resetting it – inertialSensor.resetRotation() – between each call if you want it to start from 0 degrees every time you tell it to turn. Mounting the sensor at the center of your robot’s rotation will also make this whole process easier.

4 Likes

I don’t have access to an inertial sensor, which is why I am trying to use the code itself.

Before my team got an inertial sensor, we did something like this. You can figure out how much to move the wheels by imagining they are on a circle centered on the center of your robot. This circle is the path that the wheels travel on when you turn. This means that to turn 360 degrees, your wheels need to travel the distance equal to the circumference of this circle, in opposite directions. You can then multiply the circumference by the amount you want to turn divided by 360 to get the amount the wheels need to turn to rotate the amount you want to. Just remember that the wheels need to go in opposite directions to rotate.

Here’s the whole equation:

Where:
wt: Track width (distance between wheels)
cw: Circumference of wheels
r: The amount you want to rotate
output: The number of rotations your wheels need to spin for

This should work fine, but if you have the chance to you should definitely get an inertial sensor. It will be a lot more accurate and will get messed up less.

2 Likes

Thanks! I will be sure to try this out. Before, I used to use the distance from the bottom left wheel to the top right, but I suppose I should use the distance between the middle wheels (on a 6 wheel base). Is this correct?

Yes, you want the distance between the middle wheels. If you want to be super specific, you should measure the distance between the center of the wheels, which should be a little more accurate.

1 Like