Subversion Repositories FlightCtrl

Rev

Rev 2020 | Rev 2032 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2020 Rev 2026
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include <util/delay.h>
52
#include <util/delay.h>
53
#include <stddef.h>
53
#include <stddef.h>
54
#include <string.h>
54
#include <string.h>
55
#include "configuration.h"
55
#include "configuration.h"
56
#include "sensors.h"
56
#include "sensors.h"
57
#include "rc.h"
57
#include "rc.h"
58
#include "uart0.h"
58
#include "uart0.h"
59
 
59
 
60
int16_t variables[VARIABLE_COUNT];
60
int16_t variables[VARIABLE_COUNT];
61
paramset_t staticParams;
61
paramset_t staticParams;
62
channelMap_t channelMap;
62
channelMap_t channelMap;
63
mixerMatrix_t mixerMatrix;
63
mixerMatrix_t mixerMatrix;
64
volatile dynamicParam_t dynamicParams;
64
volatile dynamicParam_t dynamicParams;
65
 
65
 
66
uint8_t CPUType = ATMEGA644;
66
uint8_t CPUType = ATMEGA644;
67
uint8_t boardRelease = 13;
67
uint8_t boardRelease = 13;
68
uint8_t requiredMotors;
68
uint8_t requiredMotors;
69
 
69
 
70
VersionInfo_t versionInfo;
70
VersionInfo_t versionInfo;
71
 
71
 
72
// MK flags. TODO: Replace by enum. State machine.
72
// MK flags. TODO: Replace by enum. State machine.
73
uint16_t isFlying = 0;
73
uint16_t isFlying = 0;
74
volatile uint8_t MKFlags = 0;
74
volatile uint8_t MKFlags = 0;
75
 
75
 
76
/************************************************************************
76
/************************************************************************
77
 * Map the parameter to pot values                                    
77
 * Map the parameter to pot values                                    
78
 * Replacing this code by the code below saved almost 1 kbyte.
78
 * Replacing this code by the code below saved almost 1 kbyte.
79
 ************************************************************************/
79
 ************************************************************************/
80
 
80
 
81
void configuration_applyVariablesToParams(void) {
81
void configuration_applyVariablesToParams(void) {
82
  uint8_t i;
82
  uint8_t i;
83
 
83
 
84
  debugOut.analog[20] = variables[0];
84
  debugOut.analog[20] = variables[0];
85
 
85
 
86
#define SET_POT_MM(b,a,min,max) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a; if(b<=min) b=min; else if(b>=max) b=max;}
86
#define SET_POT_MM(b,a,min,max) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a; if(b<=min) b=min; else if(b>=max) b=max;}
87
#define SET_POT(b,a) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}
87
#define SET_POT(b,a) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}
88
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
88
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
89
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
89
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
90
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
90
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
91
  SET_POT(dynamicParams.IFactor, staticParams.IFactor);
91
  SET_POT(dynamicParams.IFactor, staticParams.IFactor);
92
  SET_POT(dynamicParams.yawIFactor, staticParams.yawIFactor);
92
  SET_POT(dynamicParams.yawIFactor, staticParams.yawIFactor);
93
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);
93
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);
94
 
94
 
95
  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
95
  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
96
  SET_POT(dynamicParams.axisCoupling1, staticParams.axisCoupling1);
96
  SET_POT(dynamicParams.axisCoupling1, staticParams.axisCoupling1);
97
  SET_POT(dynamicParams.axisCoupling2, staticParams.axisCoupling2);
97
  SET_POT(dynamicParams.axisCoupling2, staticParams.axisCoupling2);
98
  SET_POT(dynamicParams.axisCouplingYawCorrection, staticParams.axisCouplingYawCorrection);
98
  SET_POT(dynamicParams.axisCouplingYawCorrection, staticParams.axisCouplingYawCorrection);
99
  SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
99
  SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
