Subversion Repositories FlightCtrl

Rev

Rev 838 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
838 MikeW 1
/*
2
Copyright 2008, by Michael Walter
3
 
4
All functions written by Michael Walter are free software and can be redistributed and/or modified under the terms of the GNU Lesser
5
General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but
6
WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public
8
License along with this program. If not, see <http://www.gnu.org/licenses/>.
9
 
10
Please note: The software is based on the framework provided by H. Buss and I. Busker in their Mikrokopter projekt. All functions that
11
are not written by Michael Walter are under the license by H. Buss and I. Busker (license_buss.txt) published by www.mikrokopter.de
12
unless it is stated otherwise.
13
*/
14
 
15
#include "main.h"
966 MikeW 16
#include "FlightControl.h"
838 MikeW 17
 
18
 
966 MikeW 19
int UBat = 100; /* Initial Battery Guess */
20
int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0;
21
int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
22
int AdWertNick_Raw = 0, AdWertRoll_Raw = 0, AdWertGier_Raw = 0;
23
int AdWertAccRoll_Raw = 0,AdWertAccNick_Raw = 0,AdWertAccHoch_Raw = 0;
24
 
838 MikeW 25
int AccumulatedACC_X = 0, AccumulatedACC_Y = 0, AccumulatedACC_Z = 0, AccumulatedAirPressure = 0;
26
int AccumulatedACC_X_cnt = 0, AccumulatedACC_Y_cnt = 0, AccumulatedACC_Z_cnt = 0, AccumulatedAirPressure_cnt = 0;
27
int AccumulatedRoll_X = 0, AccumulatedRoll_Y = 0, AccumulatedRoll_Z = 0;
28
int AccumulatedRoll_X_cnt = 0, AccumulatedRoll_Y_cnt = 0, AccumulatedRoll_Z_cnt = 0;
29
 
966 MikeW 30
unsigned int AdWertAirPressure_Raw = 1023;
838 MikeW 31
 
32
/* ****************************************************************************
33
Functionname:     ADC_Init                      */ /*!
34
Description:
35
 
36
  @return           void
37
  @pre              -
38
  @post             -
39
  @author           H. Buss / I. Busker
40
**************************************************************************** */
41
void ADC_Init(void)
42
{
966 MikeW 43
#ifdef INTERNAL_REFERENCE 
44
  ADMUX = 64;/* Internal Reference 5V */
45
#else
46
  ADMUX = 0; /* External Reference */
47
#endif
48
  ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
838 MikeW 49
}
50
 
51
/* ****************************************************************************
52
Functionname:     SucheLuftruckOffset                      */ /*!
53
Description:
54
 
55
  @return           void
56
  @pre              -
57
  @post             -
58
  @author           H. Buss / I. Busker
59
**************************************************************************** */
60
void SucheLuftruckOffset(void)
61
{
966 MikeW 62
  unsigned int off;
63
  off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]);
64
  if(off > 20) off -= 10;
65
  OCR0A = off;
66
  Delay_ms_Mess(100);
67
  if(AdWertAirPressure_Raw < 850) off = 0;
68
  for(; off < 250;off++)
69
  {
70
    OCR0A = off;
71
    Delay_ms_Mess(50);
72
    printf(".");  
73
    if(AdWertAirPressure_Raw < 900) break;
74
  }
75
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off);
76
  Delay_ms_Mess(300);
838 MikeW 77
}
78
 
79
/* ****************************************************************************
80
Functionname:     SIGNAL                      */ /*!
81
Description:
82
 
83
  @return           void
84
  @pre              -
85
  @post             -
86
  @author           Michael Walter
87
**************************************************************************** */
88
SIGNAL(SIG_ADC)
89
{
966 MikeW 90
  static unsigned char kanal=0,state = 0;
91
  ANALOG_OFF;
92
  switch(state++)
93
  {
94
  case 0:
95
    AdWertGier = ADC;
96
    AdWertGier_Raw = ADC;
97
    AccumulatedRoll_Z += (ADC - AdNeutralGier);
98
    AccumulatedRoll_Z_cnt++;
99
    kanal = 1;
100
    break;     
101
  case 1:
102
    AdWertRoll = ADC;
103
    AdWertRoll_Raw = ADC;
104
    AccumulatedRoll_X += (ADC - AdNeutralRoll);
105
    AccumulatedRoll_X_cnt++;
106
    kanal = 2;
107
    break;             
108
  case 2:
109
    AdWertNick = ADC;
110
    AdWertNick_Raw = ADC;
111
    AccumulatedRoll_Y += (ADC - AdNeutralNick);
112
    AccumulatedRoll_Y_cnt++;
113
    kanal = 4;
114
    break;                     
115
  case 3:
116
#ifdef INTERNAL_REFERENCE 
117
    UBat = (3 * UBat + (5 * ADC) / 9) / 4; /* The internal Voltage is 5V instesd of 3V */
118
#else
119
    UBat = (3 * UBat + ADC / 3) / 4;
120
#endif
121
    kanal = 6;
122
    break;
123
  case 4:
124
    AdWertAccRoll = NeutralAccY - ADC;
125
    AdWertAccRoll_Raw = ADC;
126
    AccumulatedACC_Y += (NeutralAccY - ADC);
127
    AccumulatedACC_Y_cnt++;
128
    kanal = 7;
129
    break;
130
  case 5:
131
    AdWertAccNick = ADC - NeutralAccX;
132
    AdWertAccNick_Raw = ADC;
133
    AccumulatedACC_X += (ADC - NeutralAccX);
134
    AccumulatedACC_X_cnt++;
135
    kanal = 5;
136
    break;
137
  case 6:      
138
    AdWertAccHoch = (ADC - (NeutralAccX + NeutralAccY) / 2);
139
    AdWertAccHoch_Raw = ADC;
140
    AccumulatedACC_Z += (ADC - NeutralAccZ);
141
    AccumulatedACC_Z_cnt++;
142
    kanal = 3;
143
    break;
144
  case 7:
145
    AdWertAirPressure_Raw = ADC;
146
    AccumulatedAirPressure += ADC;
147
    AccumulatedAirPressure_cnt++;
148
    kanal = 0;
149
    state = 0;
150
    break;
151
  default:
152
    kanal = 0;
153
    state = 0;
154
    break;
155
  }
156
  ADMUX = kanal;
157
#ifdef INTERNAL_REFERENCE 
158
  /* Add 64 in order to use the internal 5v refenrence */
159
  ADMUX += 64;
160
#endif
161
  ANALOG_ON;
838 MikeW 162
}