00001 /********************************************************** 00002 Header files 00003 **********************************************************/ 00004 00005 #include "stdio.h" 00006 #include "main.h" 00007 #include "system.h" 00008 #include "uart.h" 00009 #include <math.h> 00010 #include "hardware.h" 00011 #include "irq.h" 00012 #include "i2c.h" 00013 #include "gpsmath.h" 00014 #include "adc.h" 00015 #include "uart.h" 00016 #include "uart1.h" 00017 #include "ssp.h" 00018 #include "LL_HL_comm.h" 00019 #include "sdk.h" 00020 #include <unistd.h> 00021 00022 /* ********************************************************* 00023 Function declarations 00024 ********************************************************* */ 00025 00026 //void Initialize(void); 00027 //void feed(void); 00028 00029 /********************************************************** 00030 Global Variables 00031 **********************************************************/ 00032 struct HL_STATUS HL_Status; 00033 struct IMU_RAWDATA IMU_RawData; 00034 volatile unsigned int int_cnt = 0, cnt = 0, mainloop_cnt = 0; 00035 volatile unsigned char mainloop_trigger = 0; 00036 volatile unsigned int GPS_timeout = 0; 00037 volatile char SYSTEM_initialized=0; //new 00038 00039 //extern unsigned char data_requested; 00040 //extern int ZeroDepth; 00041 00042 volatile unsigned int trigger_cnt = 0; 00043 unsigned int logs_per_second = 0, total_logs_per_second = 0; 00044 00045 unsigned char packets = 0x00; 00046 unsigned char packetsTemp; 00047 unsigned int uart_cnt; 00048 unsigned char DataOutputsPerSecond = 20; 00049 00050 struct IMU_CALCDATA IMU_CalcData, IMU_CalcData_tmp; 00051 struct GPS_TIME GPS_Time; 00052 struct SYSTEM_PERMANENT_DATA SYSTEM_Permanent_Data; 00053 00054 float g_imu_gravity; // as reported in LL units (not exactly mg) 00055 00056 void timer0ISR(void) __irq 00057 { 00058 T0IR = 0x01; //Clear the timer 0 interrupt 00059 IENABLE; 00060 trigger_cnt++; 00061 if (trigger_cnt == ControllerCyclesPerSecond) 00062 { 00063 trigger_cnt = 0; 00064 HL_Status.up_time++; 00065 HL_Status.cpu_load = mainloop_cnt; 00066 00067 mainloop_cnt = 0; 00068 } 00069 00070 if (mainloop_trigger < 10) 00071 mainloop_trigger++; 00072 g_timestamp += ControllerCyclesPerSecond; 00073 00074 IDISABLE; 00075 VICVectAddr = 0; // Acknowledge Interrupt 00076 } 00077 00078 void timer1ISR(void) __irq 00079 { 00080 T1IR = 0x01; //Clear the timer 1 interrupt 00081 IENABLE; 00082 00083 IDISABLE; 00084 VICVectAddr = 0; // Acknowledge Interrupt 00085 } 00086 00087 /********************************************************** 00088 MAIN 00089 **********************************************************/ 00090 int main(void) 00091 { 00092 static int vbat1, vbat2; 00093 int vbat; 00094 static int bat_cnt = 0, bat_warning = 1000; 00095 static char bat_warning_enabled = 1; 00096 00097 IDISABLE; 00098 00099 init(); 00100 LL_write_init(); 00101 00102 HL_Status.up_time = 0; 00103 00104 printf("\n\nProgramm is running ... \n"); 00105 printf("Processor Clock Frequency: %d Hz\n", processorClockFrequency()); 00106 printf("Peripheral Clock Frequency: %d Hz\n", peripheralClockFrequency()); 00107 00108 IENABLE; 00109 00110 packetsTemp = packets; 00111 00112 LED(1, ON); 00113 00114 sdkInit(); 00115 00116 beeper(OFF); 00117 wait(5000000); 00118 calibrate(); 00119 //g_imu_gravity = 1022.0; // TODO it this correct?? 00120 while (1) 00121 { 00122 if (mainloop_trigger > 0) 00123 { 00124 mainloop_cnt++; 00125 if (++bat_cnt == 100) 00126 bat_cnt = 0; 00127 00128 //battery monitoring 00129 vbat1 = (vbat1 * 29 + (ADC0Read(VOLTAGE_1) * 9872 / 579)) / 30; //voltage in mV //*9872/579 00130 00131 HL_Status.battery_voltage_1 = vbat1; 00132 HL_Status.battery_voltage_2 = vbat2; 00133 00134 vbat = vbat1; 00135 00136 if (vbat < BATTERY_WARNING_VOLTAGE) //decide if it's really an empty battery 00137 { 00138 if (bat_warning < ControllerCyclesPerSecond * 2) 00139 bat_warning++; 00140 else 00141 bat_warning_enabled = 1; 00142 } 00143 else 00144 { 00145 if (bat_warning > 10) 00146 bat_warning -= 5; 00147 else 00148 { 00149 bat_warning_enabled = 0; 00150 beeper(OFF); 00151 } 00152 } 00153 if (bat_warning_enabled) 00154 { 00155 if (bat_cnt > ((vbat - 9000) / BAT_DIV)) 00156 beeper(ON); 00157 else 00158 beeper(OFF); 00159 } 00160 00161 if (mainloop_trigger) 00162 mainloop_trigger--; 00163 mainloop(); 00164 } 00165 } 00166 return 0; 00167 } 00168 00169 00170 void mainloop(void) 00171 { 00172 SDK_mainloop(); 00173 00174 HL2LL_write_cycle(); //write data to transmit buffer for immediate transfer to LL processor 00175 } 00176 00177 void calibrate() 00178 { 00179 double sum = 0.0; 00180 00181 for (int i = 0; i < 2000; ++i) 00182 { 00183 wait(1200); // wait 1 ms 00184 HL2LL_write_cycle(); 00185 double ax = LL_1khz_attitude_data.acc_x; 00186 double ay = LL_1khz_attitude_data.acc_y; 00187 double az = LL_1khz_attitude_data.acc_z; 00188 00189 double a = sqrt(ax*ax + ay*ay + az*az); 00190 sum +=a; 00191 } 00192 00193 g_imu_gravity = sum / 2000.0; 00194 } 00195