Subversion Repositories FlightCtrl

Rev

Rev 2071 | Rev 2074 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1775 - 1
#include <inttypes.h>
2033 - 2
#include "analog.h"
1775 - 3
#include "attitude.h"
4
#include "configuration.h"
2048 - 5
#include "controlMixer.h"
1775 - 6
 
7
// for digital / LED debug.
8
#include "output.h"
9
 
10
// For scope debugging only!
11
#include "rc.h"
12
 
13
#define INTEGRAL_LIMIT 100000
14
 
2069 - 15
#define LATCH_TIME 3
1775 - 16
 
17
int32_t targetHeight;
2069 - 18
//int32_t rampedTargetHeight;
1775 - 19
 
2069 - 20
//rampinguint8_t heightRampingTimer = 0;
1961 - 21
int32_t maxHeightThisFlight;
1775 - 22
int32_t iHeight;
23
 
24
void HC_setGround(void) {
2033 - 25
  analog_setGround();
1960 - 26
  // This should also happen when height control is enabled in-flight.
2069 - 27
  /*rampedTargetHeight = */ targetHeight = analog_getHeight();
1961 - 28
  maxHeightThisFlight = 0;
1960 - 29
  iHeight = 0;
1775 - 30
}
31
 
2069 - 32
uint8_t HC_isSwitchOn() {
33
  return (dynamicParams.heightSetting >= 255/3);
34
}
35
 
2039 - 36
void HC_periodicTask(void) {
2033 - 37
  int32_t height = analog_getHeight();
1960 - 38
  static uint8_t setHeightLatch = 0;
39
 
1961 - 40
  if (height > maxHeightThisFlight)
41
    maxHeightThisFlight = height;
1960 - 42
 
2059 - 43
  // debugOut.analog[25] = dynamicParams.heightSetting;
44
 
1961 - 45
  if (staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) {
2069 - 46
    if (HC_isSwitchOn()) {
1960 - 47
      // Switch is ON
48
      if (setHeightLatch <= LATCH_TIME) {
2069 - 49
        if (setHeightLatch == LATCH_TIME) {
50
          // Freeze the height as target. We want to do this exactly once each time the switch is thrown ON.
51
          /* rampedTargetHeight = */ targetHeight = height;
2071 - 52
          iHeight = 0;
2069 - 53
        }
54
        // Time not yet reached.
55
        setHeightLatch++;
1960 - 56
      }
57
    } else {
58
      // Switch is OFF.
59
      setHeightLatch = 0;
60
    }
61
  } else {
62
    // Switch is not activated; take the "max-height" as the target height.
2033 - 63
    targetHeight = (uint16_t) dynamicParams.heightSetting * 25L - 500L; // should be: 100 (or make a param out of it)
1960 - 64
  }
65
 
2069 - 66
  /*
1960 - 67
  if (++heightRampingTimer == INTEGRATION_FREQUENCY / 10) {
68
    heightRampingTimer = 0;
1961 - 69
    if (rampedTargetHeight < targetHeight) {
70
      // climbing
71
      if (rampedTargetHeight < targetHeight - staticParams.heightSlewRate) {
72
        rampedTargetHeight += staticParams.heightSlewRate;
73
      } else {
74
        rampedTargetHeight = targetHeight;
75
      }
2026 - 76
    } else {
1961 - 77
      // descending
78
      if (rampedTargetHeight > targetHeight + staticParams.heightSlewRate) {
79
        rampedTargetHeight -= staticParams.heightSlewRate;
80
      } else {
81
        rampedTargetHeight = targetHeight;
82
      }
1960 - 83
    }
84
  }
2069 - 85
  */
1970 - 86
 
1960 - 87
  // height, in meters (so the division factor is: 100)
2059 - 88
  // debugOut.analog[24] = (117100 - filteredAirPressure) / 100;
1971 - 89
  // Calculated 0 alt number: 108205
1980 - 90
  // Experimental 0 alt number: 117100
1775 - 91
}
92
 
