Subversion Repositories FlightCtrl

Rev

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

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <util/delay.h>
#include <avr/eeprom.h>
#include <stddef.h>
#include "configuration.h"
#include "eeprom.h"
#include "timer0.h"

int16_t variables[8] = {0,0,0,0,0,0,0,0};
dynamicParam_t dynamicParams = {48,251,16,58,64,8,150,150,2,10,{0,0,0,0,0,0,0,0},100,70,90,65,64,100,0,0,0};
uint8_t CPUType = ATMEGA644;
uint8_t BoardRelease = 13;

/************************************************************************
 * Map the parameter to pot values                                    
 * Replacing this code by the code below saved almost 1 kbyte.
 ************************************************************************/


void configuration_applyVariablesToParams(void) {
  uint8_t i;
#define SET_POT_MM(b,a,min,max) {if (a<255) {if (a>=251) b=variables[a-251]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
#define SET_POT(b,a) { if (a<255) {if (a>=251) b=variables[a-251]; else b=a;}}
  SET_POT(dynamicParams.MaxHeight,staticParams.MaxHeight);
  SET_POT_MM(dynamicParams.HeightD,staticParams.HeightD,0,100);
  SET_POT_MM(dynamicParams.HeightP,staticParams.HeightP,0,100);
  SET_POT(dynamicParams.Height_ACC_Effect,staticParams.Height_ACC_Effect);
  SET_POT(dynamicParams.CompassYawEffect,staticParams.CompassYawEffect);
  SET_POT_MM(dynamicParams.GyroP,staticParams.GyroP,10,255);
  SET_POT(dynamicParams.GyroI,staticParams.GyroI);
  SET_POT(dynamicParams.GyroD,staticParams.GyroD);
  SET_POT(dynamicParams.IFactor,staticParams.IFactor);
  for (i=0; i<sizeof(staticParams.UserParams1); i++) {
    SET_POT(dynamicParams.UserParams[i],staticParams.UserParams1[i]);
  }
  for (i=0; i<sizeof(staticParams.UserParams2); i++) {
    SET_POT(dynamicParams.UserParams[i + sizeof(staticParams.UserParams1)], staticParams.UserParams2[i]);
  }
  SET_POT(dynamicParams.ServoPitchControl,staticParams.ServoPitchControl);
  SET_POT(dynamicParams.LoopGasLimit,staticParams.LoopGasLimit);
  SET_POT(dynamicParams.AxisCoupling1,staticParams.AxisCoupling1);
  SET_POT(dynamicParams.AxisCoupling2,staticParams.AxisCoupling2);
  SET_POT(dynamicParams.AxisCouplingYawCorrection,staticParams.AxisCouplingYawCorrection);
  SET_POT(dynamicParams.DynamicStability,staticParams.DynamicStability);
  SET_POT_MM(dynamicParams.J16Timing,staticParams.J16Timing,1,255);
  SET_POT_MM(dynamicParams.J17Timing,staticParams.J17Timing,1,255);
#if defined (USE_MK3MAG)
  SET_POT(dynamicParams.NaviGpsModeControl,staticParams.NaviGpsModeControl);
  SET_POT(dynamicParams.NaviGpsGain,staticParams.NaviGpsGain);
  SET_POT(dynamicParams.NaviGpsP,staticParams.NaviGpsP);
  SET_POT(dynamicParams.NaviGpsI,staticParams.NaviGpsI);
  SET_POT(dynamicParams.NaviGpsD,staticParams.NaviGpsD);
  SET_POT(dynamicParams.NaviGpsACC,staticParams.NaviGpsACC);
  SET_POT_MM(dynamicParams.NaviOperatingRadius,staticParams.NaviOperatingRadius,10, 255);
  SET_POT(dynamicParams.NaviWindCorrection,staticParams.NaviWindCorrection);
  SET_POT(dynamicParams.NaviSpeedCompensation,staticParams.NaviSpeedCompensation);
#endif
  SET_POT(dynamicParams.ExternalControl,staticParams.ExternalControl);
}

const XLATION XLATIONS[] = {
  {offsetof(paramset_t, MaxHeight), offsetof(dynamicParam_t, MaxHeight)},
  {offsetof(paramset_t, Height_ACC_Effect), offsetof(dynamicParam_t, Height_ACC_Effect)},
  {offsetof(paramset_t, CompassYawEffect), offsetof(dynamicParam_t, CompassYawEffect)},
  {offsetof(paramset_t, GyroI), offsetof(dynamicParam_t, GyroI)},
  {offsetof(paramset_t, GyroD), offsetof(dynamicParam_t, GyroD)},
  {offsetof(paramset_t, IFactor), offsetof(dynamicParam_t, IFactor)},
  {offsetof(paramset_t, ServoPitchControl), offsetof(dynamicParam_t, ServoPitchControl)},
  {offsetof(paramset_t, LoopGasLimit), offsetof(dynamicParam_t, LoopGasLimit)},
  {offsetof(paramset_t, AxisCoupling1), offsetof(dynamicParam_t, AxisCoupling1)},
  {offsetof(paramset_t, AxisCoupling2), offsetof(dynamicParam_t, AxisCoupling2)},
  {offsetof(paramset_t, AxisCouplingYawCorrection), offsetof(dynamicParam_t, AxisCouplingYawCorrection)},
  {offsetof(paramset_t, DynamicStability), offsetof(dynamicParam_t, DynamicStability)},
  {offsetof(paramset_t, NaviGpsModeControl),
   offsetof(dynamicParam_t, NaviGpsModeControl)},
  {offsetof(paramset_t, NaviGpsGain), offsetof(dynamicParam_t, NaviGpsGain)},
  {offsetof(paramset_t, NaviGpsP), offsetof(dynamicParam_t, NaviGpsP)},
  {offsetof(paramset_t, NaviGpsI), offsetof(dynamicParam_t, NaviGpsI)},
  {offsetof(paramset_t, NaviGpsD), offsetof(dynamicParam_t, NaviGpsD)},
  {offsetof(paramset_t, NaviGpsACC), offsetof(dynamicParam_t, NaviGpsACC)},
  {offsetof(paramset_t, NaviWindCorrection), offsetof(dynamicParam_t, NaviWindCorrection)},
  {offsetof(paramset_t, NaviSpeedCompensation), offsetof(dynamicParam_t, NaviSpeedCompensation)},
  {offsetof(paramset_t, ExternalControl), offsetof(dynamicParam_t, ExternalControl)}
};

