
Wrapped Freescale Cup code for the UT CS summer boot camp.
Diff: CarBaseAPI.h
- Revision:
- 0:2010bcffbae0
- Child:
- 4:4233d072b5af
diff -r 000000000000 -r 2010bcffbae0 CarBaseAPI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CarBaseAPI.h Thu Aug 22 20:50:08 2013 +0000 @@ -0,0 +1,158 @@ +#ifndef CAR_BASE_API_046o73i4277823 +#define CAR_BASE_API_046o73i4277823 + +#include "mbed.h" +#include "TFC.h" +#include <MMA8451Q.h> + + +Ticker TFC_TickerObj; +Serial pc(USBTX,USBRX); +// AnalogIn pot (p15); + +#define NUM_TFC_TICKERS 4 +#define MMA8451_I2C_ADDRESS (0x1d<<1) +// Define serial ports +volatile uint32_t TFC_Ticker[NUM_TFC_TICKERS]; + + +float turnOffset = 0.002;// the turn turnOffset +float crashSensitivity = 0.5; // crash sensitivity +bool debug = false; + +MMA8451Q accelerometer(PTE25,PTE24, MMA8451_I2C_ADDRESS); + +void TFC_TickerUpdate() +{ + int i; + for(i=0; i<NUM_TFC_TICKERS; i++) + { + if(TFC_Ticker[i]<0xFFFFFFFF) + { + TFC_Ticker[0]++; + } + } +} + + +void flushSerialBuffer(void) { while (pc.readable()) { pc.getc(); } return; } + +void _blinkSequence(bool left){ + static float w = 0.125; + if (left) { + TFC_BAT_LED0_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED0_TOGGLE; + TFC_BAT_LED1_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + 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 { + TFC_BAT_LED3_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED3_TOGGLE; + TFC_BAT_LED2_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + 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; + } +} + +void blinkSequence(){ + _blinkSequence(false); + _blinkSequence(true); +} + +void blinkSequence2(){ + float w = 0.125; + TFC_BAT_LED0_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED1_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED2_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED3_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED3_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED2_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED1_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); + TFC_BAT_LED0_TOGGLE; + if (TFC_PUSH_BUTTON_0_PRESSED) return; + wait(w); +} +/* turn the car */ +void turn(float d, float turnAngle){ + if(debug) pc.printf("turn(%f)",turnAngle); + TFC_SetServo(d,turnAngle + turnOffset); +} + +void _initialize(){ + if(debug) pc.printf("initialize()"); + //TFC_TickerObj.attach_us(&TFC_TickerUpdate,1000); //Things go to heck if this is enabled + TFC_Init(); + + // Initialize Baud Rate for PC Console + pc.baud(38400); + + //flash startup + while(!TFC_PUSH_BUTTON_0_PRESSED){ + blinkSequence(); + } + wait(2); + //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) + //press button 0 to stop parking brake and turn off motor - goes into wait period w/ LED1 blinking + while (!TFC_PUSH_BUTTON_0_PRESSED){ + /// sharon's parking break code + TFC_SetMotorPWM(0.1,0.1); //slight spinning of wheel + wait(0.1); + } + + //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(); + } +} + + +#endif \ No newline at end of file