Subversion Repositories FlightCtrl

Rev

Rev 1612 | Blame | 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.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/*
OBSOLETED BY controlMixer.c. But this is how it looked - maybe somebody will find it simpler?

#include <stdlib.h>
#include "control.h"

#include "rc.h"
#include "configuration.h"
#include "attitude.h"
#include "eeprom.h"
#include "flight.h"

#define RCChannel(dimension) (PPM_in[staticParams.ChannelAssignment[dimension]])

uint16_t maxStickPitch = 0, maxStickRoll = 0;
int16_t stickPitch = 0, stickRoll = 0, stickYaw = 0, stickThrottle = 0;
int16_t GPSStickPitch = 0, GPSStickRoll = 0;
int16_t externalStickPitch = 0, externalStickRoll = 0, externalStickYaw = 0, externalHeightValue = -20;

// dongfang's own experiment: Cablibrated sticks.
int16_t stickOffsetPitch = 0, stickOffsetRoll = 0;

// Looping-or-not flags.
uint8_t loopingPitch = 0, loopingRoll = 0;
uint8_t loopingLeft = 0, loopingRight = 0, loopingDown = 0, loopingTop = 0;

// Internal variables for reading commands made with an R/S stick.
uint8_t lastStickCommand  = STICK_COMMAND_UNDEF;
uint8_t stickCommandTimer = 0;

ExternalControl_t externalControl;

/ *
 * Stick diagram:
 * 2--3--4
 * |     |  +
 * 1  9  5  ^ 0
 * |     |  |  
 * 8--7--6
 *    
 * + <--
 *    0
 *
 * Not in any of these positions: 0
 * /

/ *
 * The stick most be further from center than this to indicate a settings number (1-5).
 * /
#define STICK_SETTINGSELECTION_THRESHOLD 70

uint8_t control_getLeftRCStickIndex(int16_t thresholdThrottle, int16_t thresholdYaw) {
  if(RCChannel(CH_THROTTLE) > thresholdThrottle) {
    // throttle is up
    if(RCChannel(CH_YAW) > thresholdYaw)
      return STICK_COMMAND_GYROCAL;
    if(RCChannel(CH_YAW) < -thresholdYaw)
      return STICK_COMMAND_ACCCAL;
    return STICK_COMMAND_UNDEF;
  } else if(RCChannel(CH_THROTTLE) < -thresholdThrottle) {
    // pitch is down
    if(RCChannel(CH_YAW) > thresholdYaw)
      return STICK_COMMAND_STOP;
    if(RCChannel(CH_YAW) < -thresholdYaw)
      return STICK_COMMAND_START;
    return STICK_COMMAND_UNDEF;
  } else {
    // pitch is around center
    return STICK_COMMAND_UNDEF;
  }
}

uint8_t control_getRightRCStickIndex(void) {
  if(RCChannel(CH_PITCH) > STICK_SETTINGSELECTION_THRESHOLD) {
    // pitch is up
    if(RCChannel(CH_ROLL) > STICK_SETTINGSELECTION_THRESHOLD)
      return 2;
    if(RCChannel(CH_ROLL) < -STICK_SETTINGSELECTION_THRESHOLD)
      return 4;
    return 3;
  } else if(RCChannel(CH_PITCH) < -STICK_SETTINGSELECTION_THRESHOLD) {
    // pitch is down
    if(RCChannel(CH_ROLL) > STICK_SETTINGSELECTION_THRESHOLD)
      return 8;
    if(RCChannel(CH_ROLL) < -STICK_SETTINGSELECTION_THRESHOLD)
      return 6;
    return 7;
  } else {
    // pitch is around center
    if(RCChannel(CH_ROLL) > STICK_SETTINGSELECTION_THRESHOLD)
      return 1;
    if(RCChannel(CH_ROLL) < -STICK_SETTINGSELECTION_THRESHOLD)
      return 5;
    return 9;
  }
}

/ *
 * This could be expanded to take calibrate / start / stop commands from ohter sources
 * than the R/C (read: Custom MK R/C project)
 * /
void control_senseStickCommands(void) {
  uint8_t stickCommandNow = control_getLeftRCStickIndex(85, 85);
  if (stickCommandNow != lastStickCommand) {
    lastStickCommand = stickCommandNow;
    stickCommandTimer = 0;
  } else {
    if (stickCommandTimer < 201)
      stickCommandTimer++;
  }
}

/ *
 * This could be expanded to take calibrate / start / stop commands from ohter sources
 * than the R/C (read: Custom MK R/C project)
 * /
uint8_t control_getStickCommand(void) {
  // If the same command was made 200 times, it's stable.
  if (stickCommandTimer >= 200) {
    return lastStickCommand;
  }
  return STICK_COMMAND_UNDEF;
}

uint8_t control_isStickCommandRepeated(void) {
  return stickCommandTimer > 200 ? 1 : 0;
}

/ *
 * To be fired only when the right stick is in the center position.
 * This will cause the value of pitch and roll stick to be adjusted
 * to zero (not just to near zero, as per the assumption in rc.c
 * about the rc signal. I had values about 50..70 with a Futaba
 * R617 receiver.) This calibration is not strictly necessary, but
 * for control logic that depends on the exact (non)center position
 * of a stick, it may be useful.
 * /
void control_setNeutral(void) {
  stickOffsetPitch += stickPitch;
  stickOffsetRoll += stickRoll;
}

/ *
 * Set the potientiometer values to the values of the respective R/C channel
 * right now. No slew rate limit.
 * /
void control_initPots(void) {
  uint8_t i;
  for (i=0; i<4; i++) {
    pots[i] = RCChannel(CH_POTS + i) + POT_OFFSET;
  }
  for (i=4; i<8; i++) {
    pots[i] = PPM_in[9 + (i-4)] + POT_OFFSET;
  }
}

/ *
 * Update potentiometer values with slow slew rate. Could be made faster if desired.
 * /
void control_updatePots(void) {
  uint8_t i;
  uint16_t targetvalue;
  for (i=0; i<8; i++) {
    if (i<4)  // configured pots
      targetvalue = RCChannel(CH_POTS + i) + POT_OFFSET;
    else      // PPM24-Extension
      targetvalue = PPM_in[9 + i] + POT_OFFSET;
    if (targetvalue < 0) targetvalue = 0;
    if (pots[i] < targetvalue && pots[i] < 255) pots[i]++; else if(pots[i] > 0 && pots[i] > targetvalue) pots[i]--;
  }
}

/ *
 * Update the variables indicating stick position from the sum of R/C, GPS and external control.
 * /
void control_update(void) {
  // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
  stickPitch = RCChannel(CH_PITCH) * staticParams.StickP;
  // (stick_pitch * 3 + RCChannel(CH_PITCH) * staticParams.StickP) / 4;
  stickPitch += PPM_diff[staticParams.ChannelAssignment[CH_PITCH]] * staticParams.StickD;
  stickPitch = stickPitch - stickOffsetPitch - GPSStickPitch;
 
  stickRoll = RCChannel(CH_ROLL) * staticParams.StickP;
  // stick_roll = (stick_roll * 3 + RCChannel(CH_ROLL) * staticParams.StickP) / 4;
  stickRoll += PPM_diff[staticParams.ChannelAssignment[CH_ROLL]] * staticParams.StickD;
  stickRoll = stickRoll - stickOffsetRoll - GPSStickRoll;
 
  // mapping of yaw
  stickYaw = -RCChannel(CH_YAW);
  // (range of -2 .. 2 is set to zero, to avoid unwanted yaw trimming on compass correction)
  if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) {
    if (stickYaw > 2) stickYaw-= 2;
    else if (stickYaw< -2) stickYaw += 2;
    else stickYaw = 0;
  }
 
  // mapping of gas
  stickThrottle = RCChannel(CH_THROTTLE) + 120;// shift to positive numbers
 
  if(externalControl.config & 0x01 && dynamicParams.ExternalControl > 128) {
    stickPitch += (int16_t) externalControl.pitch * (int16_t) staticParams.StickP;
    stickRoll += (int16_t) externalControl.roll * (int16_t) staticParams.StickP;
    stickYaw += externalControl.yaw;
    // ExternHeightValue =  (int16_t) ExternControl.Height * (int16_t)staticParams.Height_Gain;
    // Dubious: Lowest throttle setting has precedence.
    if(externalControl.throttle < stickThrottle) stickThrottle = externalControl.throttle;
  }
 
  if(stickThrottle < 0) stickThrottle = 0;

  if(abs(stickPitch / STICK_GAIN) > maxStickPitch) {
    maxStickPitch = abs(stickPitch) / STICK_GAIN;
    if(maxStickPitch > 100) maxStickPitch = 100;
  }
  else if (maxStickPitch) maxStickPitch--;

  if(abs(stickRoll / STICK_GAIN) > maxStickRoll) {
    maxStickRoll = abs(stickRoll) / STICK_GAIN;
    if(maxStickRoll > 100) maxStickRoll = 100;
  }
  else if (maxStickRoll) maxStickRoll--;

  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // Looping? Do not consider external or GPS input for this :)
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  if((RCChannel(CH_ROLL) > staticParams.LoopThreshold) && staticParams.BitConfig & CFG_LOOP_LEFT)  loopingLeft = 1;
  else {
    if(loopingLeft) { // Hysteresis
      if((RCChannel(CH_ROLL) < (staticParams.LoopThreshold - staticParams.LoopHysteresis))) loopingLeft = 0;
    }
  }
 
  if((RCChannel(CH_ROLL) < -staticParams.LoopThreshold) && staticParams.BitConfig & CFG_LOOP_RIGHT) loopingRight = 1;
  else {
    if(loopingRight) { // Hysteresis
      if(RCChannel(CH_ROLL) > -(staticParams.LoopThreshold - staticParams.LoopHysteresis)) loopingRight = 0;
    }
  }
 
  if((RCChannel(CH_PITCH) > staticParams.LoopThreshold) && staticParams.BitConfig & CFG_LOOP_UP) loopingTop = 1;
  else {
    if(loopingTop) { // Hysteresis
      if((RCChannel(CH_PITCH) < (staticParams.LoopThreshold - staticParams.LoopHysteresis))) loopingTop = 0;
    }
  }
 
  if((RCChannel(CH_PITCH) < -staticParams.LoopThreshold) && staticParams.BitConfig & CFG_LOOP_DOWN) loopingDown = 1;
  else {
    if(loopingDown) { // Hysteresis
      if(RCChannel(CH_PITCH) > -(staticParams.LoopThreshold - staticParams.LoopHysteresis)) loopingDown = 0;
    }
  }
 
  if(loopingLeft || loopingRight)  loopingRoll = 1; else loopingRoll = 0;
  if(loopingTop  || loopingDown) { loopingPitch = 1; loopingRoll = 0; loopingLeft = 0; loopingRight = 0;} else  loopingPitch = 0;
}

void setCompassCalState(void) {
  static uint8_t stick = 1;
  // if pitch is centered or top set stick to zero
  if(RCChannel(CH_PITCH) > -20) stick = 0;
  // if pitch is down trigger to next cal state
  if((RCChannel(CH_PITCH) < -70) && !stick) {
    stick = 1;
    compassCalState++;
    if(compassCalState < 5) beepNumber(compassCalState);
    else beep(1000);
  }
}

/ *
 *
 * /
uint8_t control_hasNewRCData(void) {
  // return !NewPpmData--;
  return (NewPpmData-- == 0) ? 1 : 0;
}

void control_performPilotCalibrationCommands(uint8_t stickCommand) {
  if (stickCommand == STICK_COMMAND_GYROCAL && !control_isStickCommandRepeated()) {
    // Run gyro calibration but do not repeat it.
    GRN_OFF;
     
    // TODO: out of here. Anyway, MKFLAG_MOTOR_RUN is cleared. Not enough?
    // isFlying = 0;
    // check roll/pitch stick position
    // if pitch stick is top or roll stick is left or right --> change parameter setting
    // according to roll/pitch stick position
     
    uint8_t setting = control_getRightRCStickIndex();
     
    if ((setting > 0 && setting < 6) || setting == 9) {
      // Gyro calinbration, with or without selecting a new parameter-set.
      if(setting > 0 && setting < 6) {
        // A valid parameter-set (1..5) was chosen - use it.
        setActiveParamSet(setting);
      }
      ParamSet_ReadFromEEProm(getActiveParamSet());
      attitude_setNeutral();
      flight_setNeutral();
      if (setting == 9) { // Right stick is centered; calibrate it to zero (hmm strictly does not belong here).
        control_setNeutral(); // Calibrate right stick neutral position.
      }
      beepNumber(getActiveParamSet());
    } else if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE) && setting == 7) {
      // If right stick is centered and down
      compassCalState = 1;
      beep(1000);
    }
  }
   
  // save the ACC neutral setting to eeprom
  else  {
    if(stickCommand == STICK_COMMAND_ACCCAL && !control_isStickCommandRepeated()) {
      // Run gyro and acc. meter calibration but do not repeat it.
      GRN_OFF;
      analog_calibrateAcc();
      attitude_setNeutral();
      flight_setNeutral();
      control_setNeutral(); // Calibrate right stick neutral position.
      beepNumber(getActiveParamSet());
    }
  }
} // end !MOTOR_RUN condition.
/*