Subversion Repositories FlightCtrl

Rev

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

Rev 2109 Rev 2116
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"
-
 
6
#include "rc.h"
5
#include "rc.h"
7
#include "output.h"
6
#include "output.h"
8
#include "flight.h"
7
#include "flight.h"
9
 
8
 
10
int16_t variables[VARIABLE_COUNT];
9
int16_t variables[VARIABLE_COUNT];
11
ParamSet_t staticParams;
10
ParamSet_t staticParams;
12
channelMap_t channelMap;
11
ChannelMap_t channelMap;
13
IMUConfig_t IMUConfig;
12
IMUConfig_t IMUConfig;
14
volatile DynamicParams_t dynamicParams;
13
volatile DynamicParams_t dynamicParams;
15
 
14
 
16
uint8_t CPUType;
15
uint8_t CPUType;
17
uint8_t boardRelease;
16
uint8_t boardRelease;
18
uint8_t requiredMotors;
17
uint8_t requiredMotors;
19
 
18
 
20
VersionInfo_t versionInfo;
19
VersionInfo_t versionInfo;
21
 
20
 
22
FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL;
21
FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL;
23
volatile uint16_t isFlying= 0;
22
volatile uint16_t isFlying= 0;
24
 
23
 
25
const MMXLATION XLATIONS[] = {
24
const MMXLATION XLATIONS[] = {
26
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
25
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
27
 
26
 
28
{offsetof(ParamSet_t, gyroPID[0].P), offsetof(DynamicParams_t, gyroPID[0].P),0,255},
27
{offsetof(ParamSet_t, gyroPID[0].P), offsetof(DynamicParams_t, gyroPID[0].P),0,255},
29
{offsetof(ParamSet_t, gyroPID[0].I), offsetof(DynamicParams_t, gyroPID[0].I),0,255},
28
{offsetof(ParamSet_t, gyroPID[0].I), offsetof(DynamicParams_t, gyroPID[0].I),0,255},
30
{offsetof(ParamSet_t, gyroPID[0].D), offsetof(DynamicParams_t, gyroPID[0].D),0,255},
29
{offsetof(ParamSet_t, gyroPID[0].D), offsetof(DynamicParams_t, gyroPID[0].D),0,255},
31
 
30
 
32
{offsetof(ParamSet_t, gyroPID[1].P), offsetof(DynamicParams_t, gyroPID[1].P),0,255},
31
{offsetof(ParamSet_t, gyroPID[1].P), offsetof(DynamicParams_t, gyroPID[1].P),0,255},
33
{offsetof(ParamSet_t, gyroPID[1].I), offsetof(DynamicParams_t, gyroPID[1].I),0,255},
32
{offsetof(ParamSet_t, gyroPID[1].I), offsetof(DynamicParams_t, gyroPID[1].I),0,255},
34
{offsetof(ParamSet_t, gyroPID[1].D), offsetof(DynamicParams_t, gyroPID[1].D),0,255},
33
{offsetof(ParamSet_t, gyroPID[1].D), offsetof(DynamicParams_t, gyroPID[1].D),0,255},
35
 
34
 
36
{offsetof(ParamSet_t, gyroPID[2].P), offsetof(DynamicParams_t, gyroPID[2].P),0,255},
35
{offsetof(ParamSet_t, gyroPID[2].P), offsetof(DynamicParams_t, gyroPID[2].P),0,255},
37
{offsetof(ParamSet_t, gyroPID[2].I), offsetof(DynamicParams_t, gyroPID[2].I),0,255},
36
{offsetof(ParamSet_t, gyroPID[2].I), offsetof(DynamicParams_t, gyroPID[2].I),0,255},
38
{offsetof(ParamSet_t, gyroPID[2].D), offsetof(DynamicParams_t, gyroPID[2].D),0,255},
37
{offsetof(ParamSet_t, gyroPID[2].D), offsetof(DynamicParams_t, gyroPID[2].D),0,255},
39
 
38
 
40
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
39
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
41
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
40
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
42
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
41
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
43
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
42
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
44
};
43
};
45
 
