Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CarAPI.cpp
- Committer:
- ccoleman
- Date:
- 2013-08-23
- Revision:
- 4:4233d072b5af
- Parent:
- 3:1afe0cfab2d1
- Child:
- 5:8dde418f0d1e
File content as of revision 4:4233d072b5af:
#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) utrace("move(%f,%f,%f)\r\n",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) utrace("parkingBrake()\r\n");
TFC_SetMotorPWM(0.1,0.1); //slight spinning of wheel
wait(0.1);
}
/** do nothing for the specified time */
void sleep(float seconds){
if(debug) utrace("sleep(%f)\r\n",seconds);
}
/******************************** INPUTS AND OUTPUTS ********************************/
/** check to see if the car is crashing into something */
bool checkIsCrashing(){
if(debug) utrace("checkIsCrashing()\r\n");
return abs(accelerometer.getAccX()) >= crashSensitivity;
}
/** check to see if the car has stopped */
bool isStopped()
{
double x = accelerometer.getAccX();
double y = accelerometer.getAccY();
double z = accelerometer.getAccZ();
double calAcc = sqrt(x*x + y*y + z*z);
if (calAcc < stopSensitivity)
return 1;
return 0;
}
/** 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) utrace("toggleLED0()\r\n");
TFC_BAT_LED0_TOGGLE;
}
/* toggle led 1 */
void toggleLED1(){
if(debug) utrace("toggleLED1()\r\n");
TFC_BAT_LED1_TOGGLE;
}
/* toggle led 2 */
void toggleLED2(){
if(debug) utrace("toggleLED2()\r\n");
TFC_BAT_LED2_TOGGLE;
}
/* toggle led 3 */
void toggleLED3(){
if(debug) utrace("toggleLED3()\r\n");
TFC_BAT_LED3_TOGGLE;
}
/** is Button B pressed */
bool isButtonBPressed(){
if(debug) utrace("isButtonBPressed()=%d\r\n",TFC_PUSH_BUTTON_1_PRESSED);
return TFC_PUSH_BUTTON_1_PRESSED;
}
/** read Potentiometer 0. returns [-1,1] */
float getPot0(){
if(debug) utrace("getPot0()= %f\r\n", TFC_ReadPot(0));
return TFC_ReadPot(0);
}
/** read Potentiometer 1. returns [-1,1] */
float getPot1(){
if(debug) utrace("getPot1()= %f\r\n", TFC_ReadPot(1));
return TFC_ReadPot(1);
}
/** How much batter remains. returns [0,1] */
float batteryLife(){
if(debug) utrace("batteryLife()= %f\r\n", TFC_ReadBatteryVoltage());
return TFC_ReadBatteryVoltage();
}
/******************************** Changing Variables ********************************/
/** set the base turn offset*/
void setOffset(float _turnOffset){
if(debug) utrace("setOffset(%f)\r\n", _turnOffset);
turnOffset = _turnOffset;
}
/** get the base turn offset */
float getOffset(){
if(debug) utrace("getOffset()= %f\r\n", turnOffset);
return turnOffset;
}
/** set the base crash sensitivity*/
void setCrashSensitivity(float _sensitivity){
if(debug) utrace("setCrashSensitivity(%f)\r\n", _sensitivity);
crashSensitivity = _sensitivity;
}
/** return the crash sensitivity */
float getCrashSensitivity(){
if(debug) utrace("getCrashSensitivity()= %f\r\n", crashSensitivity);
return crashSensitivity;
}
/** set the base stop sensitivity*/
void setStopSensitivity(float _sensitivity){
if(debug) utrace("setStopSensitivity(%f)\r\n", _sensitivity);
stopSensitivity = _sensitivity;
}
/** return the stop sensitivity */
float getStopSensitivity(){
if(debug) utrace("getStopSensitivity()= %f\r\n", stopSensitivity);
return stopSensitivity;
}
/** turn on debugging*/
void setDebug(bool _debug){
if(debug) utrace("setDebug()= %d\r\n", _debug);
debug = _debug;
}
