Subversion Repositories FlightCtrl

Rev

Rev 1955 | Rev 1961 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1955 Rev 1960
Line 47... Line 47...
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <util/delay.h>
51
#include <util/delay.h>
52
#include <avr/eeprom.h>
-
 
53
#include <stddef.h>
52
#include <stddef.h>
-
 
53
#include <string.h>
54
#include "configuration.h"
54
#include "configuration.h"
55
#include "eeprom.h"
55
#include "sensors.h"
-
 
56
#include "rc.h"
Line 56... Line 57...
56
 
57
 
-
 
58
int16_t variables[8] = {0,0,0,0,0,0,0,0};
-
 
59
paramset_t staticParams;
-
 
60
channelMap_t channelMap;
57
int16_t variables[8] = {0,0,0,0,0,0,0,0};
61
mixerMatrix_t mixerMatrix;
58
dynamicParam_t dynamicParams = {48,251,16,58,64,8,150,150,2,10,{0,0,0,0,0,0,0,0},100,70,90,65,64,100,0,0,0};
62
dynamicParam_t dynamicParams; // = {48,251,16,58,64,8,150,150,2,10,{0,0,0,0,0,0,0,0},100,70,90,65,64,100,0,0,0};
59
uint8_t CPUType = ATMEGA644;
63
uint8_t CPUType = ATMEGA644;
60
uint8_t BoardRelease = 13;
-
 
-
 
64
uint8_t boardRelease = 13;
61
 
65
uint8_t requiredMotors;
62
/************************************************************************
66
/************************************************************************
63
 * Map the parameter to pot values                                    
67
 * Map the parameter to pot values                                    
64
 * Replacing this code by the code below saved almost 1 kbyte.
68
 * Replacing this code by the code below saved almost 1 kbyte.
Line 65... Line 69...
65
 ************************************************************************/
69
 ************************************************************************/
66
 
70
 
