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

Committer:
jebradshaw
Date:
2018-03-16
Revision:
11:7cea0061a144
Parent:
10:8e93b06e7ff2

File content as of revision 11:7cea0061a144:

// J. Bradshaw - Motor Control Libary for support of most H-Bridge drivers with
//  a single PWM (enable active high) pin and one or two direction pins
//  to support forward/reverse (complimentary)
#include "mbed.h"
#include "MotCon.h"

//Constructor
MotCon::MotCon(PinName pwm_pin, PinName dir1_pin) : _pwm_pin(pwm_pin), _dir1_pin(dir1_pin), _dir2_pin(NC) {
    _dir2 = false;
    _pwm_pin.period_us(50);
    _pwm_pin = 0.0;
    _dir1_pin = 0;
}
MotCon::MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin) : _pwm_pin(pwm_pin), _dir1_pin(dir1_pin), _dir2_pin(dir2_pin) {
    _dir2 = true;
    _pwm_pin.period_us(50);
    _pwm_pin = 0.0;
    _dir1_pin = 0;
    _dir2_pin = 0;
    
    mc_mode = 0;    //mode pin determines braking (1 = dynamic braking, 0 = free-wheeling)    
}
// dc is signed duty cycle (+/-1.0)
void MotCon::mot_control(float dc){    
    if(dc>1.0)
        dc=1.0;
    if(dc<-1.0)
        dc=-1.0;
            
    if(_dir2){            
        if(dc > 0.001){
            _dir1_pin = 0;
            _dir2_pin = 1;
            _pwm_pin = dc;
        }
        else if(dc < -0.001){
            _dir2_pin = 0;
            _dir1_pin = 1;        
            _pwm_pin = abs(dc);
        }
        else{
            if(mc_mode){
                _dir1_pin = 0;
                _dir2_pin = 0;
                _pwm_pin = 1.0;                
            }
            else{
                _dir1_pin = 0;
                _dir2_pin = 0;
                _pwm_pin = 0.0;
            }
        }         
    }
    else{            
        if(dc > 0.001){
            _dir1_pin = 0;
            _pwm_pin = dc;
        }
        else if(dc < -0.001){
            _dir1_pin = 1;
            _pwm_pin = abs(dc);
        }
        else{
            _dir1_pin = 0;
            _pwm_pin = 0.0;
        }             
    }
}

// dc is signed duty cycle (+/-1.0)
void MotCon::mot_control(float dc, int invert){
    if(dc>1.0)
        dc=1.0;
    if(dc<-1.0)
        dc=-1.0;
                
    if(_dir2){
        if(invert==0){
            if(dc > 0.001){
                _dir1_pin = 0;
                _dir2_pin = 1;
                _pwm_pin = dc;
            }
            else if(dc < -0.001){
                _dir2_pin = 0;
                _dir1_pin = 1;
                _pwm_pin = abs(dc);
            }
            else{
                if(mc_mode){
                    _dir1_pin = 0;
                    _dir2_pin = 0;
                    _pwm_pin = 1.0;                
                }
                else{
                    _dir1_pin = 0;
                    _dir2_pin = 0;
                    _pwm_pin = 0.0;
                }
            }
        }
        else{
            if(dc > 0.001){
                _dir2_pin = 0;
                _dir1_pin = 1;
                _pwm_pin = dc;
            }
            else if(dc < -0.001){
                _dir1_pin = 0;
                _dir2_pin = 1;
                _pwm_pin = abs(dc);
            }
            else{
                if(mc_mode){
                    _dir1_pin = 0;
                    _dir2_pin = 0;
                    _pwm_pin = 1.0;                
                }
                else{
                    _dir1_pin = 0;
                    _dir2_pin = 0;
                    _pwm_pin = 0.0;
                }
            }
        }
    }
    else{        
        if(invert==0){
            if(dc > 0.001){
                _dir1_pin = 0;
                _pwm_pin = dc;
            }
            else if(dc < -0.001){
                _dir1_pin = 1;
                _pwm_pin = abs(dc);
            }
            else{
                _dir1_pin = 0;
                _pwm_pin = 0.0;
            }
        }
        else{
            if(dc > 0.001){
                _dir1_pin = 1;
                _pwm_pin = dc;
            }
            else if(dc < -0.001){
                _dir1_pin = 0;
                _pwm_pin = abs(dc);
            }
            else{
                _dir1_pin = 0;
                _pwm_pin = 0.0;
            }
        }    
    }    
}

void MotCon::setMode(int mode){
    mc_mode = mode;
}

int MotCon::getMode(void){
    return mc_mode;
}

float MotCon::read(void){
    float ret = this->duty_cycle;

    return ret;
}

MotCon& MotCon::operator= (float value){
    mot_control(value);
    
    return *this;
}

MotCon& MotCon::operator= (MotCon& rhs) {
  // Underlying call is thread safe
  mot_control(rhs.read());
  
  return *this;
}
  
MotCon::operator float(){
// Underlying call is thread safe
   return this->read(); 
}