V1
Dependencies: mbed
Motor.h@0:96a0fbdff740, 2019-02-18 (annotated)
- Committer:
- bernardusrendy
- Date:
- Mon Feb 18 12:54:07 2019 +0000
- Revision:
- 0:96a0fbdff740
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bernardusrendy | 0:96a0fbdff740 | 1 | /* mbed simple H-bridge motor controller |
bernardusrendy | 0:96a0fbdff740 | 2 | * Copyright (c) 2007-2010, sford |
bernardusrendy | 0:96a0fbdff740 | 3 | * |
bernardusrendy | 0:96a0fbdff740 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
bernardusrendy | 0:96a0fbdff740 | 5 | * of this software and associated documentation files (the "Software"), to deal |
bernardusrendy | 0:96a0fbdff740 | 6 | * in the Software without restriction, including without limitation the rights |
bernardusrendy | 0:96a0fbdff740 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
bernardusrendy | 0:96a0fbdff740 | 8 | * copies of the Software, and to permit persons to whom the Software is |
bernardusrendy | 0:96a0fbdff740 | 9 | * furnished to do so, subject to the following conditions: |
bernardusrendy | 0:96a0fbdff740 | 10 | * |
bernardusrendy | 0:96a0fbdff740 | 11 | * The above copyright notice and this permission notice shall be included in |
bernardusrendy | 0:96a0fbdff740 | 12 | * all copies or substantial portions of the Software. |
bernardusrendy | 0:96a0fbdff740 | 13 | * |
bernardusrendy | 0:96a0fbdff740 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
bernardusrendy | 0:96a0fbdff740 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
bernardusrendy | 0:96a0fbdff740 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
bernardusrendy | 0:96a0fbdff740 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
bernardusrendy | 0:96a0fbdff740 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bernardusrendy | 0:96a0fbdff740 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
bernardusrendy | 0:96a0fbdff740 | 20 | * THE SOFTWARE. |
bernardusrendy | 0:96a0fbdff740 | 21 | */ |
bernardusrendy | 0:96a0fbdff740 | 22 | |
bernardusrendy | 0:96a0fbdff740 | 23 | #ifndef MBED_MOTOR_H |
bernardusrendy | 0:96a0fbdff740 | 24 | #define MBED_MOTOR_H |
bernardusrendy | 0:96a0fbdff740 | 25 | |
bernardusrendy | 0:96a0fbdff740 | 26 | #include "mbed.h" |
bernardusrendy | 0:96a0fbdff740 | 27 | |
bernardusrendy | 0:96a0fbdff740 | 28 | #define BRAKE_HIGH 1 |
bernardusrendy | 0:96a0fbdff740 | 29 | #define BRAKE_LOW 0 |
bernardusrendy | 0:96a0fbdff740 | 30 | |
bernardusrendy | 0:96a0fbdff740 | 31 | /** Interface to control a standard DC motor |
bernardusrendy | 0:96a0fbdff740 | 32 | * with an H-bridge using a PwmOut and 2 DigitalOuts |
bernardusrendy | 0:96a0fbdff740 | 33 | */ |
bernardusrendy | 0:96a0fbdff740 | 34 | class Motor { |
bernardusrendy | 0:96a0fbdff740 | 35 | public: |
bernardusrendy | 0:96a0fbdff740 | 36 | |
bernardusrendy | 0:96a0fbdff740 | 37 | /** Create a motor control interface |
bernardusrendy | 0:96a0fbdff740 | 38 | * |
bernardusrendy | 0:96a0fbdff740 | 39 | * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed |
bernardusrendy | 0:96a0fbdff740 | 40 | * @param fwd A DigitalOut, set high when the motor should go forward |
bernardusrendy | 0:96a0fbdff740 | 41 | * @param rev A DigitalOut, set high when the motor should go backwards |
bernardusrendy | 0:96a0fbdff740 | 42 | */ |
bernardusrendy | 0:96a0fbdff740 | 43 | Motor(PinName pwm, PinName fwd, PinName rev); |
bernardusrendy | 0:96a0fbdff740 | 44 | |
bernardusrendy | 0:96a0fbdff740 | 45 | /** Set the speed of the motor |
bernardusrendy | 0:96a0fbdff740 | 46 | * |
bernardusrendy | 0:96a0fbdff740 | 47 | * @param speed The speed of the motor as a normalised value between -1.0 and 1.0 |
bernardusrendy | 0:96a0fbdff740 | 48 | */ |
bernardusrendy | 0:96a0fbdff740 | 49 | void speed(float speed); |
bernardusrendy | 0:96a0fbdff740 | 50 | |
bernardusrendy | 0:96a0fbdff740 | 51 | /** Set the period of the pwm duty cycle. |
bernardusrendy | 0:96a0fbdff740 | 52 | * |
bernardusrendy | 0:96a0fbdff740 | 53 | * Wrapper for PwmOut::period() |
bernardusrendy | 0:96a0fbdff740 | 54 | * |
bernardusrendy | 0:96a0fbdff740 | 55 | * @param seconds - Pwm duty cycle in seconds. |
bernardusrendy | 0:96a0fbdff740 | 56 | */ |
bernardusrendy | 0:96a0fbdff740 | 57 | void period(float period); |
bernardusrendy | 0:96a0fbdff740 | 58 | |
bernardusrendy | 0:96a0fbdff740 | 59 | /** Brake the H-bridge to GND or VCC. |
bernardusrendy | 0:96a0fbdff740 | 60 | * |
bernardusrendy | 0:96a0fbdff740 | 61 | * Defaults to breaking to VCC. |
bernardusrendy | 0:96a0fbdff740 | 62 | * |
bernardusrendy | 0:96a0fbdff740 | 63 | * Brake to GND => inA = inB = 0 |
bernardusrendy | 0:96a0fbdff740 | 64 | * Brake to VCC => inA = inB = 1 |
bernardusrendy | 0:96a0fbdff740 | 65 | */ |
bernardusrendy | 0:96a0fbdff740 | 66 | void brake(int highLow = BRAKE_HIGH); |
bernardusrendy | 0:96a0fbdff740 | 67 | |
bernardusrendy | 0:96a0fbdff740 | 68 | protected: |
bernardusrendy | 0:96a0fbdff740 | 69 | PwmOut _pwm; |
bernardusrendy | 0:96a0fbdff740 | 70 | DigitalOut _fwd; |
bernardusrendy | 0:96a0fbdff740 | 71 | DigitalOut _rev; |
bernardusrendy | 0:96a0fbdff740 | 72 | |
bernardusrendy | 0:96a0fbdff740 | 73 | }; |
bernardusrendy | 0:96a0fbdff740 | 74 | |
bernardusrendy | 0:96a0fbdff740 | 75 | #endif |