Subversion Repositories FlightCtrl

Rev

Rev 2069 | Rev 2073 | 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();
2069 - 99
  int32_t heightError = /*ramped*/ 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
 
2033 - 117
  // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec....
118
  iHeight += heightError;
2032 - 119
 
2033 - 120
#define IHEIGHT_SCALE 24
121
  // dThrottle is in the range between +/- 1<<(IHEIGHT_SCALE+8)>>(IHEIGHT_SCALE) = +/- 256
2035 - 122
  int16_t dThrottleI =  (iHeight * (int32_t)dynamicParams.heightI) >> (IHEIGHT_SCALE);
2033 - 123
 
2035 - 124
  if (dThrottleI > staticParams.heightControlMaxIntegral) {
125
    dThrottleI = staticParams.heightControlMaxIntegral;
126
    iHeight = ((int32_t)staticParams.heightControlMaxIntegral << IHEIGHT_SCALE) / dynamicParams.heightI;
127
  } else if (dThrottleI < -staticParams.heightControlMaxIntegral) {
128
    dThrottleI = -staticParams.heightControlMaxIntegral;
129
    iHeight = -((int32_t)staticParams.heightControlMaxIntegral << IHEIGHT_SCALE) / dynamicParams.heightI;
2033 - 130
  }
131
 
2035 - 132
  int16_t dThrottleP = (heightError * dynamicParams.heightP) >> 10;
133
  int16_t dThrottleD = (dHeight * dynamicParams.heightD) >> 7;
2033 - 134
 
2055 - 135
  //debugOut.analog[24] = dThrottleP;
136
  //debugOut.analog[25] = dThrottleI;
137
  //debugOut.analog[26] = dThrottleD;
2032 - 138
 
2045 - 139
  //debugOut.analog[27] = dynamicParams.heightP;
140
  //debugOut.analog[28] = dynamicParams.heightI;
141
  //debugOut.analog[29] = dynamicParams.heightD;
2035 - 142
 
2071 - 143
  int16_t dThrottle = dThrottleI + dThrottleP - dThrottleD;
2035 - 144
 
1961 - 145
  if (dThrottle > staticParams.heightControlMaxThrottleChange)
146
    dThrottle = staticParams.heightControlMaxThrottleChange;
147
  else if (dThrottle < -staticParams.heightControlMaxThrottleChange)
148
    dThrottle = -staticParams.heightControlMaxThrottleChange;
2026 - 149
 
2055 - 150
  /*
2035 - 151
  debugOut.analog[19] = throttle;
152
  debugOut.analog[20] = dThrottle;
153
  debugOut.analog[21] = height;
154
  debugOut.analog[22] = rampedTargetHeight;
155
  debugOut.analog[23] = heightError;
2055 - 156
  */
2026 - 157
 
1961 - 158
  if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) {
2069 - 159
    if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) || HC_isSwitchOn()) {
1960 - 160
      // If switch is not in use --> Just apply height control.
161
      // If switch is in use     --> only apply height control when switch is also ON.
162
      throttle += dThrottle;
163
    }
164
  }
1961 - 165
 
166
  /* Experiment: Find hover-throttle */
167
 
168
#define DEFAULT_HOVERTHROTTLE 50
169
int32_t stronglyFilteredHeightDiff = 0;
2036 - 170
// uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE;
1961 - 171
uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE;
172
#define HOVERTHROTTLEFILTER 25
1960 - 173
 
174
  stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff
175
                                * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER;
176
  stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER
177
                                                          - 1) + throttle) / HOVERTHROTTLEFILTER;
178
 
2035 - 179
  /*
1960 - 180
  if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3
181
      && stronglyFilteredHeightDiff > -3) {
182
    hoverThrottle = stronglyFilteredThrottle;
183
    debugOut.digital[0] |= DEBUG_HOVERTHROTTLE;
184
  } else
185
    debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE;
2035 - 186
 
187
    */
1960 - 188
 
2048 - 189
  PRTY[CONTROL_THROTTLE] = throttle;
1775 - 190
}
191
 
192
/*
1821 - 193
 For a variometer thingy:
194
 When switch is thrown on, freeze throttle (capture it into variable)
195
 For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping.
196
 Output = frozen throttle + whatever is computed +/-. Integral?
197
 */