const MMXLATION MMXLATIONS[] = {
  {offsetof(paramset_t, HeightD), offsetof(dynamicParam_t, HeightD),0,100},
  {offsetof(paramset_t, HeightP), offsetof(dynamicParam_t, HeightD),0,150},
  {offsetof(paramset_t, GyroP), offsetof(dynamicParam_t, GyroP),0,255},
  {offsetof(paramset_t, J16Timing), offsetof(dynamicParam_t, J16Timing),1,255},
  {offsetof(paramset_t, J17Timing), offsetof(dynamicParam_t, J17Timing),1,255},
  {offsetof(paramset_t, NaviOperatingRadius), offsetof(dynamicParam_t, NaviOperatingRadius),10,255}
};

uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
  uint8_t result;
  if (src>=251) result = variables[src-251];
  else result = src;
  if (result < min) result = min;
  else if (result > max) result = max;
  return result;
}

void configuration_applyVariablesToParams_dead(void) {
  uint8_t i, src;
  uint8_t* pointerToTgt;
  for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
    src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
    if (src < 255) {
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
    }
  }

  for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
    src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
    if (src < 255) {
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
    }
  }
 
  for (i=0; i<sizeof(staticParams.UserParams1); i++) {
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, UserParams1) + i));
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, UserParams) + i);
    if (src < 255) {
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
    }    
  }

  for (i=0; i<sizeof(staticParams.UserParams2); i++) {
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, UserParams2) + i));
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, UserParams) + sizeof(staticParams.UserParams1) + i);
    if (src < 255) {
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
    }    
  }
}

uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
  uint8_t CPUType = ATMEGA644;
  if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P;  // initial Values for 644P after reset
  return CPUType;
}

/*
 * Automatic detection of hardware components is not supported in this development-oriented
 * FC firmware. It would go against the point of it: To enable alternative hardware
 * configurations with otherwise unsupported components. Instead, one should write
 * custom code + adjust constants for the new hardware, and include the relevant code
 * from the makefile.
 * However - we still do detect the board release. Reason: Otherwise it would be too
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
 * on different HW version....
 */


uint8_t getBoardRelease(void) {
  uint8_t BoardRelease = 13;
  // the board release is coded via the pull up or down the 2 status LED

  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input

  _delay_loop_2(1000); // make some delay

  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
    case 0x00:
      BoardRelease = 10; // 1.0
      break;
    case 0x01:
      BoardRelease = 11; // 1.1 or 1.2
      break;
    case 0x02:
      BoardRelease = 20; // 2.0
      break;
    case 0x03:
      BoardRelease = 13; // 1.3
      break;
    default:
      break;
    }
  // set LED ports as output
  DDRB |= (1<<DDB1)|(1<<DDB0);
  RED_ON;
  GRN_OFF;
  return BoardRelease;
}

void beep(uint16_t millis) {
  BeepTime = millis;
}

/*
 * Make [numbeeps] beeps.
 */

void beepNumber(uint8_t numbeeps) {
  while(numbeeps--) {
    if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren!
    beep(100); // 0.1 second
    Delay_ms(250); // blocks 250 ms as pause to next beep,
    // this will block the flight control loop,
    // therefore do not use this function if motors are running
  }
}

/*
 * Beep the R/C alarm signal
 */

void beepRCAlarm(void) {
  if(BeepModulation == 0xFFFF) { // If not already beeping an alarm signal (?)
    BeepTime = 15000; // 1.5 seconds
    BeepModulation = 0x0C00;
  }
}

/*
 * Beep the I2C bus error signal
 */

void beepI2CAlarm(void) {
  if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN)) {
    BeepTime = 10000; // 1 second
    BeepModulation = 0x0080;
  }
}

/*
 * Beep the battery low alarm signal
 */

void beepBatteryAlarm(void) {
  BeepModulation = 0x0300;
  if(!BeepTime) {
    BeepTime = 6000; // 0.6 seconds
  }
}

/*
 * Beep the EEPROM checksum alarm
 */

void beepEEPROMAlarm(void) {
  BeepModulation = 0x0007;
  if(!BeepTime) {
    BeepTime = 6000; // 0.6 seconds
  }
}