Silvan Li
/
mbedKart_sp
single player mbedKart
(notes)
main.cpp
- Committer:
- DerekW2015
- Date:
- 2018-12-13
- Revision:
- 45:29d1f7734b9b
- Parent:
- 44:40183935dc5f
File content as of revision 45:29d1f7734b9b:
#include "mbed.h" #include <stdio.h> #include "Motor.h" #include "rgbled.h" #include "rgbSensor.h" #include "universal.h" #include <ctime> // Define threads and mutexes Thread thread1; Thread thread2; Thread thread3; // Global game actions bool paused = false; void check_unpause() { char bnum=0; char bhit=0; while (paused) { if (blue.getc()=='B') { //button data packet bnum = blue.getc(); //button number bhit = blue.getc(); //1=hit, 0=release if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? if (bnum == '1') { //number button 1, pause if (bhit=='1') paused = false; } } } } } void game_paused() { // Cycle through LEDs sstate = pause; Thread pause_thread; pause_thread.start(check_unpause); while (paused) { int num = 1; for (int i = 0; i < 3; i++) { num *= 2; myled = num; ThisThread::sleep_for(100); } ThisThread::sleep_for(500); for (int i = 3; i > 0; i--) { num /= 2; myled = num; ThisThread::sleep_for(100); } ThisThread::sleep_for(500); } pause_thread.terminate(); } void win() { while(1) { unsigned int rgbColor[3]; // Start off with red. rgbColor[0] = 255; rgbColor[1] = 0; rgbColor[2] = 0; // Choose the colors to increment and decrement. for (int decColor = 0; decColor < 3; decColor += 1) { int incColor = decColor == 2 ? 0 : decColor + 1; // cross-fade the two colors. for(int i = 0; i < 255; i += 1) { rgbColor[decColor] -= 1; rgbColor[incColor] += 1; myRGBled.write(rgbColor[0]/255.0, rgbColor[1]/255.0, rgbColor[2]/255.0); ThisThread::sleep_for(1); } } } } // Thread to control speed void speed_control() { // The kart has to be in one of three states at all times while(1) { while(sstate == coasting) { if (speed_cmd > 0.0) speed_cmd -= coast_rate; else speed_cmd = 0.0; left.speed(speed_cmd * left_multiplier); right.speed(speed_cmd * right_multiplier); ThisThread::sleep_for(200); } while(sstate == accelerating) { if (speed_cmd < max_speed) speed_cmd += acceleration_rate; else speed_cmd = max_speed; left.speed(speed_cmd * left_multiplier); right.speed(speed_cmd * right_multiplier); ThisThread::sleep_for(200); } while(sstate == braking) { if (speed_cmd > -1.0) speed_cmd -= brake_rate; else speed_cmd = -1.0; left.speed(speed_cmd * left_multiplier); right.speed(speed_cmd * right_multiplier); ThisThread::sleep_for(200); } while(sstate == pause) { left.speed(0.0); right.speed(0.0); } } } void powerupthread() { while(1){ if(powerup){ max_speed = 1.0; ThisThread::sleep_for(5000); max_speed = 0.8; powerup = false; } ThisThread::sleep_for(300); } } // Thread for checking rgb sensor values and updating game variables // Change later depending on behavior of RGB sensor and colors used int t = 8000; //change this depending on RGB values void check_RGB() { while(true){ rgbsensor.update(); int C_value = rgbsensor.get_C(); int R_value = rgbsensor.get_R(); int G_value = rgbsensor.get_G(); int B_value = rgbsensor.get_B(); if(R_value < 3000 && B_value > 6500) { //Check for if cart runs over a speed boost panel pc.printf("Found blue"); powerup = true; //change this depending on speed up panel algs cstate = 0; } else if(R_value > 3000 && G_value < 2000 && B_value < 2000) { //Check if cart reaches next checkpoint. cstate is set to 1 upon seeing first color of checkpoint pc.printf("Found red"); paused = true; win(); } pc.printf("[C: %d, R: %d, G: %d, B: %d]\r\n", C_value, R_value, G_value, B_value); ThisThread::sleep_for(500); } } int main() { pc.printf("Initializing "); // Start threads thread1.start(check_RGB); thread2.start(speed_control); // Since we're stopped, this won't do anything thread3.start(powerupthread); std::clock_t start; double duration; start = std::clock(); // Bluetooth controller code char bnum=0; char bhit=0; pc.printf("running"); while(running) { if (blue.getc()=='!') { if (blue.getc()=='B') { //button data packet bnum = blue.getc(); //button number bhit = blue.getc(); //1=hit, 0=release if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? switch (bnum) { case '1': //number button 1, pause if (bhit=='1') { paused = true; game_paused(); } break; case '2': //number button 2, accelerate if (bhit=='1') { sstate = accelerating; pc.printf("accelerating"); } else { sstate = coasting; } break; case '3': //number button 3 if (bhit=='1') { } else { } break; case '4': //number button 4, brakes if (bhit=='1') { sstate = braking; } else { sstate = coasting; } break; case '5': //button 5 up arrow if (bhit=='1') { pc.printf("up"); } else { } break; case '6': //button 6 down arrow if (bhit=='1') { } else { } break; case '7': //button 7 left arrow if (bhit=='1') { pc.printf("left"); left_multiplier = 0.5; right_multiplier = 1.0; } else { left_multiplier = 1.0; right_multiplier = 1.0; } break; case '8': //button 8 right arrow if (bhit=='1') { pc.printf("right"); left_multiplier = 1.0; right_multiplier = 0.5; } else { left_multiplier = 1.0; right_multiplier = 1.0; } break; default: break; } } } } } // Clean up duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; pc.printf("%d",duration); thread1.terminate(); thread2.terminate(); thread3.terminate(); }