Subversion Repositories FlightCtrl

Rev

Rev 1963 | Rev 1969 | 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 und 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 Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt und genannt 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 example: 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 <stddef.h>
#include <string.h>
#include "configuration.h"
#include "sensors.h"
#include "rc.h"

int16_t variables[VARIABLE_COUNT];
paramset_t staticParams;
channelMap_t channelMap;
mixerMatrix_t mixerMatrix;
dynamicParam_t dynamicParams;
uint8_t CPUType = ATMEGA644;
uint8_t boardRelease = 13;
uint8_t requiredMotors;

// MK flags. TODO: Replace by enum. State machine.
uint16_t isFlying = 0;
volatile uint8_t MKFlags = 0;

/************************************************************************
 * 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>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
#define SET_POT(b,a) { if (a<255) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}}
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
  SET_POT(dynamicParams.IFactor, staticParams.IFactor);
  SET_POT(dynamicParams.yawIFactor, staticParams.yawIFactor);
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);

  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
  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(dynamicParams.maxControlActivityForAcc,staticParams.maxControlActivityForAcc);

  SET_POT_MM(dynamicParams.heightP, staticParams.heightP,0,100);
  SET_POT_MM(dynamicParams.heightD, staticParams.heightD,0,100);
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
  SET_POT(dynamicParams.heightACCEffect,staticParams.heightACCEffect);
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);

  for (i=0; i<sizeof(staticParams.userParams); i++) {
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
  }

  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);

  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
}

const XLATION XLATIONS[] = {
  {offsetof(paramset_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
};

const MMXLATION MMXLATIONS[] = {
  {offsetof(paramset_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
};

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.userParams); i++) {
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, userParams) + i));
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + 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_OFF;
  GRN_OFF;
  return boardRelease;
}

void setOtherDefaults(void) {
  // Height Control
  staticParams.heightP = 10;
  staticParams.heightD = 30;
  staticParams.heightSetting = 251;
  staticParams.heightControlMaxThrottleChange = 10;
  staticParams.heightSlewRate = 4;
 
  // Control
  staticParams.stickP = 8;
  staticParams.stickD = 12;
  staticParams.stickYawP = 12;
  staticParams.stickThrottleD = 12;
  staticParams.minThrottle = 8;
  staticParams.maxThrottle = 230;
  staticParams.externalControl = 0;
 
  // IMU
  staticParams.gyroPIDFilterConstant = 1;
  staticParams.gyroATTFilterConstant = 1;
  staticParams.gyroDFilterConstant = 1;
  staticParams.accFilterConstant = 4;

  staticParams.gyroP = 60;
  staticParams.gyroI = 80;
  staticParams.gyroD = 4;
 
  // set by gyro-specific code: gyro_setDefaults().
  // staticParams.zerothOrderCorrection =
  // staticParams.driftCompDivider =
  // staticParams.driftCompLimit =
 
  staticParams.axisCoupling1 = 90;
  staticParams.axisCoupling2 = 67;
  staticParams.axisCouplingYawCorrection = 0;
  staticParams.dynamicStability = 50;
  staticParams.IFactor = 32;  
  staticParams.compassYawEffect = 128;

  // Servos
  for (uint8_t i=0; i<2; i++) {
    staticParams.servoConfigurations[i].manualControl = 128;
    staticParams.servoConfigurations[i].compensationFactor = 0;
    staticParams.servoConfigurations[i].minValue = 64;
    staticParams.servoConfigurations[i].maxValue = 192;
    staticParams.servoConfigurations[i].flags = 0;
  }
  staticParams.servoCount = 4;

  // Battery warning and emergency flight
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
  staticParams.emergencyThrottle = 35;
  staticParams.emergencyFlightDuration = 30;

  // Outputs
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
  staticParams.outputFlash[0].timing = 15;
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
  staticParams.outputFlash[1].timing = 15;

  staticParams.outputDebugMask = 16;
  staticParams.outputOptions = 8;
}

/***************************************************/
/*    Default Values for parameter set 1           */
/***************************************************/
void paramSet_default(uint8_t setnumber) {
  setOtherDefaults();
  gyro_setDefaults();
 
  for (uint8_t i=0; i<8; i++) {
    staticParams.userParams[i] = 0;
  }

  staticParams.bitConfig =
    CFG_GYRO_SATURATION_PREVENTION | CFG_HEADING_HOLD;

  memcpy(staticParams.name, "Default\0", 6);
}

/***************************************************/
/*    Default Values for Mixer Table               */
/***************************************************/
void mixerMatrix_default(void) { // Quadro
  uint8_t i;
  // mixerMatric.revision = EEMIXER_REVISION;
  // clear mixer table (but preset throttle)
  for (i = 0; i < 16; i++) {
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
    mixerMatrix.motor[i][MIX_PITCH] = 0;
    mixerMatrix.motor[i][MIX_ROLL] = 0;
    mixerMatrix.motor[i][MIX_YAW] = 0;
  }
  // default = Quadro
  mixerMatrix.motor[0][MIX_PITCH] = +64;
  mixerMatrix.motor[0][MIX_YAW] = +64;
  mixerMatrix.motor[1][MIX_PITCH] = -64;
  mixerMatrix.motor[1][MIX_YAW] = +64;
  mixerMatrix.motor[2][MIX_ROLL] = -64;
  mixerMatrix.motor[2][MIX_YAW] = -64;
  mixerMatrix.motor[3][MIX_ROLL] = +64;
  mixerMatrix.motor[3][MIX_YAW] = -64;
  memcpy(mixerMatrix.name, "Quadro\0", 7);
}

void channelMap_default(void) {
  channelMap.channels[CH_PITCH] = 2;
  channelMap.channels[CH_ROLL] = 1;
  channelMap.channels[CH_THROTTLE] = 3;
  channelMap.channels[CH_YAW] = 4;
  channelMap.channels[CH_POTS + 0] = 5;
  channelMap.channels[CH_POTS + 1] = 6;
  channelMap.channels[CH_POTS + 2] = 7;
  channelMap.channels[CH_POTS + 3] = 8;
}