93
// takes 180-200 usec (with integral term). That is too heavy!!!
94
// takes 100 usec without integral term.
2048 - 95
void HC_periodicTaskAndPRTY(int16_t* PRTY) {
96
  HC_periodicTask();
97
  int16_t throttle = PRTY[CONTROL_THROTTLE];
2033 - 98
  int32_t height = analog_getHeight();
2073 - 99
  int32_t heightError = targetHeight - height;
2033 - 100
  int16_t dHeight = analog_getDHeight();
1960 - 101
 
2058 - 102
  debugOut.analog[22] = height/10L;
103
  debugOut.analog[23] = dHeight;
2055 - 104
 
2026 - 105
  if (heightError > 0) {
1960 - 106
    debugOut.digital[0] |= DEBUG_HEIGHT_DIFF;
2035 - 107
  } else {
1961 - 108
    debugOut.digital[0] &= ~DEBUG_HEIGHT_DIFF;
2035 - 109
  }
110
 
111
  if (dHeight > 0) {
1960 - 112
    debugOut.digital[1] |= DEBUG_HEIGHT_DIFF;
2035 - 113
  } else {
114
    debugOut.digital[1] &= ~DEBUG_HEIGHT_DIFF;
1960 - 115
  }
2032 - 116
 
2073 - 117
  int32_t heightErrorForIntegral = heightError;
118
  int32_t heightErrorForIntegralLimit = staticParams.heightControlMaxIntegralIn << 10;
119
 
120
  if (heightErrorForIntegral > heightErrorForIntegralLimit) {
121
    heightErrorForIntegral = heightErrorForIntegralLimit;
122
  } else if (heightErrorForIntegral < -heightErrorForIntegralLimit) {
123
    heightErrorForIntegral =- heightErrorForIntegralLimit;
124
  }
125
 
2033 - 126
  // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec....
2073 - 127
  iHeight += heightErrorForIntegral;
2032 - 128
 
2033 - 129
#define IHEIGHT_SCALE 24
130
  // dThrottle is in the range between +/- 1<<(IHEIGHT_SCALE+8)>>(IHEIGHT_SCALE) = +/- 256
2035 - 131
  int16_t dThrottleI =  (iHeight * (int32_t)dynamicParams.heightI) >> (IHEIGHT_SCALE);
2033 - 132
 
2073 - 133
  if (dThrottleI > staticParams.heightControlMaxIntegralOut) {
134
    dThrottleI = staticParams.heightControlMaxIntegralOut;
135
    iHeight = ((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI;
136
  } else if (dThrottleI < -staticParams.heightControlMaxIntegralOut) {
137
    dThrottleI = -staticParams.heightControlMaxIntegralOut;
138
    iHeight = -((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI;
2033 - 139
  }
140
 
2035 - 141
  int16_t dThrottleP = (heightError * dynamicParams.heightP) >> 10;
142
  int16_t dThrottleD = (dHeight * dynamicParams.heightD) >> 7;
2033 - 143
 
2073 - 144
  debugOut.analog[10] = dThrottleP;
145
  debugOut.analog[11] = dThrottleI;
146
  debugOut.analog[12] = dThrottleD;
147
  debugOut.analog[13] = heightError/10;
2032 - 148
 
2045 - 149
  //debugOut.analog[27] = dynamicParams.heightP;
150
  //debugOut.analog[28] = dynamicParams.heightI;
151
  //debugOut.analog[29] = dynamicParams.heightD;
2035 - 152
 
2071 - 153
  int16_t dThrottle = dThrottleI + dThrottleP - dThrottleD;
2035 - 154
 
1961 - 155
  if (dThrottle > staticParams.heightControlMaxThrottleChange)
156
    dThrottle = staticParams.heightControlMaxThrottleChange;
157
  else if (dThrottle < -staticParams.heightControlMaxThrottleChange)
158
    dThrottle = -staticParams.heightControlMaxThrottleChange;
2026 - 159
 
2055 - 160
  /*
2035 - 161
  debugOut.analog[19] = throttle;
162
  debugOut.analog[20] = dThrottle;
163
  debugOut.analog[21] = height;
164
  debugOut.analog[22] = rampedTargetHeight;
165
  debugOut.analog[23] = heightError;
2055 - 166
  */
2026 - 167
 
1961 - 168
  if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) {
2069 - 169
    if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) || HC_isSwitchOn()) {
1960 - 170
      // If switch is not in use --> Just apply height control.
171
      // If switch is in use     --> only apply height control when switch is also ON.
172
      throttle += dThrottle;
173
    }
174
  }
1961 - 175
 
176
  /* Experiment: Find hover-throttle */
177
 
178
#define DEFAULT_HOVERTHROTTLE 50
179
int32_t stronglyFilteredHeightDiff = 0;
2036 - 180
// uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE;
1961 - 181
uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE;
182
#define HOVERTHROTTLEFILTER 25
1960 - 183
 
184
  stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff
185
                                * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER;
186
  stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER
187
                                                          - 1) + throttle) / HOVERTHROTTLEFILTER;
188
 
2035 - 189
  /*
1960 - 190
  if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3
191
      && stronglyFilteredHeightDiff > -3) {
192
    hoverThrottle = stronglyFilteredThrottle;
193
    debugOut.digital[0] |= DEBUG_HOVERTHROTTLE;
194
  } else
195
    debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE;
2035 - 196
 
197
    */
1960 - 198
 
2048 - 199
  PRTY[CONTROL_THROTTLE] = throttle;
1775 - 200
}
201
 
202
/*
1821 - 203
 For a variometer thingy:
204
 When switch is thrown on, freeze throttle (capture it into variable)
205
 For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping.
206
 Output = frozen throttle + whatever is computed +/-. Integral?
207
 */