Subversion Repositories FlightCtrl

Rev

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