44
 
46
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
45
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
47
  uint8_t result;
46
  uint8_t result;
48
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
47
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
49
  else result = src;
48
  else result = src;
50
  if (result < min) result = min;
49
  if (result < min) result = min;
51
  else if (result > max) result = max;
50
  else if (result > max) result = max;
52
  return result;
51
  return result;
53
}
52
}
54
 
53
 
55
void configuration_applyVariablesToParams(void) {
54
void configuration_applyVariablesToParams(void) {
56
  uint8_t i, src;
55
  uint8_t i, src;
57
  uint8_t* pointerToTgt;
56
  uint8_t* pointerToTgt;
58
 
57
 
59
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
58
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
60
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
59
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
61
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
60
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
62
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
61
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
63
  }
62
  }
64
 
63
 
65
  // User parameters are always variable.
64
  // User parameters are always variable.
66
  for (i=0; i<sizeof(staticParams.userParams); i++) {
65
  for (i=0; i<sizeof(staticParams.userParams); i++) {
67
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
66
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
68
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
67
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
69
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
68
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
70
  }
69
  }
71
}
70
}
72
 
71
 
73
void setCPUType(void) {   // works only after reset or power on when the registers have default values
72
void setCPUType(void) {   // works only after reset or power on when the registers have default values
74
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
73
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
75
  else CPUType = ATMEGA644;
74
  else CPUType = ATMEGA644;
76
}
75
}
77
 
76
 
78
/*
77
/*
79
 * Automatic detection of hardware components is not supported in this development-oriented
78
 * Automatic detection of hardware components is not supported in this development-oriented
80
 * FC firmware. It would go against the point of it: To enable alternative hardware
79
 * FC firmware. It would go against the point of it: To enable alternative hardware
81
 * configurations with otherwise unsupported components. Instead, one should write
80
 * configurations with otherwise unsupported components. Instead, one should write
82
 * custom code + adjust constants for the new hardware, and include the relevant code
81
 * custom code + adjust constants for the new hardware, and include the relevant code
83
 * from the makefile.
82
 * from the makefile.
84
 * However - we still do detect the board release. Reason: Otherwise it would be too
83
 * However - we still do detect the board release. Reason: Otherwise it would be too
85
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
84
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
86
 * on different HW version....
85
 * on different HW version....
87
 */
86
 */
88
void setBoardRelease(void) {
87
void setBoardRelease(void) {
89
  // the board release is coded via the pull up or down the 2 status LED
88
  // the board release is coded via the pull up or down the 2 status LED
90
 
89
 
91
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
90
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
92
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
91
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
93
 
92
 
94
  _delay_loop_2(1000); // make some delay
93
  _delay_loop_2(1000); // make some delay
95
 
94
 
96
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
95
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
97
    case 0x00:
96
    case 0x00:
98
      boardRelease = 10; // 1.0
97
      boardRelease = 10; // 1.0
99
      break;
98
      break;
100
    case 0x01:
99
    case 0x01:
101
      boardRelease = 11; // 1.1 or 1.2
100
      boardRelease = 11; // 1.1 or 1.2
102
      break;
101
      break;
103
    case 0x02:
102
    case 0x02:
104
      boardRelease = 20; // 2.0
103
      boardRelease = 20; // 2.0
105
      break;
104
      break;
106
    case 0x03:
105
    case 0x03:
107
      boardRelease = 13; // 1.3
106
      boardRelease = 13; // 1.3
108
      break;
107
      break;
109
    default:
108
    default:
110
      break;
109
      break;
111
    }
110
    }
112
  // set LED ports as output
111
  // set LED ports as output
113
  DDRB |= (1<<DDB1)|(1<<DDB0);
112
  DDRB |= (1<<DDB1)|(1<<DDB0);
