top of page

PoluluMotor Control Library for Arduino

Well, this is my first blog post... and I am not a really the writer type, so bare with me! In this post, I shall describe in brief a library for motor interfacing with Arduino that I wrote recently.

Arduino has a very large community and there are several libraries available for motor interfacing as well as quadrature encoder interfacing. However, I decided to combine both of them together into a library with easily configurable interface.

The library is pretty easy to use and a documentation is available in the README.md available with the library. Here, I shall discuss some of the caveats and tricks that might simplify your life along with discussion about the example codes provided!

So let's jump into first example.

MotorWithoutFeedback.

We start by creating 2 instances of PoluluMotor class, named m1, m2. Further, we create two variables, one each to store next duty cycle to be applied to Motor 1 and next reference speed to be set for Motor 2.

Finally, in loop() we set the duty cycle and reference speed for respective motors.

PoluluMotor m1(M1_PWM, M1_IN1, M1_IN2);

PoluluMotor m2(M2_PWM, M2_IN1, M2_IN2);

double m1_duty = 0.0;

unsigned long int m2_speed = 0;

void loop(){

m1.setDuty(m1_duty);

m2.setRefSpeed(m2_speed);

m2.applyUpdate();

}

The example displays two methods for controlling the speed of motor. First is by using setDuty method and second by using setRefSpeed & applyUpdate methods. In first method, the duty cycle passed is directly applied to motor without any validation. However, in latter, the reference speed is first validated to be in permissible (as well as configurable) range then converted to proportional duty cycle and applied to motor. Furthermore, the applyUpdate function checks if the motor is enabled or not. If motor is not enabled, the PWM control signal is not generated at all.

In general, it would be recommended to use second method as it's safer and can easily be modified to using feedback as demonstrated in next example.

MotorWithFeedback:

In this example, we first interface both methods using reference-speed method. Next we simply add two lines to tell arduino to listen to encoder pulses. The encoder counts are used to compute speed. The resolution of encoder is modified internally according to the configuration in which encoder is used -- single channel or both channels.

m1.attachEncoder(5);

m2.attachEncoder(6, 7);

Next, in loop() we make calls to getSpeed() function to get the speed of respective motors in RPM.

m1.getSpeed();

m2.getSpeed();

I'll make a short detour here for theoretically oriented readers. (Feel free to skip this part if you are not interested) There are two methods in which the speed of motor can be calculated from encoder; namely frequency-method and period-method. This library is based on PoluluWheelEncoders library, which in turn is based on frequency method. The error percentagae in frequency method increases with falling speed and consequently, this library may not provide accurate speed measurement at lower speeds.

That's the take away message... be careful while trusting the speed measurements at low speeds!

MotorPIDControl:

Well, there are no surprises here... To use PID controller for speed control, just add two more lines of code to set the desired gains and enable the PID controller.

m1.setGains(0.3, 0.1, 0.05);

m1.setPIDEngage(true);

Configurations:

One last comment to make -- you may customize the library for your motor, that is if you have encoder with CPR other that 48. Just modify the PoluluMotor.h file under title MAKE CONFIGURATION CHANGES HERE.

So that should be it!! I hope you enjoyed it..

If you have any doubts or you spotted some bug in code, do let me know...

Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page