Rev 1937 | Rev 1940 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1937 | Rev 1938 | ||
---|---|---|---|
1 | // ######################## SPI - FlightCtrl ################### |
1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
2 | #include "main.h" |
3 | #include "eeprom.h" |
3 | #include "eeprom.h" |
4 | 4 | ||
5 | 5 | ||
6 | //struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
6 | //struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
7 | //struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
7 | //struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
8 | struct str_ToNaviCtrl ToNaviCtrl; |
8 | struct str_ToNaviCtrl ToNaviCtrl; |
9 | struct str_FromNaviCtrl FromNaviCtrl; |
9 | struct str_FromNaviCtrl FromNaviCtrl; |
10 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
10 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
11 | struct str_SPI_VersionInfo NC_Version; |
11 | struct str_SPI_VersionInfo NC_Version; |
12 | struct str_GPSInfo GPSInfo; |
12 | struct str_GPSInfo GPSInfo; |
13 | 13 | ||
14 | unsigned char SPI_BufferIndex; |
14 | unsigned char SPI_BufferIndex; |
15 | unsigned char SPI_RxBufferIndex; |
15 | unsigned char SPI_RxBufferIndex; |
16 | signed char FromNC_Rotate_C = 32, FromNC_Rotate_S = 0; |
16 | signed char FromNC_Rotate_C = 32, FromNC_Rotate_S = 0; |
17 | 17 | ||
18 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
18 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
19 | unsigned char *SPI_TX_Buffer; |
19 | unsigned char *SPI_TX_Buffer; |
20 | 20 | ||
21 | unsigned char SPITransferCompleted, SPI_ChkSum; |
21 | unsigned char SPITransferCompleted, SPI_ChkSum; |
22 | unsigned char SPI_RxDataValid,NaviDataOkay = 250; |
22 | unsigned char SPI_RxDataValid,NaviDataOkay = 250; |
23 | 23 | ||
24 | unsigned char SPI_CommandSequence[] = {SPI_FCCMD_STICK, SPI_FCCMD_USER, SPI_FCCMD_PARAMETER1, SPI_FCCMD_STICK, SPI_FCCMD_MISC, SPI_FCCMD_VERSION, SPI_FCCMD_STICK, SPI_FCCMD_SERVOS, SPI_FCCMD_ACCU}; |
24 | unsigned char SPI_CommandSequence[] = {SPI_FCCMD_STICK, SPI_FCCMD_USER, SPI_FCCMD_PARAMETER1, SPI_FCCMD_STICK, SPI_FCCMD_MISC, SPI_FCCMD_VERSION, SPI_FCCMD_STICK, SPI_FCCMD_SERVOS, SPI_FCCMD_ACCU}; |
25 | unsigned char SPI_CommandCounter = 0; |
25 | unsigned char SPI_CommandCounter = 0; |
26 | unsigned char NC_ErrorCode = 0; |
26 | unsigned char NC_ErrorCode = 0; |
27 | unsigned char NC_GPS_ModeCharacter = ' '; |
27 | unsigned char NC_GPS_ModeCharacter = ' '; |
28 | signed int POI_KameraNick = 0; // in 0,1° |
28 | signed int POI_KameraNick = 0; // in 0,1° |
29 | vector16_t MagVec = {0,0,0}; |
29 | vector16_t MagVec = {0,0,0}; |
30 | 30 | ||
31 | #ifdef USE_SPI_COMMUNICATION |
31 | #ifdef USE_SPI_COMMUNICATION |
32 | 32 | ||
33 | //------------------------------------------------------ |
33 | //------------------------------------------------------ |
34 | void SPI_MasterInit(void) |
34 | void SPI_MasterInit(void) |
35 | { |
35 | { |
36 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
36 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
37 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
37 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
38 | 38 | ||
39 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
39 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
40 | SPSR = 0;//(1<<SPI2X); |
40 | SPSR = 0;//(1<<SPI2X); |
41 | 41 | ||
42 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
42 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
43 | SPITransferCompleted = 1; |
43 | SPITransferCompleted = 1; |
44 | 44 | ||
45 | //SPDR = 0x00; // dummy write |
45 | //SPDR = 0x00; // dummy write |
46 | 46 | ||
47 | ToNaviCtrl.Sync1 = 0xAA; |
47 | ToNaviCtrl.Sync1 = 0xAA; |
48 | ToNaviCtrl.Sync2 = 0x83; |
48 | ToNaviCtrl.Sync2 = 0x83; |
49 | 49 | ||
50 | ToNaviCtrl.Command = SPI_FCCMD_USER; |
50 | ToNaviCtrl.Command = SPI_FCCMD_USER; |
51 | ToNaviCtrl.IntegralNick = 0; |
51 | ToNaviCtrl.IntegralNick = 0; |
52 | ToNaviCtrl.IntegralRoll = 0; |
52 | ToNaviCtrl.IntegralRoll = 0; |
53 | FromNaviCtrl_Value.SerialDataOkay = 0; |
53 | FromNaviCtrl_Value.SerialDataOkay = 0; |
54 | SPI_RxDataValid = 0; |
54 | SPI_RxDataValid = 0; |
55 | 55 | ||
56 | } |
56 | } |
57 | 57 | ||
58 | //------------------------------------------------------ |
58 | //------------------------------------------------------ |
59 | void SPI_StartTransmitPacket(void) |
59 | void SPI_StartTransmitPacket(void) |
60 | { |
60 | { |
61 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
61 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
62 | if (!SPITransferCompleted) return; |
62 | if (!SPITransferCompleted) return; |
63 | // _delay_us(30); |
63 | // _delay_us(30); |
64 | 64 | ||
65 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
65 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
66 | SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl; |
66 | SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl; |
67 | 67 | ||
68 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
68 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
69 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
69 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
70 | 70 | ||
71 | SPITransferCompleted = 0; |
71 | SPITransferCompleted = 0; |
72 | UpdateSPI_Buffer(); // update buffer |
72 | UpdateSPI_Buffer(); // update buffer |
73 | 73 | ||
74 | SPI_BufferIndex = 1; |
74 | SPI_BufferIndex = 1; |
75 | //ebugOut.Analog[16]++; |
75 | //ebugOut.Analog[16]++; |
76 | // -- Debug-Output --- |
76 | // -- Debug-Output --- |
77 | //---- |
77 | //---- |
78 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
78 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
79 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
79 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
80 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
80 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
81 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; |
81 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; |
82 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
82 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
83 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
83 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
84 | 84 | ||
85 | } |
85 | } |
86 | 86 | ||
87 | //------------------------------------------------------ |
87 | //------------------------------------------------------ |
88 | //SIGNAL(SIG_SPI) |
88 | //SIGNAL(SIG_SPI) |
89 | void SPI_TransmitByte(void) |
89 | void SPI_TransmitByte(void) |
90 | { |
90 | { |
91 | static unsigned char SPI_RXState = 0; |
91 | static unsigned char SPI_RXState = 0; |
92 | unsigned char rxdata; |
92 | unsigned char rxdata; |
93 | static unsigned char rxchksum; |
93 | static unsigned char rxchksum; |
94 | 94 | ||
95 | if (SPITransferCompleted) return; |
95 | if (SPITransferCompleted) return; |
96 | if (!(SPSR & (1 << SPIF))) return; |
96 | if (!(SPSR & (1 << SPIF))) return; |
97 | SendSPI = 4; |
97 | SendSPI = 4; |
98 | 98 | ||
99 | // _delay_us(30); |
99 | // _delay_us(30); |
100 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
100 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
101 | 101 | ||
102 | rxdata = SPDR; |
102 | rxdata = SPDR; |
103 | switch ( SPI_RXState) |
103 | switch ( SPI_RXState) |
104 | { |
104 | { |
105 | case 0: |
105 | case 0: |
106 | 106 | ||
107 | SPI_RxBufferIndex = 0; |
107 | SPI_RxBufferIndex = 0; |
108 | rxchksum = rxdata; |
108 | rxchksum = rxdata; |
109 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
109 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
110 | 110 | ||
111 | break; |
111 | break; |
112 | 112 | ||
113 | case 1: |
113 | case 1: |
114 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
114 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
115 | else SPI_RXState = 0; |
115 | else SPI_RXState = 0; |
116 | break; |
116 | break; |
117 | 117 | ||
118 | case 2: |
118 | case 2: |
119 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
119 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
120 | //DebugOut.Analog[19]++; |
120 | //DebugOut.Analog[19]++; |
121 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl)) |
121 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl)) |
122 | { |
122 | { |
123 | 123 | ||
124 | if (rxdata == rxchksum) |
124 | if (rxdata == rxchksum) |
125 | { |
125 | { |
126 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
126 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
127 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
127 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
128 | SPI_RxDataValid = 1; |
128 | SPI_RxDataValid = 1; |
129 | } |
129 | } |
130 | else |
130 | else |
131 | { |
131 | { |
132 | SPI_RxDataValid = 0; |
132 | SPI_RxDataValid = 0; |
133 | } |
133 | } |
134 | 134 | ||
135 | 135 | ||
136 | SPI_RXState = 0; |
136 | SPI_RXState = 0; |
137 | } |
137 | } |
138 | else rxchksum += rxdata; |
138 | else rxchksum += rxdata; |
139 | break; |
139 | break; |
140 | 140 | ||
141 | } |
141 | } |
142 | 142 | ||
143 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
143 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
144 | { |
144 | { |
145 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
145 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
146 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
146 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
147 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
147 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
148 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
148 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
149 | 149 | ||
150 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
150 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
151 | ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
151 | ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
152 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
152 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
153 | 153 | ||
154 | } |
154 | } |
155 | else SPITransferCompleted = 1; |
155 | else SPITransferCompleted = 1; |
156 | 156 | ||
157 | SPI_BufferIndex++; |
157 | SPI_BufferIndex++; |
158 | } |
158 | } |
159 | 159 | ||
160 | 160 | ||
161 | //------------------------------------------------------ |
161 | //------------------------------------------------------ |
162 | void UpdateSPI_Buffer(void) |
162 | void UpdateSPI_Buffer(void) |
163 | { |
163 | { |
164 | signed int tmp; |
164 | signed int tmp; |
165 | ToNaviCtrl.IntegralNick = (int) (IntegralNick / (long)(EE_Parameter.GyroAccFaktor * 4)); |
165 | ToNaviCtrl.IntegralNick = (int) (IntegralNick / (long)(EE_Parameter.GyroAccFaktor * 4)); |
166 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / (long)(EE_Parameter.GyroAccFaktor * 4)); |
166 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / (long)(EE_Parameter.GyroAccFaktor * 4)); |
167 | ToNaviCtrl.GyroCompass = (10 * ErsatzKompass) / GIER_GRAD_FAKTOR; |
167 | ToNaviCtrl.GyroCompass = (10 * ErsatzKompass) / GIER_GRAD_FAKTOR; |
168 | ToNaviCtrl.GyroGier = (signed int) AdNeutralGier - AdWertGier; |
168 | ToNaviCtrl.GyroGier = (signed int) AdNeutralGier - AdWertGier; |
169 | ToNaviCtrl.AccNick = ((int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc))/4; |
169 | ToNaviCtrl.AccNick = ((int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc))/4; |
170 | ToNaviCtrl.AccRoll = ((int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc))/4; |
170 | ToNaviCtrl.AccRoll = ((int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc))/4; |
171 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
171 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
172 | // ToNaviCtrl.User8 = Parameter_UserParam8; |
172 | // ToNaviCtrl.User8 = Parameter_UserParam8; |
173 | // ToNaviCtrl.CalState = WinkelOut.CalcState; |
173 | // ToNaviCtrl.CalState = WinkelOut.CalcState; |
174 | switch(ToNaviCtrl.Command) // |
174 | switch(ToNaviCtrl.Command) // |
175 | { |
175 | { |
176 | case SPI_FCCMD_USER: |
176 | case SPI_FCCMD_USER: |
177 | ToNaviCtrl.Param.Byte[0] = Parameter_UserParam1; |
177 | ToNaviCtrl.Param.Byte[0] = Parameter_UserParam1; |
178 | ToNaviCtrl.Param.Byte[1] = Parameter_UserParam2; |
178 | ToNaviCtrl.Param.Byte[1] = Parameter_UserParam2; |
179 | ToNaviCtrl.Param.Byte[2] = Parameter_UserParam3; |
179 | ToNaviCtrl.Param.Byte[2] = Parameter_UserParam3; |
180 | ToNaviCtrl.Param.Byte[3] = Parameter_UserParam4; |
180 | ToNaviCtrl.Param.Byte[3] = Parameter_UserParam4; |
181 | ToNaviCtrl.Param.Byte[4] = Parameter_UserParam5; |
181 | ToNaviCtrl.Param.Byte[4] = Parameter_UserParam5; |
182 | ToNaviCtrl.Param.Byte[5] = Parameter_UserParam6; |
182 | ToNaviCtrl.Param.Byte[5] = Parameter_UserParam6; |
183 | ToNaviCtrl.Param.Byte[6] = Parameter_UserParam7; |
183 | ToNaviCtrl.Param.Byte[6] = Parameter_UserParam7; |
184 | ToNaviCtrl.Param.Byte[7] = Parameter_UserParam8; |
184 | ToNaviCtrl.Param.Byte[7] = Parameter_UserParam8; |
185 | ToNaviCtrl.Param.Byte[8] = FC_StatusFlags; |
185 | ToNaviCtrl.Param.Byte[8] = FC_StatusFlags; |
186 | FC_StatusFlags &= ~(FC_STATUS_CALIBRATE | FC_STATUS_START); |
186 | FC_StatusFlags &= ~(FC_STATUS_CALIBRATE | FC_STATUS_START); |
187 | ToNaviCtrl.Param.Byte[9] = GetActiveParamSet(); |
187 | ToNaviCtrl.Param.Byte[9] = GetActiveParamSet(); |
188 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.ComingHomeAltitude; |
188 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.ComingHomeAltitude; |
189 | ToNaviCtrl.Param.Byte[11] = FC_StatusFlags2; |
189 | ToNaviCtrl.Param.Byte[11] = FC_StatusFlags2; |
190 | break; |
190 | break; |
191 | 191 | ||
192 | case SPI_FCCMD_ACCU: |
192 | case SPI_FCCMD_ACCU: |
193 | ToNaviCtrl.Param.Int[0] = Capacity.ActualCurrent; // 0.1A |
193 | ToNaviCtrl.Param.Int[0] = Capacity.ActualCurrent; // 0.1A |
194 | ToNaviCtrl.Param.Int[1] = Capacity.UsedCapacity; // mAh |
194 | ToNaviCtrl.Param.Int[1] = Capacity.UsedCapacity; // mAh |
195 | ToNaviCtrl.Param.Byte[4] = (unsigned char) UBat; // 0.1V |
195 | ToNaviCtrl.Param.Byte[4] = (unsigned char) UBat; // 0.1V |
196 | ToNaviCtrl.Param.Byte[5] = (unsigned char) BattLowVoltageWarning; //0.1V |
196 | ToNaviCtrl.Param.Byte[5] = (unsigned char) BattLowVoltageWarning; //0.1V |
197 | ToNaviCtrl.Param.Byte[6] = VarioCharacter; |
197 | ToNaviCtrl.Param.Byte[6] = VarioCharacter; |
198 | ToNaviCtrl.Param.Byte[7] = Parameter_GlobalConfig; |
198 | ToNaviCtrl.Param.Byte[7] = Parameter_GlobalConfig; |
199 | ToNaviCtrl.Param.Byte[8] = Parameter_ExtraConfig; |
199 | ToNaviCtrl.Param.Byte[8] = Parameter_ExtraConfig; |
200 | break; |
200 | break; |
201 | 201 | ||
202 | case SPI_FCCMD_PARAMETER1: |
202 | case SPI_FCCMD_PARAMETER1: |
203 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl; // Parameters for the Naviboard |
203 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl; // Parameters for the Naviboard |
204 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain; |
204 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain; |
205 | ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP; |
205 | ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP; |
206 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI; |
206 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI; |
207 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD; |
207 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD; |
208 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC; |
208 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC; |
209 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat; |
209 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat; |
210 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold; |
210 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold; |
211 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius; |
211 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius; |
212 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection; |
212 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection; |
213 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviAccCompensation; |
213 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviAccCompensation; |
214 | ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation; |
214 | ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation; |
215 | break; |
215 | break; |
216 | 216 | ||
217 | case SPI_FCCMD_STICK: |
217 | case SPI_FCCMD_STICK: |
218 | cli(); |
218 | cli(); |
219 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
219 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
220 | ToNaviCtrl.Param.Byte[0] = (char) tmp; |
220 | ToNaviCtrl.Param.Byte[0] = (char) tmp; |
221 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
221 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
222 | ToNaviCtrl.Param.Byte[1] = (char) tmp; |
222 | ToNaviCtrl.Param.Byte[1] = (char) tmp; |
223 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
223 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
224 | ToNaviCtrl.Param.Byte[2] = (char) tmp; |
224 | ToNaviCtrl.Param.Byte[2] = (char) tmp; |
225 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
225 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
226 | sei(); |
226 | sei(); |
227 | ToNaviCtrl.Param.Byte[3] = (char) tmp; |
227 | ToNaviCtrl.Param.Byte[3] = (char) tmp; |
228 | ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti[0]; |
228 | ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti[0]; |
229 | ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti[1]; |
229 | ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti[1]; |
230 | ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti[2]; |
230 | ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti[2]; |
231 | ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti[3]; |
231 | ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti[3]; |
232 | ToNaviCtrl.Param.Byte[8] = (unsigned char) Poti[4]; |
232 | ToNaviCtrl.Param.Byte[8] = (unsigned char) Poti[4]; |
233 | ToNaviCtrl.Param.Byte[9] = (unsigned char) Poti[5]; |
233 | ToNaviCtrl.Param.Byte[9] = (unsigned char) Poti[5]; |
234 | ToNaviCtrl.Param.Byte[10] = (unsigned char) Poti[6]; |
234 | ToNaviCtrl.Param.Byte[10] = (unsigned char) Poti[6]; |
235 | ToNaviCtrl.Param.Byte[11] = (unsigned char) Poti[7]; |
235 | ToNaviCtrl.Param.Byte[11] = (unsigned char) Poti[7]; |
236 | break; |
236 | break; |
237 | case SPI_FCCMD_MISC: |
237 | case SPI_FCCMD_MISC: |
238 | if(WinkelOut.CalcState > 5) |
238 | if(WinkelOut.CalcState > 5) |
239 | { |
239 | { |
240 | WinkelOut.CalcState = 0; |
240 | WinkelOut.CalcState = 0; |
241 | ToNaviCtrl.Param.Byte[0] = 5; |
241 | ToNaviCtrl.Param.Byte[0] = 5; |
242 | } |
242 | } |
243 | else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState; |
243 | else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState; |
244 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime; |
244 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime; |
245 | ToNaviCtrl.Param.Int[1] = (int)(HoehenWert/5); |
245 | ToNaviCtrl.Param.Int[1] = (int)(HoehenWert/5); |
246 | ToNaviCtrl.Param.Int[2] = (int)(SollHoehe/5); |
246 | ToNaviCtrl.Param.Int[2] = (int)(SollHoehe/5); |
247 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsPLimit; |
247 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsPLimit; |
248 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviGpsILimit; |
248 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviGpsILimit; |
249 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviGpsDLimit; |
249 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviGpsDLimit; |
250 | ToNaviCtrl.Param.Byte[9] = (unsigned char) SenderOkay; |
250 | ToNaviCtrl.Param.Byte[9] = (unsigned char) SenderOkay; |
251 | ToNaviCtrl.Param.Byte[10] = (unsigned char) PPM_in[0]; |
251 | ToNaviCtrl.Param.Byte[10] = (unsigned char) PPM_in[0]; |
252 | ToNaviCtrl.Param.Byte[11] = DebugOut.Analog[7] / 4; //GasMischanteil |
252 | ToNaviCtrl.Param.Byte[11] = DebugOut.Analog[7] / 4; //GasMischanteil |
253 | break; |
253 | break; |
254 | case SPI_FCCMD_VERSION: |
254 | case SPI_FCCMD_VERSION: |
255 | ToNaviCtrl.Param.Byte[0] = VERSION_MAJOR; |
255 | ToNaviCtrl.Param.Byte[0] = VERSION_MAJOR; |
256 | ToNaviCtrl.Param.Byte[1] = VERSION_MINOR; |
256 | ToNaviCtrl.Param.Byte[1] = VERSION_MINOR; |
257 | ToNaviCtrl.Param.Byte[2] = VERSION_PATCH; |
257 | ToNaviCtrl.Param.Byte[2] = VERSION_PATCH; |
258 | ToNaviCtrl.Param.Byte[3] = NC_SPI_COMPATIBLE; |
258 | ToNaviCtrl.Param.Byte[3] = NC_SPI_COMPATIBLE; |
259 | ToNaviCtrl.Param.Byte[4] = PlatinenVersion; |
259 | ToNaviCtrl.Param.Byte[4] = PlatinenVersion; |
260 | ToNaviCtrl.Param.Byte[5] = VersionInfo.HardwareError[0]; |
260 | ToNaviCtrl.Param.Byte[5] = VersionInfo.HardwareError[0]; |
261 | ToNaviCtrl.Param.Byte[6] = VersionInfo.HardwareError[1]; |
261 | ToNaviCtrl.Param.Byte[6] = VersionInfo.HardwareError[1]; |
262 | VersionInfo.HardwareError[0] = 0; |
262 | VersionInfo.HardwareError[0] = 0; |
263 | VersionInfo.HardwareError[1] &= 0xff - FC_ERROR1_MIXER; |
263 | VersionInfo.HardwareError[1] &= FC_ERROR1_MIXER; |
264 | ToNaviCtrl.Param.Byte[7] = VersionInfo.HardwareError[2]; |
264 | ToNaviCtrl.Param.Byte[7] = VersionInfo.HardwareError[2]; |
265 | ToNaviCtrl.Param.Byte[8] = VersionInfo.HardwareError[3]; |
265 | ToNaviCtrl.Param.Byte[8] = VersionInfo.HardwareError[3]; |
266 | ToNaviCtrl.Param.Byte[9] = VersionInfo.HardwareError[4]; |
266 | ToNaviCtrl.Param.Byte[9] = VersionInfo.HardwareError[4]; |
267 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.OrientationAngle; |
267 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.OrientationAngle; |
268 | break; |
268 | break; |
269 | case SPI_FCCMD_SERVOS: |
269 | case SPI_FCCMD_SERVOS: |
270 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.ServoNickRefresh; // Parameters for the Servo Control |
270 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.ServoNickRefresh; // Parameters for the Servo Control |
271 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.ServoCompInvert; |
271 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.ServoCompInvert; |
272 | ToNaviCtrl.Param.Byte[2] = Parameter_ServoNickControl; |
272 | ToNaviCtrl.Param.Byte[2] = Parameter_ServoNickControl; |
273 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.ServoNickComp; |
273 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.ServoNickComp; |
274 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.ServoNickMin; |
274 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.ServoNickMin; |
275 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.ServoNickMax; |
275 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.ServoNickMax; |
276 | ToNaviCtrl.Param.Byte[6] = Parameter_ServoRollControl; |
276 | ToNaviCtrl.Param.Byte[6] = Parameter_ServoRollControl; |
277 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.ServoRollComp; |
277 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.ServoRollComp; |
278 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.ServoRollMin; |
278 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.ServoRollMin; |
279 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.ServoRollMax; |
279 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.ServoRollMax; |
280 | break; |
280 | break; |
281 | } |
281 | } |
282 | 282 | ||
283 | if(SPI_RxDataValid) |
283 | if(SPI_RxDataValid) |
284 | { |
284 | { |
285 | NaviDataOkay = 250; |
285 | NaviDataOkay = 250; |
286 | CalculateCompassTimer = 1; |
286 | CalculateCompassTimer = 1; |
287 | if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)) |
287 | if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)) |
288 | { |
288 | { |
289 | GPS_Nick = FromNaviCtrl.GPS_Nick; |
289 | GPS_Nick = FromNaviCtrl.GPS_Nick; |
290 | GPS_Roll = FromNaviCtrl.GPS_Roll; |
290 | GPS_Roll = FromNaviCtrl.GPS_Roll; |
291 | } |
291 | } |
292 | 292 | ||
293 | // update compass readings |
293 | // update compass readings |
294 | // MagVec.x = FromNaviCtrl.MagVecX; |
294 | // MagVec.x = FromNaviCtrl.MagVecX; |
295 | // MagVec.y = FromNaviCtrl.MagVecY; |
295 | // MagVec.y = FromNaviCtrl.MagVecY; |
296 | // MagVec.z = FromNaviCtrl.MagVecZ; |
296 | // MagVec.z = FromNaviCtrl.MagVecZ; |
297 | 297 | ||
298 | if(FromNaviCtrl.CompassValue <= 360) KompassValue = FromNaviCtrl.CompassValue; |
298 | if(FromNaviCtrl.CompassValue <= 360) KompassValue = FromNaviCtrl.CompassValue; |
299 | KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
299 | KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
300 | 300 | ||
301 | if(FromNaviCtrl.BeepTime > beeptime /*&& !WinkelOut.CalcState*/) beeptime = FromNaviCtrl.BeepTime; |
301 | if(FromNaviCtrl.BeepTime > beeptime /*&& !WinkelOut.CalcState*/) beeptime = FromNaviCtrl.BeepTime; |
302 | #define FLAG_GPS_AID 0x01 |
302 | #define FLAG_GPS_AID 0x01 |
303 | switch (FromNaviCtrl.Command) |
303 | switch (FromNaviCtrl.Command) |
304 | { |
304 | { |
305 | case SPI_NCCMD_KALMAN: |
305 | case SPI_NCCMD_KALMAN: |
306 | FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.sByte[0]; |
306 | FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.sByte[0]; |
307 | FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.sByte[1]; |
307 | FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.sByte[1]; |
308 | FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.sByte[2]; |
308 | FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.sByte[2]; |
309 | FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3]; |
309 | FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3]; |
310 | FromNaviCtrl_Value.GpsZ = FromNaviCtrl.Param.Byte[4]; |
310 | FromNaviCtrl_Value.GpsZ = FromNaviCtrl.Param.Byte[4]; |
311 | FromNC_Rotate_C = FromNaviCtrl.Param.Byte[5]; |
311 | FromNC_Rotate_C = FromNaviCtrl.Param.Byte[5]; |
312 | FromNC_Rotate_S = FromNaviCtrl.Param.Byte[6]; |
312 | FromNC_Rotate_S = FromNaviCtrl.Param.Byte[6]; |
313 | if(FromNaviCtrl.Param.Byte[7] & FLAG_GPS_AID) GPS_AidMode = 1; else GPS_AidMode = 0; |
313 | if(FromNaviCtrl.Param.Byte[7] & FLAG_GPS_AID) GPS_AidMode = 1; else GPS_AidMode = 0; |
314 | if(CareFree && FromNaviCtrl.Param.sInt[4] >= 0) |
314 | if(CareFree && FromNaviCtrl.Param.sInt[4] >= 0) |
315 | { |
315 | { |
316 | KompassSollWert = FromNaviCtrl.Param.sInt[4]; // bei Carefree kann NC den Kompass-Sollwinkel vorgeben |
316 | KompassSollWert = FromNaviCtrl.Param.sInt[4]; // bei Carefree kann NC den Kompass-Sollwinkel vorgeben |
317 | if(EE_Parameter.CamOrientation) // Kamera angle is not front |
317 | if(EE_Parameter.CamOrientation) // Kamera angle is not front |
318 | { |
318 | { |
319 | KompassSollWert += 360 - ((unsigned int) EE_Parameter.CamOrientation * 15); |
319 | KompassSollWert += 360 - ((unsigned int) EE_Parameter.CamOrientation * 15); |
320 | KompassSollWert %= 360; |
320 | KompassSollWert %= 360; |
321 | } |
321 | } |
322 | } |
322 | } |
323 | POI_KameraNick = (POI_KameraNick + FromNaviCtrl.Param.sInt[5]) / 2; // FromNaviCtrl.Param.sInt[5]; // Nickwinkel |
323 | POI_KameraNick = (POI_KameraNick + FromNaviCtrl.Param.sInt[5]) / 2; // FromNaviCtrl.Param.sInt[5]; // Nickwinkel |
324 | break; |
324 | break; |
325 | case SPI_NCCMD_VERSION: |
325 | case SPI_NCCMD_VERSION: |
326 | NC_Version.Major = FromNaviCtrl.Param.Byte[0]; |
326 | NC_Version.Major = FromNaviCtrl.Param.Byte[0]; |
327 | NC_Version.Minor = FromNaviCtrl.Param.Byte[1]; |
327 | NC_Version.Minor = FromNaviCtrl.Param.Byte[1]; |
328 | NC_Version.Patch = FromNaviCtrl.Param.Byte[2]; |
328 | NC_Version.Patch = FromNaviCtrl.Param.Byte[2]; |
329 | NC_Version.Compatible = FromNaviCtrl.Param.Byte[3]; |
329 | NC_Version.Compatible = FromNaviCtrl.Param.Byte[3]; |
330 | NC_Version.Hardware = FromNaviCtrl.Param.Byte[4]; |
330 | NC_Version.Hardware = FromNaviCtrl.Param.Byte[4]; |
331 | DebugOut.Status[0] |= FromNaviCtrl.Param.Byte[5]; |
331 | DebugOut.Status[0] |= FromNaviCtrl.Param.Byte[5]; |
332 | DebugOut.Status[1] = (DebugOut.Status[1] & (0x01|0x02)) | (FromNaviCtrl.Param.Byte[6] & (0x04 | 0x08)); |
332 | DebugOut.Status[1] = (DebugOut.Status[1] & (0x01|0x02)) | (FromNaviCtrl.Param.Byte[6] & (0x04 | 0x08)); |
333 | NC_ErrorCode = FromNaviCtrl.Param.Byte[7]; |
333 | NC_ErrorCode = FromNaviCtrl.Param.Byte[7]; |
334 | NC_GPS_ModeCharacter = FromNaviCtrl.Param.Byte[8]; |
334 | NC_GPS_ModeCharacter = FromNaviCtrl.Param.Byte[8]; |
335 | break; |
335 | break; |
336 | 336 | ||
337 | case SPI_NCCMD_GPSINFO: |
337 | case SPI_NCCMD_GPSINFO: |
338 | GPSInfo.Flags = FromNaviCtrl.Param.Byte[0]; |
338 | GPSInfo.Flags = FromNaviCtrl.Param.Byte[0]; |
339 | GPSInfo.NumOfSats = FromNaviCtrl.Param.Byte[1]; |
339 | GPSInfo.NumOfSats = FromNaviCtrl.Param.Byte[1]; |
340 | GPSInfo.SatFix = FromNaviCtrl.Param.Byte[2]; |
340 | GPSInfo.SatFix = FromNaviCtrl.Param.Byte[2]; |
341 | GPSInfo.Speed = FromNaviCtrl.Param.Byte[3]; |
341 | GPSInfo.Speed = FromNaviCtrl.Param.Byte[3]; |
342 | GPSInfo.HomeDistance = FromNaviCtrl.Param.Int[2]; |
342 | GPSInfo.HomeDistance = FromNaviCtrl.Param.Int[2]; |
343 | GPSInfo.HomeBearing = FromNaviCtrl.Param.sInt[3]; |
343 | GPSInfo.HomeBearing = FromNaviCtrl.Param.sInt[3]; |
344 | PPM_in[25] = (signed char) FromNaviCtrl.Param.Byte[8]; // WP_EVENT-Channel-Value |
344 | PPM_in[25] = (signed char) FromNaviCtrl.Param.Byte[8]; // WP_EVENT-Channel-Value |
345 | FromNC_AltitudeSpeed = FromNaviCtrl.Param.Byte[9]; |
345 | FromNC_AltitudeSpeed = FromNaviCtrl.Param.Byte[9]; |
346 | FromNC_AltitudeSetpoint = (long) FromNaviCtrl.Param.sInt[5] * 10; // in cm |
346 | FromNC_AltitudeSetpoint = (long) FromNaviCtrl.Param.sInt[5] * 10; // in cm |
347 | break; |
347 | break; |
348 | // 0 = 0,1 |
348 | // 0 = 0,1 |
349 | // 1 = 2,3 |
349 | // 1 = 2,3 |
350 | // 2 = 4,5 |
350 | // 2 = 4,5 |
351 | // 3 = 6,7 |
351 | // 3 = 6,7 |
352 | // 4 = 8,9 |
352 | // 4 = 8,9 |
353 | // 5 = 10,11 |
353 | // 5 = 10,11 |
354 | default: |
354 | default: |
355 | break; |
355 | break; |
356 | } |
356 | } |
357 | } |
357 | } |
358 | else |
358 | else |
359 | { |
359 | { |
360 | // KompassValue = 0; |
360 | // KompassValue = 0; |
361 | // KompassRichtung = 0; |
361 | // KompassRichtung = 0; |
362 | GPS_Nick = 0; |
362 | GPS_Nick = 0; |
363 | GPS_Roll = 0; |
363 | GPS_Roll = 0; |
364 | } |
364 | } |
365 | } |
365 | } |
366 | 366 | ||
367 | #endif |
367 | #endif |
368 | 368 | ||
369 | 369 | ||
370 | 370 |