Go Down

Topic: Creating non-Blocking motor control with Encoders (Read 438 times) previous topic - next topic


I am attempting to create a library that will help to control motors (continuous rotation servos) in conjunction with encoders to control distance.  Lets say I call this "magicMotor()"  I want to pass this the following
Code: [Select]
magicMotor(motorPortNumber, motorPowerLevel, distance, encoderPortNumber(s))

For those of you that have used Botball equipment I'm attempting to implement the "move relative position" or "mrp" command.

I can create this function but it would be blocking.  Ideally I would want to be able to turn on multiple motors at a time.  The following code would be used to turn on 2 motors to drive straight.

Code: [Select]
magicMotor(10, 255, 3000, 0);   // Turn on motor port 10 full power, for 3000 tick from encoder on interrupt port 0
magicMotor(11, 255, 3000, 2);    // Turn on motor port 11 full power, for 3000 tick from encoder on interrupt port 2

Any ideas?  Is this even possible without multi-threading?


Is this even possible without multi-threading?

Of course. It's similar to controlling stepper motors asynchronously, but rather simpler. You need to provide some code which runs when the encoder inputs change, which keeps track of the output position and controls the motor to stop at the required position. You can either poll the inputs (for example by defining a maintenance method in your class which the user is required to call regularly) or by attaching an interrupt handler to the encoder input pins. If you use interrupts, note that the handler needs to be a function not a method, so you would need to provide a global interrupt handler that then used global static data to locate the motor instances and notify them of the interrupt.

Go Up