67
void configuration_applyVariablesToParams(void) {
71
void configuration_applyVariablesToParams(void) {
68
  uint8_t i;
72
  uint8_t i;
69
#define SET_POT_MM(b,a,min,max) {if (a<255) {if (a>=251) b=variables[a-251]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
-
 
70
#define SET_POT(b,a) { if (a<255) {if (a>=251) b=variables[a-251]; else b=a;}}
-
 
71
  SET_POT(dynamicParams.MaxHeight,staticParams.MaxHeight);
-
 
72
  SET_POT_MM(dynamicParams.HeightD,staticParams.HeightD,0,100);
-
 
73
  SET_POT_MM(dynamicParams.HeightP,staticParams.HeightP,0,100);
-
 
74
  SET_POT(dynamicParams.Height_ACC_Effect,staticParams.Height_ACC_Effect);
73
#define SET_POT_MM(b,a,min,max) {if (a<255) {if (a>=251) b=variables[a-251]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
75
  SET_POT(dynamicParams.CompassYawEffect,staticParams.CompassYawEffect);
74
#define SET_POT(b,a) { if (a<255) {if (a>=251) b=variables[a-251]; else b=a;}}
76
  SET_POT_MM(dynamicParams.GyroP,staticParams.GyroP,0,255);
75
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
77
  SET_POT(dynamicParams.GyroI,staticParams.GyroI);
76
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
78
  SET_POT(dynamicParams.GyroD,staticParams.GyroD);
-
 
79
  SET_POT(dynamicParams.IFactor,staticParams.IFactor);
77
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
80
  for (i=0; i<sizeof(staticParams.UserParams1); i++) {
-
 
81
    SET_POT(dynamicParams.UserParams[i],staticParams.UserParams1[i]);
-
 
82
  }
78
  SET_POT(dynamicParams.IFactor, staticParams.IFactor);
83
  for (i=0; i<sizeof(staticParams.UserParams2); i++) {
79
  SET_POT(dynamicParams.yawIFactor, staticParams.yawIFactor);
84
    SET_POT(dynamicParams.UserParams[i + sizeof(staticParams.UserParams1)], staticParams.UserParams2[i]);
80
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);
85
  }
-
 
86
  SET_POT(dynamicParams.ServoPitchControl,staticParams.ServoPitchControl);
81
 
87
  SET_POT(dynamicParams.LoopGasLimit,staticParams.LoopGasLimit);
82
  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
88
  SET_POT(dynamicParams.AxisCoupling1,staticParams.AxisCoupling1);
83
  SET_POT(dynamicParams.axisCoupling1, staticParams.axisCoupling1);
89
  SET_POT(dynamicParams.AxisCoupling2,staticParams.AxisCoupling2);
84
  SET_POT(dynamicParams.axisCoupling2, staticParams.axisCoupling2);
90
  SET_POT(dynamicParams.AxisCouplingYawCorrection,staticParams.AxisCouplingYawCorrection);
-
 
91
  SET_POT(dynamicParams.DynamicStability,staticParams.DynamicStability);
85
  SET_POT(dynamicParams.axisCouplingYawCorrection, staticParams.axisCouplingYawCorrection);
92
  SET_POT_MM(dynamicParams.J16Timing,staticParams.J16Timing,1,255);
86
  SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
93
  SET_POT_MM(dynamicParams.J17Timing,staticParams.J17Timing,1,255);
87
  SET_POT(dynamicParams.maxControlActivityForAcc,staticParams.maxControlActivityForAcc);
94
 
88
 
95
#if defined (USE_NAVICTRL)
89
  SET_POT_MM(dynamicParams.heightP, staticParams.heightP,0,100);
96
  SET_POT(dynamicParams.NaviGpsModeControl,staticParams.NaviGpsModeControl);
90
  SET_POT_MM(dynamicParams.heightD, staticParams.heightD,0,100);
97
  SET_POT(dynamicParams.NaviGpsGain,staticParams.NaviGpsGain);
91
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
-
 
92
  SET_POT(dynamicParams.heightACCEffect,staticParams.heightACCEffect);
98
  SET_POT(dynamicParams.NaviGpsP,staticParams.NaviGpsP);
93
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
99
  SET_POT(dynamicParams.NaviGpsI,staticParams.NaviGpsI);
94
 
-
 
95
  for (i=0; i<sizeof(staticParams.userParams); i++) {
-
 
96
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
100
  SET_POT(dynamicParams.NaviGpsD,staticParams.NaviGpsD);
97
  }
101
  SET_POT(dynamicParams.NaviGpsACC,staticParams.NaviGpsACC);
98
 
-
 
99
  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
102
  SET_POT_MM(dynamicParams.NaviOperatingRadius,staticParams.NaviOperatingRadius,10, 255);
100
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
103
  SET_POT(dynamicParams.NaviWindCorrection,staticParams.NaviWindCorrection);
-
 
104
  SET_POT(dynamicParams.NaviSpeedCompensation,staticParams.NaviSpeedCompensation);
101
 
105
#endif
102
  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
Line 106... Line 103...
106
  SET_POT(dynamicParams.ExternalControl,staticParams.ExternalControl);
103
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
107
}
104
}
108
 
-
 
