This is an example program that actually allows the car to race using the FRDM-TFC library!

Dependencies:   FRDM-TFC

Fork of TFC-RACING-DEMO by Daniel Hadad

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TFC.h"
00003 
00004 #include "common.h"
00005 #include "Spices.h"
00006 
00007 // Exercises designed for Mentoring Matters Car Summer Camp
00008 //   for Summer 2014
00009 //   at Freescale, Inc.
00010 //   in Austin, Texas
00011 //
00012 // 5 MODES OR EXERCISES THAT WILL ALIGN WITH DIP SWITCH SETTINGS IN BINARY FASHION
00013 //  e.g. Mode 1 = 001 = switch 1 is on, switch 2 is off, switch 3 is off
00014 //
00015 // Modes:
00016 //  0 = 000 = Garage Mode, button light test to see if car alive!!
00017 //             PUSHBUTTON A - Light LEDs 0 and 1
00018 //             PUSHBUTTON B - Light LEDs 2 and 3
00019 //            -switch 4 does nothing-
00020 //
00021 //  1 = 001 = Garage Mode, forward/reverse adjust, no auto steer, terminal output
00022 //            -switch 4 does nothing-
00023 //
00024 //  2 = 010 = Garage Mode, steering adjust, no auto steer, terminal output
00025 //             POT 1 - Controls Servo:
00026 //                CCW = turn left
00027 //                CW = turn right
00028 //            -switch 4 does nothing-
00029 //
00030 //  3 = 011 = Garage Mode, Camera test, some auto steer, terminal output
00031 //            switch 4:
00032 //               OFF = normal dec data
00033 //                ON = o-scope mode
00034 //
00035 //  4 = 100 = Track Mode, Auto Steer, safe settings
00036 //            switch 4 = terminal output on/off
00037 //
00038 //  5 = 101 = Track Mode, Auto Steer, fast settings
00039 //            switch 4 = terminal output on/off
00040 //
00041 //  6 = 110 = future upgrades
00042 //
00043 //  7 = 111 = future upgrades
00044 
00045 /* NOTES
00046 Camera unmounted
00047 motors unhooked
00048 only servo / steering hooked up 
00049 
00050 exercise 1 - garage drive
00051 - manual motors forward / reverse
00052 - ensure motors hooked up properly - forward/reverse/left/right
00053 - show terminal value
00054 
00055 exercise 2 - garage steer
00056 - manual steering - left /right 
00057 - calibrate steering
00058 - show terminal to get feedback
00059 
00060 exercise 3 - garage see
00061 - focus camera
00062 - mount camera
00063 - camera + servo line tracking (race mode)
00064 garage mode use paper show following
00065 - bad Kp value
00066 - min speed = 0.5
00067 
00068 exercise 4 - track - slow
00069 - fine tuning max speed
00070 - fine tune proportional control - Kp
00071 
00072 exercise 5 - track - fast
00073 - ratio of differential motor speed on curves
00074 - dead zone with high speed on straights
00075 
00076 */
00077 
00078 void TFC_TickerUpdate()
00079 {
00080     int i;
00081  
00082     for(i=0; i<NUM_TFC_TICKERS; i++)
00083      {
00084         if(TFC_Ticker[i]<0xFFFFFFFF) 
00085         {
00086             TFC_Ticker[i]++;
00087         }
00088     }
00089 }
00090 
00091 // Garage Mode
00092 //   Car not meant to run on track
00093 //   Use this to test out car features
00094 //   and calibrate car
00095 //
00096 void GarageMode()
00097 {
00098   uint32_t i,j = 0;
00099   float ReadPot0, ReadPot1;
00100 
00101 
00102   //This Demo program will look at the first 2 switches to select one of 4 demo / garage modes
00103   switch(TFC_GetDIP_Switch()&0x03)
00104   {
00105   default:
00106   case 0 : // Mode 0
00107       TFC_HBRIDGE_DISABLE;
00108 
00109       TERMINAL_PRINTF("MODE 0\r\n");  
00110       
00111       if(TFC_PUSH_BUTTON_0_PRESSED)
00112       {
00113           TFC_BAT_LED0_ON;
00114           TFC_BAT_LED1_ON;
00115       } else {
00116           TFC_BAT_LED0_OFF;
00117           TFC_BAT_LED1_OFF;
00118       }
00119       
00120       if(TFC_PUSH_BUTTON_1_PRESSED)
00121       {
00122           TFC_BAT_LED2_ON;
00123           TFC_BAT_LED3_ON;
00124       } else {
00125           TFC_BAT_LED2_OFF;
00126           TFC_BAT_LED3_OFF;
00127       }     
00128       break;
00129           
00130   case 1 : // Mode 1
00131       TFC_HBRIDGE_ENABLE;
00132      
00133       ReadPot0 = TFC_ReadPot(0);
00134       ReadPot1 = TFC_ReadPot(1);
00135       TERMINAL_PRINTF("MODE 1: ");   
00136       TERMINAL_PRINTF("Left drive setting = %1.2f\t\t", ReadPot1);
00137       TERMINAL_PRINTF("Right drive setting = %1.2f\r\n", ReadPot0);
00138       TFC_SetMotorPWM(ReadPot0,ReadPot1);
00139               
00140       break;
00141   
00142    case 2:                  
00143       //Make sure motors are off 
00144       TFC_SetMotorPWM(0,0);
00145       TFC_HBRIDGE_DISABLE;
00146 
00147       if(TFC_Ticker[0]>=20) // every 40mS output data to terminal
00148       {
00149           TFC_Ticker[0] = 0; //reset the Ticker
00150           //update the Servos
00151           TERMINAL_PRINTF("MODE 2: ");   
00152           ReadPot0 = TFC_ReadPot(0);
00153 //          ReadPot1 = TFC_ReadPot(1);
00154           TFC_SetServo(0,ReadPot0);
00155 //          TFC_SetServo(1,ReadPot1);
00156           TERMINAL_PRINTF("Steer1 setting = %1.2f\r\n", ReadPot0);
00157 //          TERMINAL_PRINTF("Steer2 setting = %1.2f\r\n", ReadPot0);
00158       }    
00159       break;
00160       
00161    case 3 :
00162       TFC_HBRIDGE_DISABLE;
00163 
00164       if(TFC_Ticker[0]>1000 && TFC_LineScanImageReady>0) // every 1000 ticks (1s if 10ms)
00165           {
00166            // TERMINAL_PRINTF("MODE 3:\r\n");
00167            TFC_Ticker[0] = 0;
00168            TFC_LineScanImageReady=0; // must reset to 0 after detecting non-zero
00169 
00170            if (terminalMode()) {
00171              // print values to terminal as if were o-scope...
00172              
00173              for(j=20;j>0;j--) {
00174                for(i=0;i<128;i++) {
00175                  if ((TFC_LineScanImage0[i]<=(4096*j/20)) && (TFC_LineScanImage0[i]>=(4096*(j-1)/20)))
00176                    TERMINAL_PRINTF("*");
00177                  else
00178                    TERMINAL_PRINTF(" ");
00179                }
00180                TERMINAL_PRINTF("\r\n");
00181              }
00182    
00183            } else { 
00184              for(i=0;i<128;i++) // print one line worth of data (128) from Camera 0
00185              {
00186                TERMINAL_PRINTF("%d",TFC_LineScanImage0[i]);
00187               
00188                if(i==127)  // when last data reached put in line return
00189                  TERMINAL_PRINTF("\r\n",TFC_LineScanImage0[i]);
00190                else
00191                  TERMINAL_PRINTF(",",TFC_LineScanImage0[i]);               
00192              }
00193              TERMINAL_PRINTF("============================================================================================\r\n");
00194              wait_ms(10);
00195            }
00196               
00197                                
00198                   
00199           }
00200           
00201 
00202 
00203       break;
00204   } // end case
00205 
00206 }
00207 
00208  
00209 int main()
00210 {
00211     // TERMINAL TYPE  
00212     PC.baud(115200); // works with Excel and TeraTerm 
00213     //PC.baud(9600); // works with USB Serial Monitor Lite: https://play.google.com/store/apps/details?id=jp.ksksue.app.terminal; doesn't work > 9600
00214     TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); // update ticker array every 2mS (2000 uS)
00215    
00216     TFC_Init();
00217     
00218     for(;;)
00219     {   
00220         if(getMode() < 4)
00221           // Run Garage Mode
00222           GarageMode();
00223         else      
00224           // Run Track Mode
00225           TrackMode();
00226  
00227     } // end of infinite for loop
00228     
00229  
00230 }
00231