100
  SET_POT(dynamicParams.maxAccVector,staticParams.maxAccVector);
100
  SET_POT(dynamicParams.maxAccVector,staticParams.maxAccVector);
101
 
101
 
102
  SET_POT_MM(dynamicParams.heightP, staticParams.heightP,0,100);
102
  SET_POT_MM(dynamicParams.heightP, staticParams.heightP,0,100);
103
  SET_POT_MM(dynamicParams.heightD, staticParams.heightD,0,100);
103
  SET_POT_MM(dynamicParams.heightD, staticParams.heightD,0,100);
104
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
104
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
105
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
105
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
106
 
106
 
107
  for (i=0; i<sizeof(staticParams.userParams); i++) {
107
  for (i=0; i<sizeof(staticParams.userParams); i++) {
108
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
108
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
109
  }
109
  }
110
 
110
 
111
  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
111
  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
112
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
112
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
113
 
113
 
114
  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
114
  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
115
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
115
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
116
 
116
 
117
  SET_POT(dynamicParams.levelCorrection[0], staticParams.levelCorrection[0]);
117
  SET_POT(dynamicParams.levelCorrection[0], staticParams.levelCorrection[0]);
118
  SET_POT(dynamicParams.levelCorrection[1], staticParams.levelCorrection[1]);
118
  SET_POT(dynamicParams.levelCorrection[1], staticParams.levelCorrection[1]);
119
}
119
}
120
 
120
 
121
const XLATION XLATIONS[] = {
121
const XLATION XLATIONS[] = {
122
  {offsetof(paramset_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
122
  {offsetof(paramset_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
123
};
123
};
124
 
124
 
125
const MMXLATION MMXLATIONS[] = {
125
const MMXLATION MMXLATIONS[] = {
126
  {offsetof(paramset_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
126
  {offsetof(paramset_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
127
};
127
};
128
 
128
 
129
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
129
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
130
  uint8_t result;
130
  uint8_t result;
131
  if (src>=251) result = variables[src-251];
131
  if (src>=251) result = variables[src-251];
132
  else result = src;
132
  else result = src;
133
  if (result < min) result = min;
133
  if (result < min) result = min;
134
  else if (result > max) result = max;
134
  else if (result > max) result = max;
135
  return result;
135
  return result;
136
}
136
}
137
 
137
 
138
void configuration_applyVariablesToParams_dead(void) {
138
void configuration_applyVariablesToParams_dead(void) {
139
  uint8_t i, src;
139
  uint8_t i, src;
140
  uint8_t* pointerToTgt;
140
  uint8_t* pointerToTgt;
141
  for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
141
  for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
142
    src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
142
    src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
143
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
143
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
144
    if (src < 255) {
144
    if (src < 255) {
145
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
145
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
146
    }
146
    }
147
  }
147
  }
148
 
148
 
149
  for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
149
  for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
150
    src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
150
    src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
151
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
151
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
152
    if (src < 255) {
152
    if (src < 255) {
153
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
153
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
154
    }
154
    }
155
  }
155
  }
156
 
156
 
157
  for (i=0; i<sizeof(staticParams.userParams); i++) {
157
  for (i=0; i<sizeof(staticParams.userParams); i++) {
158
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, userParams) + i));
158
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, userParams) + i));
159
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
159
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
160
    if (src < 255) {
160
    if (src < 255) {
161
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
161
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
162
    }    
162
    }    
163
  }
163
  }
164
}
164
}
165
 
165
 
166
uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
166
uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
167
  uint8_t CPUType = ATMEGA644;
167
  uint8_t CPUType = ATMEGA644;
168
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
168
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
169
  return CPUType;
169
  return CPUType;
170
}
170
}
171
 
171
 
