Subversion Repositories FlightCtrl

Rev

Rev 1963 | Rev 1971 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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