109
const XLATION XLATIONS[] = {
-
 
110
  {offsetof(paramset_t, MaxHeight), offsetof(dynamicParam_t, MaxHeight)},
-
 
111
  {offsetof(paramset_t, Height_ACC_Effect), offsetof(dynamicParam_t, Height_ACC_Effect)},
-
 
112
  {offsetof(paramset_t, CompassYawEffect), offsetof(dynamicParam_t, CompassYawEffect)},
-
 
113
  {offsetof(paramset_t, GyroI), offsetof(dynamicParam_t, GyroI)},
-
 
114
  {offsetof(paramset_t, GyroD), offsetof(dynamicParam_t, GyroD)},
-
 
115
  {offsetof(paramset_t, IFactor), offsetof(dynamicParam_t, IFactor)},
-
 
116
  {offsetof(paramset_t, ServoPitchControl), offsetof(dynamicParam_t, ServoPitchControl)},
-
 
117
  {offsetof(paramset_t, LoopGasLimit), offsetof(dynamicParam_t, LoopGasLimit)},
-
 
118
  {offsetof(paramset_t, AxisCoupling1), offsetof(dynamicParam_t, AxisCoupling1)},
-
 
119
  {offsetof(paramset_t, AxisCoupling2), offsetof(dynamicParam_t, AxisCoupling2)},
-
 
120
  {offsetof(paramset_t, AxisCouplingYawCorrection), offsetof(dynamicParam_t, AxisCouplingYawCorrection)},
-
 
121
  {offsetof(paramset_t, DynamicStability), offsetof(dynamicParam_t, DynamicStability)},
-
 
122
  {offsetof(paramset_t, NaviGpsModeControl),
-
 
123
   offsetof(dynamicParam_t, NaviGpsModeControl)},
-
 
124
  {offsetof(paramset_t, NaviGpsGain), offsetof(dynamicParam_t, NaviGpsGain)},
-
 
125
  {offsetof(paramset_t, NaviGpsP), offsetof(dynamicParam_t, NaviGpsP)},
-
 
126
  {offsetof(paramset_t, NaviGpsI), offsetof(dynamicParam_t, NaviGpsI)},
-
 
127
  {offsetof(paramset_t, NaviGpsD), offsetof(dynamicParam_t, NaviGpsD)},
-
 
128
  {offsetof(paramset_t, NaviGpsACC), offsetof(dynamicParam_t, NaviGpsACC)},
-
 
129
  {offsetof(paramset_t, NaviWindCorrection), offsetof(dynamicParam_t, NaviWindCorrection)},
105
 
Line 130... Line 106...
130
  {offsetof(paramset_t, NaviSpeedCompensation), offsetof(dynamicParam_t, NaviSpeedCompensation)},
106
const XLATION XLATIONS[] = {
131
  {offsetof(paramset_t, ExternalControl), offsetof(dynamicParam_t, ExternalControl)}
107
  //  {offsetof(paramset_t, MaxHeight), offsetof(dynamicParam_t, MaxHeight)},
132
};
-
 
133
 
-
 
134
const MMXLATION MMXLATIONS[] = {
-
 
135
  {offsetof(paramset_t, HeightD), offsetof(dynamicParam_t, HeightD),0,100},
-
 
136
  {offsetof(paramset_t, HeightP), offsetof(dynamicParam_t, HeightP),0,150},
-
 
137
  {offsetof(paramset_t, GyroP), offsetof(dynamicParam_t, GyroP),0,255},
108
};
Line 138... Line 109...
138
  {offsetof(paramset_t, J16Timing), offsetof(dynamicParam_t, J16Timing),1,255},
109
 
139
  {offsetof(paramset_t, J17Timing), offsetof(dynamicParam_t, J17Timing),1,255},
110
const MMXLATION MMXLATIONS[] = {
140
  {offsetof(paramset_t, NaviOperatingRadius), offsetof(dynamicParam_t, NaviOperatingRadius),10,255}
111
  //  {offsetof(paramset_t, HeightD), offsetof(dynamicParam_t, HeightD),0,100},
Line 166... Line 137...
166
    if (src < 255) {
137
    if (src < 255) {
167
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
138
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
168
    }
139
    }
169
  }
140
  }
Line 170... Line 141...
170
 
141
 
171
  for (i=0; i<sizeof(staticParams.UserParams1); i++) {
142
  for (i=0; i<sizeof(staticParams.userParams); i++) {
172
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, UserParams1) + i));
143
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, userParams) + i));
173
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, UserParams) + i);
-
 
174
    if (src < 255) {
-
 
175
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
-
 
176
    }    
-
 
177
  }
-
 
178
 
-
 
179
  for (i=0; i<sizeof(staticParams.UserParams2); i++) {
-
 
180
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, UserParams2) + i));
-
 
181
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, UserParams) + sizeof(staticParams.UserParams1) + i);
144
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
182
    if (src < 255) {
145
    if (src < 255) {
183
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
146
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
184
    }    
147
    }    
185
  }
148
  }
