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