Subversion Repositories FlightCtrl

Rev

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