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