Tugas Magang-1
Dependencies: mbed
Mundur.cpp@0:8e7dd2d20e4a, 2019-03-20 (annotated)
- Committer:
- ferozfernando
- Date:
- Wed Mar 20 16:44:22 2019 +0000
- Revision:
- 0:8e7dd2d20e4a
Tugas Magang KRTMI-1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ferozfernando | 0:8e7dd2d20e4a | 1 | #include <iostream> |
ferozfernando | 0:8e7dd2d20e4a | 2 | #include "Motor.h" |
ferozfernando | 0:8e7dd2d20e4a | 3 | #include "mbed.h" |
ferozfernando | 0:8e7dd2d20e4a | 4 | |
ferozfernando | 0:8e7dd2d20e4a | 5 | bool loop = true; |
ferozfernando | 0:8e7dd2d20e4a | 6 | |
ferozfernando | 0:8e7dd2d20e4a | 7 | Motor::Motor(PinName pwm, PinName fwd, PinName rev): |
ferozfernando | 0:8e7dd2d20e4a | 8 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
ferozfernando | 0:8e7dd2d20e4a | 9 | |
ferozfernando | 0:8e7dd2d20e4a | 10 | // Set initial condition of PWM |
ferozfernando | 0:8e7dd2d20e4a | 11 | _pwm.period(0.002); |
ferozfernando | 0:8e7dd2d20e4a | 12 | _pwm = 0; |
ferozfernando | 0:8e7dd2d20e4a | 13 | |
ferozfernando | 0:8e7dd2d20e4a | 14 | // Initial condition of output enables |
ferozfernando | 0:8e7dd2d20e4a | 15 | _fwd = 0; |
ferozfernando | 0:8e7dd2d20e4a | 16 | _rev = 0; |
ferozfernando | 0:8e7dd2d20e4a | 17 | } |
ferozfernando | 0:8e7dd2d20e4a | 18 | |
ferozfernando | 0:8e7dd2d20e4a | 19 | void Motor::speed(float speed) { |
ferozfernando | 0:8e7dd2d20e4a | 20 | _fwd = (speed > (float)0.0); |
ferozfernando | 0:8e7dd2d20e4a | 21 | _rev = (speed < (float)0.0); |
ferozfernando | 0:8e7dd2d20e4a | 22 | _pwm = fabs(speed); |
ferozfernando | 0:8e7dd2d20e4a | 23 | } |
ferozfernando | 0:8e7dd2d20e4a | 24 | |
ferozfernando | 0:8e7dd2d20e4a | 25 | void Motor::period(float period){ |
ferozfernando | 0:8e7dd2d20e4a | 26 | _pwm.period(period); |
ferozfernando | 0:8e7dd2d20e4a | 27 | } |
ferozfernando | 0:8e7dd2d20e4a | 28 | |
ferozfernando | 0:8e7dd2d20e4a | 29 | // Ilustrasi Kondisi Motor : |
ferozfernando | 0:8e7dd2d20e4a | 30 | /* |
ferozfernando | 0:8e7dd2d20e4a | 31 | -__________+ -- > Motor 3 |
ferozfernando | 0:8e7dd2d20e4a | 32 | |
ferozfernando | 0:8e7dd2d20e4a | 33 | |
ferozfernando | 0:8e7dd2d20e4a | 34 | - + |
ferozfernando | 0:8e7dd2d20e4a | 35 | \ / |
ferozfernando | 0:8e7dd2d20e4a | 36 | \ / --- > Kiri : Motor 1, Kanan : Motor 2 |
ferozfernando | 0:8e7dd2d20e4a | 37 | \ / |
ferozfernando | 0:8e7dd2d20e4a | 38 | + - |
ferozfernando | 0:8e7dd2d20e4a | 39 | */ |
ferozfernando | 0:8e7dd2d20e4a | 40 | |
ferozfernando | 0:8e7dd2d20e4a | 41 | |
ferozfernando | 0:8e7dd2d20e4a | 42 | int main () { |
ferozfernando | 0:8e7dd2d20e4a | 43 | Motor Motor1 (PC_7, PA_9, PA_8); // Deklarasi objek motor |
ferozfernando | 0:8e7dd2d20e4a | 44 | Motor Motor2 (PA_7, PA_6, PA_5); |
ferozfernando | 0:8e7dd2d20e4a | 45 | Motor Motor3 (PB_3, PA_10, PA_2); |
ferozfernando | 0:8e7dd2d20e4a | 46 | |
ferozfernando | 0:8e7dd2d20e4a | 47 | while (loop == true) { |
ferozfernando | 0:8e7dd2d20e4a | 48 | Motor1.speed(-0.5); |
ferozfernando | 0:8e7dd2d20e4a | 49 | Motor2.speed(0.5); |
ferozfernando | 0:8e7dd2d20e4a | 50 | } |
ferozfernando | 0:8e7dd2d20e4a | 51 | return 0; |
ferozfernando | 0:8e7dd2d20e4a | 52 | } |