You are viewing an older revision! See the latest version
Motor
An interface for driving a standard DC motor with PWM and an H-Bridge
Hello World!¶
main.cpp
// Sweep speed from full-speed reverse (-1.0) to forwards (1.0)
#include "mbed.h"
#include "Motor.h"
Motor m(p23, p6, p5); // pwm, fwd, rev
int main() {
for (float s= -1.0; s < 1.0 ; s += 0.01) {
m.speed(s);
wait(0.02);
}
}
Library¶
Example¶
This example show you how to wire up the L293D dual H bridge driver. Data sheet can be found at:

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 diagrams to follow when Ive worked out how to upload them, poking paper at the ethernet port didn't work. it connects to a L293 in the same way.