int getDistance() { int i,j; float lat_proper; //These store the properly formatted lat/longs in decimal. float long_proper; LCDWriteText("Locating... ", 0, 0); LCDWriteText(" ", 1, 0); #ifdef EASYOPEN //Untested, should work. int open=true; for(int i=0;i<3000;i++) { if (GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)!=0) {open = false;} } if (open == true) {return 0;} #endif for (i = 0;i<TIMEOUT;i++) //for TIMEOUT seconds of trying { if (haveFix == 1) { //This should all work - the values given are reasonable, but needs to be tested outside. lat_proper = 10*(latitude[0]-ATD)+latitude[1]-ATD; lat_proper += ((float)(10*(latitude[2]-ATD) + latitude[3]-ATD))/60.0; long_proper = 10*(longitude[1]-ATD)+longitude[2]-ATD; long_proper += ((float)(10*(longitude[3]-ATD)+longitude[4]-ATD))/60.0; for (j=0;j<5;j++) { lat_proper += ((float)(latitude[5+j]-ATD) /(600.0 * (float)(10^j))); long_proper += ((float)(longitude[6+j]-ATD) /(600.0 * (float)(10^j))); } if (neglat){lat_proper = -lat_proper;} if (neglong){long_proper = -long_proper;} double dlat1=lat_proper*(M_PI/180); double dlong1=long_proper*(M_PI/180); double dlat2 = FINALLAT * (M_PI/180); double dlong2= FINALLONG * (M_PI/180); double dLong=dlong1-dlong2; double dLat=dlat1-dlat2; double aHarv= pow(sin(dLat/2.0),2.0) + cos(dlat1)*cos(dlat2)*pow(sin(dLong/2),2); double cHarv=2*atan2(sqrt(aHarv),sqrt(1.0-aHarv)); //double distance=EARTHRADIUS*cHarv; haveFix = 0; return (int)(EARTHRADIUS*cHarv*1000); //whatever, need to get correct distance here. } SysCtlDelay(SysCtlClockGet()/3); } //if got fix, return distance. //else, return 99999 to signal couldn't fix so no penalty return 99999; }
void openLock() { GPIOPinWrite(GPIO_PORTE_BASE,V5POWER,0xFF); //IMPORTANT!!!!!!!!!! LCDWriteText("Correct location", 0, 0); LCDWriteText("Opening... ", 1, 0); servomson=10; //Unlock. SysCtlDelay(SysCtlClockGet()*3); //Wait for 9s servomson=15; //Lock. SysCtlDelay(SysCtlClockGet()/3); //Wait for 1s GPIOPinWrite(GPIO_PORTE_BASE,V5POWER,0x00);// IMPORTANT!!!!!!!!!! }
void ShowVelocity(void){ unsigned char centena, dezena, unidade; float kmh; //velocityx é a velocidade atual. kmh = velocityx * 3.6; centena = kmh / 100; dezena = (kmh - (centena * 100)) / 10; unidade = kmh - (centena * 100) - (dezena * 10); LCDWriteChar(centena | 0x30,2,30); LCDWriteChar(dezena | 0x30,2,36); LCDWriteChar(unidade | 0x30,2,42); LCDWriteText("km/h",2,48); }
int main(void) { char stringbuffer[17]; int distance = 0; // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. ROM_FPUEnable(); ROM_FPULazyStackingEnable(); ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); //This wasn't clear at all. Note to self, everything needs enabling on this chip. ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, WAKEPIN); ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, REEDPIN); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V5POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V3POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, SERVO); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0xFF); #ifdef EASYOPEN ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); #endif GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); GPIOPinConfigure(GPIO_PC6_U3RX); GPIOPinConfigure(GPIO_PC7_U3TX); ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7); ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_UARTConfigSetExpClk(UART3_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_IntEnable(INT_UART0); ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); ROM_IntEnable(INT_UART3); ROM_UARTIntEnable(UART3_BASE, UART_INT_RX | UART_INT_RT); SysTickPeriodSet(SysCtlClockGet()/10000); SysTickIntRegister(&ServoDriver); SysTickIntEnable(); SysTickEnable(); ROM_IntMasterEnable(); GPIOIntTypeSet(GPIO_PORTA_BASE, REEDPIN, GPIO_FALLING_EDGE); GPIOPinIntClear(GPIO_PORTA_BASE, REEDPIN); GPIOPinIntEnable(GPIO_PORTA_BASE, REEDPIN); IntEnable(INT_GPIOA); // while(1){} /* SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0xFF); //Turn on the 5V power to LCD + servo. SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon.*/ EEPROMInit(); initLCD(); LCDCommand(0x0c); #ifdef LOOPBACKUART while(1){} #endif #ifdef FIRSTRUN //First run, sets the eeprom to have as many tries as is desired. EEPROMMassErase(); EEPROMProgram(&initialNumTries,eepromAddress,sizeof(initialNumTries)); LCDWriteText("Setup Complete. ", 0, 0); LCDWriteText("Reflash Firmware", 1, 0); while (1){} //Don't want to do anything else now. #endif EEPROMRead(&numTrieslong,eepromAddress,sizeof(numTrieslong)); // numTries=(int)numTrieslong; // openLock(); // numTrieslong=0; if (numTrieslong > initialNumTries-3) //Has already opened once, so just open as needed if stuck. { openLock(); numTrieslong--; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. } else { distance = getDistance(); if(distance==99999){ //No fix :/ LCDWriteText("Location unknown", 0, 0); LCDWriteText("Take me outside ", 1, 0); SysCtlDelay(SysCtlClockGet()); //Waits 3 seconds. } else if (distance>NEARENOUGH) //Valid fix, too far away. { if ((int)numTrieslong>0) //Any attempts remaining? { usnprintf(stringbuffer,17,"Distance: %4dm ",distance); LCDWriteText(stringbuffer, 0, 0); numTrieslong--; // numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. usnprintf(stringbuffer,17,"%2d Attempts left",(int)numTrieslong); LCDWriteText(stringbuffer, 1, 0); SysCtlDelay(SysCtlClockGet()*2); } else { LCDWriteText("Oh dear... ", 0, 0); //Not really sure what to do, hopefully this code never runs. LCDWriteText("Opening anyway. ", 1, 0); // numTrieslong=initialNumTries+1; // EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Set to big value SysCtlDelay(10*SysCtlClockGet()/3); openLock(); } } else //Found the location! { openLock(); numTrieslong=initialNumTries+1; //numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Lock will now open straight away. } } // BLINK(RED); HibernateEnableExpClk(SysCtlClockGet()); HibernateGPIORetentionEnable(); //Enables GPIO retention after wake from hibernate. HibernateClockSelect(HIBERNATE_CLOCK_SEL_RAW); HibernateWakeSet(HIBERNATE_WAKE_PIN); HibernateIntRegister(&HibernateInterrupt); HibernateIntEnable(HIBERNATE_INT_PIN_WAKE); //BLINK(BLUE); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTB_BASE, RS|E|D4|D5|D6|D7, 0xFF); //Pull all data pins to LCD high so we're not phantom powering it through ESD diodes. ROM_GPIOPinWrite(GPIO_PORTF_BASE, SERVO, 0xFF); //Likewise for the servo SysCtlDelay(SysCtlClockGet()/6); HibernateRequest();// we want to be looping'n'shit. while(1){} //Lalala, I'm a sleeping right now. }