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

motorPWM.h

Controls DC motors using pulse width modulation (PWM).
This file is a 'catch all' in case your specific motor driver is not listed in DC Motors
This module assumes that your motor driver has three inputs which can be used to achieve the following states:-
MotorPWMtable.png
Two Pin Operation
If you are short of I/O pins then you can drive each motor with only two I/O pins but you need to add some additional hardware (which I call a 'tri-state switch').
tristate.png
This circuit will take the output of the two pins from your processor and convert it into the three control pins required for the optimum control of your motor driver. The hardware PWM pin is forwarded on to the enable pin of the driver and is used to set the speed of rotation by alternating between 'full speed' and 'coast'. The direction pin is forwarded on to one of the inputs to the driver and is also inverted by the transistor to for the second input to the driver.
By making the direction pin into a high impedance input pin then the resistors will guarantee that both of the inputs to the driver have the same value which will result in the brake being applied.
If you want to make your own motor controller board that can be used with this hardware then read http://www.societyofrobots.com/member_tutorials/node/159 for a range of DC and stepper motor boards based on the L293D, L298 or SN754410 which can be driven in this way.
Since we are using hardware PWM then the pulses sent to the motors are very exact. This library can cope with any number of motors and so the only real limitation is the number of PWM channels that your microcontroller provides.
If you attempt to use pins that are not valid: say because they are not PWM pins, or the timer doesn't provide hardware PWM then error codes will be set on the status LED.
It's very easy to use: you just give it a list of motors and that's it.
Note that you cannot connect a DC Motor directly to the microprocessor board - as the current requirements are too big and you will fry the chip. So the outputs from the board will need to be fed into a motor controller board.
Here is an example of the code in your application:-
#include "motorPWM.h"
// Define two motors
MOTOR left = MAKE_MOTOR(FALSE, B1,B4);
MOTOR right = MAKE_MOTOR(TRUE , B2,B5);
// Create the list - remember to place an & at the
// start of each motor name
MOTOR_LIST PROGMEM motors[] = {&left,&right};
// Create a driver for the list of motors
MOTOR_DRIVER bank1 = MAKE_MOTOR_DRIVER(motors);
// In my initialising code - pass the list of motors to control
void appInitHardware(void){
    // Initialise the motor controller to use 5kHz PWM
    motorPWMInit(&bank1, 5000);
}
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    // Give each servo a start value - make them go full ahead
    act_setSpeed(&left,DRIVE_SPEED_MAX);
    act_setSpeed(&right,DRIVE_SPEED_MAX);
    return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
    .. nothing to do. But in reality I would use act_speed to change the speed of each motor ...
    return 0;
}
Three Pin Operation
If you don't want to add the additional circuitry then you can still use this module but you will need to use 3 I/O pins rather than two.
The only difference to the code above is to replace the MAKE_MOTOR calls with MAKE_MOTOR_3_PIN instead:-
MOTOR left = MAKE_MOTOR_3_PIN(FALSE, B1,B4,B3);
MOTOR right = MAKE_MOTOR_3_PIN(TRUE , B2,B5,B6);

 

Function

 


motorPWMInit(MOTOR_DRIVER* driver, uint32_t freq)

Tells the motor driver what motors it controls and the approximate PWM frequency to use in Hz.
The PWM frequency is typically 10,000 or 5,000 Hz but you may want to experiment. Higher values reduce motor hum but can be less efficient.

MAKE_MOTOR(inverted, motorPin, directionPin)

Creates a DC motor.
The inverted parameter specifies whether the direction of the motor should be reversed: TRUE or FALSE.
The second parameter specifies an IOPin that can produce hardware PWM.
The final parameter specifies any IOPin that is used to specify the rotation direction.

MAKE_MOTOR_3_PIN(inverted, pwmPin, input1,input2)

Creates a DC motor.
The inverted parameter specifies whether the direction of the motor should be reversed: TRUE or FALSE.
The second parameter specifies an IOPin that can produce hardware PWM.
The final two parameters specify the IOPins that are used to control both sides of the H-Bridge to control the rotation direction.

Valid XHTML 1.0 Transitional