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@3:9febeec1f271, 2022-11-15 (annotated)
- Committer:
- yjaafar3
- Date:
- Tue Nov 15 22:58:30 2022 +0000
- Revision:
- 3:9febeec1f271
- Parent:
- 2:002f2def130e
- Child:
- 5:e5bf798e0522
Added turret functionality
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" |
| yjaafar3 | 3:9febeec1f271 | 4 | #include "Servo.h" |
| cwang776 | 0:16c007acb3a2 | 5 | |
| yjaafar3 | 2:002f2def130e | 6 | Serial blue(p28, p27); |
| yjaafar3 | 3:9febeec1f271 | 7 | Servo turret(p24); |
| cwang776 | 0:16c007acb3a2 | 8 | |
| yjaafar3 | 2:002f2def130e | 9 | volatile float speed_left = 0; |
| yjaafar3 | 2:002f2def130e | 10 | volatile float speed_right = 0; |
| yjaafar3 | 2:002f2def130e | 11 | volatile float turret_delta = 0; |
| yjaafar3 | 2:002f2def130e | 12 | volatile bool fire = false; |
| cwang776 | 0:16c007acb3a2 | 13 | |
| cwang776 | 0:16c007acb3a2 | 14 | |
| yjaafar3 | 2:002f2def130e | 15 | Mutex drive_mutex; |
| yjaafar3 | 2:002f2def130e | 16 | Mutex turret_mutex; |
| yjaafar3 | 2:002f2def130e | 17 | Mutex fire_mutex; |
| yjaafar3 | 2:002f2def130e | 18 | |
| yjaafar3 | 2:002f2def130e | 19 | Thread thread_drive; |
| yjaafar3 | 2:002f2def130e | 20 | Thread thread_turret; |
| yjaafar3 | 2:002f2def130e | 21 | Thread thread_fire; |
| yjaafar3 | 2:002f2def130e | 22 | |
| yjaafar3 | 2:002f2def130e | 23 | void input_loop(); |
| yjaafar3 | 2:002f2def130e | 24 | void set_speed(float l, float r); |
| yjaafar3 | 2:002f2def130e | 25 | void set_turret_delta(float delta); |
| yjaafar3 | 2:002f2def130e | 26 | void fire_cannon(bool f); |
| yjaafar3 | 2:002f2def130e | 27 | |
| yjaafar3 | 2:002f2def130e | 28 | void input_thread() { |
| yjaafar3 | 2:002f2def130e | 29 | |
| cwang776 | 0:16c007acb3a2 | 30 | } |
| cwang776 | 0:16c007acb3a2 | 31 | |
| yjaafar3 | 2:002f2def130e | 32 | void drive_thread() { |
| yjaafar3 | 2:002f2def130e | 33 | |
| yjaafar3 | 2:002f2def130e | 34 | } |
| cwang776 | 0:16c007acb3a2 | 35 | |
| yjaafar3 | 2:002f2def130e | 36 | void turret_thread() { |
| yjaafar3 | 3:9febeec1f271 | 37 | while (1) { |
| yjaafar3 | 3:9febeec1f271 | 38 | turret_mutex.lock(); |
| yjaafar3 | 3:9febeec1f271 | 39 | if (turret + turret_delta < 0) { |
| yjaafar3 | 3:9febeec1f271 | 40 | turret = 1; |
| yjaafar3 | 3:9febeec1f271 | 41 | } else if (turret + turret_delta > 1){ |
| yjaafar3 | 3:9febeec1f271 | 42 | turret = 0; |
| yjaafar3 | 3:9febeec1f271 | 43 | } else { |
| yjaafar3 | 3:9febeec1f271 | 44 | turret = turret + turret_delta; |
| yjaafar3 | 3:9febeec1f271 | 45 | } |
| yjaafar3 | 3:9febeec1f271 | 46 | turret_mutex.unlock(); |
| yjaafar3 | 3:9febeec1f271 | 47 | Thread::wait(50); |
| yjaafar3 | 3:9febeec1f271 | 48 | } |
| cwang776 | 0:16c007acb3a2 | 49 | } |
| yjaafar3 | 2:002f2def130e | 50 | |
| yjaafar3 | 2:002f2def130e | 51 | void fire_thread() { |
| yjaafar3 | 2:002f2def130e | 52 | |
| yjaafar3 | 2:002f2def130e | 53 | } |
| yjaafar3 | 2:002f2def130e | 54 | |
| cwang776 | 0:16c007acb3a2 | 55 | |
| cwang776 | 0:16c007acb3a2 | 56 | int main() { |
| yjaafar3 | 2:002f2def130e | 57 | thread_drive.start(drive_thread); |
| yjaafar3 | 2:002f2def130e | 58 | thread_turret.start(turret_thread); |
| yjaafar3 | 2:002f2def130e | 59 | thread_fire.start(fire_thread); |
| yjaafar3 | 2:002f2def130e | 60 | |
| yjaafar3 | 2:002f2def130e | 61 | input_loop(); |
| cwang776 | 0:16c007acb3a2 | 62 | } |
| yjaafar3 | 2:002f2def130e | 63 | |
| yjaafar3 | 2:002f2def130e | 64 | void input_loop() { |
| yjaafar3 | 2:002f2def130e | 65 | char bnum=0; |
| yjaafar3 | 2:002f2def130e | 66 | char bhit=0; |
| yjaafar3 | 2:002f2def130e | 67 | while(1) { |
| yjaafar3 | 2:002f2def130e | 68 | if (blue.getc()=='!') { |
| yjaafar3 | 2:002f2def130e | 69 | if (blue.getc()=='B') { //button data packet |
| yjaafar3 | 2:002f2def130e | 70 | bnum = blue.getc(); //button number |
| yjaafar3 | 2:002f2def130e | 71 | bhit = blue.getc(); //1=hit, 0=release |
| yjaafar3 | 2:002f2def130e | 72 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
| yjaafar3 | 2:002f2def130e | 73 | switch(bnum) { |
| yjaafar3 | 2:002f2def130e | 74 | case '1': //Turret Left Button |
| yjaafar3 | 2:002f2def130e | 75 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 76 | set_turret_delta(-0.05); |
| yjaafar3 | 2:002f2def130e | 77 | } else { |
| yjaafar3 | 2:002f2def130e | 78 | set_turret_delta(0); |
| yjaafar3 | 2:002f2def130e | 79 | } |
| yjaafar3 | 2:002f2def130e | 80 | break; |
| yjaafar3 | 2:002f2def130e | 81 | case '2': //Turret Right Button |
| yjaafar3 | 2:002f2def130e | 82 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 83 | set_turret_delta(0.05); |
| yjaafar3 | 2:002f2def130e | 84 | } else { |
| yjaafar3 | 2:002f2def130e | 85 | set_turret_delta(0); |
| yjaafar3 | 2:002f2def130e | 86 | } |
| yjaafar3 | 2:002f2def130e | 87 | break; |
| yjaafar3 | 2:002f2def130e | 88 | case '3': //Fire Button |
| yjaafar3 | 2:002f2def130e | 89 | if (bhit == '1') { |
| yjaafar3 | 2:002f2def130e | 90 | fire_cannon(true); |
| yjaafar3 | 2:002f2def130e | 91 | } else { |
| yjaafar3 | 2:002f2def130e | 92 | fire_cannon(false); |
| yjaafar3 | 2:002f2def130e | 93 | } |
| yjaafar3 | 2:002f2def130e | 94 | break; |
| yjaafar3 | 2:002f2def130e | 95 | case '5': //button 5 up arrow |
| yjaafar3 | 2:002f2def130e | 96 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 97 | set_speed(0.5, 0.5); |
| yjaafar3 | 2:002f2def130e | 98 | } else { |
| yjaafar3 | 2:002f2def130e | 99 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 100 | } |
| yjaafar3 | 2:002f2def130e | 101 | break; |
| yjaafar3 | 2:002f2def130e | 102 | case '6': //button 6 down arrow |
| yjaafar3 | 2:002f2def130e | 103 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 104 | set_speed(-0.5, -0.5); |
| yjaafar3 | 2:002f2def130e | 105 | } else { |
| yjaafar3 | 2:002f2def130e | 106 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 107 | } |
| yjaafar3 | 2:002f2def130e | 108 | break; |
| yjaafar3 | 2:002f2def130e | 109 | case '7': //button 7 left arrow |
| yjaafar3 | 2:002f2def130e | 110 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 111 | set_speed(-0.5, 0.5); |
| yjaafar3 | 2:002f2def130e | 112 | } else { |
| yjaafar3 | 2:002f2def130e | 113 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 114 | } |
| yjaafar3 | 2:002f2def130e | 115 | break; |
| yjaafar3 | 2:002f2def130e | 116 | case '8': //button 8 right arrow |
| yjaafar3 | 2:002f2def130e | 117 | if (bhit=='1') { |
| yjaafar3 | 2:002f2def130e | 118 | set_speed(0.5, -0.5); |
| yjaafar3 | 2:002f2def130e | 119 | } else { |
| yjaafar3 | 2:002f2def130e | 120 | set_speed(0, 0); |
| yjaafar3 | 2:002f2def130e | 121 | } |
| yjaafar3 | 2:002f2def130e | 122 | break; |
| yjaafar3 | 2:002f2def130e | 123 | default: |
| yjaafar3 | 2:002f2def130e | 124 | break; |
| yjaafar3 | 2:002f2def130e | 125 | } |
| yjaafar3 | 2:002f2def130e | 126 | } |
| yjaafar3 | 2:002f2def130e | 127 | } |
| yjaafar3 | 2:002f2def130e | 128 | } |
| yjaafar3 | 3:9febeec1f271 | 129 | Thread::wait(50); |
| yjaafar3 | 2:002f2def130e | 130 | } |
| yjaafar3 | 2:002f2def130e | 131 | } |
| yjaafar3 | 2:002f2def130e | 132 | |
| yjaafar3 | 2:002f2def130e | 133 | void set_speed(float left, float right) { |
| yjaafar3 | 2:002f2def130e | 134 | drive_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 135 | speed_left = left; |
| yjaafar3 | 2:002f2def130e | 136 | speed_right = right; |
| yjaafar3 | 2:002f2def130e | 137 | drive_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 138 | } |
| yjaafar3 | 2:002f2def130e | 139 | |
| yjaafar3 | 2:002f2def130e | 140 | void set_turret_delta(float delta) { |
| yjaafar3 | 2:002f2def130e | 141 | turret_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 142 | turret_delta = delta; |
| yjaafar3 | 2:002f2def130e | 143 | turret_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 144 | } |
| yjaafar3 | 2:002f2def130e | 145 | |
| yjaafar3 | 2:002f2def130e | 146 | void fire_cannon(bool f) { |
| yjaafar3 | 2:002f2def130e | 147 | fire_mutex.lock(); |
| yjaafar3 | 2:002f2def130e | 148 | fire = f; |
| yjaafar3 | 2:002f2def130e | 149 | fire_mutex.unlock(); |
| yjaafar3 | 2:002f2def130e | 150 | } |
