Battle bots

Dependencies:   mbed-src

main.cpp

Committer:
JorisW
Date:
2016-03-22
Revision:
3:17628f05a166
Parent:
2:df2b6f20928c

File content as of revision 3:17628f05a166:

#include "mbed.h"
#include <string>
/*
MOTOR A
ENA - p21
IN1 - p5    --> OUT1 - MOTORA+
IN2 - p6    --> OUT2 - MOTORA-
MOTOR B
ENB - p22
IN3 - p26   --> OUT3 - MOTORB+
IN4 - p25   --> OUT4 - MOTORB-
*/

Timeout systicker;

PwmOut in1(p5);
PwmOut in2(p6);

PwmOut in3(p26);
PwmOut in4(p25);

DigitalOut en_a(p21);
DigitalOut en_b(p22);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

Serial bluetooth(p9, p10);
string readBuffer = "";

float time_out_sec = 0.1;

/*Called when Left stick is moved
val1 degrees of stick. 0-360
val2 power center 0, max 100
*/
void JoyLeft(int val1, int val2) {
    
}

/*Called when seekbar is released
id is the id of the seekbar
value is in the range of 1-100
*/
void SeekBar(int id, int value) {
    
}

/*Called when a button is pressed
id is char A,B,C,D
state true when pressed, false when released
*/
void Button(char id, bool state) {
    
}

/*Called when a toggle button is pressed
id 1,2,3
state is true if the toggle is active, false when deactived
*/
void Toggle(int id, bool state) {
    
}

void reset_LED()
{
    led1.write(0);
    led2.write(0);
    led3.write(0);
    led4.write(0);
}

void TimeOut_handler()
{
    in1.write(0.0);
    in2.write(0.0);
    in3.write(0.0);
    in4.write(0.0);
    reset_LED();
}

/* Initialize timers for the H-bridge*/
int init_drive()
{
// Set PWM frequency to 10 kHz
    in1.period_us(100);
    in2.period_us(100);
    in3.period_us(100);
    in4.period_us(100);

    in1.write(0.0);
    in2.write(0.0);
    in3.write(0.0);
    in4.write(0.0);
    /* Set enable pin high. NEVER set this pins low during driving.
    There are no protection diodes and with EN low, H-bridge is in Hi-Z mode.
    This can generate a high voltage that breaks the H-bridge.*/
    en_a.write(1);
    en_b.write(1);

    return 1;
}

string next() {
    string value;
    for(int i = 0; i<readBuffer.size(); i++) {
        if(readBuffer[i] == ':') {
            readBuffer = readBuffer.substr(i+1,readBuffer.length());
            return value;    
        }
        value = value + readBuffer[i];
    }
    return value;    
}

void JoyRight(int val1, int val2) {
    if(val2 < 10) {
        in1.write(0.0):
        in2.write(0.0):
        in3.write(0.0);
        in4.write(0.0);
        return;    
    }
    float right;
    float left;
    float multiplier = ((float)val2)/100;
    if(val2 < 10) multiplier = 0;
    float angle = (float) val1;
    if(val1 >= 0 && val1 < 90) {
        right = 1;
        left = 1 - angle/45;
    }
    if(val1 >= 90 && val1 < 180) {
        left = 3 - angle/45;
        right = -1;
    }
    if(val1 >= 180 && val1 < 270) {
        left = -1;
        right = angle/45 - 5;
    }
    if(val1 >= 270) {
        right = angle/45 - 7;
        left = 1;
    } 
    reset_LED();
    right = right*multiplier;
    left = left*multiplier;
    
    if(right >= 0) {
        in2.write(0.0);
        in1.write(right/2+0.5);
    } else {
        in1.write(0.0);
        in2.write(-right/2+0.5);
    }
    if(left >= 0) {
        in3.write(0.0);
        in4.write(left/2+0.5);
    } else {
        in4.write(0.0);
        in3.write(-left/2+0.5);
    }
}

void handleMesage() {
    if(readBuffer[0] != '<' || readBuffer[readBuffer.size()-2] != '>') {
        //Junk received
        return;    
    }
    string part;
    readBuffer = readBuffer.substr(1,readBuffer.size()-2);
    part = next();
    if(part == "Joystick") {
        string side = next();
        int value1 = atoi(next().c_str());
        int value2 = atoi(next().c_str());
        if(side == "Left") JoyLeft(value1, value2);
        else JoyRight(value1, value2);
    } else if(part == "SeekBar") {
        int value1 = atoi(next().c_str());
        int value2 = atoi(next().c_str());
        SeekBar(value1, value2);
    } else if(part == "Button") {
        char button = next()[0];
        bool pressed = next()=="Pressed";
        Button(button, pressed);
    } else if(part == "Toggle") {
        int button = atoi(next().c_str());
        bool pressed = next()=="true";
        Toggle(button, pressed);
    }  
}

void rxCallback()
{
    while(bluetooth.readable()>0) { 
        char p = bluetooth.getc();
        readBuffer = readBuffer + p;
        //pc.printf(readBuffer.c_str());
        if(p == '\n') {
            //pc.printf(readBuffer.c_str());
            handleMesage();
            readBuffer = "";
        }
    }    
}

/* Initialize the bluetooth module*/
int init_bluetooth()
{
    bluetooth.baud(9600);
    bluetooth.attach(&rxCallback, Serial::RxIrq);
    return 1;
}

int main()
{
    init_drive();
    init_bluetooth();
    while(1) {       

    }
}