Rev 20 | Rev 22 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 20 | Rev 21 | ||
---|---|---|---|
Line 55... | Line 55... | ||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <avr/interrupt.h> |
57 | #include <avr/interrupt.h> |
58 | #include <math.h> |
58 | #include <math.h> |
59 | #include <stdlib.h> |
59 | #include <stdlib.h> |
- | 60 | #include <stdio.h> |
|
Line 60... | Line 61... | ||
60 | 61 | ||
61 | #include "main.h" |
62 | #include "main.h" |
62 | #include "timer0.h" |
63 | #include "timer0.h" |
63 | #include "twislave.h" |
64 | #include "twislave.h" |
Line 68... | Line 69... | ||
68 | 69 | ||
69 | int16_t RawMagnet1a, RawMagnet1b; // raw AD-Data |
70 | int16_t RawMagnet1a, RawMagnet1b; // raw AD-Data |
70 | int16_t RawMagnet2a, RawMagnet2b; |
71 | int16_t RawMagnet2a, RawMagnet2b; |
Line -... | Line 72... | ||
- | 72 | int16_t RawMagnet3a, RawMagnet3b; |
|
- | 73 | ||
71 | int16_t RawMagnet3a, RawMagnet3b; |
74 | uint16_t Led_Timer = 0; |
72 | 75 | ||
73 | typedef struct |
76 | typedef struct |
74 | { |
77 | { |
75 | int16_t Range; |
78 | int16_t Range; |
Line 85... | Line 88... | ||
85 | 88 | ||
86 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
89 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
Line 87... | Line 90... | ||
87 | Calibration_t Calibration; // calibration data in RAM |
90 | Calibration_t Calibration; // calibration data in RAM |
88 | 91 | ||
Line 89... | Line 92... | ||
89 | 92 | ||
Line 90... | Line 93... | ||
90 | int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ; // sensor signal difference without Scaling |
93 | int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ; // sensor signal difference without Scaling |
Line 91... | Line 94... | ||
91 | int16_t MagnetX, MagnetY, MagnetZ; // rescaled magnetic field readings |
94 | int16_t MagnetX, MagnetY, MagnetZ; // rescaled magnetic field readings |
92 | 95 | ||
93 | uint8_t PC_Connected = 0; |
96 | uint8_t PC_Connected = 0; |
Line 110... | Line 113... | ||
110 | void CalcHeading(void) |
113 | void CalcHeading(void) |
111 | { |
114 | { |
112 | double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz; |
115 | double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz; |
113 | int16_t heading = -1; |
116 | int16_t heading = -1; |
Line -... | Line 117... | ||
- | 117 | ||
- | 118 | // blink code for normal operation |
|
- | 119 | if(CheckDelay(Led_Timer)) |
|
- | 120 | { |
|
- | 121 | LED_GRN_TOGGLE; |
|
- | 122 | Led_Timer = SetDelay(500); |
|
- | 123 | } |
|
Line 114... | Line 124... | ||
114 | 124 | ||
115 | 125 | ||
116 | Cx = MagnetX; |
126 | Cx = MagnetX; |
Line 144... | Line 154... | ||
144 | 154 | ||
145 | 155 | ||
146 | void Calibrate(void) |
156 | void Calibrate(void) |
- | 157 | { |
|
147 | { |
158 | uint8_t cal; |
- | 159 | static uint8_t calold = 0; |
|
148 | uint8_t cal; |
160 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
149 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
161 | static uint8_t blinkcount = 0; |
150 | 162 | /* |
|
151 | // check both sources of communication for calibration request |
163 | // check both sources of communication for calibration request |
- | 164 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
|
- | 165 | else cal = ExternData.CalState;*/ |
|
- | 166 | ||
- | 167 | cal = ExternData.CalState; |
|
- | 168 | ||
- | 169 | // blink code for current calibration state |
|
- | 170 | if(cal) |
|
- | 171 | { |
|
- | 172 | if(CheckDelay(Led_Timer) || (cal != calold)) |
|
- | 173 | { |
|
- | 174 | if(blinkcount & 0x01) LED_GRN_OFF; |
|
- | 175 | else LED_GRN_ON; |
|
- | 176 | ||
- | 177 | // end of blinkcount sequence |
|
- | 178 | if( (blinkcount + 1 ) >= (2 * cal) ) |
|
- | 179 | { |
|
- | 180 | blinkcount = 0; |
|
- | 181 | Led_Timer = SetDelay(1000); |
|
- | 182 | } |
|
- | 183 | else |
|
- | 184 | { |
|
- | 185 | blinkcount++; |
|
- | 186 | Led_Timer = SetDelay(170); |
|
- | 187 | } |
|
- | 188 | } |
|
- | 189 | } |
|
- | 190 | else |
|
- | 191 | { |
|
- | 192 | LED_GRN_OFF; |
|
Line 152... | Line 193... | ||
152 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
193 | } |
153 | else cal = ExternData.CalState; |
194 | |
154 | 195 | ||
155 | // calibration state machine |
196 | // calibration state machine |
156 | switch(cal) |
- | |
157 | { |
197 | switch(cal) |
Line 158... | Line 198... | ||
158 | case 0: // no calibration |
198 | { |
159 | LED_GRN_ON; |
199 | case 0: // no calibration |
160 | break; |
200 | break; |
161 | - | ||
162 | case 1: // 1st step of calibration |
201 | |
163 | // initialize ranges |
202 | case 1: // 1st step of calibration |
164 | // used to change the orientation of the MK3MAG in the horizontal plane |
203 | // initialize ranges |
165 | LED_GRN_OFF; |
204 | // used to change the orientation of the MK3MAG in the horizontal plane |
166 | Xmin = 10000; |
205 | Xmin = 10000; |
167 | Xmax = -10000; |
206 | Xmax = -10000; |
168 | Ymin = 10000; |
207 | Ymin = 10000; |
Line 169... | Line 208... | ||
169 | Ymax = -10000; |
208 | Ymax = -10000; |
170 | Zmin = 10000; |
209 | Zmin = 10000; |
171 | Zmax = -10000; |
- | |
172 | break; |
210 | Zmax = -10000; |
173 | 211 | break; |
|
174 | case 2: // 2nd step of calibration |
212 | |
175 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
213 | case 2: // 2nd step of calibration |
176 | LED_GRN_ON; |
214 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
Line 177... | Line 215... | ||
177 | if(UncalMagnetX < Xmin) Xmin = UncalMagnetX; |
215 | if(UncalMagnetX < Xmin) Xmin = UncalMagnetX; |
178 | if(UncalMagnetX > Xmax) Xmax = UncalMagnetX; |
216 | if(UncalMagnetX > Xmax) Xmax = UncalMagnetX; |
179 | if(UncalMagnetY < Ymin) Ymin = UncalMagnetY; |
- | |
180 | if(UncalMagnetY > Ymax) Ymax = UncalMagnetY; |
217 | if(UncalMagnetY < Ymin) Ymin = UncalMagnetY; |
Line 181... | Line 218... | ||
181 | break; |
218 | if(UncalMagnetY > Ymax) Ymax = UncalMagnetY; |
182 | 219 | break; |
|
183 | case 3: // 3rd step of calibration |
- | |
184 | // used to change the orietation of the MK3MAG vertical to the horizontal plane |
220 | |
185 | LED_GRN_OFF; |
221 | case 3: // 3rd step of calibration |
186 | break; |
222 | // used to change the orietation of the MK3MAG vertical to the horizontal plane |
Line 187... | Line 223... | ||
187 | 223 | break; |
|
188 | case 4: |
224 | |
189 | // find Min and Max of the Z-Sensor |
- | |
190 | LED_GRN_ON; |
- | |
191 | if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ; |
- | |
192 | if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ; |
- | |
193 | break; |
- | |
194 | 225 | case 4: |
|
195 | case 5: |
- | |
196 | LED_GRN_OFF; // Save values |
226 | // find Min and Max of the Z-Sensor |
- | 227 | if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ; |
|
- | 228 | if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ; |
|
- | 229 | break; |
|
- | 230 | ||
- | 231 | case 5: |
|
- | 232 | // Save values |
|
- | 233 | if(cal != calold) // avoid continously wrinting of eeprom! |
|
- | 234 | { |
|
- | 235 | Calibration.X.Range = Xmax - Xmin; |
|
- | 236 | Calibration.X.Offset = (Xmin + Xmax) / 2; |
|
197 | Calibration.X.Range = Xmax - Xmin; |
237 | Calibration.Y.Range = Ymax - Ymin; |
198 | Calibration.X.Offset = (Xmin + Xmax) / 2; |
238 | Calibration.Y.Offset = (Ymin + Ymax) / 2; |
- | 239 | Calibration.Z.Range = Zmax - Zmin; |
|
- | 240 | Calibration.Z.Offset = (Zmin + Zmax) / 2; |
|
- | 241 | if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150)) |
|
- | 242 | { |
|
- | 243 | // indicate write process by setting the led |
|
- | 244 | LED_GRN_ON; |
|
199 | Calibration.Y.Range = Ymax - Ymin; |
245 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t)); |
200 | Calibration.Y.Offset = (Ymin + Ymax) / 2; |
- | |
201 | Calibration.Z.Range = Zmax - Zmin; |
246 | Delay_ms(2000); |
Line 202... | Line 247... | ||
202 | Calibration.Z.Offset = (Zmin + Zmax) / 2; |
247 | // reset led state |
203 | if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150)) |
- | |
204 | { |
248 | LED_GRN_OFF; |
205 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t)); |
249 | // reset blinkcode |
- | 250 | blinkcount = 0; |
|
206 | Delay_ms(2000); |
251 | Led_Timer = SetDelay(1000); |
Line 207... | Line 252... | ||
207 | } |
252 | } |
208 | LED_GRN_ON; |
253 | } |
Line 233... | Line 278... | ||
233 | DebugOut.Analog[13] = Calibration.Z.Range; |
278 | DebugOut.Analog[13] = Calibration.Z.Range; |
234 | DebugOut.Analog[14] = ExternData.CalState; |
279 | DebugOut.Analog[14] = ExternData.CalState; |
235 | DebugOut.Analog[15] = Heading; |
280 | DebugOut.Analog[15] = Heading; |
236 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
281 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
237 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
282 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
- | 283 | DebugOut.Analog[31] = PC_Connected; |
|
238 | } |
284 | } |
Line 239... | Line 285... | ||
239 | 285 | ||
240 | 286 | ||
Line 245... | Line 291... | ||
245 | TIMER0_Init(); |
291 | TIMER0_Init(); |
246 | USART0_Init(); |
292 | USART0_Init(); |
247 | ADC_Init(); |
293 | ADC_Init(); |
248 | I2C_Init(); |
294 | I2C_Init(); |
Line -... | Line 295... | ||
- | 295 | ||
249 | 296 | ||
Line -... | Line 297... | ||
- | 297 | sei(); //Globale Interrupts Einschalten |
|
- | 298 | ||
250 | sei(); //Globale Interrupts Einschalten |
299 | |
- | 300 | ||
Line 251... | Line 301... | ||
251 | 301 | Debug_Timer = SetDelay(100); // Sendeintervall |
|
252 | Debug_Timer = SetDelay(100); // Sendeintervall |
302 | Led_Timer = SetDelay(100); |
Line 253... | Line 303... | ||
253 | 303 | ||
Line 266... | Line 316... | ||
266 | RawMagnet1a = ADC_GetValue(ADC0); |
316 | RawMagnet1a = ADC_GetValue(ADC0); |
267 | RawMagnet2a = -ADC_GetValue(ADC1); |
317 | RawMagnet2a = -ADC_GetValue(ADC1); |
268 | RawMagnet3a = ADC_GetValue(ADC7); |
318 | RawMagnet3a = ADC_GetValue(ADC7); |
269 | Delay_ms(1); |
319 | Delay_ms(1); |
Line -... | Line 320... | ||
- | 320 | ||
270 | 321 | ||
271 | FLIP_HIGH; |
322 | FLIP_HIGH; |
272 | Delay_ms(2); |
323 | Delay_ms(2); |
273 | RawMagnet1b = ADC_GetValue(ADC0); |
324 | RawMagnet1b = ADC_GetValue(ADC0); |
274 | RawMagnet2b = -ADC_GetValue(ADC1); |
325 | RawMagnet2b = -ADC_GetValue(ADC1); |
275 | RawMagnet3b = ADC_GetValue(ADC7); |
326 | RawMagnet3b = ADC_GetValue(ADC7); |
Line 276... | Line 327... | ||
276 | Delay_ms(1); |
327 | Delay_ms(1); |
Line 277... | Line 328... | ||
277 | 328 | ||
- | 329 | CalcFields(); |
|
278 | CalcFields(); |
330 | |
Line 279... | Line 331... | ||
279 | 331 | //if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
|
280 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
332 | if(ExternData.CalState) Calibrate(); |