00001 #include "main.h" 00002 #include "LL_HL_comm.h" 00003 #include "system.h" 00004 #include "gpsmath.h" 00005 #include "sdk.h" 00006 00007 unsigned short SSP_ack=0; 00008 extern char SPIWRData[128]; 00009 extern char data_sent_to_LL; 00010 extern unsigned int SPIWR_num_bytes; 00011 00012 struct LL_ATTITUDE_DATA LL_1khz_attitude_data; 00013 struct LL_CONTROL_INPUT LL_1khz_control_input; 00014 00015 void SSP_data_distribution_HL(void) 00016 { 00017 unsigned char i; 00018 unsigned char current_page=LL_1khz_attitude_data.system_flags&0x03; 00019 00020 if(LL_1khz_attitude_data.system_flags&SF_GPS_NEW) gpsDataOkTrigger=0; 00021 00022 IMU_CalcData.angle_nick=LL_1khz_attitude_data.angle_pitch*10; 00023 IMU_CalcData.angle_roll=LL_1khz_attitude_data.angle_roll*10; 00024 IMU_CalcData.angle_yaw=LL_1khz_attitude_data.angle_yaw*10; 00025 00026 IMU_CalcData.angvel_nick=LL_1khz_attitude_data.angvel_pitch; 00027 IMU_CalcData.angvel_roll=LL_1khz_attitude_data.angvel_roll; 00028 IMU_CalcData.angvel_yaw=LL_1khz_attitude_data.angvel_yaw; 00029 00030 if(!current_page) //page 0 00031 { 00032 for(i=0;i<8;i++) 00033 { 00034 RO_RC_Data.channel[i]=LL_1khz_attitude_data.RC_data[i]*16; 00035 } 00036 IMU_CalcData.acc_x_calib=LL_1khz_attitude_data.acc_x*10; 00037 IMU_CalcData.acc_y_calib=LL_1khz_attitude_data.acc_y*10; 00038 IMU_CalcData.acc_z_calib=LL_1khz_attitude_data.acc_z*10; 00039 } 00040 else if(current_page==1) //page 1 00041 { 00042 IMU_CalcData.height=LL_1khz_attitude_data.height; 00043 IMU_CalcData.dheight=LL_1khz_attitude_data.dheight; 00044 } 00045 else if(current_page==2) 00046 { 00047 IMU_CalcData.Hx=LL_1khz_attitude_data.mag_x; 00048 IMU_CalcData.Hy=LL_1khz_attitude_data.mag_y; 00049 IMU_CalcData.Hz=LL_1khz_attitude_data.mag_z; 00050 } 00051 } 00052 00053 int HL2LL_write_cycle(void) //write data to low-level processor 00054 { 00055 static char pageselect=0; 00056 00057 if(!data_sent_to_LL) return(0); 00058 00059 //update 1kHz data 00060 LL_1khz_control_input.system_flags=0|pageselect; 00061 //SSP_ack=0; //reset ack 00062 00063 if(gpsDataOkTrigger) LL_1khz_control_input.system_flags|=SF_GPS_NEW; 00064 00065 #ifndef FALCON 00066 if(WO_SDK.ctrl_enabled) LL_1khz_control_input.system_flags|=SF_HL_CONTROL_ENABLED; 00067 else LL_1khz_control_input.system_flags&=~SF_HL_CONTROL_ENABLED; 00068 00069 if(WO_SDK.ctrl_mode==0x01) LL_1khz_control_input.system_flags|=SF_DIRECT_MOTOR_CONTROL; 00070 else LL_1khz_control_input.system_flags&=~SF_DIRECT_MOTOR_CONTROL; 00071 #else //Disable Control Input if system is a FALCON 00072 LL_1khz_control_input.system_flags&=~SF_HL_CONTROL_ENABLED; 00073 LL_1khz_control_input.system_flags&=~SF_DIRECT_MOTOR_CONTROL; 00074 #endif 00075 00076 00077 00078 LL_1khz_control_input.ctrl_flags=WO_CTRL_Input.ctrl; 00079 LL_1khz_control_input.pitch=WO_CTRL_Input.pitch; 00080 LL_1khz_control_input.roll=WO_CTRL_Input.roll; 00081 LL_1khz_control_input.yaw=WO_CTRL_Input.yaw; 00082 LL_1khz_control_input.thrust=WO_CTRL_Input.thrust; 00083 00084 if(WO_SDK.ctrl_mode==0x01) 00085 { 00086 LL_1khz_control_input.direct_motor_control[0]=WO_Direct_Motor_Control.pitch; 00087 LL_1khz_control_input.direct_motor_control[1]=WO_Direct_Motor_Control.roll; 00088 LL_1khz_control_input.direct_motor_control[2]=WO_Direct_Motor_Control.yaw; 00089 LL_1khz_control_input.direct_motor_control[3]=WO_Direct_Motor_Control.thrust; 00090 } 00091 00092 /* for(i=0;i<8;i++) 00093 { 00094 LL_1khz_control_input.direct_motor_control[i]=0; 00095 } 00096 */ 00097 if(pageselect==0) 00098 { 00099 //fill struct with 500Hz data 00100 LL_1khz_control_input.latitude=GPS_Data.latitude; 00101 LL_1khz_control_input.longitude=GPS_Data.longitude; 00102 LL_1khz_control_input.height=GPS_Data.height; 00103 LL_1khz_control_input.speed_x=GPS_Data.speed_x; 00104 LL_1khz_control_input.speed_y=GPS_Data.speed_y; 00105 LL_1khz_control_input.heading=GPS_Data.heading; 00106 LL_1khz_control_input.status=GPS_Data.status; 00107 00108 //write data 00109 LL_write_ctrl_data(pageselect); 00110 //set pageselect to other page for next cycle 00111 pageselect=1; 00112 } 00113 else //pageselect=1 00114 { 00115 //fill struct with 500Hz data 00116 LL_1khz_control_input.hor_accuracy=GPS_Data.horizontal_accuracy; 00117 LL_1khz_control_input.vert_accuracy=GPS_Data.vertical_accuracy; 00118 LL_1khz_control_input.speed_accuracy=GPS_Data.speed_accuracy; 00119 LL_1khz_control_input.numSV=GPS_Data.numSV; 00120 LL_1khz_control_input.battery_voltage_1=HL_Status.battery_voltage_1; 00121 LL_1khz_control_input.battery_voltage_2=HL_Status.battery_voltage_2; 00122 LL_1khz_control_input.dummy_500Hz_2=0; 00123 LL_1khz_control_input.dummy_500Hz_3=0; 00124 00125 //write data 00126 LL_write_ctrl_data(pageselect); 00127 //set pageselect to other page for next cycle 00128 pageselect=0; 00129 } 00130 return(1); 00131 } 00132 00133 void LL_write_ctrl_data(char page) 00134 { 00135 unsigned int i; 00136 unsigned char *dataptr; 00137 static volatile short spi_chksum; 00138 00139 dataptr=(unsigned char *)&LL_1khz_control_input; 00140 00141 //initialize syncbytes 00142 SPIWRData[0]='>'; 00143 SPIWRData[1]='*'; 00144 00145 spi_chksum=0xAAAA; 00146 00147 if(!page) 00148 { 00149 for(i=2;i<40;i++) 00150 { 00151 SPIWRData[i]=*dataptr++; 00152 spi_chksum+=SPIWRData[i]; 00153 } 00154 } 00155 else 00156 { 00157 for(i=2;i<22;i++) 00158 { 00159 SPIWRData[i]=*dataptr++; 00160 spi_chksum+=SPIWRData[i]; 00161 } 00162 dataptr+=18; 00163 for(i=22;i<40;i++) 00164 { 00165 SPIWRData[i]=*dataptr++; 00166 spi_chksum+=SPIWRData[i]; 00167 } 00168 } 00169 00170 SPIWRData[40]=spi_chksum; //chksum LSB 00171 SPIWRData[41]=(spi_chksum>>8); //chksum MSB 00172 00173 SPIWR_num_bytes=42; 00174 data_sent_to_LL=0; 00175 } 00176 00177 00178 inline void SSP_rx_handler_HL(unsigned char SPI_rxdata) //rx_handler @ high-level processor 00179 { 00180 static volatile unsigned char SPI_syncstate=0; 00181 static volatile unsigned char SPI_rxcount=0; 00182 static volatile unsigned char *SPI_rxptr; 00183 static volatile unsigned char incoming_page; 00184 00185 //receive handler 00186 if (SPI_syncstate==0) 00187 { 00188 if (SPI_rxdata=='>') SPI_syncstate++; else SPI_syncstate=0; 00189 } 00190 else if (SPI_syncstate==1) 00191 { 00192 if (SPI_rxdata=='*') 00193 { 00194 SPI_syncstate++; 00195 SPI_rxptr=(unsigned char *)&LL_1khz_attitude_data; 00196 SPI_rxcount=40; 00197 } 00198 else SPI_syncstate=0; 00199 } 00200 else if (SPI_syncstate==2) 00201 { 00202 if(SPI_rxcount==26) //14 bytes transmitted => select 500Hz page 00203 { 00204 incoming_page=LL_1khz_attitude_data.system_flags&0x03; //system flags were already received 00205 if(incoming_page==1) SPI_rxptr+=26; 00206 else if(incoming_page==2) SPI_rxptr+=52; 00207 } 00208 SPI_rxcount--; 00209 *SPI_rxptr=SPI_rxdata; 00210 SPI_rxptr++; 00211 if (SPI_rxcount==0) 00212 { 00213 SPI_syncstate++; 00214 } 00215 } 00216 else if (SPI_syncstate==3) 00217 { 00218 if(SPI_rxdata=='<') //last byte ok => data should be valid 00219 { 00220 SSP_data_distribution_HL(); //only distribute data to other structs, if it was received correctly 00221 //ack data receiption 00222 } 00223 SPI_syncstate=0; 00224 } 00225 else SPI_syncstate=0; 00226 } 00227