main.c
Go to the documentation of this file.
00001 /*
00002 
00003 AscTec AutoPilot HL SDK v2.0
00004 
00005 Copyright (c) 2011, Ascending Technologies GmbH
00006 All rights reserved.
00007 
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010 
00011  * Redistributions of source code must retain the above copyright notice,
00012    this list of conditions and the following disclaimer.
00013  * Redistributions in binary form must reproduce the above copyright
00014    notice, this list of conditions and the following disclaimer in the
00015    documentation and/or other materials provided with the distribution.
00016 
00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
00018 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
00021 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00023 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00024 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 DAMAGE.
00028 
00029  */
00030 
00031 /**********************************************************
00032                   Header files
00033  **********************************************************/
00034 #include "LPC214x.h"
00035 #include "main.h"
00036 #include "system.h"
00037 #include "uart.h"
00038 #include "mymath.h"
00039 #include "hardware.h"
00040 #include "irq.h"
00041 #include "i2c.h"
00042 #include "gpsmath.h"
00043 #include "adc.h"
00044 #include "uart.h"
00045 #include "ssp.h"
00046 #include "LL_HL_comm.h"
00047 #include "sdk.h"
00048 #include "buzzer.h"
00049 #include "ublox.h"
00050 #include "pelican_ptu.h"
00051 #include "declination.h"
00052 
00053 /* *********************************************************
00054                Function declarations
00055   ********************************************************* */
00056 
00057 void Initialize(void);
00058 void feed(void);
00059 void beeper(unsigned char);
00060 
00061 /**********************************************************
00062                   Global Variables
00063  **********************************************************/
00064 struct HL_STATUS HL_Status;
00065 struct IMU_CALCDATA IMU_CalcData, IMU_CalcData_tmp;
00066 struct GPS_TIME GPS_Time;
00067 
00068 volatile unsigned int int_cnt=0, cnt=0, mainloop_cnt=0;
00069 volatile unsigned char mainloop_trigger=0;
00070 volatile unsigned int GPS_timeout=0;
00071 volatile unsigned int trigger_cnt=0;
00072 volatile char SYSTEM_initialized=0;
00073 
00074 unsigned int uart_cnt;
00075 unsigned char DataOutputsPerSecond=10;
00076 
00077 
00078 void timer0ISR(void) __irq
00079 {
00080   T0IR = 0x01;      //Clear the timer 0 interrupt
00081   IENABLE;
00082   trigger_cnt++;
00083   if(trigger_cnt==ControllerCyclesPerSecond)
00084   {
00085         trigger_cnt=0;
00086         HL_Status.up_time++;
00087         HL_Status.cpu_load=mainloop_cnt;
00088 
00089         mainloop_cnt=0;
00090   }
00091 
00092   if(mainloop_trigger<10) mainloop_trigger++;
00093 
00094   IDISABLE;
00095   VICVectAddr = 0;              // Acknowledge Interrupt
00096 }
00097 
00098 /**********************************************************
00099                        MAIN
00100 **********************************************************/
00101 int     main (void) {
00102 
00103   static int vbat1; //battery_voltage (lowpass-filtered)
00104 
00105   init();
00106   buzzer(OFF);
00107   LL_write_init();
00108   PTU_init();
00109   ADC0triggerSampling(1<<VOLTAGE_1); //activate ADC sampling
00110 
00111   HL_Status.up_time=0;
00112 
00113   LED(1,ON);
00114 
00115   while(1)
00116   {
00117       if(mainloop_trigger)
00118       {
00119         if(GPS_timeout<ControllerCyclesPerSecond) GPS_timeout++;
00120                 else if(GPS_timeout==ControllerCyclesPerSecond)
00121                 {
00122                         GPS_timeout=ControllerCyclesPerSecond+1;
00123                         GPS_Data.status=0;
00124                         GPS_Data.numSV=0;
00125                 }
00126 
00127         //battery monitoring
00128         ADC0getSamplingResults(0xFF,adcChannelValues);
00129         vbat1=(vbat1*14+(adcChannelValues[VOLTAGE_1]*9872/579))/15;     //voltage in mV
00130 
00131                 HL_Status.battery_voltage_1=vbat1;
00132         mainloop_cnt++;
00133                 if(!(mainloop_cnt%10)) buzzer_handler(HL_Status.battery_voltage_1);
00134 
00135             if(mainloop_trigger) mainloop_trigger--;
00136         mainloop();
00137       }
00138   }
00139   return 0;
00140 }
00141 
00142 
00143 void mainloop(void) //mainloop is triggered at 1 kHz
00144 {
00145     static unsigned char led_cnt=0, led_state=1;
00146         unsigned char t;
00147 
00148         //blink red led if no GPS lock available
00149         led_cnt++;
00150         if((GPS_Data.status&0xFF)==0x03)
00151         {
00152                 LED(0,OFF);
00153         }
00154         else
00155         {
00156             if(led_cnt==150)
00157             {
00158               LED(0,ON);
00159             }
00160             else if(led_cnt==200)
00161             {
00162               led_cnt=0;
00163               LED(0,OFF);
00164             }
00165         }
00166 
00167         //after first lock, determine magnetic inclination and declination
00168         if (SYSTEM_initialized)
00169         {
00170                 if ((!declinationAvailable) && (GPS_Data.horizontal_accuracy<10000) && ((GPS_Data.status&0x03)==0x03)) //make sure GPS lock is valid
00171                 {
00172                         int status;
00173                         estimatedDeclination=getDeclination(GPS_Data.latitude,GPS_Data.longitude,GPS_Data.height/1000,2012,&status);
00174                         if (estimatedDeclination<-32000) estimatedDeclination=-32000;
00175                         if (estimatedDeclination>32000) estimatedDeclination=32000;
00176                         declinationAvailable=1;
00177                 }
00178         }
00179 
00180         //toggle green LED and update SDK input struct when GPS data packet is received
00181     if (gpsLEDTrigger)
00182     {
00183                 if(led_state)
00184                 {
00185                         led_state=0;
00186                         LED(1,OFF);
00187                 }
00188                 else
00189                 {
00190                         LED(1,ON);
00191                         led_state=1;
00192                 }
00193 
00194                 RO_ALL_Data.GPS_height=GPS_Data.height;
00195                 RO_ALL_Data.GPS_latitude=GPS_Data.latitude;
00196                 RO_ALL_Data.GPS_longitude=GPS_Data.longitude;
00197                 RO_ALL_Data.GPS_speed_x=GPS_Data.speed_x;
00198                 RO_ALL_Data.GPS_speed_y=GPS_Data.speed_y;
00199                 RO_ALL_Data.GPS_status=GPS_Data.status;
00200                 RO_ALL_Data.GPS_sat_num=GPS_Data.numSV;
00201                 RO_ALL_Data.GPS_week=GPS_Time.week;
00202                 RO_ALL_Data.GPS_time_of_week=GPS_Time.time_of_week;
00203                 RO_ALL_Data.GPS_heading=GPS_Data.heading;
00204                 RO_ALL_Data.GPS_position_accuracy=GPS_Data.horizontal_accuracy;
00205                 RO_ALL_Data.GPS_speed_accuracy=GPS_Data.speed_accuracy;
00206                 RO_ALL_Data.GPS_height_accuracy=GPS_Data.vertical_accuracy;
00207 
00208                 gpsLEDTrigger=0;
00209     }
00210 
00211         //re-trigger UART-transmission if it was paused by modem CTS pin
00212         if(trigger_transmission)
00213         {
00214                 if(!(IOPIN0&(1<<CTS_RADIO)))
00215                 {
00216                         trigger_transmission=0;
00217                     if(ringbuffer(RBREAD, &t, 1))
00218                     {
00219                       transmission_running=1;
00220                       UARTWriteChar(t);
00221                     }
00222                 }
00223         }
00224 
00225         //send attitude data packet as an example how to use HL_serial_0 (please refer to uart.c for details)
00226     if(uart_cnt++==ControllerCyclesPerSecond/DataOutputsPerSecond)
00227     {
00228         uart_cnt=0;
00229         if((sizeof(RO_ALL_Data))<ringbuffer(RBFREE, 0, 0))
00230         {
00231                 UART_SendPacket(&RO_ALL_Data, sizeof(RO_ALL_Data), PD_RO_ALL_DATA);
00232         }
00233     }
00234 
00235     //handle gps data reception
00236     uBloxReceiveEngine();
00237 
00238         //run SDK mainloop. Please put all your data handling / controller code in sdk.c
00239         SDK_mainloop();
00240 
00241     //write data to transmit buffer for immediate transfer to LL processor
00242     HL2LL_write_cycle();
00243 
00244     //control pan-tilt-unit ("cam option 4" @ AscTec Pelican)
00245     PTU_update();
00246 
00247 
00248 }
00249 
00250 


asctec_hl_firmware
Author(s): Markus Achtelik, Michael Achtelik, Stephan Weiss, Laurent Kneip
autogenerated on Tue Dec 17 2013 11:39:27