single player mbedKart

Dependencies:   Motor

(notes)

main.cpp

Committer:
DerekW2015
Date:
2018-12-11
Revision:
26:708fb66f0b6a
Parent:
23:b0fe1c1046dc
Child:
27:2872e658125e

File content as of revision 26:708fb66f0b6a:

#include "mbed.h"
#include "Motor.h"
#include "rgbled.h"
#include "rgbSensor.h"
#include "xbee_functions.h"
#include "universal.h"
// 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 (true) {
        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
    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);
    }
}

// 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);
            right.speed(speed_cmd);
            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);
            right.speed(speed_cmd);
            ThisThread::sleep_for(200);
        }
        while(sstate == braking) {
            if (speed_cmd > 0.0) speed_cmd -= brake_rate;
            else speed_cmd = 0.0;
            left.speed(speed_cmd);
            right.speed(speed_cmd);
            ThisThread::sleep_for(200);
        }
    }
}

// Thread for checking rgb sensor values and updating game variables
// Change later depending on behavior of RGB sensor and colors used
int thresh = 150;   //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(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh && current_item == 0) { //Check for if cart runs over an item box
            itembox = true;
            cstate = 0;
        }
        else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check for if cart runs over a speed boost panel
            max_speed = 1;      //change this depending on speed up panel algs
            cstate = 0;
        }
        else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check if cart collides with wall
            collide = true;
        }
        else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check if cart reaches next checkpoint. cstate is set to 1 upon seeing first color of checkpoint
            cstate = 1;
        }
        else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh && cstate == 1) {   
        //Second part of checking if cart reaches next checkpoint. If the previous color was passed through previously, updates checkpoint progress
            cstate = 0;
            progress++;
        }
        else {
            cstate = 0;
        }
        ThisThread::sleep_for(500);
    }
}

void set_item() {
    while(true){
        if(itembox && current_item == 0) {
            //playsound
            for(int n = 0; n < 10; n++){    //flash the led to simulate slot machine effect
                myRGBled.write(1.0,0.0,0.0);
                ThisThread::sleep_for(50);
                myRGBled.write(0.0,1.0,0.0);
                ThisThread::sleep_for(50);
                myRGBled.write(0.0,0.0,1.0);
                ThisThread::sleep_for(50);
                myRGBled.write(1.0,1.0,0.0);
                ThisThread::sleep_for(50);
            }
            current_item = rand() % 4 + 1;      //change this depending on items implemented and random item alg
            switch(current_item) {
                case 1 : //mushroom
                    myRGBled.write(1.0,0.0,0.0);
                    break;
                case 2 : //green shell
                    myRGBled.write(0.0,1.0,0.0);
                    break;
                case 3 : //blue shell
                    myRGBled.write(0.0,0.0,1.0);
                    break;
                case 4 : //banana
                    myRGBled.write(1.0,1.0,0.0);
                    break;
                default :
                    break;
            }
            itembox = false;
        }
        ThisThread::sleep_for(500);
    }
}

void hold_item() {
    if(current_item == 0){
        return;
    }
    if(current_item == 1){
        max_speed = 1;
        current_item = 0;
    }
    else{
        holding_item = true;
    }
    return;
}

void release_item(){
    holding_item = false;
    if(current_item == 2){
        //hit player in front
        return;
    }
    if(current_item == 3){
        //hit player in first
        return;
    }
    if(current_item == 4){
        //hit player behind
        return;
    }
}

void receive_thread() {
    xbee.process_rx_frames();
    ThisThread::sleep_for(100);
}

int main() {
    pc.printf("Initializing ");

    // Initialize Xbee and remote modules
    xbee.register_receive_cb(&receive_cb);
    RadioStatus const radioStatus = xbee.init();
    xbee.set_panid(PANID);
    xbee.set_channel(CHANNEL);
    const XBeeLib::RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64);
    pc.printf("XBee initialized ");
    
    // Start threads
    thread1.start(check_RGB);
    thread2.start(speed_control); // Since we're stopped, this won't do anything
    thread3.start(receive_thread);
    
    // Bluetooth controller code
    char bnum=0;
    char bhit=0;
    while(1) {
        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') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                paused = true;
                                game_paused();
                            }
                            break;
                        case '2': //number button 2, accelerate
                            if (bhit=='1') {
                                sstate = accelerating;
                            } else {
                                sstate = coasting;
                            }
                            break;
                        case '3': //number button 3
                            if (bhit=='1') {
                                hold_item();        //holds item to protect player from incoming hazards
                            } else {
                                release_item();     //release item to attack
                            }
                            break;
                        case '4': //number button 4, brakes
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                sstate = braking;
                            } else {
                                sstate = coasting;
                            }
                            break;
                        case '5': //button 5 up arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                //throw_item();
                                //item_box();
                                send_data_to_remote_node(remoteDevice64b, "Hello");
                                pc.printf("Button 5");
                            } else {
                                
                            }
                            break;
                        case '6': //button 6 down arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(-1.0);
                                right.speed(-1.0);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        case '7': //button 7 left arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(0.3);
                                right.speed(1.0);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        case '8': //button 8 right arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(1.0);
                                right.speed(0.3);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}