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
        }
    }
}
Committer:
jebradshaw
Date:
Thu May 19 13:17:10 2016 +0000
Revision:
4:23cd902e1774
Parent:
3:69e79f1db999
updated API documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:3ba12980833b 1
jebradshaw 0:3ba12980833b 2 #include "mbed.h"
jebradshaw 0:3ba12980833b 3
jebradshaw 0:3ba12980833b 4 #ifndef MBED_MOTCON_H
jebradshaw 0:3ba12980833b 5 #define MBED_MOTCON_H
jebradshaw 0:3ba12980833b 6
jebradshaw 4:23cd902e1774 7 /** MotCon class.
jebradshaw 4:23cd902e1774 8 * Used for generating motor control pulse width modulation control
jebradshaw 4:23cd902e1774 9 * and direction signal outputs for motor driver boards
jebradshaw 4:23cd902e1774 10 */
jebradshaw 0:3ba12980833b 11 class MotCon{
jebradshaw 0:3ba12980833b 12 public:
jebradshaw 4:23cd902e1774 13 /** Create MotCon instance */
jebradshaw 0:3ba12980833b 14 MotCon(PinName _pwm, PinName _dir);
jebradshaw 4:23cd902e1774 15
jebradshaw 4:23cd902e1774 16 /** Drive motor control signals
jebradshaw 4:23cd902e1774 17 *
jebradshaw 4:23cd902e1774 18 * @param dc value from (+/-1.0)
jebradshaw 4:23cd902e1774 19 */
jebradshaw 0:3ba12980833b 20 void mot_control(float dc);
jebradshaw 4:23cd902e1774 21 /** Overloaded function for Drive motor control signals
jebradshaw 4:23cd902e1774 22 *
jebradshaw 4:23cd902e1774 23 * @param dc value from (+/-1.0)
jebradshaw 4:23cd902e1774 24 * @param invert any non-zero value inverts the motor direction signal output logic
jebradshaw 4:23cd902e1774 25 */
jebradshaw 3:69e79f1db999 26 void mot_control(float dc, int invert);
jebradshaw 0:3ba12980833b 27 private:
jebradshaw 0:3ba12980833b 28 PwmOut _pwm;
jebradshaw 0:3ba12980833b 29 DigitalOut _dir;
jebradshaw 0:3ba12980833b 30 };
jebradshaw 0:3ba12980833b 31
jebradshaw 0:3ba12980833b 32 #endif