Subversion Repositories FlightCtrl

Rev

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