Line 201... Line 164...
201
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
164
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
202
 * on different HW version....
165
 * on different HW version....
203
 */
166
 */
Line 204... Line 167...
204
 
167
 
205
uint8_t getBoardRelease(void) {
168
uint8_t getBoardRelease(void) {
206
  uint8_t BoardRelease = 13;
169
  uint8_t boardRelease = 13;
Line 207... Line 170...
207
  // the board release is coded via the pull up or down the 2 status LED
170
  // the board release is coded via the pull up or down the 2 status LED
208
 
171
 
Line 209... Line 172...
209
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
172
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
Line 210... Line 173...
210
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
173
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
211
 
174
 
212
  _delay_loop_2(1000); // make some delay
175
  _delay_loop_2(1000); // make some delay
213
 
176
 
214
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
177
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
215
    case 0x00:
178
    case 0x00:
216
      BoardRelease = 10; // 1.0
179
      boardRelease = 10; // 1.0
217
      break;
180
      break;
218
    case 0x01:
181
    case 0x01:
219
      BoardRelease = 11; // 1.1 or 1.2
182
      boardRelease = 11; // 1.1 or 1.2
220
      break;
183
      break;
221
    case 0x02:
184
    case 0x02:
222
      BoardRelease = 20; // 2.0
185
      boardRelease = 20; // 2.0
223
      break;
186
      break;
224
    case 0x03:
187
    case 0x03:
225
      BoardRelease = 13; // 1.3
188
      boardRelease = 13; // 1.3
226
      break;
189
      break;
227
    default:
190
    default:
228
      break;
191
      break;
229
    }
192
    }
230
  // set LED ports as output
193
  // set LED ports as output
231
  DDRB |= (1<<DDB1)|(1<<DDB0);
194
  DDRB |= (1<<DDB1)|(1<<DDB0);
-
 
195
  RED_ON;
-
 
196
  GRN_OFF;
-
 
197
  return boardRelease;
-
 
198
}
-
 
199
 
-
 
200
void setOtherDefaults(void) {
-
 
201
  // Height Control
-
 
202
  staticParams.heightP = 10;
-
 
203
  staticParams.heightD = 30;
-
 
204
  staticParams.heightSetting = 251;
-
 
205
  staticParams.heightMaxThrottleChange = 10;
-
 
206
  staticParams.heightSlewRate = 4;
-
 
207
  staticParams.heightACCEffect = 30;
-
 
208
 
-
 
209
  // Control
-
 
210
  staticParams.stickP = 8;
-
 
211
  staticParams.stickD = 12;
-
 
212
  staticParams.stickYawP = 12;
-
 
213
  staticParams.stickThrottleD = 12;
-
 
214
  staticParams.minThrottle = 8;
-
 
215
  staticParams.maxThrottle = 230;
-
 
216
  staticParams.externalControl = 0;
-
 
217
 
-
 
218
  // IMU
-
 
219
  staticParams.gyroPIDFilterConstant = 1;
-
 
220
  staticParams.gyroATTFilterConstant = 1;
-
 
221
  staticParams.gyroDFilterConstant = 1;
-
 
222
  staticParams.accFilterConstant = 4;
-
 
223
 
-
 
224
  staticParams.gyroP = 60;
-
 
225
  staticParams.gyroI = 80;
-
 
226
  staticParams.gyroD = 4;
-
 
227
 
-
 
228
  // set by gyro code.
-
 
229
  // staticParams.zerothOrderCorrection = 
-
 
230
  // staticParams.driftCompDivider = 
-
 
231
  // staticParams.driftCompLimit = 
-
 
232
 
-
 
233
  staticParams.axisCoupling1 = 90;
-
 
234
  staticParams.axisCoupling2 = 67;
-
 
235
  staticParams.axisCouplingYawCorrection = 0;
-
 
236
  staticParams.dynamicStability = 50;
-
 
237
  staticParams.IFactor = 32;  
-
 
238
  staticParams.compassYawEffect = 128;
-
 
239
 
-
 
240
  // Servos
-
 
241
  for (uint8_t i=0; i<2; i++) {
-
 
242
    staticParams.servoConfigurations[i].manualControl = 128;
-
 
243
    staticParams.servoConfigurations[i].compensationFactor = 0;
-
 
244
    staticParams.servoConfigurations[i].minValue = 64;
-
 
245
    staticParams.servoConfigurations[i].maxValue = 192;
-
 
246
    staticParams.servoConfigurations[i].flags = 0;
-
 
247
  }
-
 
248
  staticParams.servoCount = 4;
-
 
249
 
-
 
250
  // Battery warning and emergency flight
-
 
251
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
-
 
252
  staticParams.emergencyThrottle = 35;
-
 
253
  staticParams.emergencyFlightDuration = 30;
-
 
254
 
-
 
255
  // Outputs
-
 
256
  staticParams.outputFlash[0].bitmask = 0b01011111;
-
 
257
  staticParams.outputFlash[0].timing = 15;
-
 
258
  staticParams.outputFlash[1].bitmask = 0b11110011;
-
 
259
  staticParams.outputFlash[1].timing = 15;
-
 
260
 
-
 
261
  staticParams.outputFlags = 0;
-
 
262
}
-
 
