![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Menarik
Dependencies: mbed
Magang KRTMI Day 1
Motor.cpp@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 | #include "mbed.h" |
D4n1elR | 0:7fa27a02504b | 24 | #include "Motor.h" |
D4n1elR | 0:7fa27a02504b | 25 | #include<iostream> |
D4n1elR | 0:7fa27a02504b | 26 | |
D4n1elR | 0:7fa27a02504b | 27 | Motor::Motor(PinName pwm, PinName fwd, PinName rev): |
D4n1elR | 0:7fa27a02504b | 28 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
D4n1elR | 0:7fa27a02504b | 29 | |
D4n1elR | 0:7fa27a02504b | 30 | // Set initial condition of PWM |
D4n1elR | 0:7fa27a02504b | 31 | _pwm.period(0.002); |
D4n1elR | 0:7fa27a02504b | 32 | _pwm = 0; |
D4n1elR | 0:7fa27a02504b | 33 | |
D4n1elR | 0:7fa27a02504b | 34 | // Initial condition of output enables |
D4n1elR | 0:7fa27a02504b | 35 | _fwd = 0; |
D4n1elR | 0:7fa27a02504b | 36 | _rev = 0; |
D4n1elR | 0:7fa27a02504b | 37 | } |
D4n1elR | 0:7fa27a02504b | 38 | |
D4n1elR | 0:7fa27a02504b | 39 | void Motor::speed(float speed) { |
D4n1elR | 0:7fa27a02504b | 40 | _fwd = (speed > (float)0.0); |
D4n1elR | 0:7fa27a02504b | 41 | _rev = (speed < (float)0.0); |
D4n1elR | 0:7fa27a02504b | 42 | _pwm = fabs(speed); |
D4n1elR | 0:7fa27a02504b | 43 | } |
D4n1elR | 0:7fa27a02504b | 44 | |
D4n1elR | 0:7fa27a02504b | 45 | void Motor::period(float period){ |
D4n1elR | 0:7fa27a02504b | 46 | |
D4n1elR | 0:7fa27a02504b | 47 | _pwm.period(period); |
D4n1elR | 0:7fa27a02504b | 48 | |
D4n1elR | 0:7fa27a02504b | 49 | } |
D4n1elR | 0:7fa27a02504b | 50 | |
D4n1elR | 0:7fa27a02504b | 51 | void Motor::brake(int highLow){ |
D4n1elR | 0:7fa27a02504b | 52 | |
D4n1elR | 0:7fa27a02504b | 53 | if(highLow == BRAKE_HIGH){ |
D4n1elR | 0:7fa27a02504b | 54 | _fwd = 1; |
D4n1elR | 0:7fa27a02504b | 55 | _rev = 1; |
D4n1elR | 0:7fa27a02504b | 56 | } |
D4n1elR | 0:7fa27a02504b | 57 | else if(highLow == BRAKE_LOW){ |
D4n1elR | 0:7fa27a02504b | 58 | _fwd = 0; |
D4n1elR | 0:7fa27a02504b | 59 | _rev = 0; |
D4n1elR | 0:7fa27a02504b | 60 | } |
D4n1elR | 0:7fa27a02504b | 61 | |
D4n1elR | 0:7fa27a02504b | 62 | } |
D4n1elR | 0:7fa27a02504b | 63 | |
D4n1elR | 0:7fa27a02504b | 64 | void Motor::forcebrake(){ |
D4n1elR | 0:7fa27a02504b | 65 | _pwm = 1.0; |
D4n1elR | 0:7fa27a02504b | 66 | _fwd = 1; |
D4n1elR | 0:7fa27a02504b | 67 | _rev = 1; |
D4n1elR | 0:7fa27a02504b | 68 | } |
D4n1elR | 0:7fa27a02504b | 69 | |
D4n1elR | 0:7fa27a02504b | 70 | int main() { |
D4n1elR | 0:7fa27a02504b | 71 | string arah, rotate, haha; |
D4n1elR | 0:7fa27a02504b | 72 | |
D4n1elR | 0:7fa27a02504b | 73 | Motor depan(PC_7,PA_9,PA_8); |
D4n1elR | 0:7fa27a02504b | 74 | Motor kiri(PA_7,PA_6,PA_5); |
D4n1elR | 0:7fa27a02504b | 75 | Motor kanan(PB_3,PA_10,PA_2); |
D4n1elR | 0:7fa27a02504b | 76 | |
D4n1elR | 0:7fa27a02504b | 77 | do { |
D4n1elR | 0:7fa27a02504b | 78 | getline (cin, arah); |
D4n1elR | 0:7fa27a02504b | 79 | getline (cin, rotate); |
D4n1elR | 0:7fa27a02504b | 80 | getline (cin, haha); |
D4n1elR | 0:7fa27a02504b | 81 | if (arah == "depan"){ |
D4n1elR | 0:7fa27a02504b | 82 | kanan.speed(-1.0); |
D4n1elR | 0:7fa27a02504b | 83 | kiri.speed(1.0); |
D4n1elR | 0:7fa27a02504b | 84 | } |
D4n1elR | 0:7fa27a02504b | 85 | else if (arah == "belakang"){ |
D4n1elR | 0:7fa27a02504b | 86 | kiri.speed(-1.0); |
D4n1elR | 0:7fa27a02504b | 87 | kanan.speed(1.0); |
D4n1elR | 0:7fa27a02504b | 88 | } |
D4n1elR | 0:7fa27a02504b | 89 | else if (arah == "kiri") { |
D4n1elR | 0:7fa27a02504b | 90 | depan.speed(-1.0); |
D4n1elR | 0:7fa27a02504b | 91 | kiri.speed(1.0); |
D4n1elR | 0:7fa27a02504b | 92 | kanan.speed(1.0); |
D4n1elR | 0:7fa27a02504b | 93 | } |
D4n1elR | 0:7fa27a02504b | 94 | else if (arah == "kanan") { |
D4n1elR | 0:7fa27a02504b | 95 | depan.speed(1.0); |
D4n1elR | 0:7fa27a02504b | 96 | kiri.speed(-1.0); |
D4n1elR | 0:7fa27a02504b | 97 | kanan.speed(-1.0); |
D4n1elR | 0:7fa27a02504b | 98 | } |
D4n1elR | 0:7fa27a02504b | 99 | |
D4n1elR | 0:7fa27a02504b | 100 | if (rotate == "jam") { //searah jarum jam |
D4n1elR | 0:7fa27a02504b | 101 | depan.speed(0.5); |
D4n1elR | 0:7fa27a02504b | 102 | kiri.speed(0.5); |
D4n1elR | 0:7fa27a02504b | 103 | kanan.speed(0.5); |
D4n1elR | 0:7fa27a02504b | 104 | } |
D4n1elR | 0:7fa27a02504b | 105 | else if (rotate == "nonjam") { //berlawanan jarum jam |
D4n1elR | 0:7fa27a02504b | 106 | depan.speed(-0.5); |
D4n1elR | 0:7fa27a02504b | 107 | kiri.speed(-0.5); |
D4n1elR | 0:7fa27a02504b | 108 | kanan.speed(-0.5); |
D4n1elR | 0:7fa27a02504b | 109 | } |
D4n1elR | 0:7fa27a02504b | 110 | |
D4n1elR | 0:7fa27a02504b | 111 | if (haha == "haha") { //maju ke depan sambil berotasi berlawan |
D4n1elR | 0:7fa27a02504b | 112 | depan.speed(-0.9); |
D4n1elR | 0:7fa27a02504b | 113 | kiri.speed(-0.6); |
D4n1elR | 0:7fa27a02504b | 114 | kanan.speed(-0.75); |
D4n1elR | 0:7fa27a02504b | 115 | } |
D4n1elR | 0:7fa27a02504b | 116 | |
D4n1elR | 0:7fa27a02504b | 117 | } while(1 != 0); |
D4n1elR | 0:7fa27a02504b | 118 | } |