![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final Project
Dependencies: mbed mbed-rtos Motor
main.cpp@0:24e231906adc, 2020-04-26 (annotated)
- Committer:
- johnnykang0905
- Date:
- Sun Apr 26 18:19:47 2020 +0000
- Revision:
- 0:24e231906adc
Written by Eduard Shuvaev
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnnykang0905 | 0:24e231906adc | 1 | #include "mbed.h" |
johnnykang0905 | 0:24e231906adc | 2 | #include "Motor.h" |
johnnykang0905 | 0:24e231906adc | 3 | #include "rtos.h" |
johnnykang0905 | 0:24e231906adc | 4 | |
johnnykang0905 | 0:24e231906adc | 5 | |
johnnykang0905 | 0:24e231906adc | 6 | int count = 0; |
johnnykang0905 | 0:24e231906adc | 7 | int status = 0; |
johnnykang0905 | 0:24e231906adc | 8 | |
johnnykang0905 | 0:24e231906adc | 9 | Serial blue(p28,p27); |
johnnykang0905 | 0:24e231906adc | 10 | Motor m1(p25, p7, p8); // pwm, fwd, rev |
johnnykang0905 | 0:24e231906adc | 11 | Motor m2(p26, p5, p6); // pwm, fwd, rev |
johnnykang0905 | 0:24e231906adc | 12 | |
johnnykang0905 | 0:24e231906adc | 13 | |
johnnykang0905 | 0:24e231906adc | 14 | |
johnnykang0905 | 0:24e231906adc | 15 | // IR reciever connected to pin 14 |
johnnykang0905 | 0:24e231906adc | 16 | RawSerial device(p13, p14); |
johnnykang0905 | 0:24e231906adc | 17 | |
johnnykang0905 | 0:24e231906adc | 18 | |
johnnykang0905 | 0:24e231906adc | 19 | //speaker |
johnnykang0905 | 0:24e231906adc | 20 | PwmOut speaker (p21); |
johnnykang0905 | 0:24e231906adc | 21 | |
johnnykang0905 | 0:24e231906adc | 22 | // LED inctating and testing functionality |
johnnykang0905 | 0:24e231906adc | 23 | DigitalOut myled1(LED1); |
johnnykang0905 | 0:24e231906adc | 24 | DigitalOut myled2(LED2); |
johnnykang0905 | 0:24e231906adc | 25 | DigitalOut myled3(LED3); |
johnnykang0905 | 0:24e231906adc | 26 | DigitalOut myled4(LED4); |
johnnykang0905 | 0:24e231906adc | 27 | |
johnnykang0905 | 0:24e231906adc | 28 | |
johnnykang0905 | 0:24e231906adc | 29 | // RC control using Bluetooth and BlueFruit App |
johnnykang0905 | 0:24e231906adc | 30 | void bluetooth_thread(void const *args) |
johnnykang0905 | 0:24e231906adc | 31 | { |
johnnykang0905 | 0:24e231906adc | 32 | while(1) { |
johnnykang0905 | 0:24e231906adc | 33 | char bnum=0; |
johnnykang0905 | 0:24e231906adc | 34 | char bhit=0; |
johnnykang0905 | 0:24e231906adc | 35 | if(status == 0) |
johnnykang0905 | 0:24e231906adc | 36 | { if (blue.getc()=='!') { |
johnnykang0905 | 0:24e231906adc | 37 | if (blue.getc()=='B') { //button data packet |
johnnykang0905 | 0:24e231906adc | 38 | bnum = blue.getc(); //button number |
johnnykang0905 | 0:24e231906adc | 39 | bhit = blue.getc(); //1=hit, 0=release |
johnnykang0905 | 0:24e231906adc | 40 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
johnnykang0905 | 0:24e231906adc | 41 | // for case 1 - 4 (button 1 - 4) could be added additional functionality |
johnnykang0905 | 0:24e231906adc | 42 | switch (bnum) { |
johnnykang0905 | 0:24e231906adc | 43 | case '5': //button 5 (FORWARD) |
johnnykang0905 | 0:24e231906adc | 44 | if (bhit=='1') { |
johnnykang0905 | 0:24e231906adc | 45 | m1.speed(0.5); |
johnnykang0905 | 0:24e231906adc | 46 | m2.speed(0.55); |
johnnykang0905 | 0:24e231906adc | 47 | } else { |
johnnykang0905 | 0:24e231906adc | 48 | m1.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 49 | m2.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 50 | } |
johnnykang0905 | 0:24e231906adc | 51 | break; |
johnnykang0905 | 0:24e231906adc | 52 | case '6': //button 6 (REVERSE) |
johnnykang0905 | 0:24e231906adc | 53 | if (bhit=='1') { |
johnnykang0905 | 0:24e231906adc | 54 | m1.speed(-0.5); |
johnnykang0905 | 0:24e231906adc | 55 | m2.speed(-0.54); |
johnnykang0905 | 0:24e231906adc | 56 | } else { |
johnnykang0905 | 0:24e231906adc | 57 | m1.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 58 | m2.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 59 | } |
johnnykang0905 | 0:24e231906adc | 60 | break; |
johnnykang0905 | 0:24e231906adc | 61 | case '7': //button 7 (LEFT) |
johnnykang0905 | 0:24e231906adc | 62 | if (bhit=='1') { |
johnnykang0905 | 0:24e231906adc | 63 | m1.speed(-0.5); |
johnnykang0905 | 0:24e231906adc | 64 | m2.speed(0.5); |
johnnykang0905 | 0:24e231906adc | 65 | } else { |
johnnykang0905 | 0:24e231906adc | 66 | m1.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 67 | m2.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 68 | } |
johnnykang0905 | 0:24e231906adc | 69 | break; |
johnnykang0905 | 0:24e231906adc | 70 | case '8': //button 8 (RIGHT) |
johnnykang0905 | 0:24e231906adc | 71 | if (bhit=='1') { |
johnnykang0905 | 0:24e231906adc | 72 | m1.speed(0.5); |
johnnykang0905 | 0:24e231906adc | 73 | m2.speed(-0.54); |
johnnykang0905 | 0:24e231906adc | 74 | } else { |
johnnykang0905 | 0:24e231906adc | 75 | m1.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 76 | m2.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 77 | } |
johnnykang0905 | 0:24e231906adc | 78 | break; |
johnnykang0905 | 0:24e231906adc | 79 | default: |
johnnykang0905 | 0:24e231906adc | 80 | break; |
johnnykang0905 | 0:24e231906adc | 81 | } |
johnnykang0905 | 0:24e231906adc | 82 | } |
johnnykang0905 | 0:24e231906adc | 83 | } |
johnnykang0905 | 0:24e231906adc | 84 | } |
johnnykang0905 | 0:24e231906adc | 85 | } |
johnnykang0905 | 0:24e231906adc | 86 | |
johnnykang0905 | 0:24e231906adc | 87 | // LED 4 blinking to indicate that robot is shot and could not move |
johnnykang0905 | 0:24e231906adc | 88 | else if (status == 1) |
johnnykang0905 | 0:24e231906adc | 89 | { |
johnnykang0905 | 0:24e231906adc | 90 | myled4 = !myled4; |
johnnykang0905 | 0:24e231906adc | 91 | m1.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 92 | m2.speed(0.0); |
johnnykang0905 | 0:24e231906adc | 93 | |
johnnykang0905 | 0:24e231906adc | 94 | } |
johnnykang0905 | 0:24e231906adc | 95 | |
johnnykang0905 | 0:24e231906adc | 96 | Thread::wait(200); |
johnnykang0905 | 0:24e231906adc | 97 | |
johnnykang0905 | 0:24e231906adc | 98 | } |
johnnykang0905 | 0:24e231906adc | 99 | } |
johnnykang0905 | 0:24e231906adc | 100 | |
johnnykang0905 | 0:24e231906adc | 101 | // play sound and blinking |
johnnykang0905 | 0:24e231906adc | 102 | void blink_sound(void const *args) { |
johnnykang0905 | 0:24e231906adc | 103 | |
johnnykang0905 | 0:24e231906adc | 104 | while (1){ |
johnnykang0905 | 0:24e231906adc | 105 | |
johnnykang0905 | 0:24e231906adc | 106 | if (status == 1) |
johnnykang0905 | 0:24e231906adc | 107 | { |
johnnykang0905 | 0:24e231906adc | 108 | myled1 = !myled1; |
johnnykang0905 | 0:24e231906adc | 109 | speaker =0.9; //90% duty cycle - max volume |
johnnykang0905 | 0:24e231906adc | 110 | Thread::wait(700); |
johnnykang0905 | 0:24e231906adc | 111 | speaker=0.0; // turn off audio |
johnnykang0905 | 0:24e231906adc | 112 | } |
johnnykang0905 | 0:24e231906adc | 113 | |
johnnykang0905 | 0:24e231906adc | 114 | |
johnnykang0905 | 0:24e231906adc | 115 | Thread::wait(500); |
johnnykang0905 | 0:24e231906adc | 116 | } |
johnnykang0905 | 0:24e231906adc | 117 | |
johnnykang0905 | 0:24e231906adc | 118 | } |
johnnykang0905 | 0:24e231906adc | 119 | |
johnnykang0905 | 0:24e231906adc | 120 | |
johnnykang0905 | 0:24e231906adc | 121 | int main() |
johnnykang0905 | 0:24e231906adc | 122 | { |
johnnykang0905 | 0:24e231906adc | 123 | |
johnnykang0905 | 0:24e231906adc | 124 | Thread thread1(bluetooth_thread); |
johnnykang0905 | 0:24e231906adc | 125 | Thread thread2(blink_sound); |
johnnykang0905 | 0:24e231906adc | 126 | device.baud(2400); |
johnnykang0905 | 0:24e231906adc | 127 | speaker.period(1.0/350.0); // 350hz period |
johnnykang0905 | 0:24e231906adc | 128 | |
johnnykang0905 | 0:24e231906adc | 129 | while(1) { |
johnnykang0905 | 0:24e231906adc | 130 | |
johnnykang0905 | 0:24e231906adc | 131 | |
johnnykang0905 | 0:24e231906adc | 132 | if(device.readable()) |
johnnykang0905 | 0:24e231906adc | 133 | { |
johnnykang0905 | 0:24e231906adc | 134 | device.getc(); |
johnnykang0905 | 0:24e231906adc | 135 | myled2 = 1; |
johnnykang0905 | 0:24e231906adc | 136 | status = 1; |
johnnykang0905 | 0:24e231906adc | 137 | wait(1); |
johnnykang0905 | 0:24e231906adc | 138 | myled2 = 0; |
johnnykang0905 | 0:24e231906adc | 139 | device.putc(0); |
johnnykang0905 | 0:24e231906adc | 140 | } |
johnnykang0905 | 0:24e231906adc | 141 | |
johnnykang0905 | 0:24e231906adc | 142 | // after few cycles the robot rebooting |
johnnykang0905 | 0:24e231906adc | 143 | if(device.readable() == 0) |
johnnykang0905 | 0:24e231906adc | 144 | { |
johnnykang0905 | 0:24e231906adc | 145 | status = 0; |
johnnykang0905 | 0:24e231906adc | 146 | } |
johnnykang0905 | 0:24e231906adc | 147 | |
johnnykang0905 | 0:24e231906adc | 148 | Thread::wait(100); |
johnnykang0905 | 0:24e231906adc | 149 | } |
johnnykang0905 | 0:24e231906adc | 150 | |
johnnykang0905 | 0:24e231906adc | 151 | } |
johnnykang0905 | 0:24e231906adc | 152 |