Subversion Repositories FlightCtrl

Rev

Rev 2025 | Rev 2102 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2025 Rev 2099
Line 1... Line 1...
1
#include <util/delay.h>
1
#include <util/delay.h>
2
#include <avr/eeprom.h>
-
 
3
#include <stddef.h>
2
#include <stddef.h>
-
 
3
#include <string.h>
4
#include "configuration.h"
4
#include "configuration.h"
5
#include "eeprom.h"
5
#include "sensors.h"
6
#include "uart0.h"
6
#include "rc.h"
-
 
7
#include "output.h"
-
 
8
#include "flight.h"
7
 
9
 
8
int16_t variables[8] = {0,0,0,0,0,0,0,0};
10
int16_t variables[VARIABLE_COUNT];
-
 
11
ParamSet_t staticParams;
-
 
12
channelMap_t channelMap;
-
 
13
IMUConfig_t IMUConfig;
-
 
14
volatile DynamicParams_t dynamicParams;
9
 
15
 
10
dynamicParam_t dynamicParams;
16
uint8_t CPUType;
11
uint8_t CPUType = ATMEGA644;
17
uint8_t boardRelease;
12
uint8_t BoardRelease = 13;
18
uint8_t requiredMotors;
13
 
19
 
14
/************************************************************************
-
 
15
 * Map the parameter to pot values                                    
20
VersionInfo_t versionInfo;
-
 
21
 
16
 * Replacing this code by the code below saved almost 1 kbyte.
22
// MK flags. TODO: Replace by enum. State machine.
-
 
23
uint16_t isFlying = 0;
17
 ************************************************************************/
24
volatile uint8_t MKFlags = 0;
18
 
25
 
19
void configuration_staticToDynamic(void) {
26
const MMXLATION XLATIONS[] = {
20
  uint8_t i;
-
 
21
#define SET_POT_MM(b,a,min,max) {if (a<255) {if (a>=251) b=variables[a-251]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
-
 
22
#define SET_POT(b,a) { if (a<255) {if (a>=251) b=variables[a-251]; else b=a;}}
27
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
23
  SET_POT(dynamicParams.MaxHeight,staticParams.MaxHeight);
28
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
24
  SET_POT_MM(dynamicParams.HeightD,staticParams.HeightD,0,100);
29
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
25
  SET_POT_MM(dynamicParams.HeightP,staticParams.HeightP,0,100);
30
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
26
    SET_POT(dynamicParams.CompassYawEffect,staticParams.CompassYawEffect);
31
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
-
 
32
};
27
 
33
 
28
  SET_POT(dynamicParams.GyroPitchP,staticParams.GyroPitchP);
34
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
29
  SET_POT(dynamicParams.GyroRollP,staticParams.GyroRollP);
35
  uint8_t result;
30
  SET_POT(dynamicParams.GyroYawP,staticParams.GyroYawP);
36
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
31
 
-
 
32
  SET_POT(dynamicParams.GyroPitchD,staticParams.GyroPitchD);
37
  else result = src;
33
  SET_POT(dynamicParams.GyroRollD,staticParams.GyroRollD);
38
  if (result < min) result = min;
34
  SET_POT(dynamicParams.GyroYawD,staticParams.GyroYawD);
39
  else if (result > max) result = max;
-
 
40
  return result;
-
 
41
}
Line -... Line 42...
-
 
42
 
-
 
43
void configuration_applyVariablesToParams(void) {
-
 
44
  uint8_t i, src;
-
 
45
  uint8_t* pointerToTgt;
35
 
46
 
36
  for (i=0; i<sizeof(staticParams.UserParams); i++) {
47
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
-
 
48
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
-
 
49
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
37
    SET_POT(dynamicParams.UserParams[i],staticParams.UserParams[i]);
50
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
Line -... Line 51...
-
 
51
  }
38
  }
52
 
39
 
53
  // User parameters are always variable.
40
  SET_POT_MM(dynamicParams.J16Timing,staticParams.J16Timing,1,255);
-
 
41
  SET_POT_MM(dynamicParams.J17Timing,staticParams.J17Timing,1,255);
54
  for (i=0; i<sizeof(staticParams.userParams); i++) {
-
 
55
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
-
 
56
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
42
 
57
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
Line 43... Line 58...
43
  SET_POT(dynamicParams.ExternalControl,staticParams.ExternalControl);
58
  }
44
}
-
 
45
 
59
}
46
uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
60
 
47
  uint8_t CPUType = ATMEGA644;
61
void setCPUType(void) {   // works only after reset or power on when the registers have default values
Line 48... Line 62...
48
  //if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P;  // initial Values for 644P after reset
62
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
49
  return CPUType;
63
  else CPUType = ATMEGA644;
50
}
64
}
Line 57... Line 71...
57
 * from the makefile.
71
 * from the makefile.
58
 * However - we still do detect the board release. Reason: Otherwise it would be too
72
 * However - we still do detect the board release. Reason: Otherwise it would be too
59
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
73
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
60
 * on different HW version....
74
 * on different HW version....
61
 */
75
 */
62
 
-
 
