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

Dependencies:   MMA8451Q mbed

Committer:
ccoleman
Date:
Fri Aug 23 16:21:47 2013 +0000
Revision:
8:eb1307992dd1
Parent:
7:2901515f14c2
Um. Fixed sleep. Apparently I need more of that.

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