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.h@10:8e93b06e7ff2, 2018-03-16 (annotated)
- Committer:
- jebradshaw
- Date:
- Fri Mar 16 15:31:03 2018 +0000
- Revision:
- 10:8e93b06e7ff2
- Parent:
- 7:4d79b914ffdf
- Child:
- 11:7cea0061a144
Added shorthand read and write functions. May need to check implementation of overloaded function with invert used
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:8e93b06e7ff2 | 62 | float read(void); |
jebradshaw | 10:8e93b06e7ff2 | 63 | /** Shorthand for the write and read functions */ |
jebradshaw | 10:8e93b06e7ff2 | 64 | MotCon& operator= (float value); |
jebradshaw | 10:8e93b06e7ff2 | 65 | MotCon& operator= (MotCon& rhs); |
jebradshaw | 10:8e93b06e7ff2 | 66 | operator float(); |
jebradshaw | 10:8e93b06e7ff2 | 67 | |
jebradshaw | 10:8e93b06e7ff2 | 68 | float duty_cycle; |
jebradshaw | 10:8e93b06e7ff2 | 69 | |
jebradshaw | 5:3e07f69d8abd | 70 | bool mc_mode; |
jebradshaw | 0:3ba12980833b | 71 | private: |
jebradshaw | 5:3e07f69d8abd | 72 | bool _dir2; |
jebradshaw | 5:3e07f69d8abd | 73 | |
jebradshaw | 5:3e07f69d8abd | 74 | protected: |
jebradshaw | 5:3e07f69d8abd | 75 | PwmOut _pwm_pin; |
jebradshaw | 5:3e07f69d8abd | 76 | DigitalOut _dir1_pin; |
jebradshaw | 5:3e07f69d8abd | 77 | DigitalOut _dir2_pin; |
jebradshaw | 0:3ba12980833b | 78 | }; |
jebradshaw | 0:3ba12980833b | 79 | |
jebradshaw | 0:3ba12980833b | 80 | #endif |