63
uint8_t getBoardRelease(void) {
76
void setBoardRelease(void) {
64
  uint8_t BoardRelease = 13;
-
 
65
  // the board release is coded via the pull up or down the 2 status LED
77
  // the board release is coded via the pull up or down the 2 status LED
Line 66... Line 78...
66
 
78
 
67
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
79
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
Line 68... Line 80...
68
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
80
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
Line 69... Line 81...
69
 
81
 
70
  _delay_loop_2(1000); // make some delay
82
  _delay_loop_2(1000); // make some delay
71
 
83
 
72
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
84
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
73
    case 0x00:
85
    case 0x00:
74
      BoardRelease = 10; // 1.0
86
      boardRelease = 10; // 1.0
75
      break;
87
      break;
76
    case 0x01:
88
    case 0x01:
77
      BoardRelease = 11; // 1.1 or 1.2
89
      boardRelease = 11; // 1.1 or 1.2
78
      break;
90
      break;
79
    case 0x02:
91
    case 0x02:
80
      BoardRelease = 20; // 2.0
92
      boardRelease = 20; // 2.0
81
      break;
93
      break;
82
    case 0x03:
94
    case 0x03:
83
      BoardRelease = 13; // 1.3
95
      boardRelease = 13; // 1.3
84
      break;
96
      break;
85
    default:
97
    default:
86
      break;
98
      break;
87
    }
99
    }
88
  // set LED ports as output
100
  // set LED ports as output
-
 
101
  DDRB |= (1<<DDB1)|(1<<DDB0);
-
 
102
  RED_OFF;
-
 
103
  GRN_OFF;
-
 
104
}
-
 
105
 
-
 
106
void configuration_setFlightParameters() {
-
 
107
  // Implement: Update of stuff in flight.c
-
 
108
}
-
 
109
 
-
 
110
// Called after a change in configuration parameters, as a hook for modules to take over changes.
-
 
111
void configuration_paramSetDidChange(void) {
-
 
112
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
-
 
113
  configuration_setFlightParameters();
-
 
114
  // Immediately load changes to output, and also signal the paramset change.
-
 
115
  output_init();
-
 
116
}
-
 
117
 
-
 
118
void setOtherDefaults(void) {
-
 
119
 
-
 
120
  // Control
-
 
121
  staticParams.externalControl = 0;
-
 
122
  staticParams.IFactor = 52;
-
 
123
 
-
 
124
  // Servos
-
 
125
  staticParams.servoCount = 7;
-
 
126
  staticParams.servoManualMaxSpeed = 10;
-
 
127
  for (uint8_t i=0; i<2; i++) {
-
 
128
    staticParams.servoConfigurations[i].manualControl = 128;
-
 
129
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
-
 
130
    staticParams.servoConfigurations[i].minValue = 32;
-
 
131
    staticParams.servoConfigurations[i].maxValue = 224;
-
 
132
    staticParams.servoConfigurations[i].flags = 0;
-
 
133
  }
-
 
134
 
-
 
135
  // Battery warning and emergency flight
-
 
136
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
-
 
137
  staticParams.emergencyThrottle = 35;
-
 
138
  staticParams.emergencyFlightDuration = 30;
-
 
139
 
-
 
140
  // Outputs
-
 
141
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
-
 
142
  staticParams.outputFlash[0].timing = 15;
-
 
143
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
-
 
144
  staticParams.outputFlash[1].timing = 15;
-
 
145
 
-
 
146
  staticParams.outputDebugMask = 8;
-
 
147
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
-
 
148
}
-
 
149
 
-
 
150
/***************************************************/
-
 
151
/*    Default Values for parameter set 1           */
-
 
152
/***************************************************/
-
 
153
void paramSet_default(uint8_t setnumber) {
-
 
154
  setOtherDefaults();
-
 
155
 
-
 
156
  for (uint8_t i=0; i<8; i++) {
-
 
157
    staticParams.userParams[i] = i;
-
 
158
  }
-
 
159
 
-
 
160
  staticParams.bitConfig =
-
 
161
    CFG_GYRO_SATURATION_PREVENTION | CFG_COMPASS_ENABLED;
-
 
162
 
-
 
163
  memcpy(staticParams.name, "Default\0", 6);
-
 
164
}
-
 
165
 
-
 
166
void IMUConfig_default(void) {
-
 
167
  IMUConfig.gyroPIDFilterConstant = 1;
89
  DDRB |= (1<<DDB1)|(1<<DDB0);
168
  IMUConfig.gyroDFilterConstant = 1;
-
 
169
  IMUConfig.accFilterConstant = 10;
-
 
170
  IMUConfig.rateTolerance = 120;
-
 
171
  gyro_setDefaultParameters();
-
 
172
}
-
 
173
 
-
 
174
/***************************************************/
-
 
175
/*    Default Values for R/C Channels              */
-
 
176
/***************************************************/
-
 
177
void channelMap_default(void) {
-
 
178
  channelMap.channels[CH_ELEVATOR] = 1;
-
 
179
  channelMap.channels[CH_AILERONS] = 0;
-
 
180
  channelMap.channels[CH_THROTTLE] = 2;
-
 
181
  channelMap.channels[CH_RUDDER]   = 3;
-
 
182
  channelMap.channels[CH_POTS + 0] = 4;
90
  RED_ON;
183
  channelMap.channels[CH_POTS + 1] = 5;