172
/*
172
/*
173
 * Automatic detection of hardware components is not supported in this development-oriented
173
 * Automatic detection of hardware components is not supported in this development-oriented
174
 * FC firmware. It would go against the point of it: To enable alternative hardware
174
 * FC firmware. It would go against the point of it: To enable alternative hardware
175
 * configurations with otherwise unsupported components. Instead, one should write
175
 * configurations with otherwise unsupported components. Instead, one should write
176
 * custom code + adjust constants for the new hardware, and include the relevant code
176
 * custom code + adjust constants for the new hardware, and include the relevant code
177
 * from the makefile.
177
 * from the makefile.
178
 * However - we still do detect the board release. Reason: Otherwise it would be too
178
 * However - we still do detect the board release. Reason: Otherwise it would be too
179
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
179
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
180
 * on different HW version....
180
 * on different HW version....
181
 */
181
 */
182
 
182
 
183
uint8_t getBoardRelease(void) {
183
uint8_t getBoardRelease(void) {
184
  uint8_t boardRelease = 13;
184
  uint8_t boardRelease = 13;
185
  // the board release is coded via the pull up or down the 2 status LED
185
  // the board release is coded via the pull up or down the 2 status LED
186
 
186
 
187
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
187
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
188
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
188
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
189
 
189
 
190
  _delay_loop_2(1000); // make some delay
190
  _delay_loop_2(1000); // make some delay
191
 
191
 
192
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
192
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
193
    case 0x00:
193
    case 0x00:
194
      boardRelease = 10; // 1.0
194
      boardRelease = 10; // 1.0
195
      break;
195
      break;
196
    case 0x01:
196
    case 0x01:
197
      boardRelease = 11; // 1.1 or 1.2
197
      boardRelease = 11; // 1.1 or 1.2
198
      break;
198
      break;
199
    case 0x02:
199
    case 0x02:
200
      boardRelease = 20; // 2.0
200
      boardRelease = 20; // 2.0
201
      break;
201
      break;
202
    case 0x03:
202
    case 0x03:
203
      boardRelease = 13; // 1.3
203
      boardRelease = 13; // 1.3
204
      break;
204
      break;
205
    default:
205
    default:
206
      break;
206
      break;
207
    }
207
    }
208
  // set LED ports as output
208
  // set LED ports as output
209
  DDRB |= (1<<DDB1)|(1<<DDB0);
209
  DDRB |= (1<<DDB1)|(1<<DDB0);
210
  RED_OFF;
210
  RED_OFF;
211
  GRN_OFF;
211
  GRN_OFF;
212
  return boardRelease;
212
  return boardRelease;
213
}
213
}
214
 
214
 
