Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1112 | thjac | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + only for non-profit use |
||
4 | // + www.MikroKopter.com |
||
5 | // + see the File "License.txt" for further Informations |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | |||
8 | #include "main.h" |
||
9 | |||
10 | volatile int Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100; |
||
11 | volatile int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0; |
||
12 | volatile int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0; |
||
13 | volatile char MessanzahlNick = 0, MessanzahlRoll = 0, MessanzahlGier = 0; |
||
14 | volatile char messanzahl_AccNick = 0, messanzahl_AccRoll = 0, messanzahl_AccHoch = 0; |
||
15 | volatile long Luftdruck = 32000; |
||
16 | volatile int StartLuftdruck; |
||
17 | volatile unsigned int MessLuftdruck = 1023; |
||
18 | unsigned char DruckOffsetSetting; |
||
19 | signed char ExpandBaro = 0; |
||
20 | volatile int HoeheD = 0; |
||
21 | volatile char messanzahl_Druck; |
||
22 | volatile int tmpLuftdruck; |
||
23 | volatile unsigned int ZaehlMessungen = 0; |
||
24 | unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115; |
||
25 | unsigned char GyroDefektN = 0,GyroDefektR = 0,GyroDefektG = 0; |
||
26 | |||
27 | int airPressure = 0; |
||
28 | |||
29 | |||
30 | //####################################################################################### |
||
31 | // |
||
32 | void ADC_Init(void) |
||
33 | //####################################################################################### |
||
34 | { |
||
35 | ADMUX = 0;//Referenz ist extern |
||
36 | ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE); |
||
37 | //Free Running Mode, Division Factor 128, Interrupt on |
||
38 | } |
||
39 | |||
40 | void SucheLuftruckOffset(void) |
||
41 | { |
||
42 | unsigned int off; |
||
43 | off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]); |
||
44 | if(off > 20) off -= 10; |
||
45 | OCR0A = off; |
||
46 | ExpandBaro = 0; |
||
47 | Delay_ms_Mess(100); |
||
48 | if(MessLuftdruck < 850) off = 0; |
||
49 | for(; off < 250;off++) |
||
50 | { |
||
51 | OCR0A = off; |
||
52 | Delay_ms_Mess(50); |
||
53 | printf("."); |
||
54 | if(MessLuftdruck < 850) break; |
||
55 | } |
||
56 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off); |
||
57 | DruckOffsetSetting = off; |
||
58 | Delay_ms_Mess(300); |
||
59 | } |
||
60 | |||
61 | void SucheGyroOffset(void) |
||
62 | { |
||
63 | unsigned char i, ready = 0; |
||
64 | GyroDefektN = 0; GyroDefektR = 0; GyroDefektG = 0; |
||
65 | for(i=140; i != 0; i--) |
||
66 | { |
||
67 | if(ready == 3 && i > 10) i = 9; |
||
68 | ready = 0; |
||
69 | if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++; |
||
70 | if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++; |
||
71 | if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++; |
||
72 | twi_state = 8; |
||
73 | i2c_start(); |
||
74 | if(AnalogOffsetNick < 10) { GyroDefektN = 1; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { GyroDefektN = 1; AnalogOffsetNick = 245;}; |
||
75 | if(AnalogOffsetRoll < 10) { GyroDefektR = 1; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { GyroDefektR = 1; AnalogOffsetRoll = 245;}; |
||
76 | if(AnalogOffsetGier < 10) { GyroDefektG = 1; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { GyroDefektG = 1; AnalogOffsetGier = 245;}; |
||
77 | while(twi_state); |
||
78 | messanzahl_Druck = 0; |
||
79 | ANALOG_ON; |
||
80 | while(messanzahl_Druck == 0); |
||
81 | if(i<10) Delay_ms_Mess(10); |
||
82 | } |
||
83 | Delay_ms_Mess(70); |
||
84 | } |
||
85 | |||
86 | |||
87 | //####################################################################################### |
||
88 | // |
||
89 | SIGNAL(SIG_ADC) |
||
90 | //####################################################################################### |
||
91 | { |
||
92 | static unsigned char kanal=0,state = 0; |
||
93 | static unsigned int gier1, roll1, nick1; |
||
94 | ANALOG_OFF; |
||
95 | switch(state++) |
||
96 | { |
||
97 | case 0: |
||
98 | gier1 = ADC; |
||
99 | kanal = 1; |
||
100 | ZaehlMessungen++; |
||
101 | break; |
||
102 | case 1: |
||
103 | roll1 = ADC; |
||
104 | kanal = 2; |
||
105 | break; |
||
106 | case 2: |
||
107 | nick1 = ADC; |
||
108 | kanal = 4; |
||
109 | break; |
||
110 | case 3: |
||
111 | UBat = (3 * UBat + ADC / 3) / 4;//(UBat + ((ADC * 39) / 256) + 19) / 2; |
||
112 | kanal = 6; |
||
113 | break; |
||
114 | case 4: |
||
115 | Aktuell_ay = NeutralAccY - ADC; |
||
116 | AdWertAccRoll = Aktuell_ay; |
||
117 | kanal = 7; |
||
118 | break; |
||
119 | case 5: |
||
120 | Aktuell_ax = ADC - NeutralAccX; |
||
121 | AdWertAccNick = Aktuell_ax; |
||
122 | kanal = 0; |
||
123 | break; |
||
124 | case 6: |
||
125 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1) / 2; |
||
126 | else |
||
127 | if(PlatinenVersion == 20) AdWertGier = 1023 - (ADC + gier1); |
||
128 | else AdWertGier = ADC + gier1; |
||
129 | kanal = 1; |
||
130 | break; |
||
131 | case 7: |
||
132 | if(PlatinenVersion == 10) AdWertRoll = (ADC + roll1) / 2; |
||
133 | else AdWertRoll = ADC + roll1; |
||
134 | kanal = 2; |
||
135 | break; |
||
136 | case 8: |
||
137 | if(PlatinenVersion == 10) AdWertNick = (ADC + nick1) / 2; |
||
138 | else AdWertNick = ADC + nick1; |
||
139 | //AdWertNick = 0; |
||
140 | //AdWertNick += Poti2; |
||
141 | kanal = 5; |
||
142 | break; |
||
143 | case 9: |
||
144 | AdWertAccHoch = (signed int) ADC - NeutralAccZ; |
||
145 | // AdWertAccHoch += abs(Aktuell_ay) / 4 + abs(Aktuell_ax) / 4; |
||
146 | if(AdWertAccHoch > 1) |
||
147 | { |
||
148 | if(NeutralAccZ < 750) |
||
149 | { |
||
150 | NeutralAccZ += 0.02; |
||
151 | if(modell_fliegt < 500) NeutralAccZ += 0.1; |
||
152 | } |
||
153 | } |
||
154 | else if(AdWertAccHoch < -1) |
||
155 | { |
||
156 | if(NeutralAccZ > 550) |
||
157 | { |
||
158 | NeutralAccZ-= 0.02; |
||
159 | if(modell_fliegt < 500) NeutralAccZ -= 0.1; |
||
160 | } |
||
161 | } |
||
162 | messanzahl_AccHoch = 1; |
||
163 | Aktuell_az = ADC; |
||
164 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
||
165 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
||
166 | kanal = 3; |
||
167 | break; |
||
168 | case 10: |
||
169 | tmpLuftdruck += ADC; |
||
170 | if(++messanzahl_Druck >= 5) { |
||
171 | MessLuftdruck = ADC; |
||
172 | messanzahl_Druck = 0; |
||
173 | |||
174 | Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4; |
||
175 | airPressure = StartLuftdruck - Luftdruck; |
||
176 | tmpLuftdruck = 0; |
||
177 | |||
178 | DebugOut.Analog[5] = airPressure; |
||
179 | } |
||
180 | |||
181 | kanal = 0; |
||
182 | state = 0; |
||
183 | break; |
||
184 | default: |
||
185 | kanal = 0; |
||
186 | state = 0; |
||
187 | break; |
||
188 | } |
||
189 | ADMUX = kanal; |
||
190 | if(state != 0) ANALOG_ON; |
||
191 | } |