WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

L293D or SN754410

Support for the L293D and plug-in replacements such as the SN754410
These devices have exactly the same pin-outs and so you can swap between them without any hardware/code changes. The only difference is that SN754410 can supply 1A versus 0.6A for the L293D. Both devices can supply double the current for a fraction of a second.
The SN754410 is a more modern device and so tends to be cheaper to buy. "More current and cheaper" = "No brainer"! If you decide to go with the L293D then make sure you buy one with the 'D' at the end as this has the internal diodes fitted meaning that you don't need to add any external components.
These devices can be used by adding:
#include <motorPWM.h>
to the start of your program.
There are two different ways to use each motor: 3 pin mode or 2 pin mode. Each has advantages and disadvantages. If you have lots of available IO pins then my recommendation would be to use '3 Pin Mode'.
If you want then you can use a mixture of both 3 pin and 2 pin modes.
3 Pin Mode
This requires one hardware PWM pin plus 2 general purpose digital output pins per motor. The benefit of this mode is that it can support all the possible motor drive states required by WebbotLib ie: forward, reverse, brake and coast. Variable speed is achieved by alternative between 'full speed' and 'coast'.
To create a 3 pin motor then use MAKE_MOTOR_3_PIN(inverted, pwmPin, input1,input2)
2 Pin Mode
This requires one hardware PWM pin plus 1 general purpose digital output pin. The drawback of this mode is that it doesn't support the 'coast' motor drive state. Consequently to achieve variable speed settings the motor alternates between 'full speed' and 'brake' via the PWM pin. This places the motor under some stress and will 'wear out' the motor more quickly than the 3 Pin Mode.
To create a 2 pin motor then use MAKE_MOTOR(inverted, motorPin, directionPin)
Either Mode
Once you have defined each motor then you need to combine them into a MOTOR_LIST. This list is then passed to a MOTOR_DRIVER, and finally the driver is initialised using 'motorL293Init' or 'motorSN754410Init'.
Ok so here is an example for the Axon. Lets assume we have 2 motors called 'motor1' and 'motor2' and we are going to use the 3 Pin Mode for both of them. Here is the top of your program:-
#include <motorPWM.h>
MOTOR motor1 = MAKE_MOTOR_3_PIN(false,H5,F0,F1);
MOTOR motor2 = MAKE_MOTOR_3_PIN(false,H4,F2,F3);
static MOTOR_LIST PROGMEM motors_list[] = {&motor1,&motor2};
MOTOR_DRIVER motors = MAKE_MOTOR_DRIVER(motors_list);
Then in appInitHardware we need to initialise the driver to use a PWM frequency of, say, 5kHz :-
motorL293Init(&motors, 5000);
All done. Now we can control motor1 and motor2 using the commands in actuators.h
For example - this appControl logic will alternate between rotating the motors backward and forward every 10 seconds:-
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
    TICK_COUNT ms = loopStart / 1000; // Get current time in ms
    int16_t now = ms % (TICK_COUNT)10000; // 10 sec for a full swing
    if(now >= (int16_t)5000){ // Goes from 0ms...5000ms
        now = (int16_t)10000 - now; // then 5000ms...0ms
    // 'now' has a value from 0 to 5000
    // Map it into DRIVE_SPEED range
    DRIVE_SPEED speed = interpolate(now, 0, 5000, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
    // Set speed for all motors/servos
    return 0;

Valid XHTML 1.0 Transitional