Cynthia Wang / Mbed 2 deprecated ECE4180-FinalProject

Dependencies:   mbed-rtos mbed Servo Motor SoftPWM

Committer:
bzhu792
Date:
Thu Nov 17 22:17:31 2022 +0000
Revision:
9:0b045f2791ea
Parent:
8:c54f1cfcbfe0
Child:
10:3deaf72fa257
Everything works minus speaker

Who changed what in which revision?

UserRevisionLine numberNew 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"
bzhu792 5:e5bf798e0522 5 #include "Motor.h"
cwang776 8:c54f1cfcbfe0 6 #include "SoftPWM.h"
cwang776 0:16c007acb3a2 7
bzhu792 5:e5bf798e0522 8 RawSerial blue(p9, p10);
bzhu792 5:e5bf798e0522 9 Serial pc(USBTX, USBRX);
cwang776 8:c54f1cfcbfe0 10 SoftPWM servo(p24, true);
bzhu792 5:e5bf798e0522 11 Motor left_motor(p23, p15, p16); // pwm, fwd, rev
bzhu792 5:e5bf798e0522 12 Motor right_motor(p22, p17, p18); // pwm, fwd, rev
cwang776 0:16c007acb3a2 13
cwang776 7:f65ab31f81e7 14 // IR
cwang776 7:f65ab31f81e7 15 Serial ir_device(p13, p14); // tx, rx
cwang776 7:f65ab31f81e7 16 DigitalOut rx_led(LED1); //LED1 and LED2 indicate TX/RX activity for IR
cwang776 7:f65ab31f81e7 17 DigitalOut tx_led(LED2);
cwang776 7:f65ab31f81e7 18 PwmOut IRLED(p21);
cwang776 7:f65ab31f81e7 19
cwang776 7:f65ab31f81e7 20 // Lives LEDs
cwang776 7:f65ab31f81e7 21 BusOut lives_leds(p5, p6, p7);
cwang776 7:f65ab31f81e7 22
cwang776 7:f65ab31f81e7 23 // Speaker
bzhu792 9:0b045f2791ea 24 AnalogOut speaker(p25);
cwang776 7:f65ab31f81e7 25
yjaafar3 2:002f2def130e 26 volatile float speed_left = 0;
yjaafar3 2:002f2def130e 27 volatile float speed_right = 0;
yjaafar3 2:002f2def130e 28 volatile float turret_delta = 0;
yjaafar3 2:002f2def130e 29 volatile bool fire = false;
cwang776 0:16c007acb3a2 30
cwang776 0:16c007acb3a2 31
yjaafar3 2:002f2def130e 32 Thread thread_drive;
yjaafar3 2:002f2def130e 33 Thread thread_turret;
cwang776 7:f65ab31f81e7 34 Thread thread_ir_receive;
yjaafar3 2:002f2def130e 35
yjaafar3 2:002f2def130e 36 void input_loop();
bzhu792 5:e5bf798e0522 37 inline void set_speed(float l, float r);
bzhu792 5:e5bf798e0522 38 inline void set_turret_delta(float delta);
bzhu792 5:e5bf798e0522 39 inline void fire_cannon(bool f);
cwang776 7:f65ab31f81e7 40 inline void decrease_lives();
yjaafar3 2:002f2def130e 41
yjaafar3 2:002f2def130e 42 void input_thread() {
yjaafar3 2:002f2def130e 43
cwang776 0:16c007acb3a2 44 }
cwang776 0:16c007acb3a2 45
yjaafar3 2:002f2def130e 46 void drive_thread() {
bzhu792 5:e5bf798e0522 47 while (1) {
bzhu792 5:e5bf798e0522 48
bzhu792 5:e5bf798e0522 49 left_motor.speed(speed_left);
bzhu792 5:e5bf798e0522 50 right_motor.speed(speed_right);
bzhu792 5:e5bf798e0522 51 //pc.printf("left speed: %f, right speed: %f\n", speed_left, speed_right);
bzhu792 5:e5bf798e0522 52 Thread::wait(100);
bzhu792 5:e5bf798e0522 53 }
yjaafar3 2:002f2def130e 54 }
cwang776 0:16c007acb3a2 55
yjaafar3 2:002f2def130e 56 void turret_thread() {
bzhu792 5:e5bf798e0522 57 float pulsewidth = 0.0015; // servo is between 1 ms and 2 ms
bzhu792 5:e5bf798e0522 58
yjaafar3 3:9febeec1f271 59 while (1) {
bzhu792 5:e5bf798e0522 60 servo.period(0.020); // servo requires a 20ms period
bzhu792 5:e5bf798e0522 61 pulsewidth += turret_delta;
bzhu792 5:e5bf798e0522 62 if (pulsewidth > 0.002) pulsewidth = 0.002;
bzhu792 5:e5bf798e0522 63 else if (pulsewidth < 0.001) pulsewidth = 0.001;
bzhu792 5:e5bf798e0522 64 servo.pulsewidth(pulsewidth);
bzhu792 5:e5bf798e0522 65
cwang776 8:c54f1cfcbfe0 66 Thread::wait(25);
yjaafar3 3:9febeec1f271 67 }
cwang776 0:16c007acb3a2 68 }
yjaafar3 2:002f2def130e 69
cwang776 7:f65ab31f81e7 70 void ir_receive_thread() {
cwang776 7:f65ab31f81e7 71 // IR setup
cwang776 7:f65ab31f81e7 72 //Drive IR LED data pin with 38Khz PWM Carrier
cwang776 7:f65ab31f81e7 73 //Drive IR LED gnd pin with serial TX
bzhu792 9:0b045f2791ea 74 Thread::wait(1000); //Let other PWM threads initialize
cwang776 7:f65ab31f81e7 75 IRLED.period(1.0/38000.0);
cwang776 7:f65ab31f81e7 76 IRLED = 0.5;
cwang776 7:f65ab31f81e7 77 ir_device.baud(2400);
cwang776 7:f65ab31f81e7 78 while(1) {
bzhu792 9:0b045f2791ea 79 while (!ir_device.readable()) Thread::wait(0);
bzhu792 9:0b045f2791ea 80 char c = ir_device.getc();
bzhu792 9:0b045f2791ea 81 if (c == 'f') {
cwang776 7:f65ab31f81e7 82 rx_led = 1;
bzhu792 9:0b045f2791ea 83 pc.putc(c);
cwang776 7:f65ab31f81e7 84 decrease_lives();
cwang776 7:f65ab31f81e7 85 rx_led = 0;
bzhu792 9:0b045f2791ea 86 }
cwang776 7:f65ab31f81e7 87 }
yjaafar3 2:002f2def130e 88 }
yjaafar3 2:002f2def130e 89
cwang776 0:16c007acb3a2 90
cwang776 0:16c007acb3a2 91 int main() {
cwang776 7:f65ab31f81e7 92 lives_leds = 7;
cwang776 7:f65ab31f81e7 93
yjaafar3 2:002f2def130e 94 thread_drive.start(drive_thread);
cwang776 8:c54f1cfcbfe0 95 thread_turret.start(turret_thread);
cwang776 7:f65ab31f81e7 96 thread_ir_receive.start(ir_receive_thread);
yjaafar3 2:002f2def130e 97
yjaafar3 2:002f2def130e 98 input_loop();
cwang776 0:16c007acb3a2 99 }
yjaafar3 2:002f2def130e 100
yjaafar3 2:002f2def130e 101 void input_loop() {
yjaafar3 2:002f2def130e 102 char bnum=0;
yjaafar3 2:002f2def130e 103 char bhit=0;
yjaafar3 2:002f2def130e 104 while(1) {
yjaafar3 2:002f2def130e 105 if (blue.getc()=='!') {
yjaafar3 2:002f2def130e 106 if (blue.getc()=='B') { //button data packet
yjaafar3 2:002f2def130e 107 bnum = blue.getc(); //button number
yjaafar3 2:002f2def130e 108 bhit = blue.getc(); //1=hit, 0=release
yjaafar3 2:002f2def130e 109 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
yjaafar3 2:002f2def130e 110 switch(bnum) {
yjaafar3 2:002f2def130e 111 case '1': //Turret Left Button
yjaafar3 2:002f2def130e 112 if (bhit == '1') {
bzhu792 5:e5bf798e0522 113 set_turret_delta(-0.000025);
yjaafar3 2:002f2def130e 114 } else {
yjaafar3 2:002f2def130e 115 set_turret_delta(0);
yjaafar3 2:002f2def130e 116 }
yjaafar3 2:002f2def130e 117 break;
yjaafar3 2:002f2def130e 118 case '2': //Turret Right Button
yjaafar3 2:002f2def130e 119 if (bhit == '1') {
bzhu792 5:e5bf798e0522 120 set_turret_delta(0.000025);
yjaafar3 2:002f2def130e 121 } else {
yjaafar3 2:002f2def130e 122 set_turret_delta(0);
yjaafar3 2:002f2def130e 123 }
yjaafar3 2:002f2def130e 124 break;
yjaafar3 2:002f2def130e 125 case '3': //Fire Button
yjaafar3 2:002f2def130e 126 if (bhit == '1') {
yjaafar3 2:002f2def130e 127 fire_cannon(true);
yjaafar3 2:002f2def130e 128 } else {
yjaafar3 2:002f2def130e 129 fire_cannon(false);
yjaafar3 2:002f2def130e 130 }
yjaafar3 2:002f2def130e 131 break;
yjaafar3 2:002f2def130e 132 case '5': //button 5 up arrow
yjaafar3 2:002f2def130e 133 if (bhit=='1') {
yjaafar3 2:002f2def130e 134 set_speed(0.5, 0.5);
yjaafar3 2:002f2def130e 135 } else {
yjaafar3 2:002f2def130e 136 set_speed(0, 0);
yjaafar3 2:002f2def130e 137 }
yjaafar3 2:002f2def130e 138 break;
yjaafar3 2:002f2def130e 139 case '6': //button 6 down arrow
yjaafar3 2:002f2def130e 140 if (bhit=='1') {
yjaafar3 2:002f2def130e 141 set_speed(-0.5, -0.5);
yjaafar3 2:002f2def130e 142 } else {
yjaafar3 2:002f2def130e 143 set_speed(0, 0);
yjaafar3 2:002f2def130e 144 }
yjaafar3 2:002f2def130e 145 break;
yjaafar3 2:002f2def130e 146 case '7': //button 7 left arrow
yjaafar3 2:002f2def130e 147 if (bhit=='1') {
yjaafar3 2:002f2def130e 148 set_speed(-0.5, 0.5);
yjaafar3 2:002f2def130e 149 } else {
yjaafar3 2:002f2def130e 150 set_speed(0, 0);
yjaafar3 2:002f2def130e 151 }
yjaafar3 2:002f2def130e 152 break;
yjaafar3 2:002f2def130e 153 case '8': //button 8 right arrow
yjaafar3 2:002f2def130e 154 if (bhit=='1') {
yjaafar3 2:002f2def130e 155 set_speed(0.5, -0.5);
yjaafar3 2:002f2def130e 156 } else {
yjaafar3 2:002f2def130e 157 set_speed(0, 0);
yjaafar3 2:002f2def130e 158 }
yjaafar3 2:002f2def130e 159 break;
yjaafar3 2:002f2def130e 160 default:
yjaafar3 2:002f2def130e 161 break;
yjaafar3 2:002f2def130e 162 }
yjaafar3 2:002f2def130e 163 }
yjaafar3 2:002f2def130e 164 }
yjaafar3 2:002f2def130e 165 }
yjaafar3 3:9febeec1f271 166 Thread::wait(50);
yjaafar3 2:002f2def130e 167 }
yjaafar3 2:002f2def130e 168 }
yjaafar3 2:002f2def130e 169
bzhu792 5:e5bf798e0522 170 inline void set_speed(float left, float right) {
yjaafar3 2:002f2def130e 171 speed_left = left;
yjaafar3 2:002f2def130e 172 speed_right = right;
yjaafar3 2:002f2def130e 173 }
yjaafar3 2:002f2def130e 174
bzhu792 5:e5bf798e0522 175 inline void set_turret_delta(float delta) {
yjaafar3 2:002f2def130e 176 turret_delta = delta;
yjaafar3 2:002f2def130e 177 }
yjaafar3 2:002f2def130e 178
bzhu792 5:e5bf798e0522 179 inline void fire_cannon(bool f) {
cwang776 7:f65ab31f81e7 180 if (f) {
cwang776 7:f65ab31f81e7 181 tx_led = 1;
cwang776 7:f65ab31f81e7 182 ir_device.putc('f');
cwang776 7:f65ab31f81e7 183 tx_led = 0;
cwang776 7:f65ab31f81e7 184 }
cwang776 7:f65ab31f81e7 185 }
cwang776 7:f65ab31f81e7 186
cwang776 7:f65ab31f81e7 187 inline void decrease_lives() {
cwang776 7:f65ab31f81e7 188 if (lives_leds == 7) {
cwang776 7:f65ab31f81e7 189 lives_leds = 3;
cwang776 7:f65ab31f81e7 190 } else if (lives_leds == 3) {
cwang776 7:f65ab31f81e7 191 lives_leds = 1;
cwang776 7:f65ab31f81e7 192 } else {
cwang776 7:f65ab31f81e7 193 lives_leds = 0;
cwang776 7:f65ab31f81e7 194 }
yjaafar3 2:002f2def130e 195 }