Subversion Repositories FlightCtrl

Rev

Rev 2059 | Rev 2071 | 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;
52
        }
53
        // Time not yet reached.
54
        setHeightLatch++;
1960 - 55
      }
56
    } else {
57
      // Switch is OFF.
58
      setHeightLatch = 0;
59
    }
60
  } else {
61
    // Switch is not activated; take the "max-height" as the target height.
2033 - 62
    targetHeight = (uint16_t) dynamicParams.heightSetting * 25L - 500L; // should be: 100 (or make a param out of it)
1960 - 63
  }
64
 
2069 - 65
  /*
1960 - 66
  if (++heightRampingTimer == INTEGRATION_FREQUENCY / 10) {
67
    heightRampingTimer = 0;
1961 - 68
    if (rampedTargetHeight < targetHeight) {
69
      // climbing
70
      if (rampedTargetHeight < targetHeight - staticParams.heightSlewRate) {
71
        rampedTargetHeight += staticParams.heightSlewRate;
72
      } else {
73
        rampedTargetHeight = targetHeight;
74
      }
2026 - 75
    } else {
1961 - 76
      // descending
77
      if (rampedTargetHeight > targetHeight + staticParams.heightSlewRate) {
78
        rampedTargetHeight -= staticParams.heightSlewRate;
79
      } else {
80
        rampedTargetHeight = targetHeight;
81
      }
1960 - 82
    }
83
  }
2069 - 84
  */
1970 - 85
 
1960 - 86
  // height, in meters (so the division factor is: 100)
2059 - 87
  // debugOut.analog[24] = (117100 - filteredAirPressure) / 100;
1971 - 88
  // Calculated 0 alt number: 108205
1980 - 89
  // Experimental 0 alt number: 117100
1775 - 90
}
91
 
92
// takes 180-200 usec (with integral term). That is too heavy!!!
93
// takes 100 usec without integral term.
2048 - 94
void HC_periodicTaskAndPRTY(int16_t* PRTY) {
95
  HC_periodicTask();
96
  int16_t throttle = PRTY[CONTROL_THROTTLE];
2033 - 97
  int32_t height = analog_getHeight();
2069 - 98
  int32_t heightError = /*ramped*/ targetHeight - height;
2033 - 99
  int16_t dHeight = analog_getDHeight();
1960 - 100
 
2058 - 101
  debugOut.analog[22] = height/10L;
102
  debugOut.analog[23] = dHeight;
2055 - 103
 
2026 - 104
  if (heightError > 0) {
1960 - 105
    debugOut.digital[0] |= DEBUG_HEIGHT_DIFF;
2035 - 106
  } else {
1961 - 107
    debugOut.digital[0] &= ~DEBUG_HEIGHT_DIFF;
2035 - 108
  }
109
 
110
  if (dHeight > 0) {
1960 - 111
    debugOut.digital[1] |= DEBUG_HEIGHT_DIFF;
2035 - 112
  } else {
113
    debugOut.digital[1] &= ~DEBUG_HEIGHT_DIFF;
1960 - 114
  }
2032 - 115
 
2033 - 116
  // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec....
117
  iHeight += heightError;
2032 - 118
 
2033 - 119
#define IHEIGHT_SCALE 24
120
  // dThrottle is in the range between +/- 1<<(IHEIGHT_SCALE+8)>>(IHEIGHT_SCALE) = +/- 256
2035 - 121
  int16_t dThrottleI =  (iHeight * (int32_t)dynamicParams.heightI) >> (IHEIGHT_SCALE);
2033 - 122
 
2035 - 123
  if (dThrottleI > staticParams.heightControlMaxIntegral) {
124
    dThrottleI = staticParams.heightControlMaxIntegral;
125
    iHeight = ((int32_t)staticParams.heightControlMaxIntegral << IHEIGHT_SCALE) / dynamicParams.heightI;
126
  } else if (dThrottleI < -staticParams.heightControlMaxIntegral) {
127
    dThrottleI = -staticParams.heightControlMaxIntegral;
128
    iHeight = -((int32_t)staticParams.heightControlMaxIntegral << IHEIGHT_SCALE) / dynamicParams.heightI;
2033 - 129
  }
130
 
2035 - 131
  int16_t dThrottleP = (heightError * dynamicParams.heightP) >> 10;
132
  int16_t dThrottleD = (dHeight * dynamicParams.heightD) >> 7;
2033 - 133
 
2055 - 134
  //debugOut.analog[24] = dThrottleP;
135
  //debugOut.analog[25] = dThrottleI;
136
  //debugOut.analog[26] = dThrottleD;
2032 - 137
 
2045 - 138
  //debugOut.analog[27] = dynamicParams.heightP;
139
  //debugOut.analog[28] = dynamicParams.heightI;
140
  //debugOut.analog[29] = dynamicParams.heightD;
2035 - 141
 
142
  int16_t dThrottle = dThrottleI + dThrottleP + dThrottleD;
143
 
1961 - 144
  if (dThrottle > staticParams.heightControlMaxThrottleChange)
145
    dThrottle = staticParams.heightControlMaxThrottleChange;
146
  else if (dThrottle < -staticParams.heightControlMaxThrottleChange)
147
    dThrottle = -staticParams.heightControlMaxThrottleChange;
2026 - 148
 
2055 - 149
  /*
2035 - 150
  debugOut.analog[19] = throttle;
151
  debugOut.analog[20] = dThrottle;
152
  debugOut.analog[21] = height;
153
  debugOut.analog[22] = rampedTargetHeight;
154
  debugOut.analog[23] = heightError;
2055 - 155
  */
2026 - 156
 
1961 - 157
  if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) {
2069 - 158
    if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) || HC_isSwitchOn()) {
1960 - 159
      // If switch is not in use --> Just apply height control.
160
      // If switch is in use     --> only apply height control when switch is also ON.
161
      throttle += dThrottle;
162
    }
163
  }
1961 - 164
 
165
  /* Experiment: Find hover-throttle */
166
 
167
#define DEFAULT_HOVERTHROTTLE 50
168
int32_t stronglyFilteredHeightDiff = 0;
2036 - 169
// uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE;
1961 - 170
uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE;
171
#define HOVERTHROTTLEFILTER 25
1960 - 172
 
173
  stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff
174
                                * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER;
175
  stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER
176
                                                          - 1) + throttle) / HOVERTHROTTLEFILTER;
177
 
2035 - 178
  /*
1960 - 179
  if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3
180
      && stronglyFilteredHeightDiff > -3) {
181
    hoverThrottle = stronglyFilteredThrottle;
182
    debugOut.digital[0] |= DEBUG_HOVERTHROTTLE;
183
  } else
184
    debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE;
2035 - 185
 
186
    */
1960 - 187
 
2048 - 188
  PRTY[CONTROL_THROTTLE] = throttle;
1775 - 189
}
190
 
191
/*
1821 - 192
 For a variometer thingy:
193
 When switch is thrown on, freeze throttle (capture it into variable)
194
 For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping.
195
 Output = frozen throttle + whatever is computed +/-. Integral?
196
 */