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