My teams are trying to get better at programming sensors and, we can’t figure out how to program the shaft encoder. Can someone give me a basic code letting me measure distance? We just need a basic understanding so we can make a reliable autonomous.
Well, the shaft encoder can return the degrees that it has rotated, using commands like the rotation or position commands. The formula for circumference is 2 * PI * radius of wheel. Multiplying the circumference of the wheel by rotation/360 would probably give you the distance the wheel has traveled that the shaft encoder is connected to.
Like distance in a single direction? If so, my answer still stands, you can use the circumference and rotation to measure the distance the wheel has gone.
Pseudo code:
while (distance traveled is less than target)
{
get rotation;
convert rotation distance to inches using circumference;
drive forward
}
stop driving
If you are talking about odometry, also known as position tracking, that is a whole other slice of a whole other pie.
You wouldn’t really want to use an if statement for this (at least in my opinion). If statements are swell for checking things, but not for waiting to do something.
If you look at post 5 (it was my post), I made a little pseudo code that will drive forward until the distance is traveled (it wasn’t that good, but it probably would have worked).
Copied from post 5:
while (distance traveled is less than target)
{
get rotation;
convert rotation distance to inches using circumference;
drive forward
}
stop driving
I’ll make it a lot closer to operable code but you’ll still need to create some variables, put in motor commands, etc.
Your formula confuses me. Multiplying the circumference by the rotations then dividing by 360 doesn’t make sense, it gives me an answer of less than one which, is not close to how far my wheel moves in one rotation. Can you clarify the formula and help me understand it?
We cover how to use encoders and create odometers in our Robot Autonomy and Control Workshop. The recording is not available yet, but the files are below and there are VEXCode programs showing how to do both. You can also go through the included PDF of the presentation for a brief explanation.
Do the motors work, do the ports work, do the motors spin when not powered, does other code spin the motors?
Or
Software issue
Sending a value that is too small so that the motors don’t spin, program ends immediately after telling the motors to move, running driver instead of auton.
These are some possibilities, but there are a much greater number than I have noted. It would greatly enhance the forum’s ability to help if you posted your code.