One of the most rewarding aspects about building robots is understanding and designing individuals components, getting them to work and finally putting everything together into a more complex machine.
One of the key design aspects I am trying to learn is component re-usability. I don’t know what the technical term for this is, but in essence it means that I need to build components that are generic and can be reused in different robots with little or no modifications needed. This concept is very similar to Classes in Object Oriented Programming, something that I am trying to learn as well.
So, to build a balancing robot, I first needed to develop a motor control mechanism that would balance the robot in an upright position using only two wheels. After a little experimenting, I became quite comfortable with the L293N motor driver IC, and made a short video on how this device can be used to control 2 DC motors. Now I needed to design the software that would control the motors. The motors themselves would just be rotating based on electrical impulses and would not know or care whether they were balancing a glass of milk or carting vegetables to market.
The specs I wanted for my software that would control a pair of DC motors mounted on opposite sides of a chassis:-
1. Move forward/ backward
2. Turn left and right on the spot
3. Do a gradual turn while moving forward/ backward
4. Accelerate/Decelerate to a given speed in a given time
Only 1 and 5 are needed for a balancing application, the rest add to the “generic” functionality of the class in motor control.
These have been implemented in a c++ class I wrote that will work for Arduino devices using the L293N or L293D motor driver ICs or any other similar setup that uses 2 input pins and 1 enable pin. Using this class, any number of motor pairs can be created and controlled. A total of 2 digital pins and 1 PWM pin are required for each motor. The digital pins are used to control direction of rotation and the PWM pin is used for speed control. I made this example circuit using Fritzing. Zoom in for a more detailed view. Then entire schematic can be downloaded here.
The member functions of my c++ class are:-
BOMotorPair(int, int, int, int ,int, int); BOMotorPair(); void SetMoveDirection(int); void SetMoveSpeed(int); void SetTurnSpeed(int, int); void SetWheelSpeedDifferential(int, int); void Move(); void Move(int); void Stop(); void Accelerate(int s1, int s2, int accel); void Accelerate(int finalSpeed, int delayRate);
Most of the member functions of this class are self-explanatory, the only thing that perhaps needs to be explained a little more are the arguments to the Constructor
These are :-
a and b – digital input pins for motor1
c – PWM pin for motor1
d and e – digital input pins for motor2
f – PWM pin for motor2
My BOMotorPair class can be downloaded from here. To use this, just extract the contents and copy the BOMotorPair folder into your /arduino/libraries folder. You can then include it into your Arduino sketches and control a pair of DC motors! If you like it and find it useful, please share it with your friends. Obviously there is still a lot of functionality that will be added to this class, and I will update it as soon as I make any changes.