Subversion Repositories FlightCtrl

Rev

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

/*
Copyright 2008, by Michael Walter

All functions written by Michael Walter are free software and can be redistributed and/or modified under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see <http://www.gnu.org/licenses/>.

Please note: The software is based on the framework provided by H. Buss and I. Busker in their Mikrokopter projekt. All functions that
are not written by Michael Walter are under the license by H. Buss and I. Busker (license_buss.txt) published by www.mikrokopter.de
unless it is stated otherwise.
*/


#include "main.h"
#include "FlightControl.h"


int UBat = 100; /* Initial Battery Guess */
int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0;
int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
int AdWertNick_Raw = 0, AdWertRoll_Raw = 0, AdWertGier_Raw = 0;
int AdWertAccRoll_Raw = 0,AdWertAccNick_Raw = 0,AdWertAccHoch_Raw = 0;

int AccumulatedACC_X = 0, AccumulatedACC_Y = 0, AccumulatedACC_Z = 0, AccumulatedAirPressure = 0;
int AccumulatedACC_X_cnt = 0, AccumulatedACC_Y_cnt = 0, AccumulatedACC_Z_cnt = 0, AccumulatedAirPressure_cnt = 0;
int AccumulatedRoll_X = 0, AccumulatedRoll_Y = 0, AccumulatedRoll_Z = 0;
int AccumulatedRoll_X_cnt = 0, AccumulatedRoll_Y_cnt = 0, AccumulatedRoll_Z_cnt = 0;

unsigned int AdWertAirPressure_Raw = 1023;

/* ****************************************************************************
Functionname:     ADC_Init                      */
/*!
Description:

  @return           void
  @pre              -
  @post             -
  @author           H. Buss / I. Busker
**************************************************************************** */

void ADC_Init(void)
{
#ifdef INTERNAL_REFERENCE
  ADMUX = 64;/* Internal Reference 5V */
#else
  ADMUX = 0; /* External Reference */
#endif
  ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
}

/* ****************************************************************************
Functionname:     SucheLuftruckOffset                      */
/*!
Description:

  @return           void
  @pre              -
  @post             -
  @author           H. Buss / I. Busker
**************************************************************************** */

void SucheLuftruckOffset(void)
{
  unsigned int off;
  off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]);
  if(off > 20) off -= 10;
  OCR0A = off;
  Delay_ms_Mess(100);
  if(AdWertAirPressure_Raw < 850) off = 0;
  for(; off < 250;off++)
  {
    OCR0A = off;
    Delay_ms_Mess(50);
    printf(".");  
    if(AdWertAirPressure_Raw < 900) break;
  }
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off);
  Delay_ms_Mess(300);
}

/* ****************************************************************************
Functionname:     SIGNAL                      */
/*!
Description:

  @return           void
  @pre              -
  @post             -
  @author           Michael Walter
**************************************************************************** */

SIGNAL(SIG_ADC)
{
  static unsigned char kanal=0,state = 0;
  ANALOG_OFF;
  switch(state++)
  {
  case 0:
    AdWertGier = ADC;
    AdWertGier_Raw = ADC;
    AccumulatedRoll_Z += (ADC - AdNeutralGier);
    AccumulatedRoll_Z_cnt++;
    kanal = 1;
    break;     
  case 1:
    AdWertRoll = ADC;
    AdWertRoll_Raw = ADC;
    AccumulatedRoll_X += (ADC - AdNeutralRoll);
    AccumulatedRoll_X_cnt++;
    kanal = 2;
    break;             
  case 2:
    AdWertNick = ADC;
    AdWertNick_Raw = ADC;
    AccumulatedRoll_Y += (ADC - AdNeutralNick);
    AccumulatedRoll_Y_cnt++;
    kanal = 4;
    break;                     
  case 3:
#ifdef INTERNAL_REFERENCE
    UBat = (3 * UBat + (5 * ADC) / 9) / 4; /* The internal Voltage is 5V instesd of 3V */
#else
    UBat = (3 * UBat + ADC / 3) / 4;
#endif
    kanal = 6;
    break;
  case 4:
    AdWertAccRoll = NeutralAccY - ADC;
    AdWertAccRoll_Raw = ADC;
    AccumulatedACC_Y += (NeutralAccY - ADC);
    AccumulatedACC_Y_cnt++;
    kanal = 7;
    break;
  case 5:
    AdWertAccNick = ADC - NeutralAccX;
    AdWertAccNick_Raw = ADC;
    AccumulatedACC_X += (ADC - NeutralAccX);
    AccumulatedACC_X_cnt++;
    kanal = 5;
    break;
  case 6:      
    AdWertAccHoch = (ADC - (NeutralAccX + NeutralAccY) / 2);
    AdWertAccHoch_Raw = ADC;
    AccumulatedACC_Z += (ADC - NeutralAccZ);
    AccumulatedACC_Z_cnt++;
    kanal = 3;
    break;
  case 7:
    AdWertAirPressure_Raw = ADC;
    AccumulatedAirPressure += ADC;
    AccumulatedAirPressure_cnt++;
    kanal = 0;
    state = 0;
    break;
  default:
    kanal = 0;
    state = 0;
    break;
  }
  ADMUX = kanal;
#ifdef INTERNAL_REFERENCE
  /* Add 64 in order to use the internal 5v refenrence */
  ADMUX += 64;
#endif
  ANALOG_ON;
}