215
void setOtherDefaults(void) {
215
void setOtherDefaults(void) {
216
  // Height Control
216
  // Height Control
-
 
217
  staticParams.airpressureAccZCorrection = 128+56;
217
  staticParams.heightP = 10;
218
  staticParams.heightP = 10;
218
  staticParams.heightD = 30;
219
  staticParams.heightD = 30;
219
  staticParams.heightSetting = 251;
220
  staticParams.heightSetting = 251;
220
  staticParams.heightControlMaxThrottleChange = 10;
221
  staticParams.heightControlMaxThrottleChange = 10;
221
  staticParams.heightSlewRate = 4;
222
  staticParams.heightSlewRate = 4;
222
 
223
 
223
  // Control
224
  // Control
224
  staticParams.stickP = 8;
225
  staticParams.stickP = 8;
225
  staticParams.stickD = 12;
226
  staticParams.stickD = 12;
226
  staticParams.stickYawP = 12;
227
  staticParams.stickYawP = 12;
227
  staticParams.stickThrottleD = 12;
228
  staticParams.stickThrottleD = 12;
228
  staticParams.minThrottle = 8;
229
  staticParams.minThrottle = 8;
229
  staticParams.maxThrottle = 230;
230
  staticParams.maxThrottle = 230;
230
  staticParams.externalControl = 0;
231
  staticParams.externalControl = 0;
231
  staticParams.motorSmoothing = 0;
232
  staticParams.motorSmoothing = 0;
232
 
233
 
233
  // IMU
234
  // IMU
234
  staticParams.gyroPIDFilterConstant = 1;
235
  staticParams.gyroPIDFilterConstant = 1;
235
  // staticParams.gyroATTFilterConstant = 1;
236
  // staticParams.gyroATTFilterConstant = 1;
236
  staticParams.gyroDFilterConstant = 1;
237
  staticParams.gyroDFilterConstant = 1;
237
  staticParams.accFilterConstant = 10;
238
  staticParams.accFilterConstant = 10;
238
 
239
 
239
  staticParams.gyroP = 60;
240
  staticParams.gyroP = 60;
240
  staticParams.gyroI = 80;
241
  staticParams.gyroI = 80;
241
  staticParams.gyroD = 4;
242
  staticParams.gyroD = 4;
242
 
243
 
243
  // set by gyro-specific code: gyro_setDefaults().
244
  // set by gyro-specific code: gyro_setDefaults().
244
  // staticParams.zerothOrderCorrection = 
245
  // staticParams.zerothOrderCorrection = 
245
  // staticParams.driftCompDivider = 
246
  // staticParams.driftCompDivider = 
246
  // staticParams.driftCompLimit = 
247
  // staticParams.driftCompLimit = 
247
 
248
 
248
  staticParams.axisCoupling1 = 90;
249
  staticParams.axisCoupling1 = 90;
249
  staticParams.axisCoupling2 = 67;
250
  staticParams.axisCoupling2 = 67;
250
  staticParams.axisCouplingYawCorrection = 0;
251
  staticParams.axisCouplingYawCorrection = 0;
251
  staticParams.dynamicStability = 50;
252
  staticParams.dynamicStability = 50;
252
  staticParams.maxAccVector = 10;
253
  staticParams.maxAccVector = 10;
253
  staticParams.IFactor = 32;
254
  staticParams.IFactor = 32;
254
  staticParams.yawIFactor = 100;  
255
  staticParams.yawIFactor = 100;  
255
  staticParams.compassYawEffect = 128;
256
  staticParams.compassYawEffect = 128;
256
  staticParams.levelCorrection[0] = staticParams.levelCorrection[1] = 128;
257
  staticParams.levelCorrection[0] = staticParams.levelCorrection[1] = 128;
257
 
258
 
258
  // Servos
259
  // Servos
259
  staticParams.servoCount = 7;
260
  staticParams.servoCount = 7;
260
  staticParams.servoManualMaxSpeed = 10;
261
  staticParams.servoManualMaxSpeed = 10;
261
  for (uint8_t i=0; i<2; i++) {
262
  for (uint8_t i=0; i<2; i++) {
262
    staticParams.servoConfigurations[i].manualControl = 128;
263
    staticParams.servoConfigurations[i].manualControl = 128;
263
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
264
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
264
    staticParams.servoConfigurations[i].minValue = 32;
265
    staticParams.servoConfigurations[i].minValue = 32;
265
    staticParams.servoConfigurations[i].maxValue = 224;
266
    staticParams.servoConfigurations[i].maxValue = 224;
266
    staticParams.servoConfigurations[i].flags = 0;
267
    staticParams.servoConfigurations[i].flags = 0;
267
  }
268
  }
268
 
269
 
269
  // Battery warning and emergency flight
270
  // Battery warning and emergency flight
270
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
271
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
271
  staticParams.emergencyThrottle = 35;
272
  staticParams.emergencyThrottle = 35;
272
  staticParams.emergencyFlightDuration = 30;
273
  staticParams.emergencyFlightDuration = 30;
273
 
274
 
274
  // Outputs
275
  // Outputs
275
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
276
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
276
  staticParams.outputFlash[0].timing = 15;
277
  staticParams.outputFlash[0].timing = 15;
277
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
278
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
278
  staticParams.outputFlash[1].timing = 15;
279
  staticParams.outputFlash[1].timing = 15;
279
 
280
 
280
  staticParams.outputDebugMask = 8;
281
  staticParams.outputDebugMask = 8;
281
  staticParams.outputFlags   = 16|8|4;
282
  staticParams.outputFlags   = 16|8|4;
282
}
283
}
283
 
