Using Encoders

So my team(781) wants to use encoders because one of our motors spins forward while the other spins backward to move our robot forward. Snce the motors do not spin the same speed with the same amount of energy we must use encoders. However we have no experience with this. Can you help us program and initialize the encoder?

Thanks,
Team 781

Well, you don’t have to use encoders. If all you were concerned about was that they were spinning the same rate, just figure out how much faster the other motor can spin, then scale the input to the motor appropriately (using the assumption that the motor response AND the output from the motor controller is linear).

Of course, thats a really quick and dirty approximation, and makes a lot of assumptions. If you want them to be matched using encoders, then you probably want to look into using PID control for this problem. Do a search on the forums, there’s lots of stuff about that type of thing there.

Check out the Encoder class and associated documentation. You connect the A and B channels to digital inputs and supply the port numbers to the Encoder constructor. From there you can read the counts or rate.

You can also connect it to a PID object to control direction. For driving straight you can use something like the right-left encoder value. As one side gets ahead the value will swing either positive or negative and the PID class can apply the necessary correction to the turn rate.