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:
Wed Dec 14 12:49:53 2016 +0000
Revision:
7:4d79b914ffdf
Parent:
5:3e07f69d8abd
Child:
10:8e93b06e7ff2
Improved documentation, printf output and led toggle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 5:3e07f69d8abd 1 /* mbed MotCon Library, for the various DC motor control IC's using PWM and one
jebradshaw 5:3e07f69d8abd 2 * or two direction pins.
jebradshaw 5:3e07f69d8abd 3 * Copyright (c) 2016, Joseph Bradshaw
jebradshaw 5:3e07f69d8abd 4 *
jebradshaw 5:3e07f69d8abd 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
jebradshaw 5:3e07f69d8abd 6 * of this software and associated documentation files (the "Software"), to deal
jebradshaw 5:3e07f69d8abd 7 * in the Software without restriction, including without limitation the rights
jebradshaw 5:3e07f69d8abd 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jebradshaw 5:3e07f69d8abd 9 * copies of the Software, and to permit persons to whom the Software is
jebradshaw 5:3e07f69d8abd 10 * furnished to do so, subject to the following conditions:
jebradshaw 5:3e07f69d8abd 11 *
jebradshaw 5:3e07f69d8abd 12 * The above copyright notice and this permission notice shall be included in
jebradshaw 5:3e07f69d8abd 13 * all copies or substantial portions of the Software.
jebradshaw 5:3e07f69d8abd 14 *
jebradshaw 5:3e07f69d8abd 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jebradshaw 5:3e07f69d8abd 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jebradshaw 5:3e07f69d8abd 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jebradshaw 5:3e07f69d8abd 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jebradshaw 5:3e07f69d8abd 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jebradshaw 5:3e07f69d8abd 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jebradshaw 5:3e07f69d8abd 21 * THE SOFTWARE.
jebradshaw 5:3e07f69d8abd 22 */
jebradshaw 0:3ba12980833b 23 #include "mbed.h"
jebradshaw 0:3ba12980833b 24
jebradshaw 0:3ba12980833b 25 #ifndef MBED_MOTCON_H
jebradshaw 0:3ba12980833b 26 #define MBED_MOTCON_H
jebradshaw 0:3ba12980833b 27
jebradshaw 0:3ba12980833b 28 class MotCon{
jebradshaw 0:3ba12980833b 29 public:
jebradshaw 5:3e07f69d8abd 30 /** Create a motor control port object connected to the specified pins
jebradshaw 5:3e07f69d8abd 31 *
jebradshaw 5:3e07f69d8abd 32 * @param pwm_pin PwmOut pin for the enable or speed control
jebradshaw 5:3e07f69d8abd 33 * @param dir1_pin DigitalOut pin to control the motor direction
jebradshaw 5:3e07f69d8abd 34 */
jebradshaw 5:3e07f69d8abd 35 MotCon(PinName pwm_pin, PinName dir1_pin);
jebradshaw 5:3e07f69d8abd 36
jebradshaw 5:3e07f69d8abd 37 /** Create a motor control port object connected to the specified pins.
jebradshaw 5:3e07f69d8abd 38 * The two direction pins are typically complementary and provide
jebradshaw 5:3e07f69d8abd 39 * the capability of dynamic braking, free-wheeling, forward and reverse.
jebradshaw 5:3e07f69d8abd 40 *
jebradshaw 5:3e07f69d8abd 41 * @param pwm_pin PwmOut pin for the enable or speed control
jebradshaw 5:3e07f69d8abd 42 * @param dir1_pin DigitalOut pin to control the motor direction
jebradshaw 5:3e07f69d8abd 43 * @param dir2_pin DigitalOut pin to control the motor direction
jebradshaw 5:3e07f69d8abd 44 */
jebradshaw 5:3e07f69d8abd 45 MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin);
jebradshaw 5:3e07f69d8abd 46
jebradshaw 5:3e07f69d8abd 47 /** This function will set the direction and pwm percent scaled
jebradshaw 5:3e07f69d8abd 48 * from 0.0 - 1.0 and control the direction pin or pins
jebradshaw 5:3e07f69d8abd 49 * If two direction pins are used, the user can control the breaking
jebradshaw 5:3e07f69d8abd 50 * mode by using the mutator functions for get_mode() and set_mode()
jebradshaw 5:3e07f69d8abd 51 * If only one direction pin is used, dynamic breaking and syncronous
jebradshaw 5:3e07f69d8abd 52 * rectification are assumed. See the specific motor driver part datasheet
jebradshaw 5:3e07f69d8abd 53 * for additional details.
jebradshaw 5:3e07f69d8abd 54 *
jebradshaw 5:3e07f69d8abd 55 * @param dc is signed float duty cycle (+/-1.0)
jebradshaw 5:3e07f69d8abd 56 */
jebradshaw 5:3e07f69d8abd 57 void mot_control(float dc);
jebradshaw 7:4d79b914ffdf 58 void mot_control(float dc, int invert); //this function can be used to invert the motor direction
jebradshaw 5:3e07f69d8abd 59
jebradshaw 7:4d79b914ffdf 60 void setMode(int mode); //mode: 1 = dynamic braking, 0 = free-wheeling (default)
jebradshaw 7:4d79b914ffdf 61 int getMode(void);
jebradshaw 5:3e07f69d8abd 62 bool mc_mode;
jebradshaw 0:3ba12980833b 63 private:
jebradshaw 5:3e07f69d8abd 64 bool _dir2;
jebradshaw 5:3e07f69d8abd 65
jebradshaw 5:3e07f69d8abd 66 protected:
jebradshaw 5:3e07f69d8abd 67 PwmOut _pwm_pin;
jebradshaw 5:3e07f69d8abd 68 DigitalOut _dir1_pin;
jebradshaw 5:3e07f69d8abd 69 DigitalOut _dir2_pin;
jebradshaw 0:3ba12980833b 70 };
jebradshaw 0:3ba12980833b 71
jebradshaw 0:3ba12980833b 72 #endif