Subversion Repositories FlightCtrl

Rev

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