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 } } }
Diff: MotCon.h
- Revision:
- 5:3e07f69d8abd
- Parent:
- 2:10187d108666
- Child:
- 7:4d79b914ffdf
- Child:
- 8:67f2711fdeed
--- a/MotCon.h Mon May 23 19:06:31 2016 +0000 +++ b/MotCon.h Tue Oct 18 11:32:36 2016 +0000 @@ -1,4 +1,25 @@ - +/* mbed MotCon Library, for the various DC motor control IC's using PWM and one + * or two direction pins. + * Copyright (c) 2016, Joseph Bradshaw + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ #include "mbed.h" #ifndef MBED_MOTCON_H @@ -6,12 +27,46 @@ class MotCon{ public: - MotCon(PinName _pwm, PinName _dir); - void mot_control(float dc); - void mot_control(float dc, int invert); + /** Create a motor control port object connected to the specified pins + * + * @param pwm_pin PwmOut pin for the enable or speed control + * @param dir1_pin DigitalOut pin to control the motor direction + */ + MotCon(PinName pwm_pin, PinName dir1_pin); + + /** Create a motor control port object connected to the specified pins. + * The two direction pins are typically complementary and provide + * the capability of dynamic braking, free-wheeling, forward and reverse. + * + * @param pwm_pin PwmOut pin for the enable or speed control + * @param dir1_pin DigitalOut pin to control the motor direction + * @param dir2_pin DigitalOut pin to control the motor direction + */ + MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin); + + /** This function will set the direction and pwm percent scaled + * from 0.0 - 1.0 and control the direction pin or pins + * If two direction pins are used, the user can control the breaking + * mode by using the mutator functions for get_mode() and set_mode() + * If only one direction pin is used, dynamic breaking and syncronous + * rectification are assumed. See the specific motor driver part datasheet + * for additional details. + * + * @param dc is signed float duty cycle (+/-1.0) + */ + void mot_control(float dc); + void mot_control(float dc, int invert); + + void setMode(int mode); + bool getMode(void); + bool mc_mode; private: - PwmOut _pwm; - DigitalOut _dir; + bool _dir2; + +protected: + PwmOut _pwm_pin; + DigitalOut _dir1_pin; + DigitalOut _dir2_pin; }; #endif