2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

main.cpp

Committer:
twighk
Date:
2013-04-03
Revision:
4:1be0f6c6ceae
Parent:
3:717de74f6ebd
Child:
5:56a5fdd373c9

File content as of revision 4:1be0f6c6ceae:

#pragma Otime // Compiler Optimisations

// Eurobot13 main.cpp

#include "mbed.h"
Serial pc(USBTX, USBRX);

#include "Actuators/Arms/Arm.h"
#include "Actuators/MainMotors/MainMotor.h"
#include "Others/EmergencyStop/EmergencyStop.h"
#include "Sensors/Encoders/Encoder.h"
#include "Sensors/Colour/Colour.h"
#include "Sensors/Colour/Led.h"
#include "Sensors/Colour/Phototransistor.h"



void motortest();
void encodertest();
void motorencodetest();
void motorencodetestline();
void motorsandservostest();
void armtest();
void motortestline();
void ledtest();
void phototransistortest();
void ledphototransistortest();
void colourtest();

int main() {
/*  Setup EmergencyStop button for actuators.
    Derive all actuators from the pure virtual actuator class!
*/  EmergencyStop EStop(p8); 
    
/*****************
 *   Test Code   *
 *****************/
    //motortest();
    //encodertest();
    //motorencodetest();
    //motorencodetestline();
    //motorsandservostest();
    //armtest();
    //motortestline();
    //ledtest();
    //phototransistortest();
    //ledphototransistortest();
    colourtest(); // Red SnR too low
}

void colourtest(){
    Colour c(p9,p10,p20);
    c.Calibrate();
    while(true){
        wait(0.1);
        ColourEnum ce = c.getColour();
        switch(ce){
            case BLUE :
                pc.printf("BLUE\n\r");
                break;
            case RED:
                pc.printf("RED\n\r");
                break;
            case WHITE:
                pc.printf("WHITE\n\r");
                break;
            case INCONCLUSIVE:
                pc.printf("INCONCLUSIVE\n\r");
                break;
            default:
                pc.printf("BUG\n\r");
        }
    }

}


void ledphototransistortest(){
    Led blue(p9), red(p10);
    Phototransistor pt(p20); 
    Serial pc(USBTX, USBRX);

    while(true){
        blue.on();red.off();
        for(int i = 0; i != 5; i++){
            wait(0.1);
            pc.printf("Phototransistor Analog is (blue): %f \n\r", pt.read());
        }
        blue.off();red.on();
        for(int i = 0; i != 5; i++){
            wait(0.1);
            pc.printf("Phototransistor Analog is (red ): %f \n\r", pt.read());
        }
    } 
}

void phototransistortest(){
    Phototransistor pt(p20); 
    while(true){
        wait(0.1);
        pc.printf("Phototransistor Analog is: %f \n\r", pt.read());
    }

}

void ledtest(){
    Led blue(p9), red(p10);
    while(true){
        blue.on();red.off();
        wait(0.2);
        blue.off();red.on();
        wait(0.2);
    
    }
}

void armtest(){
    Arm white(p26), black(p25, false, 0.0005, 180);
    while(true){
        white(0);
        black(0);
        wait(1);
        white(1);
        black(1);
        wait(1);
    }
}


void motorsandservostest(){
    Encoder Eleft(p27, p28), Eright(p30, p29);
    MainMotor mleft(p24,p23), mright(p21,p22);
    Arm sTop(p25), sBottom(p26);
    //Serial pc(USBTX, USBRX);
    const float speed = 0.0;
    const float dspeed = 0.0;
    
    Timer servoTimer;
    mleft(speed); mright(speed);
    servoTimer.start();
    while (true){
        pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
        if (Eleft.getPoint() < Eright.getPoint()){
            mleft(speed);
            mright(speed - dspeed);
        } else {
            mright(speed);
            mleft(speed - dspeed);
        }
        if (servoTimer.read() < 1){
            sTop.clockwise();
        } else if (servoTimer.read() < 4) {
            sTop.halt();
        } else if (servoTimer.read() < 5) {
            sBottom.anticlockwise();
            //Led=1;
        } else if (servoTimer.read() < 6) {
            sBottom.clockwise();
            //Led=0;
        } else if (servoTimer.read() < 7) {
            sBottom.halt();
        }else {
            sTop.anticlockwise();
        }
        if (servoTimer.read() >= 9) servoTimer.reset();
    }
}

void motortestline(){
    MainMotor mleft(p24,p23), mright(p21,p22);
    const float speed = 0.2;
    mleft(speed); mright(speed);
    while(true) wait(1);
}

void motorencodetestline(){
    Encoder Eleft(p27, p28), Eright(p30, p29);
    MainMotor mleft(p24,p23), mright(p21,p22);
    //Serial pc(USBTX, USBRX);
    const float speed = 0.2;
    const float dspeed = 0.1;

    mleft(speed); mright(speed);
    while (true){
    //left 27 cm = 113 -> 0.239 cm/pulse
    //right 27 cm = 72 -> 0.375 cm/pulse
        pc.printf("Position is: %i \t %i \n\r", (int)(Eleft.getPoint()*0.239), (int)(Eright.getPoint()*0.375));
        if (Eleft.getPoint()*0.239 < Eright.getPoint()*0.375){
            mright(speed - dspeed);
        } else {
            mright(speed + dspeed);
        }
    }

}

void motorencodetest(){
    Encoder Eleft(p28, p27), Eright(p29, p30);
    MainMotor mleft(p23,p24), mright(p22,p21);
    Serial pc(USBTX, USBRX);
    
    const float speed = -0.3;
    const int enc = -38;
    while(true){
        mleft(speed); mright(0);
        while(Eleft.getPoint()>enc){
            pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
        }
        Eleft.reset(); Eright.reset();
        mleft(0); mright(speed);
        while(Eright.getPoint()>enc){
            pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
        }
        Eleft.reset(); Eright.reset();
    }
}

void encodertest(){
    Encoder E1(p28, p27);
    Encoder E2(p29, p30);
    Serial pc(USBTX, USBRX);
    while(true){
        wait(0.1);
        pc.printf("Position is: %i \t %i \n\r", E1.getPoint(), E2.getPoint());
    }

}
void motortest(){
    MainMotor mright(p22,p21), mleft(p23,p24);
    while(true) {
        wait(1);
        mleft(0.8); mright(0.8);
        wait(1);
        mleft(-0.2); mright(0.2);
        wait(1);
        mleft(0); mright(0);
    }
}