Simple DC motor control commands for driving DC motor conroller with PWM and up to 2 direction signals (complementary). Takes float value from -1.0 to 1.0.
Dependents: mbed_ES410_simpleSpeedMeasurement mbed_ES20X_V21_Tester
This MotCon motor driver class can be used with a variety of motor driver integrated circuits for driving PM DC motors. The MotCon class is overloaded to accommodate either one or two direction pins and a PwmOut pin for speed control/motor enable .
include the mbed library with this snippet
#include "MotCon.h" //uses the MotCon.h library for controlling the motor ports //PC serial connection Serial pc(USBTX, USBRX); //tx, rx via USB connection DigitalOut led(LED1); MotCon m1(p25, p27); //uses p25 for PWM and p27 for direction MotCon m2(p26, p29, p30); //uses p26 for pwm and p29 and 30 for direction (complimentary) //------------ Main ------------------------------ int main() { pc.baud(921600);//fast baud rate for USB PC connection while(1) { //iterate through 2*pi cycles in .01 increments for(float cycle=0;cycle<3.14159*2.0;cycle+=.01){ float m1_dc = .85*sin(cycle); m1.mot_control(m1_dc); float m2_dc = .85*cos(cycle); m2.mot_control(m2_dc); pc.printf("cycle=%.3f m1_dc = %.2f m1_dc = %.2f\r\n", cycle, m1_dc, m2_dc); wait(.01); //determines period led = !led; //toggle LED1 to indicate activity } } }
MotCon.cpp@0:3ba12980833b, 2015-08-31 (annotated)
- Committer:
- jebradshaw
- Date:
- Mon Aug 31 17:13:55 2015 +0000
- Revision:
- 0:3ba12980833b
- Child:
- 1:382121501487
simple motor control command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jebradshaw | 0:3ba12980833b | 1 | #include "MotCon.h" |
jebradshaw | 0:3ba12980833b | 2 | |
jebradshaw | 0:3ba12980833b | 3 | //Constructor |
jebradshaw | 0:3ba12980833b | 4 | MotCon::MotCon(PinName pwm, PinName dir) : _pwm(pwm), _dir(dir) { |
jebradshaw | 0:3ba12980833b | 5 | _pwm.period_us(50); |
jebradshaw | 0:3ba12980833b | 6 | _pwm = 0.0; |
jebradshaw | 0:3ba12980833b | 7 | _dir = 0; |
jebradshaw | 0:3ba12980833b | 8 | } |
jebradshaw | 0:3ba12980833b | 9 | |
jebradshaw | 0:3ba12980833b | 10 | // dc is signed duty cycle (+/-1.0) |
jebradshaw | 0:3ba12980833b | 11 | void MotCon::mot_control(float dc){ |
jebradshaw | 0:3ba12980833b | 12 | if(dc>1.0) |
jebradshaw | 0:3ba12980833b | 13 | dc=1.0; |
jebradshaw | 0:3ba12980833b | 14 | if(dc<-1.0) |
jebradshaw | 0:3ba12980833b | 15 | dc=-1.0; |
jebradshaw | 0:3ba12980833b | 16 | |
jebradshaw | 0:3ba12980833b | 17 | if(dc > 0.001){ |
jebradshaw | 0:3ba12980833b | 18 | _dir = 0; |
jebradshaw | 0:3ba12980833b | 19 | _pwm = dc; |
jebradshaw | 0:3ba12980833b | 20 | } |
jebradshaw | 0:3ba12980833b | 21 | else if(dc < -0.001){ |
jebradshaw | 0:3ba12980833b | 22 | _dir = 1; |
jebradshaw | 0:3ba12980833b | 23 | _pwm = abs(dc); |
jebradshaw | 0:3ba12980833b | 24 | } |
jebradshaw | 0:3ba12980833b | 25 | else{ |
jebradshaw | 0:3ba12980833b | 26 | _dir = 0; |
jebradshaw | 0:3ba12980833b | 27 | _pwm = 0.0; |
jebradshaw | 0:3ba12980833b | 28 | } |
jebradshaw | 0:3ba12980833b | 29 | } |