Wrapped Freescale Cup code for the UT CS summer boot camp.

Dependencies:   MMA8451Q mbed

Committer:
ccoleman
Date:
Thu Aug 22 20:50:08 2013 +0000
Revision:
0:2010bcffbae0
Child:
4:4233d072b5af
Wrapped Freescale Cup code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ccoleman 0:2010bcffbae0 1 #ifndef CAR_BASE_API_046o73i4277823
ccoleman 0:2010bcffbae0 2 #define CAR_BASE_API_046o73i4277823
ccoleman 0:2010bcffbae0 3
ccoleman 0:2010bcffbae0 4 #include "mbed.h"
ccoleman 0:2010bcffbae0 5 #include "TFC.h"
ccoleman 0:2010bcffbae0 6 #include <MMA8451Q.h>
ccoleman 0:2010bcffbae0 7
ccoleman 0:2010bcffbae0 8
ccoleman 0:2010bcffbae0 9 Ticker TFC_TickerObj;
ccoleman 0:2010bcffbae0 10 Serial pc(USBTX,USBRX);
ccoleman 0:2010bcffbae0 11 // AnalogIn pot (p15);
ccoleman 0:2010bcffbae0 12
ccoleman 0:2010bcffbae0 13 #define NUM_TFC_TICKERS 4
ccoleman 0:2010bcffbae0 14 #define MMA8451_I2C_ADDRESS (0x1d<<1)
ccoleman 0:2010bcffbae0 15 // Define serial ports
ccoleman 0:2010bcffbae0 16 volatile uint32_t TFC_Ticker[NUM_TFC_TICKERS];
ccoleman 0:2010bcffbae0 17
ccoleman 0:2010bcffbae0 18
ccoleman 0:2010bcffbae0 19 float turnOffset = 0.002;// the turn turnOffset
ccoleman 0:2010bcffbae0 20 float crashSensitivity = 0.5; // crash sensitivity
ccoleman 0:2010bcffbae0 21 bool debug = false;
ccoleman 0:2010bcffbae0 22
ccoleman 0:2010bcffbae0 23 MMA8451Q accelerometer(PTE25,PTE24, MMA8451_I2C_ADDRESS);
ccoleman 0:2010bcffbae0 24
ccoleman 0:2010bcffbae0 25 void TFC_TickerUpdate()
ccoleman 0:2010bcffbae0 26 {
ccoleman 0:2010bcffbae0 27 int i;
ccoleman 0:2010bcffbae0 28 for(i=0; i<NUM_TFC_TICKERS; i++)
ccoleman 0:2010bcffbae0 29 {
ccoleman 0:2010bcffbae0 30 if(TFC_Ticker[i]<0xFFFFFFFF)
ccoleman 0:2010bcffbae0 31 {
ccoleman 0:2010bcffbae0 32 TFC_Ticker[0]++;
ccoleman 0:2010bcffbae0 33 }
ccoleman 0:2010bcffbae0 34 }
ccoleman 0:2010bcffbae0 35 }
ccoleman 0:2010bcffbae0 36
ccoleman 0:2010bcffbae0 37
ccoleman 0:2010bcffbae0 38 void flushSerialBuffer(void) { while (pc.readable()) { pc.getc(); } return; }
ccoleman 0:2010bcffbae0 39
ccoleman 0:2010bcffbae0 40 void _blinkSequence(bool left){
ccoleman 0:2010bcffbae0 41 static float w = 0.125;
ccoleman 0:2010bcffbae0 42 if (left) {
ccoleman 0:2010bcffbae0 43 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 44 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 45 wait(w);
ccoleman 0:2010bcffbae0 46 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 47 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 48 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 49 wait(w);
ccoleman 0:2010bcffbae0 50 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 51 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 52 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 53 wait(w);
ccoleman 0:2010bcffbae0 54 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 55 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 56 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 57 wait(w);
ccoleman 0:2010bcffbae0 58 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 59 } else {
ccoleman 0:2010bcffbae0 60 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 61 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 62 wait(w);
ccoleman 0:2010bcffbae0 63 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 64 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 65 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 66 wait(w);
ccoleman 0:2010bcffbae0 67 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 68 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 69 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 70 wait(w);
ccoleman 0:2010bcffbae0 71 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 72 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 73 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 74 wait(w);
ccoleman 0:2010bcffbae0 75 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 76 }
ccoleman 0:2010bcffbae0 77 }
ccoleman 0:2010bcffbae0 78
ccoleman 0:2010bcffbae0 79 void blinkSequence(){
ccoleman 0:2010bcffbae0 80 _blinkSequence(false);
ccoleman 0:2010bcffbae0 81 _blinkSequence(true);
ccoleman 0:2010bcffbae0 82 }
ccoleman 0:2010bcffbae0 83
ccoleman 0:2010bcffbae0 84 void blinkSequence2(){
ccoleman 0:2010bcffbae0 85 float w = 0.125;
ccoleman 0:2010bcffbae0 86 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 87 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 88 wait(w);
ccoleman 0:2010bcffbae0 89 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 90 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 91 wait(w);
ccoleman 0:2010bcffbae0 92 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 93 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 94 wait(w);
ccoleman 0:2010bcffbae0 95 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 96 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 97 wait(w);
ccoleman 0:2010bcffbae0 98 TFC_BAT_LED3_TOGGLE;
ccoleman 0:2010bcffbae0 99 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 100 wait(w);
ccoleman 0:2010bcffbae0 101 TFC_BAT_LED2_TOGGLE;
ccoleman 0:2010bcffbae0 102 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 103 wait(w);
ccoleman 0:2010bcffbae0 104 TFC_BAT_LED1_TOGGLE;
ccoleman 0:2010bcffbae0 105 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 106 wait(w);
ccoleman 0:2010bcffbae0 107 TFC_BAT_LED0_TOGGLE;
ccoleman 0:2010bcffbae0 108 if (TFC_PUSH_BUTTON_0_PRESSED) return;
ccoleman 0:2010bcffbae0 109 wait(w);
ccoleman 0:2010bcffbae0 110 }
ccoleman 0:2010bcffbae0 111 /* turn the car */
ccoleman 0:2010bcffbae0 112 void turn(float d, float turnAngle){
ccoleman 0:2010bcffbae0 113 if(debug) pc.printf("turn(%f)",turnAngle);
ccoleman 0:2010bcffbae0 114 TFC_SetServo(d,turnAngle + turnOffset);
ccoleman 0:2010bcffbae0 115 }
ccoleman 0:2010bcffbae0 116
ccoleman 0:2010bcffbae0 117 void _initialize(){
ccoleman 0:2010bcffbae0 118 if(debug) pc.printf("initialize()");
ccoleman 0:2010bcffbae0 119 //TFC_TickerObj.attach_us(&TFC_TickerUpdate,1000); //Things go to heck if this is enabled
ccoleman 0:2010bcffbae0 120 TFC_Init();
ccoleman 0:2010bcffbae0 121
ccoleman 0:2010bcffbae0 122 // Initialize Baud Rate for PC Console
ccoleman 0:2010bcffbae0 123 pc.baud(38400);
ccoleman 0:2010bcffbae0 124
ccoleman 0:2010bcffbae0 125 //flash startup
ccoleman 0:2010bcffbae0 126 while(!TFC_PUSH_BUTTON_0_PRESSED){
ccoleman 0:2010bcffbae0 127 blinkSequence();
ccoleman 0:2010bcffbae0 128 }
ccoleman 0:2010bcffbae0 129 wait(2);
ccoleman 0:2010bcffbae0 130 //starts power to back motor - hear high frequency pitch
ccoleman 0:2010bcffbae0 131 TFC_HBRIDGE_ENABLE;
ccoleman 0:2010bcffbae0 132 }
ccoleman 0:2010bcffbae0 133
ccoleman 0:2010bcffbae0 134 void _finish(){
ccoleman 0:2010bcffbae0 135 if(debug) pc.printf("finish()");
ccoleman 0:2010bcffbae0 136 //parking brake
ccoleman 0:2010bcffbae0 137 //need to keep spinning wheel slightly or the car will come crashing down backwards the ramp b/c of gravity
ccoleman 0:2010bcffbae0 138 //slight spin is not enough to move the car (even on flat surface)
ccoleman 0:2010bcffbae0 139 //press button 0 to stop parking brake and turn off motor - goes into wait period w/ LED1 blinking
ccoleman 0:2010bcffbae0 140 while (!TFC_PUSH_BUTTON_0_PRESSED){
ccoleman 0:2010bcffbae0 141 /// sharon's parking break code
ccoleman 0:2010bcffbae0 142 TFC_SetMotorPWM(0.1,0.1); //slight spinning of wheel
ccoleman 0:2010bcffbae0 143 wait(0.1);
ccoleman 0:2010bcffbae0 144 }
ccoleman 0:2010bcffbae0 145
ccoleman 0:2010bcffbae0 146 //reset to straight direction and stop spinning motor
ccoleman 0:2010bcffbae0 147 TFC_SetMotorPWM(0,0);
ccoleman 0:2010bcffbae0 148
ccoleman 0:2010bcffbae0 149 //stops the power to back motor - stop hearing the high pitch frequency
ccoleman 0:2010bcffbae0 150 TFC_HBRIDGE_DISABLE;
ccoleman 0:2010bcffbae0 151
ccoleman 0:2010bcffbae0 152 while(1){
ccoleman 0:2010bcffbae0 153 blinkSequence2();
ccoleman 0:2010bcffbae0 154 }
ccoleman 0:2010bcffbae0 155 }
ccoleman 0:2010bcffbae0 156
ccoleman 0:2010bcffbae0 157
ccoleman 0:2010bcffbae0 158 #endif