Yolanda Tania
/
PID_tangan
udah bisa looo
motor/Motor.cpp@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 | #include "Motor.h" |
Yolandataniaa | 0:aa8e05bc0533 | 24 | |
Yolandataniaa | 0:aa8e05bc0533 | 25 | Motor::Motor(PinName pwm, PinName fwd, PinName rev): |
Yolandataniaa | 0:aa8e05bc0533 | 26 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
Yolandataniaa | 0:aa8e05bc0533 | 27 | |
Yolandataniaa | 0:aa8e05bc0533 | 28 | // Set initial condition of PWM |
Yolandataniaa | 0:aa8e05bc0533 | 29 | _pwm.period(0.002); |
Yolandataniaa | 0:aa8e05bc0533 | 30 | _pwm = 0; |
Yolandataniaa | 0:aa8e05bc0533 | 31 | |
Yolandataniaa | 0:aa8e05bc0533 | 32 | // Initial condition of output enables |
Yolandataniaa | 0:aa8e05bc0533 | 33 | _fwd = 0; |
Yolandataniaa | 0:aa8e05bc0533 | 34 | _rev = 0; |
Yolandataniaa | 0:aa8e05bc0533 | 35 | } |
Yolandataniaa | 0:aa8e05bc0533 | 36 | |
Yolandataniaa | 0:aa8e05bc0533 | 37 | void Motor::speed(float speed) { |
Yolandataniaa | 0:aa8e05bc0533 | 38 | _fwd = (speed > (float)0.0); |
Yolandataniaa | 0:aa8e05bc0533 | 39 | _rev = (speed < (float)0.0); |
Yolandataniaa | 0:aa8e05bc0533 | 40 | _pwm = fabs(speed); |
Yolandataniaa | 0:aa8e05bc0533 | 41 | } |
Yolandataniaa | 0:aa8e05bc0533 | 42 | |
Yolandataniaa | 0:aa8e05bc0533 | 43 | void Motor::period(float period){ |
Yolandataniaa | 0:aa8e05bc0533 | 44 | |
Yolandataniaa | 0:aa8e05bc0533 | 45 | _pwm.period(period); |
Yolandataniaa | 0:aa8e05bc0533 | 46 | |
Yolandataniaa | 0:aa8e05bc0533 | 47 | } |
Yolandataniaa | 0:aa8e05bc0533 | 48 | |
Yolandataniaa | 0:aa8e05bc0533 | 49 | void Motor::brake(int highLow){ |
Yolandataniaa | 0:aa8e05bc0533 | 50 | |
Yolandataniaa | 0:aa8e05bc0533 | 51 | if(highLow == BRAKE_HIGH){ |
Yolandataniaa | 0:aa8e05bc0533 | 52 | _pwm = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 53 | _fwd = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 54 | _rev = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 55 | } |
Yolandataniaa | 0:aa8e05bc0533 | 56 | else if(highLow == BRAKE_LOW){ |
Yolandataniaa | 0:aa8e05bc0533 | 57 | _fwd = 0; |
Yolandataniaa | 0:aa8e05bc0533 | 58 | _rev = 0; |
Yolandataniaa | 0:aa8e05bc0533 | 59 | } |
Yolandataniaa | 0:aa8e05bc0533 | 60 | |
Yolandataniaa | 0:aa8e05bc0533 | 61 | } |
Yolandataniaa | 0:aa8e05bc0533 | 62 | |
Yolandataniaa | 0:aa8e05bc0533 | 63 | void Motor::forcebrake(){ |
Yolandataniaa | 0:aa8e05bc0533 | 64 | _pwm = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 65 | _fwd = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 66 | _rev = 1; |
Yolandataniaa | 0:aa8e05bc0533 | 67 | } |