Subversion Repositories FlightCtrl

Rev

Rev 2132 | Rev 2136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2108 - 1
#include <util/delay.h>
2
#include <stddef.h>
3
#include <string.h>
4
#include "configuration.h"
5
#include "rc.h"
6
#include "output.h"
7
#include "flight.h"
8
 
9
int16_t variables[VARIABLE_COUNT];
10
ParamSet_t staticParams;
2116 - 11
ChannelMap_t channelMap;
2129 - 12
RCTrim_t rcTrim;
2108 - 13
IMUConfig_t IMUConfig;
14
volatile DynamicParams_t dynamicParams;
15
 
16
VersionInfo_t versionInfo;
17
 
18
FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL;
19
// updated by considering airspeed..
20
volatile uint16_t isFlying = 0;
21
 
22
const MMXLATION XLATIONS[] = {
23
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
24
 
25
{offsetof(ParamSet_t, gyroPID[0].P), offsetof(DynamicParams_t, gyroPID[0].P),0,255},
26
{offsetof(ParamSet_t, gyroPID[0].I), offsetof(DynamicParams_t, gyroPID[0].I),0,255},
27
{offsetof(ParamSet_t, gyroPID[0].D), offsetof(DynamicParams_t, gyroPID[0].D),0,255},
28
 
29
{offsetof(ParamSet_t, gyroPID[1].P), offsetof(DynamicParams_t, gyroPID[1].P),0,255},
30
{offsetof(ParamSet_t, gyroPID[1].I), offsetof(DynamicParams_t, gyroPID[1].I),0,255},
31
{offsetof(ParamSet_t, gyroPID[1].D), offsetof(DynamicParams_t, gyroPID[1].D),0,255},
32
 
33
{offsetof(ParamSet_t, gyroPID[2].P), offsetof(DynamicParams_t, gyroPID[2].P),0,255},
34
{offsetof(ParamSet_t, gyroPID[2].I), offsetof(DynamicParams_t, gyroPID[2].I),0,255},
35
{offsetof(ParamSet_t, gyroPID[2].D), offsetof(DynamicParams_t, gyroPID[2].D),0,255},
36
 
2129 - 37
{offsetof(ParamSet_t, gimbalServoConfigurations[0].manualControl), offsetof(DynamicParams_t, gimbalServoManualControl[0]),0,255},
38
{offsetof(ParamSet_t, gimbalServoConfigurations[1].manualControl), offsetof(DynamicParams_t, gimbalServoManualControl[1]),0,255},
2108 - 39
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
40
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
41
};
42
 
43
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
44
  uint8_t result;
45
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
46
  else result = src;
47
  if (result < min) result = min;
48
  else if (result > max) result = max;
49
  return result;
50
}
51
 
52
void configuration_applyVariablesToParams(void) {
53
  uint8_t i, src;
54
  uint8_t* pointerToTgt;
55
 
56
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
57
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
58
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
59
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
60
  }
61
 
62
  // User parameters are always variable.
63
  for (i=0; i<sizeof(staticParams.userParams); i++) {
64
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
65
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
66
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
67
  }
68
}
69
 
70
void configuration_setFlightParameters(uint8_t newFlightMode) {
71
        currentFlightMode = newFlightMode;
72
        flight_updateFlightParametersToFlightMode();
73
}
74
 
75
// Called after a change in configuration parameters, as a hook for modules to take over changes.
76
void configuration_paramSetDidChange(void) {
77
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
78
  configuration_setFlightParameters(currentFlightMode);
79
  // Immediately load changes to output, and also signal the paramset change.
80
  output_init();
81
}
82
 
83
void setOtherDefaults(void) {
84
  // Control
85
  staticParams.externalControl = 0;
86
  staticParams.IFactor = 52;
87
 
2109 - 88
  staticParams.airspeedCorrection = 100;
89
  staticParams.isFlyingThreshold = 100;
90
 
2108 - 91
  // Servos
92
  staticParams.servoCount = 7;
2135 - 93
  staticParams.servosReverse = CONTROL_SERVO_REVERSE_AILERONS;
2129 - 94
 
2119 - 95
  staticParams.gimbalServoMaxManualSpeed = 10;
2108 - 96
  for (uint8_t i=0; i<2; i++) {
2129 - 97
    staticParams.gimbalServoConfigurations[i].manualControl = 128;
98
    staticParams.gimbalServoConfigurations[i].stabilizationFactor = 0;
99
    staticParams.gimbalServoConfigurations[i].minValue = 32;
100
    staticParams.gimbalServoConfigurations[i].maxValue = 224;
101
    staticParams.gimbalServoConfigurations[i].flags = 0;
2108 - 102
  }
103
 
104
  // Battery warning and emergency flight
2119 - 105
  staticParams.batteryWarningVoltage = 101;  // 3.7 each for 3S
2108 - 106
 
107
  for (uint8_t i=0; i<3; i++) {
108
          staticParams.gyroPID[i].P = 80;
2132 - 109
          staticParams.gyroPID[i].I = 40;
2108 - 110
          staticParams.gyroPID[i].D = 40;
2109 - 111
          staticParams.gyroPID[i].iMax = 45;
2108 - 112
  }
113
 
2132 - 114
  staticParams.stickIElevator = 40;
115
  staticParams.stickIAilerons = 60;
116
  staticParams.stickIRudder = 20;
2108 - 117
 
118
  // Outputs
119
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
120
  staticParams.outputFlash[0].timing = 15;
121
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
122
  staticParams.outputFlash[1].timing = 15;
123
 
124
  staticParams.outputDebugMask = 8;
125
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
126
}
127
 
128
/***************************************************/
129
/*    Default Values for parameter set 1           */
130
/***************************************************/
131
void paramSet_default(uint8_t setnumber) {
132
  setOtherDefaults();
133
 
134
  for (uint8_t i=0; i<8; i++) {
135
    staticParams.userParams[i] = i;
136
  }
137
 
138
  staticParams.bitConfig =
139
    CFG_GYRO_SATURATION_PREVENTION;
140
 
141
  memcpy(staticParams.name, "Default\0", 6);
142
}
143
 
144
void IMUConfig_default(void) {
145
  IMUConfig.gyroPIDFilterConstant = 1;
146
  IMUConfig.gyroDFilterConstant = 1;
147
  IMUConfig.rateTolerance = 120;
148
  IMUConfig.gyroDWindowLength = 3;
2135 - 149
  IMUConfig.gyroQuadrant = 4;
150
  IMUConfig.imuReversedFlags = IMU_REVERSE_GYRO_PR;
2129 - 151
  IMUConfig.gyroCalibrationTweak[0] =
152
  IMUConfig.gyroCalibrationTweak[1] =
153
  IMUConfig.gyroCalibrationTweak[2] = 0;
2108 - 154
}
155
 
156
/***************************************************/
157
/*    Default Values for R/C Channels              */
158
/***************************************************/
159
void channelMap_default(void) {
2129 - 160
  channelMap.RCPolarity = 1;
2135 - 161
  channelMap.HWTrim = 166;
162
  channelMap.variableOffset = 131;
2108 - 163
  channelMap.channels[CH_ELEVATOR] = 1;
164
  channelMap.channels[CH_AILERONS] = 0;
165
  channelMap.channels[CH_THROTTLE] = 2;
166
  channelMap.channels[CH_RUDDER]   = 3;
167
  channelMap.channels[CH_POTS + 0] = 4;
168
  channelMap.channels[CH_POTS + 1] = 5;
169
  channelMap.channels[CH_POTS + 2] = 6;
170
  channelMap.channels[CH_POTS + 3] = 7;
171
}