RobotC and Quad Encoders

Could anyone using quad encoders and RobotC please enlighten me about programming, reading and using them? Our efforts have only resulted in 1’s and 0’s that do not reliably change… Hoping someone has had success and can point us in the correct direction.

Kevin Reid
Team 2609

Thats Quadrature encoders…

We use Grayhill 61K128 quadrature encoders, for wheels and anything else that needs position or speed information. Quad encoders provide direction of rotation, whereas the geartooth sensors in the KOP do not.

The encoders have two output signal wires (plus ground and 5 volts) which are connected to two Digital Inputs on the Robot Controller. EasyC has a nice Quad Encoder function, you just tell it which digital inputs you are using and it counts the pulses for you - use the same function to read the current count value, which increases with forward motion of the encoder, decreases with reverse motion.

If you want to calculate speed using encoders, your software loop should run at a fixed frequency - say once every 50 msec (or - the standard 18.2 msec, if you prefer).

Encoder count = distance
Distance MINUS previous Distance = speed (ticks)
Speed MINUS previous Speed = acceleration (ticks)

If you want your motors to run at a particular speed, adjust the PWM output up or down depending on whether the Speed is too slow or too fast. Often a PID Control Loop is used for this http://www.simbotics.org/files/first-robotics-canada/workshop-presentations/programming-pid.pdf.

Problems arise if your encoder fails or one of the Digital Input wires fall off … your control loop sees the encoder count NOT CHANGING and assumes the motor is not moving … when in actuality your robot wheel is turning at max speed and the 'bot is spinning in a circle :slight_smile:

If you want your motor to move an arm to a particular position, use the encoder count and a home limit switch connected to a Digital Input. Rotate your mechanism backward using PWM value until the home limit switch closes, then reset the Encoder COUNT to zero. Now you can rotate the mechanism forward and stop the motor when the Encoder COUNT reaches a particular value.

Sorry Kevin,

Can’t be of any help there with RobotC, but if you’re interested in getting all your code over to MPLAB. Give me a call or e-mail, and we’ll get you cooking with gas.

Distance MINUS previous Distance = speed (ticks)
Speed MINUS previous Speed = acceleration (ticks)

those should both be divided by time since the previous loop

(Distance - previous Distance)/time = speed (ticks)
(Speed - previous Speed)/time = acceleration (ticks)

Yes, we do that. Our loop is constrained to run at a fixed rate, 50 msec intervals. So the speeds are ticks-per-50msec-interval.

I would also be willing to help port your code to easyC, you can use the syntax editor instead of blocks if you wish.