V1
Dependencies: mbed
main.cpp@1:ed6ad03b86e4, 2019-04-16 (annotated)
- Committer:
- bernardusrendy
- Date:
- Tue Apr 16 12:54:03 2019 +0000
- Revision:
- 1:ed6ad03b86e4
- Parent:
- 0:96a0fbdff740
hhhhh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bernardusrendy | 0:96a0fbdff740 | 1 | #include "JoystickPS3.h" |
bernardusrendy | 0:96a0fbdff740 | 2 | #include "Motor.h" |
bernardusrendy | 0:96a0fbdff740 | 3 | #include "mbed.h" |
bernardusrendy | 0:96a0fbdff740 | 4 | /* Inisialisasi Pin TX-RX Joystik dan PC */ |
bernardusrendy | 0:96a0fbdff740 | 5 | joysticknucleo joystick(PA_0,PA_1); |
bernardusrendy | 0:96a0fbdff740 | 6 | Serial pc(USBTX,USBRX); |
bernardusrendy | 0:96a0fbdff740 | 7 | /* Deklarasi Motor Base */ |
bernardusrendy | 1:ed6ad03b86e4 | 8 | Motor motorDepan(PB_8, PC_6, PC_5); //(PB_9, PA_12, PC_5); |
bernardusrendy | 1:ed6ad03b86e4 | 9 | Motor motorBelakang(PB_9, PA_12, PA_6); |
bernardusrendy | 1:ed6ad03b86e4 | 10 | //Motor motorKiri (PB_9, PA_6, PA_12); |
bernardusrendy | 1:ed6ad03b86e4 | 11 | |
bernardusrendy | 0:96a0fbdff740 | 12 | /****************************************************/ |
bernardusrendy | 0:96a0fbdff740 | 13 | /* Deklarasi Fungsi dan Procedure */ |
bernardusrendy | 0:96a0fbdff740 | 14 | /****************************************************/ |
bernardusrendy | 0:96a0fbdff740 | 15 | int case_joystick() |
bernardusrendy | 0:96a0fbdff740 | 16 | { |
bernardusrendy | 0:96a0fbdff740 | 17 | //---------------------------------------------------// |
bernardusrendy | 0:96a0fbdff740 | 18 | // Gerak Motor Base // |
bernardusrendy | 0:96a0fbdff740 | 19 | // Case 1 : Pivot kanan // |
bernardusrendy | 0:96a0fbdff740 | 20 | // Case 2 : Pivot Kiri // |
bernardusrendy | 0:96a0fbdff740 | 21 | // Case 3 : Maju // |
bernardusrendy | 0:96a0fbdff740 | 22 | // Case 4 : Kiri // |
bernardusrendy | 0:96a0fbdff740 | 23 | // Case 5 : Bawah // |
bernardusrendy | 0:96a0fbdff740 | 24 | // Case 6 : Kanan // |
bernardusrendy | 0:96a0fbdff740 | 25 | //---------------------------------------------------// |
bernardusrendy | 0:96a0fbdff740 | 26 | |
bernardusrendy | 0:96a0fbdff740 | 27 | int caseJoystick; |
bernardusrendy | 0:96a0fbdff740 | 28 | if ((joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) { |
bernardusrendy | 0:96a0fbdff740 | 29 | // Pivot Kanan |
bernardusrendy | 0:96a0fbdff740 | 30 | caseJoystick = 1; |
bernardusrendy | 0:96a0fbdff740 | 31 | } |
bernardusrendy | 0:96a0fbdff740 | 32 | else if ((!joystick.R1)&&(joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) { |
bernardusrendy | 0:96a0fbdff740 | 33 | // Pivot Kiri |
bernardusrendy | 0:96a0fbdff740 | 34 | caseJoystick = 2; |
bernardusrendy | 0:96a0fbdff740 | 35 | } |
bernardusrendy | 0:96a0fbdff740 | 36 | else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) { |
bernardusrendy | 0:96a0fbdff740 | 37 | // Pivot Kiri |
bernardusrendy | 0:96a0fbdff740 | 38 | caseJoystick = 3; |
bernardusrendy | 0:96a0fbdff740 | 39 | } |
bernardusrendy | 0:96a0fbdff740 | 40 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) { |
bernardusrendy | 1:ed6ad03b86e4 | 41 | // Pivot |
bernardusrendy | 0:96a0fbdff740 | 42 | caseJoystick = 4; |
bernardusrendy | 0:96a0fbdff740 | 43 | } |
bernardusrendy | 1:ed6ad03b86e4 | 44 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) { |
bernardusrendy | 0:96a0fbdff740 | 45 | // Pivot Kiri |
bernardusrendy | 0:96a0fbdff740 | 46 | caseJoystick = 5; |
bernardusrendy | 0:96a0fbdff740 | 47 | } |
bernardusrendy | 0:96a0fbdff740 | 48 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) { |
bernardusrendy | 0:96a0fbdff740 | 49 | // Pivot Kiri |
bernardusrendy | 0:96a0fbdff740 | 50 | caseJoystick = 6; |
bernardusrendy | 0:96a0fbdff740 | 51 | } |
bernardusrendy | 0:96a0fbdff740 | 52 | return(caseJoystick); |
bernardusrendy | 0:96a0fbdff740 | 53 | } |
bernardusrendy | 0:96a0fbdff740 | 54 | |
bernardusrendy | 1:ed6ad03b86e4 | 55 | void aktuator(int case_joy) |
bernardusrendy | 0:96a0fbdff740 | 56 | { |
bernardusrendy | 0:96a0fbdff740 | 57 | switch (case_joy) { |
bernardusrendy | 0:96a0fbdff740 | 58 | case (1): |
bernardusrendy | 0:96a0fbdff740 | 59 | { |
bernardusrendy | 0:96a0fbdff740 | 60 | // Pivot Kanan |
bernardusrendy | 1:ed6ad03b86e4 | 61 | motorDepan.speed(0.2); |
bernardusrendy | 1:ed6ad03b86e4 | 62 | motorBelakang.speed(-0.2); |
bernardusrendy | 0:96a0fbdff740 | 63 | break; |
bernardusrendy | 0:96a0fbdff740 | 64 | } |
bernardusrendy | 0:96a0fbdff740 | 65 | case (2): |
bernardusrendy | 0:96a0fbdff740 | 66 | { |
bernardusrendy | 0:96a0fbdff740 | 67 | // Pivot Kiri |
bernardusrendy | 1:ed6ad03b86e4 | 68 | motorDepan.speed(-0.2); |
bernardusrendy | 1:ed6ad03b86e4 | 69 | motorBelakang.speed(0.2); |
bernardusrendy | 0:96a0fbdff740 | 70 | break; |
bernardusrendy | 0:96a0fbdff740 | 71 | } |
bernardusrendy | 0:96a0fbdff740 | 72 | case (3): |
bernardusrendy | 0:96a0fbdff740 | 73 | { |
bernardusrendy | 0:96a0fbdff740 | 74 | // Maju |
bernardusrendy | 1:ed6ad03b86e4 | 75 | motorDepan.speed(0.2); |
bernardusrendy | 1:ed6ad03b86e4 | 76 | motorBelakang.speed(0.2); |
bernardusrendy | 0:96a0fbdff740 | 77 | break; |
bernardusrendy | 0:96a0fbdff740 | 78 | } |
bernardusrendy | 0:96a0fbdff740 | 79 | case (4): |
bernardusrendy | 0:96a0fbdff740 | 80 | { |
bernardusrendy | 1:ed6ad03b86e4 | 81 | // Mundur |
bernardusrendy | 1:ed6ad03b86e4 | 82 | motorDepan.speed(-0.2); |
bernardusrendy | 1:ed6ad03b86e4 | 83 | motorBelakang.speed(-0.2); |
bernardusrendy | 0:96a0fbdff740 | 84 | break; |
bernardusrendy | 0:96a0fbdff740 | 85 | } |
bernardusrendy | 0:96a0fbdff740 | 86 | default : |
bernardusrendy | 0:96a0fbdff740 | 87 | { |
bernardusrendy | 1:ed6ad03b86e4 | 88 | break; |
bernardusrendy | 0:96a0fbdff740 | 89 | } |
bernardusrendy | 0:96a0fbdff740 | 90 | } |
bernardusrendy | 1:ed6ad03b86e4 | 91 | } |
bernardusrendy | 0:96a0fbdff740 | 92 | /*********************************************************/ |
bernardusrendy | 0:96a0fbdff740 | 93 | /* Main Function */ |
bernardusrendy | 0:96a0fbdff740 | 94 | /*********************************************************/ |
bernardusrendy | 0:96a0fbdff740 | 95 | |
bernardusrendy | 0:96a0fbdff740 | 96 | int main (void) |
bernardusrendy | 0:96a0fbdff740 | 97 | { |
bernardusrendy | 0:96a0fbdff740 | 98 | // Set baud rate - 115200 |
bernardusrendy | 0:96a0fbdff740 | 99 | joystick.setup(); |
bernardusrendy | 0:96a0fbdff740 | 100 | pc.baud(115200); |
bernardusrendy | 0:96a0fbdff740 | 101 | wait_ms(1000); |
bernardusrendy | 1:ed6ad03b86e4 | 102 | int case_joy; |
bernardusrendy | 0:96a0fbdff740 | 103 | // startMillis(); |
bernardusrendy | 0:96a0fbdff740 | 104 | while(1) |
bernardusrendy | 0:96a0fbdff740 | 105 | { |
bernardusrendy | 0:96a0fbdff740 | 106 | // Interrupt Serial |
bernardusrendy | 0:96a0fbdff740 | 107 | joystick.idle(); |
bernardusrendy | 0:96a0fbdff740 | 108 | if(joystick.readable()) |
bernardusrendy | 0:96a0fbdff740 | 109 | { |
bernardusrendy | 0:96a0fbdff740 | 110 | // Panggil fungsi pembacaan joystik |
bernardusrendy | 0:96a0fbdff740 | 111 | joystick.baca_data(); |
bernardusrendy | 0:96a0fbdff740 | 112 | // Panggil fungsi pengolahan data joystik |
bernardusrendy | 0:96a0fbdff740 | 113 | joystick.olah_data(); |
bernardusrendy | 0:96a0fbdff740 | 114 | // Masuk ke case joystick |
bernardusrendy | 0:96a0fbdff740 | 115 | case_joy = case_joystick(); |
bernardusrendy | 0:96a0fbdff740 | 116 | //pc.printf("%d\n",case_joy); |
bernardusrendy | 1:ed6ad03b86e4 | 117 | aktuator(case_joy); |
bernardusrendy | 0:96a0fbdff740 | 118 | } |
bernardusrendy | 0:96a0fbdff740 | 119 | else |
bernardusrendy | 0:96a0fbdff740 | 120 | { |
bernardusrendy | 0:96a0fbdff740 | 121 | joystick.idle(); |
bernardusrendy | 0:96a0fbdff740 | 122 | } |
bernardusrendy | 0:96a0fbdff740 | 123 | |
bernardusrendy | 0:96a0fbdff740 | 124 | // if(millis() - previousMillis5 >= 400) |
bernardusrendy | 0:96a0fbdff740 | 125 | // { |
bernardusrendy | 0:96a0fbdff740 | 126 | // display.write(0,((int) rpm2) / 10); |
bernardusrendy | 0:96a0fbdff740 | 127 | // display.write(1,((int)rpm2) % 10); |
bernardusrendy | 0:96a0fbdff740 | 128 | // display.write(2, (int)target_rpm2 / 10); |
bernardusrendy | 0:96a0fbdff740 | 129 | // display.write(3, (int)target_rpm2 % 10); |
bernardusrendy | 0:96a0fbdff740 | 130 | // display.setColon(true); |
bernardusrendy | 0:96a0fbdff740 | 131 | // |
bernardusrendy | 0:96a0fbdff740 | 132 | // previousMillis5 = millis(); |
bernardusrendy | 0:96a0fbdff740 | 133 | // } |
bernardusrendy | 0:96a0fbdff740 | 134 | |
bernardusrendy | 0:96a0fbdff740 | 135 | } |
bernardusrendy | 0:96a0fbdff740 | 136 | |
bernardusrendy | 0:96a0fbdff740 | 137 | } |