Subversion Repositories FlightCtrl

Rev

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

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