Motor

An interface for driving a standard DC motor with PWM and an H-Bridge

Hello World!

Import program

00001 // Sweep the motor speed from full-speed reverse (-1.0) to full speed forwards (1.0)
00002 
00003 #include "mbed.h"
00004 #include "Motor.h"
00005 
00006 Motor m(p23, p6, p5); // pwm, fwd, rev
00007 
00008 int main() {
00009     for (float s= -1.0; s < 1.0 ; s += 0.01) {
00010        m.speed(s); 
00011        wait(0.02);
00012     }
00013 }

Library

Import library

Public Member Functions

Motor (PinName pwm, PinName fwd, PinName rev)
Create a motor control interface.
void speed (float speed)
Set the speed of the motor.

Example

This example show you how to wire up the L293D dual H bridge driver. Data sheet can be found at:

/media/uploads/chris/_scaled_l293d.png

Some minor additions to the libary above resulted in this libary. It has been tested on L298 motor drivers, but uses the same method of speed control.

Import library

Public Member Functions

Motor (PinName pwm, PinName fwd, PinName rev, int brakeable)
Create a motor control interface.
float speed (float speed)
Set the speed of the motor.
void coast (void)
Set the the motor to coast.
float stop (float duty)
Set the motor to dynamicaly brake.
float state (void)
return the current state of the motor

In terms of use,

the speed function takes a value from 1 to -1. full foward to full reverse. however it will not switch from foward to reverse or reverse to foward without going through speed = 0, coast or stop. this stops the motor from going from full one direction to the other, and the large current draw associated with this. it returns the duty on the PWM line.

the coast function means the motor driver switches off, and does nothing.

the stop function dynamicaly brakes, which is supported on L298's but if dynamic braking develops more current than the motor drivers are rated for, things start to fail. read data sheet, and if in doubt, don't. it returns the duty on the PWM line, or -1 if you didn't set brakeable to 1.

main.cpp

// test code, this demonstrates working motor drivers. 
// full reverse to full stop, dynamicaly brake and switch off.
#include motordriver.h
Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can brake 
Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake
int main() {
    for (float s= -1.0; s < 1.0 ; s += 0.01) {
        A.speed(s); 
        B.speed(s); 
        wait(0.02);
    }
    A.stop();
    B.stop();
    wait(1);
    A.coast();
    B.coast();
}

data sheet for L298

conection diagram, note the current sense pins(1,15) should be connected to ground through a 0.5 Ohm resistor. attaching to analog inputs is not required.

/media/uploads/littlexc/_scaled_l298.png


All wikipages