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

Dependencies:   FRDM-TFC

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 
00008 void TFC_TickerUpdate()
00009 {
00010     int i;
00011  
00012     for(i=0; i<NUM_TFC_TICKERS; i++)
00013      {
00014         if(TFC_Ticker[i]<0xFFFFFFFF) 
00015         {
00016             TFC_Ticker[i]++;
00017         }
00018     }
00019 }
00020 
00021 void DemoProgram()
00022 {
00023   uint32_t i,j,t = 0;
00024   float ReadPot0, ReadPot1;
00025 
00026 
00027   //This Demo program will look at the middle 2 switch to select one of 4 demo modes.
00028   //Let's look at the middle 2 switches
00029   switch((TFC_GetDIP_Switch()>>1)&0x03)
00030   {
00031   default:
00032   case 0 :
00033       //Demo mode 0 just tests the switches and LED's
00034       if(TFC_PUSH_BUTTON_0_PRESSED)
00035           TFC_BAT_LED0_ON;
00036       else
00037           TFC_BAT_LED0_OFF;
00038       
00039       if(TFC_PUSH_BUTTON_1_PRESSED)
00040           TFC_BAT_LED3_ON;
00041       else
00042           TFC_BAT_LED3_OFF;
00043       
00044       
00045 /*      if(TFC_GetDIP_Switch()&0x01)
00046           TFC_BAT_LED1_ON;
00047       else
00048           TFC_BAT_LED1_OFF;
00049       
00050       if(TFC_GetDIP_Switch()&0x08)
00051           TFC_BAT_LED2_ON;
00052       else
00053           TFC_BAT_LED2_OFF; */
00054       
00055       break;
00056           
00057   case 1:
00058      TFC_HBRIDGE_DISABLE;
00059                     
00060      // if (TFC_HBRIDGE_ENABLED) {
00061         
00062     //    TFC_HBRIDGE_ENABLED = false;
00063     //  }
00064 
00065       //Demo mode 1 will just move the servos with the on-board potentiometers
00066       if(TFC_Ticker[0]>=20) // every 40mS...
00067       {
00068           TFC_Ticker[0] = 0; //reset the Ticker
00069           //update the Servos
00070           ReadPot0 = TFC_ReadPot(0);
00071           ReadPot1 = TFC_ReadPot(1);
00072           TFC_SetServo(0,ReadPot0);
00073           TFC_SetServo(1,ReadPot1);
00074           TERMINAL_PRINTF("Pot0 = %1.2f\r\n", ReadPot0);
00075       //    TERMINAL_PRINTF("Pot1 = %1.2f\r\n", ReadPot1);
00076       }
00077       //Let's put a pattern on the LEDs
00078       if(TFC_Ticker[1] >= 125) // every 250mS... cycle through LEDs
00079       {
00080           TFC_Ticker[1] = 0;
00081           t++;
00082           if(t>4)
00083           {
00084               t=0;
00085           }           
00086           TFC_SetBatteryLED_Level(t);
00087       }
00088       
00089       TFC_SetMotorPWM(0,0); //Make sure motors are off 
00090       TFC_HBRIDGE_DISABLE;
00091 
00092       break;
00093       
00094   case 2 :
00095       TFC_HBRIDGE_ENABLE;
00096      
00097       ReadPot0 = TFC_ReadPot(0);
00098       ReadPot1 = TFC_ReadPot(1);
00099  //     TERMINAL_PRINTF("Pot0 = %1.2f\n", ReadPot0);
00100  //     TERMINAL_PRINTF("Pot1 = %1.2f\n", ReadPot1);
00101       TFC_SetMotorPWM(ReadPot0,ReadPot1);
00102               
00103       //Let's put a pattern on the LEDs
00104       if(TFC_Ticker[1] >= 125)
00105           {
00106               TFC_Ticker[1] = 0;
00107                   t++;
00108                   if(t>4)
00109                   {
00110                       t=0;
00111                   }           
00112               TFC_SetBatteryLED_Level(t);
00113           }
00114       break;
00115   
00116   case 3 :
00117       //Demo Mode 3 will be in Freescale Garage Mode.  It will beam data from the Camera to the 
00118       //Labview Application
00119       //note that there are some issues 
00120       if(TFC_Ticker[0]>1000 && TFC_LineScanImageReady>0) // every 2s ...
00121           {
00122            TFC_Ticker[0] = 0;
00123            TFC_LineScanImageReady=0; // must reset to 0 after detecting non-zero
00124           
00125               if(t==0)
00126                   t=4;
00127               else
00128                   t--;
00129               
00130                TFC_SetBatteryLED_Level(t);
00131               
00132                for(i=0;i<8;i++) // print one line worth of data (128) from Camera 0
00133                {
00134                   for(j=0;j<16;j++)
00135                   {
00136                        
00137                        TERMINAL_PRINTF("0x%X",TFC_LineScanImage0[(i*16)+j]);
00138                       
00139                        if((i==7) && (j==15))  // when last data reached put in line return
00140                            TERMINAL_PRINTF("\r\n",TFC_LineScanImage0[(i*16)+j]);
00141                        else
00142                            TERMINAL_PRINTF(",",TFC_LineScanImage0[(i*16)+j]);
00143                       
00144                   }
00145                   wait_ms(10);
00146                }
00147               
00148      /*          for(i=0;i<8;i++) // print one line worth of data (128) from Camera 1 ??
00149                {
00150                     for(j=0;j<16;j++)
00151                      {
00152                        TERMINAL_PRINTF("0x%X",TFC_LineScanImage1[(i*16)+j]);
00153                       
00154                        if((i==7) && (j==15))  // when last data reached put in line return
00155                            TERMINAL_PRINTF("\r\n",TFC_LineScanImage1[(i*16)+j]);
00156                        else
00157                            TERMINAL_PRINTF(",",TFC_LineScanImage1[(i*16)+j]);
00158                       }   
00159                       
00160                       wait_ms(10);  
00161               }      */                                 
00162                   
00163           }
00164           
00165 
00166 
00167       break;
00168   } // end case
00169 
00170 }
00171 
00172  
00173 int main()
00174 {
00175     // TERMINAL TYPE  
00176     PC.baud(115200); // works with Excel and TeraTerm 
00177     //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
00178     TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); // update ticker array every 2mS (2000 uS)
00179    
00180     TFC_Init();
00181     
00182     for(;;)
00183     {      
00184         //TFC_Task must be called in your main loop.  This keeps certain processing happy (I.E. Serial port queue check)
00185         //   TFC_Task();
00186 
00187 
00188         // If DIP switch 1 is high, then run MCP, else Demo program
00189         if(TFC_GetDIP_Switch()&0x01)
00190           // Run MCP
00191           MasterControlProgram();
00192         else      
00193           // Run Demo Program
00194           DemoProgram();
00195  
00196     } // end of infinite for loop
00197     
00198  
00199 }
00200