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