114
  RED_OFF;
113
  RED_OFF;
115
  GRN_OFF;
114
  GRN_OFF;
116
}
115
}
117
 
116
 
118
void configuration_setFlightParameters(uint8_t newFlightMode) {
117
void configuration_setFlightParameters(uint8_t newFlightMode) {
119
        currentFlightMode = newFlightMode;
118
        currentFlightMode = newFlightMode;
120
        flight_updateFlightParametersToFlightMode();
119
        flight_updateFlightParametersToFlightMode();
121
}
120
}
122
 
121
 
123
// Called after a change in configuration parameters, as a hook for modules to take over changes.
122
// Called after a change in configuration parameters, as a hook for modules to take over changes.
124
void configuration_paramSetDidChange(void) {
123
void configuration_paramSetDidChange(void) {
125
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
124
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
126
  configuration_setFlightParameters(currentFlightMode);
125
  configuration_setFlightParameters(currentFlightMode);
127
  // Immediately load changes to output, and also signal the paramset change.
126
  // Immediately load changes to output, and also signal the paramset change.
128
  output_init();
127
  output_init();
129
}
128
}
130
 
129
 
131
void setOtherDefaults(void) {
130
void setOtherDefaults(void) {
132
  // Control
131
  // Control
133
  staticParams.externalControl = 0;
132
  staticParams.externalControl = 0;
134
  staticParams.IFactor = 52;
133
  staticParams.IFactor = 52;
135
 
134
 
136
  staticParams.airspeedCorrection = 100;
135
  staticParams.airspeedCorrection = 100;
137
  staticParams.isFlyingThreshold = 100;
136
  staticParams.isFlyingThreshold = 100;
138
 
137
 
139
  // Servos
138
  // Servos
140
  staticParams.servoCount = 7;
139
  staticParams.servoCount = 7;
141
  staticParams.servoManualMaxSpeed = 10;
140
  staticParams.servoManualMaxSpeed = 10;
142
  for (uint8_t i=0; i<2; i++) {
141
  for (uint8_t i=0; i<2; i++) {
143
    staticParams.servoConfigurations[i].manualControl = 128;
142
    staticParams.servoConfigurations[i].manualControl = 128;
144
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
143
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
145
    staticParams.servoConfigurations[i].minValue = 32;
144
    staticParams.servoConfigurations[i].minValue = 32;
146
    staticParams.servoConfigurations[i].maxValue = 224;
145
    staticParams.servoConfigurations[i].maxValue = 224;
147
    staticParams.servoConfigurations[i].flags = 0;
146
    staticParams.servoConfigurations[i].flags = 0;
148
  }
147
  }
149
 
148
 
150
  // Battery warning and emergency flight
149
  // Battery warning and emergency flight
151
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
150
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
152
  staticParams.emergencyThrottle = 35;
151
  staticParams.emergencyThrottle = 35;
153
  staticParams.emergencyFlightDuration = 30;
152
  staticParams.emergencyFlightDuration = 30;
154
 
153
 
155
  for (uint8_t i=0; i<3; i++) {
154
  for (uint8_t i=0; i<3; i++) {
156
          staticParams.gyroPID[i].P = 80;
155
          staticParams.gyroPID[i].P = 80;
157
          staticParams.gyroPID[i].I = 80;
156
          staticParams.gyroPID[i].I = 80;
158
          staticParams.gyroPID[i].D = 40;
157
          staticParams.gyroPID[i].D = 40;
159
          staticParams.gyroPID[i].iMax = 45;
158
          staticParams.gyroPID[i].iMax = 45;
160
  }
159
  }
161
 
160
 
162
  staticParams.stickIElevator = 80;
161
  staticParams.stickIElevator = 80;
163
  staticParams.stickIAilerons = 120;
162
  staticParams.stickIAilerons = 120;
164
  staticParams.stickIRudder = 40;
163
  staticParams.stickIRudder = 40;
165
 
164
 
166
  // Outputs
165
  // Outputs
167
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
166
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
168
  staticParams.outputFlash[0].timing = 15;
167
  staticParams.outputFlash[0].timing = 15;
169
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
168
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
170
  staticParams.outputFlash[1].timing = 15;
169
  staticParams.outputFlash[1].timing = 15;
171
 
170
 
172
  staticParams.outputDebugMask = 8;
171
  staticParams.outputDebugMask = 8;
173
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
172
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
174
}
173
}
175
 