284
 
284
/***************************************************/
285
/***************************************************/
285
/*    Default Values for parameter set 1           */
286
/*    Default Values for parameter set 1           */
286
/***************************************************/
287
/***************************************************/
287
void paramSet_default(uint8_t setnumber) {
288
void paramSet_default(uint8_t setnumber) {
288
  setOtherDefaults();
289
  setOtherDefaults();
289
  gyro_setDefaultParameters();
290
  gyro_setDefaultParameters();
290
 
291
 
291
  for (uint8_t i=0; i<8; i++) {
292
  for (uint8_t i=0; i<8; i++) {
292
    staticParams.userParams[i] = 0;
293
    staticParams.userParams[i] = 0;
293
  }
294
  }
294
 
295
 
295
  staticParams.bitConfig =
296
  staticParams.bitConfig =
296
    CFG_GYRO_SATURATION_PREVENTION | CFG_HEADING_HOLD;
297
    CFG_GYRO_SATURATION_PREVENTION | CFG_HEADING_HOLD;
297
 
298
 
298
  memcpy(staticParams.name, "Default\0", 6);
299
  memcpy(staticParams.name, "Default\0", 6);
299
}
300
}
300
 
301
 
301
/***************************************************/
302
/***************************************************/
302
/*    Default Values for Mixer Table               */
303
/*    Default Values for Mixer Table               */
303
/***************************************************/
304
/***************************************************/
304
void mixerMatrix_default(void) { // Quadro 
305
void mixerMatrix_default(void) { // Quadro 
305
  uint8_t i;
306
  uint8_t i;
306
  // mixerMatric.revision = EEMIXER_REVISION;
307
  // mixerMatric.revision = EEMIXER_REVISION;
307
  // clear mixer table (but preset throttle)
308
  // clear mixer table (but preset throttle)
308
  for (i = 0; i < 16; i++) {
309
  for (i = 0; i < 16; i++) {
309
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
310
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
310
    mixerMatrix.motor[i][MIX_PITCH] = 0;
311
    mixerMatrix.motor[i][MIX_PITCH] = 0;
311
    mixerMatrix.motor[i][MIX_ROLL] = 0;
312
    mixerMatrix.motor[i][MIX_ROLL] = 0;
312
    mixerMatrix.motor[i][MIX_YAW] = 0;
313
    mixerMatrix.motor[i][MIX_YAW] = 0;
313
  }
314
  }
314
  /*
-
 
315
  // default = Quadro
315
  // default = Quadro
316
  mixerMatrix.motor[0][MIX_PITCH] = +64;
316
  mixerMatrix.motor[0][MIX_PITCH] = +64;
317
  mixerMatrix.motor[0][MIX_YAW] = +64;
317
  mixerMatrix.motor[0][MIX_YAW] = +64;
318
  mixerMatrix.motor[1][MIX_PITCH] = -64;
318
  mixerMatrix.motor[1][MIX_PITCH] = -64;
319
  mixerMatrix.motor[1][MIX_YAW] = +64;
319
  mixerMatrix.motor[1][MIX_YAW] = +64;
320
  mixerMatrix.motor[2][MIX_ROLL] = -64;
320
  mixerMatrix.motor[2][MIX_ROLL] = -64;
321
  mixerMatrix.motor[2][MIX_YAW] = -64;
321
  mixerMatrix.motor[2][MIX_YAW] = -64;
322
  mixerMatrix.motor[3][MIX_ROLL] = +64;
322
  mixerMatrix.motor[3][MIX_ROLL] = +64;
323
  mixerMatrix.motor[3][MIX_YAW] = -64;
323
  mixerMatrix.motor[3][MIX_YAW] = -64;
324
  memcpy(mixerMatrix.name, "Quadro\0", 7);
324
  memcpy(mixerMatrix.name, "Quadro\0", 7);
325
  */
-
 
-
 
325
 
326
 
326
  /*
327
  // default = X
327
  // default = X
328
  mixerMatrix.motor[0][MIX_PITCH] = +45;
328
  mixerMatrix.motor[0][MIX_PITCH] = +45;
329
  mixerMatrix.motor[0][MIX_ROLL]  = +45;
329
  mixerMatrix.motor[0][MIX_ROLL]  = +45;
330
  mixerMatrix.motor[0][MIX_YAW]   = +64;
330
  mixerMatrix.motor[0][MIX_YAW]   = +64;
331
 
331
 
332
  mixerMatrix.motor[1][MIX_PITCH] = -45;
332
  mixerMatrix.motor[1][MIX_PITCH] = -45;
333
  mixerMatrix.motor[1][MIX_ROLL]  = -45;
333
  mixerMatrix.motor[1][MIX_ROLL]  = -45;
334
  mixerMatrix.motor[1][MIX_YAW]   = +64;
334
  mixerMatrix.motor[1][MIX_YAW]   = +64;
335
 
335
 
336
  mixerMatrix.motor[2][MIX_PITCH] = +45;
336
  mixerMatrix.motor[2][MIX_PITCH] = +45;
337
  mixerMatrix.motor[2][MIX_ROLL]  = -45;
337
  mixerMatrix.motor[2][MIX_ROLL]  = -45;
338
  mixerMatrix.motor[2][MIX_YAW]   = -64;
338
  mixerMatrix.motor[2][MIX_YAW]   = -64;
339
 
339
 
340
  mixerMatrix.motor[3][MIX_PITCH] = -45;
340
  mixerMatrix.motor[3][MIX_PITCH] = -45;
341
  mixerMatrix.motor[3][MIX_ROLL]  = +45;
341
  mixerMatrix.motor[3][MIX_ROLL]  = +45;
342
  mixerMatrix.motor[3][MIX_YAW]   = -64;
342
  mixerMatrix.motor[3][MIX_YAW]   = -64;
-
 
343
  */
343
 
344
 
344
  memcpy(mixerMatrix.name, "X\0", 7);
345
  memcpy(mixerMatrix.name, "X\0", 7);
345
}
346
}
346
 
