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