Subversion Repositories FlightCtrl

Rev

Rev 966 | 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 */
973 MikeW 20
int AdWertNick_Raw = 0, AdWertRoll_Raw = 0, AdWertGier_Raw = 0;
966 MikeW 21
int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
22
int AdWertAccRoll_Raw = 0,AdWertAccNick_Raw = 0,AdWertAccHoch_Raw = 0;
23
 
838 MikeW 24
int AccumulatedACC_X = 0, AccumulatedACC_Y = 0, AccumulatedACC_Z = 0, AccumulatedAirPressure = 0;
25
int AccumulatedACC_X_cnt = 0, AccumulatedACC_Y_cnt = 0, AccumulatedACC_Z_cnt = 0, AccumulatedAirPressure_cnt = 0;
973 MikeW 26
int AccumulatedRoll = 0, AccumulatedNick = 0, AccumulatedGier = 0;
27
int AccumulatedRoll_cnt = 0, AccumulatedNick_cnt = 0, AccumulatedGier_cnt = 0;
838 MikeW 28
 
966 MikeW 29
unsigned int AdWertAirPressure_Raw = 1023;
838 MikeW 30
 
31
/* ****************************************************************************
32
Functionname:     ADC_Init                      */ /*!
33
Description:
34
 
35
  @return           void
36
  @pre              -
37
  @post             -
38
  @author           H. Buss / I. Busker
39
**************************************************************************** */
40
void ADC_Init(void)
41
{
966 MikeW 42
#ifdef INTERNAL_REFERENCE 
43
  ADMUX = 64;/* Internal Reference 5V */
44
#else
45
  ADMUX = 0; /* External Reference */
46
#endif
47
  ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
838 MikeW 48
}
49
 
50
/* ****************************************************************************
51
Functionname:     SucheLuftruckOffset                      */ /*!
52
Description:
53
 
54
  @return           void
55
  @pre              -
56
  @post             -
57
  @author           H. Buss / I. Busker
58
**************************************************************************** */
59
void SucheLuftruckOffset(void)
60
{
966 MikeW 61
  unsigned int off;
62
  off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]);
63
  if(off > 20) off -= 10;
64
  OCR0A = off;
65
  Delay_ms_Mess(100);
66
  if(AdWertAirPressure_Raw < 850) off = 0;
67
  for(; off < 250;off++)
68
  {
69
    OCR0A = off;
70
    Delay_ms_Mess(50);
71
    printf(".");  
72
    if(AdWertAirPressure_Raw < 900) break;
73
  }
74
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off);
75
  Delay_ms_Mess(300);
838 MikeW 76
}
77
 
78
/* ****************************************************************************
79
Functionname:     SIGNAL                      */ /*!
80
Description:
81
 
82
  @return           void
83
  @pre              -
84
  @post             -
85
  @author           Michael Walter
86
**************************************************************************** */
87
SIGNAL(SIG_ADC)
88
{
966 MikeW 89
  static unsigned char kanal=0,state = 0;
90
  ANALOG_OFF;
91
  switch(state++)
92
  {
93
  case 0:
94
    AdWertGier_Raw = ADC;
973 MikeW 95
    AccumulatedGier += (ADC - AdNeutralGier);
96
    AccumulatedGier_cnt++;
966 MikeW 97
    kanal = 1;
98
    break;     
99
  case 1:
100
    AdWertRoll_Raw = ADC;
973 MikeW 101
    AccumulatedRoll += (ADC - AdNeutralRoll);
102
    AccumulatedRoll_cnt++;
966 MikeW 103
    kanal = 2;
104
    break;             
105
  case 2:
106
    AdWertNick_Raw = ADC;
973 MikeW 107
    AccumulatedNick += (ADC - AdNeutralNick);
108
    AccumulatedNick_cnt++;
966 MikeW 109
    kanal = 4;
110
    break;                     
111
  case 3:
112
#ifdef INTERNAL_REFERENCE 
113
    UBat = (3 * UBat + (5 * ADC) / 9) / 4; /* The internal Voltage is 5V instesd of 3V */
114
#else
115
    UBat = (3 * UBat + ADC / 3) / 4;
116
#endif
117
    kanal = 6;
118
    break;
119
  case 4:
120
    AdWertAccRoll = NeutralAccY - ADC;
121
    AdWertAccRoll_Raw = ADC;
122
    AccumulatedACC_Y += (NeutralAccY - ADC);
123
    AccumulatedACC_Y_cnt++;
124
    kanal = 7;
125
    break;
126
  case 5:
127
    AdWertAccNick = ADC - NeutralAccX;
128
    AdWertAccNick_Raw = ADC;
129
    AccumulatedACC_X += (ADC - NeutralAccX);
130
    AccumulatedACC_X_cnt++;
131
    kanal = 5;
132
    break;
133
  case 6:      
134
    AdWertAccHoch = (ADC - (NeutralAccX + NeutralAccY) / 2);
135
    AdWertAccHoch_Raw = ADC;
136
    AccumulatedACC_Z += (ADC - NeutralAccZ);
137
    AccumulatedACC_Z_cnt++;
138
    kanal = 3;
139
    break;
140
  case 7:
141
    AdWertAirPressure_Raw = ADC;
142
    AccumulatedAirPressure += ADC;
143
    AccumulatedAirPressure_cnt++;
144
    kanal = 0;
145
    state = 0;
146
    break;
147
  default:
148
    kanal = 0;
149
    state = 0;
150
    break;
151
  }
152
  ADMUX = kanal;
153
#ifdef INTERNAL_REFERENCE 
154
  /* Add 64 in order to use the internal 5v refenrence */
155
  ADMUX += 64;
156
#endif
157
  ANALOG_ON;
838 MikeW 158
}