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