Clare Coleman / Mbed 2 deprecated UTCSBootcamp

Dependencies:   MMA8451Q mbed

CarAPI.cpp

Committer:
ccoleman
Date:
2013-08-22
Revision:
0:2010bcffbae0
Child:
3:1afe0cfab2d1

File content as of revision 0:2010bcffbae0:

#include "CarAPI.h"
#include "CarBaseAPI.h"

/******************************** Init and Finish ********************************/

void init(){
    _initialize();
}

void finish(){
    _finish();
}

/******************************** Wheels and Motor ********************************/

/* turn the car.  turnAngle should be between [-1,1] */
void turn(float turnAngle){
    turn(0,turnAngle);
}

/** move forward with the given power, turn angle, for a specified time */
void move(float power, float seconds){
    move (power,power,seconds);
}

/** move forward with the given powers for each wheel, turn angle, for a specified time */
void move(float leftWheelPower,float rightWheelPower, float seconds){
    if(debug) pc.printf("move(%f,%f,%f)",leftWheelPower,rightWheelPower,seconds);
    TFC_SetMotorPWM(leftWheelPower,rightWheelPower); // power for n seconds with both wheels the same power
    wait(seconds);
    TFC_SetMotorPWM(0,0); // turn off power to both wheels
    wait(0.05); //need a wait to register or sebsequent calls to this method won't spin the wheel; hardware can't detect instaneous input?    
}

/** lock the wheels*/
void parkingBrake(){
    if(debug) pc.printf("parkingBrake()");
    TFC_SetMotorPWM(0.1,0.1); //slight spinning of wheel
    wait(0.1);
}

/******************************** INPUTS AND OUTPUTS ********************************/
/** check to see if the car is crashing into something */
bool checkIsCrashing(){
    if(debug) pc.printf("checkIsCrashing()");
    return abs(accelerometer.getAccX()) >= crashSensitivity;
}

/** Return the line direction
    line to the left of center. return [-1,0
    line to the right of center. return (0,1]
    line in center. return 0
*/
float lineDirection(){
    return 0;
}

/* toggle led 0 */
void toggleLED0(){
    if(debug) pc.printf("toggleLED0()");
    TFC_BAT_LED0_TOGGLE;    
}

/* toggle led 1 */
void toggleLED1(){
    if(debug) pc.printf("toggleLED1()");
    TFC_BAT_LED1_TOGGLE;    
}

/* toggle led 2 */
void toggleLED2(){
    if(debug) pc.printf("toggleLED2()");
    TFC_BAT_LED2_TOGGLE;    
}

/* toggle led 3 */
void toggleLED3(){
    if(debug) pc.printf("toggleLED3()");
    TFC_BAT_LED3_TOGGLE;    
}

/** is Button B pressed */
bool isButtonBPressed(){
    if(debug) pc.printf("isButtonBPressed()=",TFC_PUSH_BUTTON_1_PRESSED);
    return TFC_PUSH_BUTTON_1_PRESSED;
}

/** read Potentiometer 0. returns [-1,1] */
float getPot0(){
    if(debug) pc.printf("getPot0()= %f", TFC_ReadPot(0));
    return TFC_ReadPot(0);
}

/** read Potentiometer 1. returns  [-1,1] */
float getPot1(){
    if(debug) pc.printf("getPot1()= %f", TFC_ReadPot(1));
    return TFC_ReadPot(1);
}

/** How much batter remains. returns  [0,1] */
float batteryLife(){
    if(debug) pc.printf("batteryLife()= %f", TFC_ReadBatteryVoltage());
    return TFC_ReadBatteryVoltage();
}

/******************************** Changing Variables ********************************/

/** set the base turn offset*/
void setOffset(float _turnOffset){
    if(debug) pc.printf("setOffset()= %f", _turnOffset);
    turnOffset = _turnOffset;
}

/** get the base turn offset */
float getOffset(){
    if(debug) pc.printf("getOffset()= %f", turnOffset);
    return turnOffset;
}

/** set the base crash sensitivity*/
void setCrashSensitivity(float _sensitivity){
    if(debug) pc.printf("setCrashSensitivity()= %f", _sensitivity);
    crashSensitivity = _sensitivity;
}

/** return the crash sensitivity */
float getCrashSensitivity(){
    if(debug) pc.printf("getCrashSensitivity()= %f", crashSensitivity);
    return crashSensitivity;
}

/** turn on debugging*/
void setDebug(bool _debug){
    if(debug) pc.printf("setDebug()= %d", _debug);
    debug = _debug;
}