Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed Servo Motor SoftPWM
main.cpp@2:002f2def130e, 2022-11-15 (annotated)
- Committer:
- yjaafar3
- Date:
- Tue Nov 15 22:46:55 2022 +0000
- Revision:
- 2:002f2def130e
- Parent:
- 1:965e202877f7
- Child:
- 3:9febeec1f271
Added input read functionality and volatile inputs
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cwang776 | 0:16c007acb3a2 | 1 | #include "mbed.h" |
| cwang776 | 0:16c007acb3a2 | 2 | #include "rtos.h" |
| yjaafar3 | 2:002f2def130e | 3 | #include "Serial.h" |
| cwang776 | 0:16c007acb3a2 | 4 | |
| yjaafar3 | 2:002f2def130e | 5 | Serial blue(p28, p27); |
| yjaafar3 | 2:002f2def130e | 6 | |
| cwang776 | 0:16c007acb3a2 | 7 | |
| yjaafar3 | 2:002f2def130e | 8 | volatile float speed_left = 0; |
| yjaafar3 | 2:002f2def130e | 9 | volatile float speed_right = 0; |
| yjaafar3 | 2:002f2def130e | 10 | volatile float turret_delta = 0; |
| yjaafar3 | 2:002f2def130e | 11 | volatile bool fire = false; |
| cwang776 | 0:16c007acb3a2 | 12 | |
| cwang776 | 0:16c007acb3a2 | 13 | |
| yjaafar3 | 2:002f2def130e | 14 | Mutex drive_mutex; |
| yjaafar3 | 2:002f2def130e | 15 | Mutex turret_mutex; |
| yjaafar3 | 2:002f2def130e | 16 | Mutex fire_mutex; |
| yjaafar3 | 2:002f2def130e | 17 | |
| yjaafar3 | 2:002f2def130e | 18 | Thread thread_drive; |
| yjaafar3 | 2:002f2def130e | 19 | Thread thread_turret; |
| yjaafar3 | 2:002f2def130e | 20 | Thread thread_fire; |
| yjaafar3 | 2:002f2def130e | 21 | |
| yjaafar3 | 2:002f2def130e | 22 | void input_loop(); |
| yjaafar3 | 2:002f2def130e | 23 | void set_speed(float l, float r); |
| yjaafar3 | 2:002f2def130e | 24 | void set_turret_delta(float delta); |
| yjaafar3 | 2:002f2def130e | 25 | void fire_cannon(bool f); |
| yjaafar3 | 2:002f2def130e | 26 | |
| yjaafar3 | 2:002f2def130e | 27 | void input_thread() { |
| yjaafar3 | 2:002f2def130e | 28 | |
| cwang776 | 0:16c007acb3a2 | 29 | } |
| cwang776 | 0:16c007acb3a2 | 30 | |
| yjaafar3 | 2:002f2def130e | 31 | void drive_thread() { |
| yjaafar3 | 2:002f2def130e | 32 | |
| yjaafar3 | 2:002f2def130e | 33 | } |
| cwang776 | 0:16c007acb3a2 | 34 | |
| yjaafar3 | 2:002f2def130e | 35 | void turret_thread() { |
| yjaafar3 | 2:002f2def130e | 36 | |
| cwang776 | 0:16c007acb3a2 | 37 | } |
| yjaafar3 | 2:002f2def130e | 38 | |
| yjaafar3 | 2:002f2def130e | 39 | void fire_thread() { |
| yjaafar3 | 2:002f2def130e | 40 | |
| yjaafar3 | 2:002f2def130e | 41 | } |
| yjaafar3 | 2:002f2def130e | 42 | |
| cwang776 | 0:16c007acb3a2 | 43 | |
| cwang776 | 0:16c007acb3a2 | 44 | int main() { |
| bzhu792 | 1:965e202877f7 | 45 | //test |
| cwang776 | 0:16c007acb3a2 | 46 | |
| yjaafar3 | 2:002f2def130e | 47 | //thread1.start(some_thread); |
| yjaafar3 | 2:002f2def130e | 48 | //thread2.start(other_thread); |
| yjaafar3 | 2:002f2def130e | 49 | thread_drive.start(drive_thread); |
| yjaafar3 | 2:002f2def130e | 50 | thread_turret.start(turret_thread); |
| yjaafar3 | 2:002f2def130e | 51 | thread_fire.start(fire_thread); |
| yjaafar3 | 2:002f2def130e | 52 | |
| yjaafar3 | 2:002f2def130e | 53 | input_loop(); |
| cwang776 | 0:16c007acb3a2 | 54 | } |
| yjaafar3 | 2:002f2def130e | 55 | |
| yjaafar3 | 2:002f2def130e | 56 | void input_loop() { |
| yjaafar3 | 2:002f2def130e | 57 | char bnum=0; |
| yjaafar3 | 2:002f2def130e | 58 | char bhit=0; |
| yjaafar3 | 2:002f2def130e | 59 | while(1) { |
| yjaafar3 | 2:002f2def130e | 60 | if (blue.getc()=='!') { |
| yjaafar3 | 2:002f2def130e | 61 | if (blue.getc()=='B') { //button data packet |
| yjaafar3 | 2:002f2def130e | 62 | bnum = blue.getc(); //button number |
| yjaafar3 | 2:002f2def130e | 63 | bhit = blue.getc(); //1=hit, 0=release |
| yjaafar3 | 2:002f2def130e | 64 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
| yjaafar3 | 2:002f2def130e | 65 | switch(bnum) { |
| yjaafar3 | 2:002f2def130e | 66 | case '1': //Turret Left Button |
| yjaafar3 | 2:002f2def130e | 67 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 68 | set_turret_delta(-0.05); |
| yjaafar3 | 2:002f2def130e | 69 | } else { |
| yjaafar3 | 2:002f2def130e | 70 | set_turret_delta(0); |
| yjaafar3 | 2:002f2def130e | 71 | } |
| yjaafar3 | 2:002f2def130e | 72 | break; |
| yjaafar3 | 2:002f2def130e | 73 | case '2': //Turret Right Button |
| yjaafar3 | 2:002f2def130e | 74 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 75 | set_turret_delta(0.05); |
| yjaafar3 | 2:002f2def130e | 76 | } else { |
| yjaafar3 | 2:002f2def130e | 77 | set_turret_delta(0); |
| yjaafar3 | 2:002f2def130e | 78 | } |
| yjaafar3 | 2:002f2def130e | 79 | break; |
| yjaafar3 | 2:002f2def130e | 80 | case '3': //Fire Button |
| yjaafar3 | 2:002f2def130e | 81 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 82 | fire_cannon(true); |
| yjaafar3 | 2:002f2def130e | 83 | } else { |
| yjaafar3 | 2:002f2def130e | 84 | fire_cannon(false); |
| yjaafar3 | 2:002f2def130e | 85 | } |
| yjaafar3 | 2:002f2def130e | 86 | break; |
| yjaafar3 | 2:002f2def130e | 87 | case '5': //button 5 up arrow |
| yjaafar3 | 2:002f2def130e | 88 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 89 | set_speed(0.5, 0.5); |
| yjaafar3 | 2:002f2def130e | 90 | } else { |
| yjaafar3 | 2:002f2def130e | 91 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 92 | } |
| yjaafar3 | 2:002f2def130e | 93 | break; |
| yjaafar3 | 2:002f2def130e | 94 | case '6': //button 6 down arrow |
| yjaafar3 | 2:002f2def130e | 95 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 96 | set_speed(-0.5, -0.5); |
| yjaafar3 | 2:002f2def130e | 97 | } else { |
| yjaafar3 | 2:002f2def130e | 98 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 99 | } |
| yjaafar3 | 2:002f2def130e | 100 | break; |
| yjaafar3 | 2:002f2def130e | 101 | case '7': //button 7 left arrow |
| yjaafar3 | 2:002f2def130e | 102 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 103 | set_speed(-0.5, 0.5); |
| yjaafar3 | 2:002f2def130e | 104 | } else { |
| yjaafar3 | 2:002f2def130e | 105 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 106 | } |
| yjaafar3 | 2:002f2def130e | 107 | break; |
| yjaafar3 | 2:002f2def130e | 108 | case '8': //button 8 right arrow |
| yjaafar3 | 2:002f2def130e | 109 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 110 | set_speed(0.5, -0.5); |
| yjaafar3 | 2:002f2def130e | 111 | } else { |
| yjaafar3 | 2:002f2def130e | 112 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 113 | } |
| yjaafar3 | 2:002f2def130e | 114 | break; |
| yjaafar3 | 2:002f2def130e | 115 | default: |
| yjaafar3 | 2:002f2def130e | 116 | break; |
| yjaafar3 | 2:002f2def130e | 117 | } |
| yjaafar3 | 2:002f2def130e | 118 | } |
| yjaafar3 | 2:002f2def130e | 119 | } |
| yjaafar3 | 2:002f2def130e | 120 | } |
| yjaafar3 | 2:002f2def130e | 121 | } |
| yjaafar3 | 2:002f2def130e | 122 | } |
| yjaafar3 | 2:002f2def130e | 123 | |
| yjaafar3 | 2:002f2def130e | 124 | void set_speed(float left, float right) { |
| yjaafar3 | 2:002f2def130e | 125 | drive_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 126 | speed_left = left; |
| yjaafar3 | 2:002f2def130e | 127 | speed_right = right; |
| yjaafar3 | 2:002f2def130e | 128 | drive_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 129 | } |
| yjaafar3 | 2:002f2def130e | 130 | |
| yjaafar3 | 2:002f2def130e | 131 | void set_turret_delta(float delta) { |
| yjaafar3 | 2:002f2def130e | 132 | turret_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 133 | turret_delta = delta; |
| yjaafar3 | 2:002f2def130e | 134 | turret_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 135 | } |
| yjaafar3 | 2:002f2def130e | 136 | |
| yjaafar3 | 2:002f2def130e | 137 | void fire_cannon(bool f) { |
| yjaafar3 | 2:002f2def130e | 138 | fire_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 139 | fire = f; |
| yjaafar3 | 2:002f2def130e | 140 | fire_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 141 | } |
