Clare Coleman
/
UTCSBootcamp
Wrapped Freescale Cup code for the UT CS summer boot camp.
Diff: CarBaseAPI.h
- Revision:
- 4:4233d072b5af
- Parent:
- 0:2010bcffbae0
--- a/CarBaseAPI.h Thu Aug 22 21:52:21 2013 +0000 +++ b/CarBaseAPI.h Fri Aug 23 00:38:23 2013 +0000 @@ -1,10 +1,9 @@ #ifndef CAR_BASE_API_046o73i4277823 #define CAR_BASE_API_046o73i4277823 -#include "mbed.h" +#include <stdarg.h> +#include <MMA8451Q.h> #include "TFC.h" -#include <MMA8451Q.h> - Ticker TFC_TickerObj; Serial pc(USBTX,USBRX); @@ -16,8 +15,9 @@ volatile uint32_t TFC_Ticker[NUM_TFC_TICKERS]; -float turnOffset = 0.002;// the turn turnOffset +float turnOffset = 0.0;// the turn turnOffset float crashSensitivity = 0.5; // crash sensitivity +float stopSensitivity = 0.2; // stop sensitivity bool debug = false; MMA8451Q accelerometer(PTE25,PTE24, MMA8451_I2C_ADDRESS); @@ -34,43 +34,51 @@ } } +void utrace(const char* msg, ...){ + char buffer[256]; + va_list args; + va_start (args, msg); + vsprintf (buffer,msg, args); + va_end (args); + pc.printf(buffer); +} void flushSerialBuffer(void) { while (pc.readable()) { pc.getc(); } return; } void _blinkSequence(bool left){ static float w = 0.125; if (left) { + if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED0_TOGGLE; + wait(w); TFC_BAT_LED0_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED1_TOGGLE; wait(w); - TFC_BAT_LED0_TOGGLE; TFC_BAT_LED1_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED2_TOGGLE; wait(w); - TFC_BAT_LED1_TOGGLE; TFC_BAT_LED2_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; - wait(w); - TFC_BAT_LED2_TOGGLE; TFC_BAT_LED3_TOGGLE; - if (TFC_PUSH_BUTTON_0_PRESSED) return; wait(w); TFC_BAT_LED3_TOGGLE; } else { + if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED3_TOGGLE; + wait(w); TFC_BAT_LED3_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED2_TOGGLE; wait(w); - TFC_BAT_LED3_TOGGLE; TFC_BAT_LED2_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; + TFC_BAT_LED1_TOGGLE; wait(w); - TFC_BAT_LED2_TOGGLE; TFC_BAT_LED1_TOGGLE; if (TFC_PUSH_BUTTON_0_PRESSED) return; - wait(w); - TFC_BAT_LED1_TOGGLE; TFC_BAT_LED0_TOGGLE; - if (TFC_PUSH_BUTTON_0_PRESSED) return; wait(w); TFC_BAT_LED0_TOGGLE; } @@ -110,12 +118,16 @@ } /* turn the car */ void turn(float d, float turnAngle){ - if(debug) pc.printf("turn(%f)",turnAngle); + if (turnAngle >0.7) + turnAngle = 0.7; + else if (turnAngle < -0.7) + turnAngle = -0.7; + if(debug) utrace("turn(%f)\r\n",turnAngle); TFC_SetServo(d,turnAngle + turnOffset); } void _initialize(){ - if(debug) pc.printf("initialize()"); + if(debug) utrace("initialize()\r\n"); //TFC_TickerObj.attach_us(&TFC_TickerUpdate,1000); //Things go to heck if this is enabled TFC_Init(); @@ -124,15 +136,14 @@ //flash startup while(!TFC_PUSH_BUTTON_0_PRESSED){ - blinkSequence(); + blinkSequence(); } - wait(2); + wait(0.75); //starts power to back motor - hear high frequency pitch TFC_HBRIDGE_ENABLE; } void _finish(){ - if(debug) pc.printf("finish()"); //parking brake //need to keep spinning wheel slightly or the car will come crashing down backwards the ramp b/c of gravity //slight spin is not enough to move the car (even on flat surface) @@ -140,18 +151,16 @@ while (!TFC_PUSH_BUTTON_0_PRESSED){ /// sharon's parking break code TFC_SetMotorPWM(0.1,0.1); //slight spinning of wheel - wait(0.1); + blinkSequence2(); } + turn(0,0); //reset to straight direction and stop spinning motor TFC_SetMotorPWM(0,0); //stops the power to back motor - stop hearing the high pitch frequency TFC_HBRIDGE_DISABLE; - - while(1){ - blinkSequence2(); - } + if(debug) utrace("finish()\r\n"); }