the ultimate angry owl

Dependencies:   mbed wave_player Motordriver mbed-rtos SDFileSystem

Committer:
jingyitaro
Date:
Tue Dec 10 02:06:51 2019 +0000
Revision:
0:f2f38faf17d9
The final working angry owl code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jingyitaro 0:f2f38faf17d9 1 #include "mbed.h"
jingyitaro 0:f2f38faf17d9 2 #include "motordriver.h"
jingyitaro 0:f2f38faf17d9 3 #include "rtos.h"
jingyitaro 0:f2f38faf17d9 4 #include "stdio.h"
jingyitaro 0:f2f38faf17d9 5 #include "SDFileSystem.h"
jingyitaro 0:f2f38faf17d9 6 #include "wave_player.h"
jingyitaro 0:f2f38faf17d9 7
jingyitaro 0:f2f38faf17d9 8 volatile bool alarm = false;
jingyitaro 0:f2f38faf17d9 9 DigitalOut led3(LED3);
jingyitaro 0:f2f38faf17d9 10 DigitalOut led4(LED4);
jingyitaro 0:f2f38faf17d9 11
jingyitaro 0:f2f38faf17d9 12 RawSerial pi(USBTX, USBRX);
jingyitaro 0:f2f38faf17d9 13
jingyitaro 0:f2f38faf17d9 14 Motor wingL(p22, p9, p10, 1); // pwm, fwd, rev
jingyitaro 0:f2f38faf17d9 15 Motor wingR(p21, p12, p11, 1);//wings move in opposite directions
jingyitaro 0:f2f38faf17d9 16 //reverse one motor's leads when wiring
jingyitaro 0:f2f38faf17d9 17 Motor wheelL(p24, p14, p13, 1);
jingyitaro 0:f2f38faf17d9 18 Motor wheelR(p25, p16, p15, 1);//wheels move in the same direction
jingyitaro 0:f2f38faf17d9 19 PwmOut led1(p23);
jingyitaro 0:f2f38faf17d9 20 PwmOut led2(p26);
jingyitaro 0:f2f38faf17d9 21
jingyitaro 0:f2f38faf17d9 22 SDFileSystem sd(p5, p6, p7, p8, "sd");
jingyitaro 0:f2f38faf17d9 23 AnalogOut DACout(p18);
jingyitaro 0:f2f38faf17d9 24 wave_player waver(&DACout);
jingyitaro 0:f2f38faf17d9 25 Mutex spk_mutex;
jingyitaro 0:f2f38faf17d9 26 Mutex servo_mutex;
jingyitaro 0:f2f38faf17d9 27 Mutex wing_mutex;
jingyitaro 0:f2f38faf17d9 28 Mutex alrm_mutex;
jingyitaro 0:f2f38faf17d9 29 Thread t2;
jingyitaro 0:f2f38faf17d9 30 Thread t3;
jingyitaro 0:f2f38faf17d9 31 Thread t4;
jingyitaro 0:f2f38faf17d9 32 Thread t5;
jingyitaro 0:f2f38faf17d9 33
jingyitaro 0:f2f38faf17d9 34 InterruptIn interrupt(p27);
jingyitaro 0:f2f38faf17d9 35 bool getAlarm() {
jingyitaro 0:f2f38faf17d9 36 return alarm;
jingyitaro 0:f2f38faf17d9 37 }
jingyitaro 0:f2f38faf17d9 38 void changeAlarm()
jingyitaro 0:f2f38faf17d9 39 {
jingyitaro 0:f2f38faf17d9 40 while(true)
jingyitaro 0:f2f38faf17d9 41 {
jingyitaro 0:f2f38faf17d9 42 alarm = !alarm;
jingyitaro 0:f2f38faf17d9 43 }
jingyitaro 0:f2f38faf17d9 44 }
jingyitaro 0:f2f38faf17d9 45
jingyitaro 0:f2f38faf17d9 46 void flapWings()
jingyitaro 0:f2f38faf17d9 47 {
jingyitaro 0:f2f38faf17d9 48 bool flag;
jingyitaro 0:f2f38faf17d9 49 while(true)
jingyitaro 0:f2f38faf17d9 50 {
jingyitaro 0:f2f38faf17d9 51 flag = getAlarm();
jingyitaro 0:f2f38faf17d9 52 if(flag)
jingyitaro 0:f2f38faf17d9 53 {
jingyitaro 0:f2f38faf17d9 54 for (float s= -1.0; s < 0.0 ; s += 0.001)
jingyitaro 0:f2f38faf17d9 55 {
jingyitaro 0:f2f38faf17d9 56 wingL.speed(s);
jingyitaro 0:f2f38faf17d9 57 wingR.speed(s);
jingyitaro 0:f2f38faf17d9 58 wait(0.001);
jingyitaro 0:f2f38faf17d9 59 }
jingyitaro 0:f2f38faf17d9 60 for (float s= 1.0; s > 0.0 ; s -= 0.001)
jingyitaro 0:f2f38faf17d9 61 {
jingyitaro 0:f2f38faf17d9 62 wingL.speed(s);
jingyitaro 0:f2f38faf17d9 63 wingR.speed(s);
jingyitaro 0:f2f38faf17d9 64 wait(0.001);
jingyitaro 0:f2f38faf17d9 65 }
jingyitaro 0:f2f38faf17d9 66 }
jingyitaro 0:f2f38faf17d9 67 Thread::wait(100);//frequency of cycles of wing movemnets
jingyitaro 0:f2f38faf17d9 68 }
jingyitaro 0:f2f38faf17d9 69 }
jingyitaro 0:f2f38faf17d9 70
jingyitaro 0:f2f38faf17d9 71 void playSound()
jingyitaro 0:f2f38faf17d9 72 {
jingyitaro 0:f2f38faf17d9 73 FILE *wave_file;
jingyitaro 0:f2f38faf17d9 74 bool flag;
jingyitaro 0:f2f38faf17d9 75 while(true)
jingyitaro 0:f2f38faf17d9 76 {
jingyitaro 0:f2f38faf17d9 77 flag = getAlarm();
jingyitaro 0:f2f38faf17d9 78 if(flag)
jingyitaro 0:f2f38faf17d9 79 {
jingyitaro 0:f2f38faf17d9 80 led4 = 1;
jingyitaro 0:f2f38faf17d9 81 spk_mutex.lock();
jingyitaro 0:f2f38faf17d9 82 wave_file=fopen("/sd/sound01.wav","r");
jingyitaro 0:f2f38faf17d9 83 spk_mutex.unlock();
jingyitaro 0:f2f38faf17d9 84
jingyitaro 0:f2f38faf17d9 85 waver.play(wave_file);
jingyitaro 0:f2f38faf17d9 86
jingyitaro 0:f2f38faf17d9 87 spk_mutex.lock();
jingyitaro 0:f2f38faf17d9 88 fclose(wave_file);
jingyitaro 0:f2f38faf17d9 89 spk_mutex.unlock();
jingyitaro 0:f2f38faf17d9 90 }
jingyitaro 0:f2f38faf17d9 91 else if(!flag)
jingyitaro 0:f2f38faf17d9 92 {
jingyitaro 0:f2f38faf17d9 93 led4 = 0;
jingyitaro 0:f2f38faf17d9 94 DACout=0;
jingyitaro 0:f2f38faf17d9 95 }
jingyitaro 0:f2f38faf17d9 96 Thread::wait(500);
jingyitaro 0:f2f38faf17d9 97 }
jingyitaro 0:f2f38faf17d9 98 }
jingyitaro 0:f2f38faf17d9 99
jingyitaro 0:f2f38faf17d9 100 void stopAllOutputDevices() {
jingyitaro 0:f2f38faf17d9 101 wingL.stop(.5);
jingyitaro 0:f2f38faf17d9 102 wingR.stop(.5);
jingyitaro 0:f2f38faf17d9 103 wheelL.stop(.5);
jingyitaro 0:f2f38faf17d9 104 wheelR.stop(.5);
jingyitaro 0:f2f38faf17d9 105 led1=0;
jingyitaro 0:f2f38faf17d9 106 led2=0;
jingyitaro 0:f2f38faf17d9 107
jingyitaro 0:f2f38faf17d9 108 }
jingyitaro 0:f2f38faf17d9 109
jingyitaro 0:f2f38faf17d9 110 int readPiChar() {
jingyitaro 0:f2f38faf17d9 111 char temp;
jingyitaro 0:f2f38faf17d9 112 temp = pi.getc();
jingyitaro 0:f2f38faf17d9 113
jingyitaro 0:f2f38faf17d9 114 led3 = !led3;
jingyitaro 0:f2f38faf17d9 115 alrm_mutex.lock();
jingyitaro 0:f2f38faf17d9 116 if(temp == '1') {
jingyitaro 0:f2f38faf17d9 117 alarm = 1;
jingyitaro 0:f2f38faf17d9 118 led4 = 1;
jingyitaro 0:f2f38faf17d9 119
jingyitaro 0:f2f38faf17d9 120 led1 = 1;
jingyitaro 0:f2f38faf17d9 121 led2= 1;
jingyitaro 0:f2f38faf17d9 122 return 1;
jingyitaro 0:f2f38faf17d9 123
jingyitaro 0:f2f38faf17d9 124 // pi communicates directly to led4 via usb cable...
jingyitaro 0:f2f38faf17d9 125 // can pi also output to a pin?
jingyitaro 0:f2f38faf17d9 126
jingyitaro 0:f2f38faf17d9 127 }
jingyitaro 0:f2f38faf17d9 128 if(temp == '0') {
jingyitaro 0:f2f38faf17d9 129 alarm = 0;
jingyitaro 0:f2f38faf17d9 130 led1 = 0;
jingyitaro 0:f2f38faf17d9 131 led2= 0;
jingyitaro 0:f2f38faf17d9 132 led4 = 0;
jingyitaro 0:f2f38faf17d9 133 return 0;
jingyitaro 0:f2f38faf17d9 134 }
jingyitaro 0:f2f38faf17d9 135 alrm_mutex.unlock();
jingyitaro 0:f2f38faf17d9 136 }
jingyitaro 0:f2f38faf17d9 137
jingyitaro 0:f2f38faf17d9 138 void testLED()
jingyitaro 0:f2f38faf17d9 139 {
jingyitaro 0:f2f38faf17d9 140 while(true)
jingyitaro 0:f2f38faf17d9 141 {
jingyitaro 0:f2f38faf17d9 142 if(readPiChar())
jingyitaro 0:f2f38faf17d9 143 {
jingyitaro 0:f2f38faf17d9 144 led1 = 1;
jingyitaro 0:f2f38faf17d9 145 led2= 1;
jingyitaro 0:f2f38faf17d9 146 }
jingyitaro 0:f2f38faf17d9 147 else
jingyitaro 0:f2f38faf17d9 148 {
jingyitaro 0:f2f38faf17d9 149 }
jingyitaro 0:f2f38faf17d9 150 Thread::wait(500);
jingyitaro 0:f2f38faf17d9 151 }
jingyitaro 0:f2f38faf17d9 152 }
jingyitaro 0:f2f38faf17d9 153
jingyitaro 0:f2f38faf17d9 154 void dev_recv()
jingyitaro 0:f2f38faf17d9 155 {
jingyitaro 0:f2f38faf17d9 156 char temp = 0;
jingyitaro 0:f2f38faf17d9 157 while(true)
jingyitaro 0:f2f38faf17d9 158 {
jingyitaro 0:f2f38faf17d9 159 while(pi.readable())
jingyitaro 0:f2f38faf17d9 160 {
jingyitaro 0:f2f38faf17d9 161
jingyitaro 0:f2f38faf17d9 162 //pi.putc(1);
jingyitaro 0:f2f38faf17d9 163 temp = pi.getc();
jingyitaro 0:f2f38faf17d9 164
jingyitaro 0:f2f38faf17d9 165 led3 = !led3;
jingyitaro 0:f2f38faf17d9 166 alrm_mutex.lock();
jingyitaro 0:f2f38faf17d9 167 if(temp == '1') {
jingyitaro 0:f2f38faf17d9 168 alarm = 1;
jingyitaro 0:f2f38faf17d9 169 led4 = 1;
jingyitaro 0:f2f38faf17d9 170 //flapWings();
jingyitaro 0:f2f38faf17d9 171 //moveWheels();
jingyitaro 0:f2f38faf17d9 172 // pi communicates directly to led4 via usb cable...
jingyitaro 0:f2f38faf17d9 173 // can pi also output to a pin?
jingyitaro 0:f2f38faf17d9 174
jingyitaro 0:f2f38faf17d9 175 }
jingyitaro 0:f2f38faf17d9 176 if(temp == '0') {
jingyitaro 0:f2f38faf17d9 177 alarm = 0;
jingyitaro 0:f2f38faf17d9 178 led4 = 0;
jingyitaro 0:f2f38faf17d9 179 stopAllOutputDevices();
jingyitaro 0:f2f38faf17d9 180 }
jingyitaro 0:f2f38faf17d9 181 alrm_mutex.unlock();
jingyitaro 0:f2f38faf17d9 182 }
jingyitaro 0:f2f38faf17d9 183 Thread::wait(1000);
jingyitaro 0:f2f38faf17d9 184 }
jingyitaro 0:f2f38faf17d9 185 }
jingyitaro 0:f2f38faf17d9 186 void moveWheels()
jingyitaro 0:f2f38faf17d9 187 {
jingyitaro 0:f2f38faf17d9 188 while(true)
jingyitaro 0:f2f38faf17d9 189 {
jingyitaro 0:f2f38faf17d9 190 if(readPiChar())
jingyitaro 0:f2f38faf17d9 191 {
jingyitaro 0:f2f38faf17d9 192 for (float i= -1.0; i < -0.9 ; i += 0.05)
jingyitaro 0:f2f38faf17d9 193 {
jingyitaro 0:f2f38faf17d9 194 wheelL.speed(i);
jingyitaro 0:f2f38faf17d9 195 wheelR.speed(i);
jingyitaro 0:f2f38faf17d9 196 wait(1);
jingyitaro 0:f2f38faf17d9 197 }
jingyitaro 0:f2f38faf17d9 198 wheelL.stop(0.5);
jingyitaro 0:f2f38faf17d9 199 wheelR.stop(0.5);
jingyitaro 0:f2f38faf17d9 200 for (float j= 1.0; j > 0.9 ; j -= 0.05)
jingyitaro 0:f2f38faf17d9 201 {
jingyitaro 0:f2f38faf17d9 202 wheelL.speed(j);
jingyitaro 0:f2f38faf17d9 203 wheelR.speed(j);
jingyitaro 0:f2f38faf17d9 204 wait(1);
jingyitaro 0:f2f38faf17d9 205 }
jingyitaro 0:f2f38faf17d9 206 wheelL.stop(0.5);
jingyitaro 0:f2f38faf17d9 207 wheelR.stop(0.5);
jingyitaro 0:f2f38faf17d9 208 }
jingyitaro 0:f2f38faf17d9 209 Thread::wait(200);//frequency of cycles of wing movemnets
jingyitaro 0:f2f38faf17d9 210 }
jingyitaro 0:f2f38faf17d9 211 }
jingyitaro 0:f2f38faf17d9 212
jingyitaro 0:f2f38faf17d9 213 void setAlarm() {
jingyitaro 0:f2f38faf17d9 214 alarm = 1;
jingyitaro 0:f2f38faf17d9 215 // set led and sound
jingyitaro 0:f2f38faf17d9 216
jingyitaro 0:f2f38faf17d9 217 }
jingyitaro 0:f2f38faf17d9 218 void clearAlarm() {
jingyitaro 0:f2f38faf17d9 219 alarm = 0;
jingyitaro 0:f2f38faf17d9 220
jingyitaro 0:f2f38faf17d9 221
jingyitaro 0:f2f38faf17d9 222 }
jingyitaro 0:f2f38faf17d9 223 int main()
jingyitaro 0:f2f38faf17d9 224 {
jingyitaro 0:f2f38faf17d9 225 pi.baud(9600);
jingyitaro 0:f2f38faf17d9 226 //pi.attach(&piDetectInvader, Serial::RxIrq);
jingyitaro 0:f2f38faf17d9 227 //pi.attach(&dev_recv, Serial::RxIrq);
jingyitaro 0:f2f38faf17d9 228 //t2.start(testLED);
jingyitaro 0:f2f38faf17d9 229 //t1.start(changeAlarm);
jingyitaro 0:f2f38faf17d9 230 t3.start(flapWings);
jingyitaro 0:f2f38faf17d9 231 t4.start(moveWheels);
jingyitaro 0:f2f38faf17d9 232 //t5.start(playSound);
jingyitaro 0:f2f38faf17d9 233 //may not even need threads......
jingyitaro 0:f2f38faf17d9 234 interrupt.rise(&setAlarm);
jingyitaro 0:f2f38faf17d9 235 interrupt.fall(&clearAlarm);
jingyitaro 0:f2f38faf17d9 236 while(pi.readable())
jingyitaro 0:f2f38faf17d9 237 { // main is the next thread
jingyitaro 0:f2f38faf17d9 238 Thread::wait(1000); // wait 2 seconds. Acknowledge pi that mbed is ready
jingyitaro 0:f2f38faf17d9 239 }
jingyitaro 0:f2f38faf17d9 240 }