Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed_ES410_simpleSpeedMeasurement mbed_ES20X_V21_Tester
MotCon.h@11:7cea0061a144, 2018-03-16 (annotated)
- Committer:
- jebradshaw
- Date:
- Fri Mar 16 15:34:29 2018 +0000
- Revision:
- 11:7cea0061a144
- Parent:
- 10:8e93b06e7ff2
Added shorthand operator read and write functions. May need to check overloaded function using invert
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 | 11:7cea0061a144 | 4 | * 20180316 - Added shorthand read and write operator functions. |
| jebradshaw | 11:7cea0061a144 | 5 | * May need to check implementation of overloaded class write function |
| jebradshaw | 11:7cea0061a144 | 6 | * using invert |
| jebradshaw | 5:3e07f69d8abd | 7 | * |
| jebradshaw | 5:3e07f69d8abd | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| jebradshaw | 5:3e07f69d8abd | 9 | * of this software and associated documentation files (the "Software"), to deal |
| jebradshaw | 5:3e07f69d8abd | 10 | * in the Software without restriction, including without limitation the rights |
| jebradshaw | 5:3e07f69d8abd | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| jebradshaw | 5:3e07f69d8abd | 12 | * copies of the Software, and to permit persons to whom the Software is |
| jebradshaw | 5:3e07f69d8abd | 13 | * furnished to do so, subject to the following conditions: |
| jebradshaw | 5:3e07f69d8abd | 14 | * |
| jebradshaw | 5:3e07f69d8abd | 15 | * The above copyright notice and this permission notice shall be included in |
| jebradshaw | 5:3e07f69d8abd | 16 | * all copies or substantial portions of the Software. |
| jebradshaw | 5:3e07f69d8abd | 17 | * |
| jebradshaw | 5:3e07f69d8abd | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| jebradshaw | 5:3e07f69d8abd | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| jebradshaw | 5:3e07f69d8abd | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| jebradshaw | 5:3e07f69d8abd | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| jebradshaw | 5:3e07f69d8abd | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| jebradshaw | 5:3e07f69d8abd | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| jebradshaw | 5:3e07f69d8abd | 24 | * THE SOFTWARE. |
| jebradshaw | 5:3e07f69d8abd | 25 | */ |
| jebradshaw | 0:3ba12980833b | 26 | #include "mbed.h" |
| jebradshaw | 0:3ba12980833b | 27 | |
| jebradshaw | 0:3ba12980833b | 28 | #ifndef MBED_MOTCON_H |
| jebradshaw | 0:3ba12980833b | 29 | #define MBED_MOTCON_H |
| jebradshaw | 0:3ba12980833b | 30 | |
| jebradshaw | 0:3ba12980833b | 31 | class MotCon{ |
| jebradshaw | 0:3ba12980833b | 32 | public: |
| jebradshaw | 5:3e07f69d8abd | 33 | /** Create a motor control port object connected to the specified pins |
| jebradshaw | 5:3e07f69d8abd | 34 | * |
| jebradshaw | 5:3e07f69d8abd | 35 | * @param pwm_pin PwmOut pin for the enable or speed control |
| jebradshaw | 5:3e07f69d8abd | 36 | * @param dir1_pin DigitalOut pin to control the motor direction |
| jebradshaw | 5:3e07f69d8abd | 37 | */ |
| jebradshaw | 5:3e07f69d8abd | 38 | MotCon(PinName pwm_pin, PinName dir1_pin); |
| jebradshaw | 5:3e07f69d8abd | 39 | |
| jebradshaw | 5:3e07f69d8abd | 40 | /** Create a motor control port object connected to the specified pins. |
| jebradshaw | 5:3e07f69d8abd | 41 | * The two direction pins are typically complementary and provide |
| jebradshaw | 5:3e07f69d8abd | 42 | * the capability of dynamic braking, free-wheeling, forward and reverse. |
| jebradshaw | 5:3e07f69d8abd | 43 | * |
| jebradshaw | 5:3e07f69d8abd | 44 | * @param pwm_pin PwmOut pin for the enable or speed control |
| jebradshaw | 5:3e07f69d8abd | 45 | * @param dir1_pin DigitalOut pin to control the motor direction |
| jebradshaw | 5:3e07f69d8abd | 46 | * @param dir2_pin DigitalOut pin to control the motor direction |
| jebradshaw | 5:3e07f69d8abd | 47 | */ |
| jebradshaw | 5:3e07f69d8abd | 48 | MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin); |
| jebradshaw | 5:3e07f69d8abd | 49 | |
| jebradshaw | 5:3e07f69d8abd | 50 | /** This function will set the direction and pwm percent scaled |
| jebradshaw | 5:3e07f69d8abd | 51 | * from 0.0 - 1.0 and control the direction pin or pins |
| jebradshaw | 5:3e07f69d8abd | 52 | * If two direction pins are used, the user can control the breaking |
| jebradshaw | 5:3e07f69d8abd | 53 | * mode by using the mutator functions for get_mode() and set_mode() |
| jebradshaw | 5:3e07f69d8abd | 54 | * If only one direction pin is used, dynamic breaking and syncronous |
| jebradshaw | 5:3e07f69d8abd | 55 | * rectification are assumed. See the specific motor driver part datasheet |
| jebradshaw | 5:3e07f69d8abd | 56 | * for additional details. |
| jebradshaw | 5:3e07f69d8abd | 57 | * |
| jebradshaw | 5:3e07f69d8abd | 58 | * @param dc is signed float duty cycle (+/-1.0) |
| jebradshaw | 5:3e07f69d8abd | 59 | */ |
| jebradshaw | 5:3e07f69d8abd | 60 | void mot_control(float dc); |
| jebradshaw | 7:4d79b914ffdf | 61 | void mot_control(float dc, int invert); //this function can be used to invert the motor direction |
| jebradshaw | 5:3e07f69d8abd | 62 | |
| jebradshaw | 7:4d79b914ffdf | 63 | void setMode(int mode); //mode: 1 = dynamic braking, 0 = free-wheeling (default) |
| jebradshaw | 7:4d79b914ffdf | 64 | int getMode(void); |
| jebradshaw | 10:8e93b06e7ff2 | 65 | float read(void); |
| jebradshaw | 10:8e93b06e7ff2 | 66 | /** Shorthand for the write and read functions */ |
| jebradshaw | 10:8e93b06e7ff2 | 67 | MotCon& operator= (float value); |
| jebradshaw | 10:8e93b06e7ff2 | 68 | MotCon& operator= (MotCon& rhs); |
| jebradshaw | 10:8e93b06e7ff2 | 69 | operator float(); |
| jebradshaw | 10:8e93b06e7ff2 | 70 | |
| jebradshaw | 10:8e93b06e7ff2 | 71 | float duty_cycle; |
| jebradshaw | 10:8e93b06e7ff2 | 72 | |
| jebradshaw | 5:3e07f69d8abd | 73 | bool mc_mode; |
| jebradshaw | 0:3ba12980833b | 74 | private: |
| jebradshaw | 5:3e07f69d8abd | 75 | bool _dir2; |
| jebradshaw | 5:3e07f69d8abd | 76 | |
| jebradshaw | 5:3e07f69d8abd | 77 | protected: |
| jebradshaw | 5:3e07f69d8abd | 78 | PwmOut _pwm_pin; |
| jebradshaw | 5:3e07f69d8abd | 79 | DigitalOut _dir1_pin; |
| jebradshaw | 5:3e07f69d8abd | 80 | DigitalOut _dir2_pin; |
| jebradshaw | 0:3ba12980833b | 81 | }; |
| jebradshaw | 0:3ba12980833b | 82 | |
| jebradshaw | 0:3ba12980833b | 83 | #endif |