347
 
347
/***************************************************/
348
/***************************************************/
348
/*    Default Values for R/C Channels              */
349
/*    Default Values for R/C Channels              */
349
/***************************************************/
350
/***************************************************/
350
void channelMap_default(void) {
351
void channelMap_default(void) {
351
  channelMap.channels[CH_PITCH]    = 1;
352
  channelMap.channels[CH_PITCH]    = 1;
352
  channelMap.channels[CH_ROLL]     = 0;
353
  channelMap.channels[CH_ROLL]     = 0;
353
  channelMap.channels[CH_THROTTLE] = 2;
354
  channelMap.channels[CH_THROTTLE] = 2;
354
  channelMap.channels[CH_YAW]      = 3;
355
  channelMap.channels[CH_YAW]      = 3;
355
  channelMap.channels[CH_POTS + 0] = 4;
356
  channelMap.channels[CH_POTS + 0] = 4;
356
  channelMap.channels[CH_POTS + 1] = 5;
357
  channelMap.channels[CH_POTS + 1] = 5;
357
  channelMap.channels[CH_POTS + 2] = 6;
358
  channelMap.channels[CH_POTS + 2] = 6;
358
  channelMap.channels[CH_POTS + 3] = 7;
359
  channelMap.channels[CH_POTS + 3] = 7;
359
}
360
}
360
 
361