![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Magang KRTMI Day 1 : Program Motor Driver Nucleo
ProgramMotor.cpp@3:1f003e4a9c8d, 2019-03-20 (annotated)
- Committer:
- andremanurung
- Date:
- Wed Mar 20 13:35:50 2019 +0000
- Revision:
- 3:1f003e4a9c8d
AndreSubagjaManurung-16518254
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andremanurung | 3:1f003e4a9c8d | 1 | #include "Motor.h" |
andremanurung | 3:1f003e4a9c8d | 2 | |
andremanurung | 3:1f003e4a9c8d | 3 | //------------------------------------------------------------------------------------- |
andremanurung | 3:1f003e4a9c8d | 4 | Motor::Motor(D5, D8, D7): |
andremanurung | 3:1f003e4a9c8d | 5 | _pwm(D5), _fwd(D8), _rev(D7) { |
andremanurung | 3:1f003e4a9c8d | 6 | |
andremanurung | 3:1f003e4a9c8d | 7 | _pwm.period(0.002); |
andremanurung | 3:1f003e4a9c8d | 8 | _pwm = 0; |
andremanurung | 3:1f003e4a9c8d | 9 | _fwd = 0; |
andremanurung | 3:1f003e4a9c8d | 10 | _rev = 0; |
andremanurung | 3:1f003e4a9c8d | 11 | } |
andremanurung | 3:1f003e4a9c8d | 12 | void Motor::speed(float speed) { |
andremanurung | 3:1f003e4a9c8d | 13 | _fwd = (speed > (float)0.0); |
andremanurung | 3:1f003e4a9c8d | 14 | _rev = (speed < (float)0.0); |
andremanurung | 3:1f003e4a9c8d | 15 | _pwm = fabs(speed); |
andremanurung | 3:1f003e4a9c8d | 16 | } |
andremanurung | 3:1f003e4a9c8d | 17 | void Motor::period(float period){ |
andremanurung | 3:1f003e4a9c8d | 18 | _pwm.period(period); |
andremanurung | 3:1f003e4a9c8d | 19 | |
andremanurung | 3:1f003e4a9c8d | 20 | } |
andremanurung | 3:1f003e4a9c8d | 21 | void Motor::brake(int highLow){ |
andremanurung | 3:1f003e4a9c8d | 22 | if(highLow == BRAKE_HIGH){ |
andremanurung | 3:1f003e4a9c8d | 23 | _fwd = 1; |
andremanurung | 3:1f003e4a9c8d | 24 | _rev = 1; |
andremanurung | 3:1f003e4a9c8d | 25 | } |
andremanurung | 3:1f003e4a9c8d | 26 | else if(highLow == BRAKE_LOW){ |
andremanurung | 3:1f003e4a9c8d | 27 | _fwd = 0; |
andremanurung | 3:1f003e4a9c8d | 28 | _rev = 0; |
andremanurung | 3:1f003e4a9c8d | 29 | } |
andremanurung | 3:1f003e4a9c8d | 30 | } |
andremanurung | 3:1f003e4a9c8d | 31 | void Motor::forcebrake(){ |
andremanurung | 3:1f003e4a9c8d | 32 | _pwm = 1.0; |
andremanurung | 3:1f003e4a9c8d | 33 | _fwd = 1; |
andremanurung | 3:1f003e4a9c8d | 34 | _rev = 1; |
andremanurung | 3:1f003e4a9c8d | 35 | } |
andremanurung | 3:1f003e4a9c8d | 36 | //------------------------------------------------------------------------------------- |
andremanurung | 3:1f003e4a9c8d | 37 | |
andremanurung | 3:1f003e4a9c8d | 38 | |
andremanurung | 3:1f003e4a9c8d | 39 | //------------------Ke Depan---------------------------- |
andremanurung | 3:1f003e4a9c8d | 40 | //---------------------^^----------------------------- |
andremanurung | 3:1f003e4a9c8d | 41 | //---------------------||----------------------------- |
andremanurung | 3:1f003e4a9c8d | 42 | //---------------------------------------------------- |
andremanurung | 3:1f003e4a9c8d | 43 | //----Motor depan-----OOOO---------------------------- |
andremanurung | 3:1f003e4a9c8d | 44 | //---------------------||--------------__>------------ |
andremanurung | 3:1f003e4a9c8d | 45 | //---------------------||--------------__>---Ke Kanan----- |
andremanurung | 3:1f003e4a9c8d | 46 | //-------------------//--\\---------------------------- |
andremanurung | 3:1f003e4a9c8d | 47 | //------------------//----\\-------------------------- |
andremanurung | 3:1f003e4a9c8d | 48 | //-----Motor kiri--OOO----OOO----Motor Kanan---------- |
andremanurung | 3:1f003e4a9c8d | 49 | |
andremanurung | 3:1f003e4a9c8d | 50 | int main() { |
andremanurung | 3:1f003e4a9c8d | 51 | string arah, rotate; |
andremanurung | 3:1f003e4a9c8d | 52 | |
andremanurung | 3:1f003e4a9c8d | 53 | Motor depan(); |
andremanurung | 3:1f003e4a9c8d | 54 | Motor kiri(); |
andremanurung | 3:1f003e4a9c8d | 55 | Motor kanan(); |
andremanurung | 3:1f003e4a9c8d | 56 | |
andremanurung | 3:1f003e4a9c8d | 57 | //Kekuatan motor-yang-berotasi-dan-menyebabkan-base-bergerak-clockwise adalah positif |
andremanurung | 3:1f003e4a9c8d | 58 | //Kekuatan motor-yang-berotasi-dan-menyebabkan-base-bergerak-clockwise adalah positif |
andremanurung | 3:1f003e4a9c8d | 59 | //Kekuatan motor-yang-berotasi-dan-menyebabkan-base-bergerak-clockwise adalah positif |
andremanurung | 3:1f003e4a9c8d | 60 | |
andremanurung | 3:1f003e4a9c8d | 61 | do { |
andremanurung | 3:1f003e4a9c8d | 62 | getline (cin, arah); |
andremanurung | 3:1f003e4a9c8d | 63 | getline (cin, rotate); |
andremanurung | 3:1f003e4a9c8d | 64 | |
andremanurung | 3:1f003e4a9c8d | 65 | if (arah == "depan"){ //Base bergerak ke depan |
andremanurung | 3:1f003e4a9c8d | 66 | kanan.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 67 | kiri.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 68 | } |
andremanurung | 3:1f003e4a9c8d | 69 | |
andremanurung | 3:1f003e4a9c8d | 70 | if (arah == "belakang"){ //Base bergerak ke belakang |
andremanurung | 3:1f003e4a9c8d | 71 | kiri.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 72 | kanan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 73 | } |
andremanurung | 3:1f003e4a9c8d | 74 | |
andremanurung | 3:1f003e4a9c8d | 75 | if (arah == "kiri") { //Base bergerak ke kiri |
andremanurung | 3:1f003e4a9c8d | 76 | depan.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 77 | kiri.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 78 | kanan.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 79 | } |
andremanurung | 3:1f003e4a9c8d | 80 | |
andremanurung | 3:1f003e4a9c8d | 81 | if (arah == "kanan") { //Base bergerak ke kanan |
andremanurung | 3:1f003e4a9c8d | 82 | depan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 83 | kiri.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 84 | kanan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 85 | } |
andremanurung | 3:1f003e4a9c8d | 86 | |
andremanurung | 3:1f003e4a9c8d | 87 | if (rotate == "clockwise") { //Base begerak searah jarum jam (pivot kanan) |
andremanurung | 3:1f003e4a9c8d | 88 | depan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 89 | kiri.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 90 | kanan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 91 | } |
andremanurung | 3:1f003e4a9c8d | 92 | |
andremanurung | 3:1f003e4a9c8d | 93 | if (rotate == "counterclock") { //Base begerak searah jarum jam (pivot kanan) |
andremanurung | 3:1f003e4a9c8d | 94 | depan.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 95 | kiri.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 96 | kanan.speed(-1.0); |
andremanurung | 3:1f003e4a9c8d | 97 | } |
andremanurung | 3:1f003e4a9c8d | 98 | |
andremanurung | 3:1f003e4a9c8d | 99 | if (arah == "kanan" && rotate == "clockwise"){ //Base begerak searah jarum jam dan translasi ke kanan (belum berhasil) |
andremanurung | 3:1f003e4a9c8d | 100 | depan.speed(1.0); |
andremanurung | 3:1f003e4a9c8d | 101 | kiri.speed(0.75); |
andremanurung | 3:1f003e4a9c8d | 102 | kanan.speed(-0.5); |
andremanurung | 3:1f003e4a9c8d | 103 | } |
andremanurung | 3:1f003e4a9c8d | 104 | } while(); |
andremanurung | 3:1f003e4a9c8d | 105 | } |