174
 
176
/***************************************************/
175
/***************************************************/
177
/*    Default Values for parameter set 1           */
176
/*    Default Values for parameter set 1           */
178
/***************************************************/
177
/***************************************************/
179
void paramSet_default(uint8_t setnumber) {
178
void paramSet_default(uint8_t setnumber) {
180
  setOtherDefaults();
179
  setOtherDefaults();
181
 
180
 
182
  for (uint8_t i=0; i<8; i++) {
181
  for (uint8_t i=0; i<8; i++) {
183
    staticParams.userParams[i] = i;
182
    staticParams.userParams[i] = i;
184
  }
183
  }
185
 
184
 
186
  staticParams.bitConfig =
185
  staticParams.bitConfig =
187
    CFG_GYRO_SATURATION_PREVENTION;
186
    CFG_GYRO_SATURATION_PREVENTION;
188
 
187
 
189
  memcpy(staticParams.name, "Default\0", 6);
188
  memcpy(staticParams.name, "Default\0", 6);
190
}
189
}
191
 
190
 
192
void IMUConfig_default(void) {
191
void IMUConfig_default(void) {
193
  IMUConfig.gyroPIDFilterConstant = 1;
192
  IMUConfig.gyroPIDFilterConstant = 1;
194
  IMUConfig.gyroDFilterConstant = 1;
193
  IMUConfig.gyroDFilterConstant = 1;
195
  IMUConfig.rateTolerance = 120;
194
  IMUConfig.rateTolerance = 120;
196
  IMUConfig.gyroDWindowLength = 3;
195
  IMUConfig.gyroDWindowLength = 3;
197
  IMUConfig.gyroQuadrant = 0;
196
  IMUConfig.gyroQuadrant = 0;
198
  IMUConfig.imuReversedFlags = 0;
197
  IMUConfig.imuReversedFlags = 0;
199
  gyro_setDefaultParameters();
198
  gyro_setDefaultParameters();
200
}
199
}
201
 
200
 
202
/***************************************************/
201
/***************************************************/
203
/*    Default Values for R/C Channels              */
202
/*    Default Values for R/C Channels              */
204
/***************************************************/
203
/***************************************************/
205
void channelMap_default(void) {
204
void channelMap_default(void) {
-
 
205
        channelMap.trim = 0;
206
  channelMap.channels[CH_ELEVATOR] = 1;
206
  channelMap.channels[CH_ELEVATOR] = 1;
207
  channelMap.channels[CH_AILERONS] = 0;
207
  channelMap.channels[CH_AILERONS] = 0;
208
  channelMap.channels[CH_THROTTLE] = 2;
208
  channelMap.channels[CH_THROTTLE] = 2;
209
  channelMap.channels[CH_RUDDER]   = 3;
209
  channelMap.channels[CH_RUDDER]   = 3;
210
  channelMap.channels[CH_POTS + 0] = 4;
210
  channelMap.channels[CH_POTS + 0] = 4;
211
  channelMap.channels[CH_POTS + 1] = 5;
211
  channelMap.channels[CH_POTS + 1] = 5;
212
  channelMap.channels[CH_POTS + 2] = 6;
212
  channelMap.channels[CH_POTS + 2] = 6;
213
  channelMap.channels[CH_POTS + 3] = 7;
213
  channelMap.channels[CH_POTS + 3] = 7;
214
}
214
}
215
 
215