263
 
-
 
264
/***************************************************/
-
 
265
/*    Default Values for parameter set 1           */
-
 
266
/***************************************************/
-
 
267
void paramSet_default(uint8_t setnumber) {
-
 
268
  setOtherDefaults();
-
 
269
  gyro_setDefaults();
-
 
270
 
-
 
271
  for (uint8_t i=0; i<8; i++) {
-
 
272
    staticParams.userParams[i] = 0;
-
 
273
  }
-
 
274
 
-
 
275
  staticParams.bitConfig =
-
 
276
    CFG_GYRO_SATURATION_PREVENTION |
-
 
277
    CFG_HEADING_HOLD;
-
 
278
  staticParams.bitConfig2 = 0;
-
 
279
 
-
 
280
  memcpy(staticParams.name, "Default\0", 6);
-
 
281
}
-
 
282
 
-
 
283
/***************************************************/
-
 
284
/*    Default Values for Mixer Table               */
-
 
285
/***************************************************/
-
 
286
void mixerMatrix_default(void) { // Quadro 
-
 
287
  uint8_t i;
-
 
288
  // mixerMatric.revision = EEMIXER_REVISION;
-
 
289
  // clear mixer table (but preset throttle)
-
 
290
  for (i = 0; i < 16; i++) {
-
 
291
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
-
 
292
    mixerMatrix.motor[i][MIX_PITCH] = 0;
-
 
293
    mixerMatrix.motor[i][MIX_ROLL] = 0;
-
 
294
    mixerMatrix.motor[i][MIX_YAW] = 0;
-
 
295
  }
-
 
296
  // default = Quadro
-
 
297
  mixerMatrix.motor[0][MIX_PITCH] = +64;
-
 
298
  mixerMatrix.motor[0][MIX_YAW] = +64;
-
 
299
  mixerMatrix.motor[1][MIX_PITCH] = -64;
-
 
300
  mixerMatrix.motor[1][MIX_YAW] = +64;
-
 
301
  mixerMatrix.motor[2][MIX_ROLL] = -64;
-
 
302
  mixerMatrix.motor[2][MIX_YAW] = -64;
-
 
303
  mixerMatrix.motor[3][MIX_ROLL] = +64;
-
 
304
  mixerMatrix.motor[3][MIX_YAW] = -64;
-
 
305
  memcpy(mixerMatrix.name, "Quadro\0", 7);
-
 
306
}
-
 
307
 
-
 
308
void channelMap_default(void) {
-
 
309
  channelMap.channels[CH_PITCH] = 2;
-
 
310
  channelMap.channels[CH_ROLL] = 1;
-
 
311
  channelMap.channels[CH_THROTTLE] = 3;
-
 
312
  channelMap.channels[CH_YAW] = 4;
-
 
313
  channelMap.channels[CH_POTS + 0] = 5;
-
 
314
  channelMap.channels[CH_POTS + 1] = 6;