/branches/MicroMag3_Nick666/trunc/compass.c |
---|
31,7 → 31,8 |
DDRB |= (1<<PB7)|(1<<PB5); // MOSI, SCK Ausgang |
DDRC |= (1<<PC4)|(1<<PC5); // PC5 (RESET) und PC4 (SSNOT) als Ausgang |
PORTC &= ~(1<<PC4); // PC4 permanent auf Low |
PORTC |= (1<<PC4); // PC4 (SSNOT) auf High -> MM3 passiv |
PORTC &= ~(1<<PC5); // PC5 (RESET) auf Low |
// Init Statemachine |
MM3.AXIS = MM3_X; |
49,8 → 50,9 |
{ |
switch (MM3.STATE) |
{ |
case MM3_RESET: |
PORTC |= (1<<PC5); // PC5 auf High, MM3 Reset |
case MM3_RESET: |
PORTC &= ~(1<<PC4); // MM3 aktiv |
PORTC |= (1<<PC5); // MM3 Reset |
MM3.STATE = MM3_START_TRANSFER; |
return; |
57,9 → 59,9 |
case MM3_START_TRANSFER: |
PORTC &= ~(1<<PC5); // PC5 auf Low (war ~125 µs auf High) |
if (MM3.AXIS == MM3_X) SPDR = 0x31; // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus |
else if (MM3.AXIS == MM3_Y) SPDR = 0x32; // Micromag Period Select ist auf 256 (0x30) |
else SPDR = 0x33; //if (MM3.AXIS == MM3_Z) // 1: x-Achse, 2: Y-Achse, 3: Z-Achse |
if (MM3.AXIS == MM3_X) SPDR = MM3_PERIOD_256 + MM3_X_AXIS; // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus |
else if (MM3.AXIS == MM3_Y) SPDR = MM3_PERIOD_256 + MM3_Y_AXIS; // Micromag Period Select ist 256 (0x30) |
else SPDR = MM3_PERIOD_256 + MM3_Z_AXIS; //if (MM3.AXIS == MM3_Z) |
MM3.DRDY = SetDelay(8); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms) |
MM3.STATE = MM3_WAIT_DRDY; |
78,7 → 80,7 |
//############################################################################ |
{ |
static char tmp; |
int wert; |
int value; |
switch (MM3.STATE) |
{ |
89,26 → 91,26 |
return; |
case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
wert = tmp; |
wert <<= 8; // 1. Byte an MSB-Stelle rücken |
wert |= SPDR; // 2. Byte dranpappen |
value = tmp; |
value <<= 8; // 1. Byte an MSB-Stelle rücken |
value |= SPDR; // 2. Byte dranpappen |
if(abs(wert) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
if(abs(value) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
switch (MM3.AXIS) |
{ |
case MM3_X: |
MM3.x_axis = wert; |
MM3.x_axis = value; |
MM3.AXIS = MM3_Y; |
break; |
case MM3_Y: |
MM3.y_axis = wert; |
MM3.y_axis = value; |
MM3.AXIS = MM3_Z; |
break; |
default: //case MM3_Z: |
MM3.z_axis = wert; |
MM3.z_axis = value; |
MM3.AXIS = MM3_X; |
} |
PORTC |= (1<<PORTC4); // MM3 passiv |
MM3.STATE = MM3_RESET; |
} |
} |
118,26 → 120,32 |
void calib_MM3(void) |
//############################################################################ |
{ |
signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
uint8_t measurement=50,beeper=0; |
unsigned int timer; |
int16_t x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
int16_t x_axis, y_axis, z_axis; |
uint8_t measurement=50,beeper=0; |
uint8_t tmp_sreg; |
GRN_ON; |
ROT_OFF; |
while (measurement) |
{ |
//H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis; |
{ |
tmp_sreg = SREG; |
cli(); |
x_axis = MM3.x_axis; |
y_axis = MM3.y_axis; |
z_axis = MM3.z_axis; |
SREG = tmp_sreg; |
if (x_axis > x_max) x_max = x_axis; |
else if (x_axis < x_min) x_min = x_axis; |
if (MM3.x_axis > x_max) x_max = MM3.x_axis; |
else if (MM3.x_axis < x_min) x_min = MM3.x_axis; |
if (y_axis > y_max) y_max = y_axis; |
else if (y_axis < y_min) y_min = y_axis; |
if (MM3.y_axis > y_max) y_max = MM3.y_axis; |
else if (MM3.y_axis < y_min) y_min = MM3.y_axis; |
if (z_axis > z_max) z_max = z_axis; |
else if (z_axis < z_min) z_min = z_axis; |
if (MM3.z_axis > z_max) z_max = MM3.z_axis; |
else if (MM3.z_axis < z_min) z_min = MM3.z_axis; |
if (!beeper) |
{ |
ROT_FLASH; |
146,10 → 154,9 |
beeper = 50; |
} |
beeper--; |
// Schleife mit 100 Hz |
timer = SetDelay(10); |
while(!CheckDelay(timer)); |
Delay_ms(10); |
// Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden |
if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
177,11 → 184,21 |
{ |
uint16_t div_faktor; |
int16_t sin_nick, cos_nick, sin_roll, cos_roll; |
int16_t mm3_x_axis, mm3_y_axis, mm3_z_axis; |
int32_t Hx, Hy, Hz, x_corr, y_corr; |
int16_t heading; |
int8_t tilt; |
/* |
// Berechnung von sinus und cosinus |
uint8_t tmp_sreg; |
// 16bit-Werte lesen |
tmp_sreg = SREG; |
cli(); |
mm3_x_axis = MM3.x_axis; |
mm3_y_axis = MM3.y_axis; |
mm3_z_axis = MM3.z_axis; |
SREG = tmp_sreg; |
// Lage-Berechnung mittels Acc-Messwerte |
tilt = atan2_i(Aktuell_az-556,AdWertAccNick*64); |
sin_nick = sin_i(tilt); |
cos_nick = cos_i(tilt); |
189,12 → 206,10 |
tilt = atan2_i(Aktuell_az-556,AdWertAccRoll*64); |
sin_roll = sin_i(tilt); |
cos_roll = cos_i(tilt); |
*/ |
// calibration factor for transforming Gyro Integrals to angular degrees |
/* |
// Lage-Berechnung mittels Gyro-Integral |
div_faktor = (uint16_t)EE_Parameter.UserParam3 *8; |
// calculate sinus cosinus of pitch and tilt angle |
tilt = (IntegralNick /div_faktor); |
sin_nick = sin_i(tilt); |
cos_nick = cos_i(tilt); |
202,11 → 217,11 |
tilt = (IntegralRoll /div_faktor); |
sin_roll = sin_i(tilt); |
cos_roll = cos_i(tilt); |
*/ |
// Offset und Normalisierung |
Hx = (((int32_t)(MM3.x_axis - MM3_calib.X_off)) *1024) /MM3_calib.X_range; |
Hy = (((int32_t)(MM3.y_axis - MM3_calib.Y_off)) *1024) /MM3_calib.Y_range; |
Hz = (((int32_t)(MM3.z_axis - MM3_calib.Z_off)) *1024) /MM3_calib.Z_range; |
Hx = (((int32_t)(mm3_x_axis - MM3_calib.X_off)) *1024) /MM3_calib.X_range; |
Hy = (((int32_t)(mm3_y_axis - MM3_calib.Y_off)) *1024) /MM3_calib.Y_range; |
Hz = (((int32_t)(mm3_z_axis - MM3_calib.Z_off)) *1024) /MM3_calib.Z_range; |
// Neigungskompensierung |
x_corr = Hx * cos_nick; |
215,7 → 230,7 |
y_corr = Hy * cos_roll; |
y_corr += Hz * sin_roll; |
y_corr /= 16; |
y_corr /= 16; // atan2_i erwartet y_corr *64. Deshalb /16 und nicht /1024 |
// Winkelberechnung |
heading = atan2_i(x_corr, y_corr); |
/branches/MicroMag3_Nick666/trunc/compass.h |
---|
3,11 → 3,11 |
struct MM3_working_struct |
{ |
uint8_t STATE; |
unsigned int DRDY; |
uint8_t AXIS; |
volatile signed int x_axis; |
volatile signed int y_axis; |
volatile signed int z_axis; |
uint16_t DRDY; |
int16_t x_axis; |
int16_t y_axis; |
int16_t z_axis; |
}; |
29,6 → 29,21 |
void calib_MM3(void); |
int heading_MM3(void); |
// MM3-Konfiguration |
#define MM3_X_AXIS 0x01 |
#define MM3_Y_AXIS 0x02 |
#define MM3_Z_AXIS 0x03 |
#define MM3_PERIOD_32 0x00 |
#define MM3_PERIOD_64 0x10 |
#define MM3_PERIOD_128 0x20 |
#define MM3_PERIOD_256 0x30 |
#define MM3_PERIOD_512 0x40 |
#define MM3_PERIOD_1024 0x50 |
#define MM3_PERIOD_2048 0x60 |
#define MM3_PERIOD_4096 0x70 |
// Spikes Filtern |
#define Max_Axis_Value 500 |
// Die Werte der Statemachine |
/branches/MicroMag3_Nick666/trunc/fc.c |
---|
77,6 → 77,8 |
int KompassRichtung = 0; |
uint8_t updKompass = 0; |
uint8_t DebugOutBlock = 0; |
unsigned char MAX_GAS,MIN_GAS; |
unsigned char Notlandung = 0; |
unsigned char HoehenReglerAktiv = 0; |
215,9 → 217,6 |
MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll; |
MesswertNick = (signed int) AdWertNick - AdNeutralNick; |
//DebugOut.Analog[26] = MesswertNick; |
DebugOut.Analog[28] = MesswertRoll; |
// Beschleunigungssensor ++++++++++++++++++++++++++++++++++++++++++++++++ |
Mittelwert_AccNick = ((long)Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 2L; |
Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 2L; |
368,11 → 367,6 |
if(MotorTest[3]) Motor_Rechts = MotorTest[3]; |
} |
DebugOut.Analog[12] = Motor_Vorne; |
DebugOut.Analog[13] = Motor_Hinten; |
DebugOut.Analog[14] = Motor_Links; |
DebugOut.Analog[15] = Motor_Rechts; |
//Start I2C Interrupt Mode |
twi_state = 0; |
motor = 0; |
785,7 → 779,6 |
MittelIntegralRoll2 /= ABGLEICH_ANZAHL; |
tmp_long = IntegralNick2 - IntegralNick; |
tmp_long2 = IntegralRoll2 - IntegralRoll; |
//DebugOut.Analog[25] = MittelIntegralRoll2 / 26; |
IntegralFehlerNick = tmp_long; |
IntegralFehlerRoll = tmp_long2; |
795,17 → 788,6 |
// IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2; |
// IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2; |
DebugOut.Analog[17] = IntegralAccNick / 26; |
DebugOut.Analog[18] = IntegralAccRoll / 26; |
DebugOut.Analog[19] = IntegralFehlerNick;// / 26; |
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26; |
DebugOut.Analog[21] = MittelIntegralNick / 26; |
DebugOut.Analog[22] = MittelIntegralRoll / 26; |
//DebugOut.Analog[28] = ausgleichNick; |
DebugOut.Analog[29] = ausgleichRoll; |
DebugOut.Analog[30] = LageKorrekturRoll * 10; |
#define FEHLER_LIMIT (ABGLEICH_ANZAHL * 4) |
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) |
#define BEWEGUNGS_LIMIT 20000 |
876,9 → 858,6 |
if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
if(IntegralFehlerRoll > FEHLER_LIMIT) AdNeutralRoll += cnt; |
if(IntegralFehlerRoll < -FEHLER_LIMIT) AdNeutralRoll -= cnt; |
DebugOut.Analog[27] = ausgleichRoll; |
DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick); |
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
} |
else |
{ |
900,7 → 879,6 |
MittelIntegralRoll2 = 0; |
ZaehlMessungen = 0; |
} |
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor); |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// Gieren |
923,66 → 901,68 |
{ |
if (!updKompass--) // Aufruf mit ~10 Hz |
{ |
KompassValue = heading_MM3(); |
KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
updKompass = 50; |
if(NeueKompassRichtungMerken) |
{ |
KompassStartwert = KompassValue; |
NeueKompassRichtungMerken = 0; |
if ((MaxStickNick < 40) && (MaxStickRoll < 40)) // Bei extremen Flugmanövern keine Kompassauswertung |
{ |
KompassValue = heading_MM3(); |
KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
if(NeueKompassRichtungMerken) |
{ |
KompassStartwert = KompassValue; |
NeueKompassRichtungMerken = 0; |
} |
Mess_Integral_Gier -= (KompassRichtung *Parameter_KompassWirkung); // nach Kompass ausrichten |
} |
Mess_Integral_Gier -= (KompassRichtung *Parameter_KompassWirkung); // nach Kompass ausrichten |
} |
} |
} |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// Debugwerte zuordnen |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
if(!TimerWerteausgabe--) |
{ |
TimerWerteausgabe = 24; |
if(!TimerWerteausgabe--) |
{ |
TimerWerteausgabe = 24; |
DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor; |
DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor; |
DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor; |
DebugOut.Analog[2] = Mittelwert_AccNick; |
DebugOut.Analog[3] = Mittelwert_AccRoll; |
DebugOut.Analog[4] = MesswertGier; |
DebugOut.Analog[5] = HoehenWert; |
DebugOut.Analog[6] =(Mess_Integral_Hoch / 512); |
DebugOut.Analog[6] = Mess_Integral_Hoch / 512; |
DebugOut.Analog[7] = GasMischanteil; |
DebugOut.Analog[8] = KompassValue; |
DebugOut.Analog[9] = UBat; |
DebugOut.Analog[10] = SenderOkay; |
DebugOut.Analog[12] = Motor_Vorne; |
DebugOut.Analog[13] = Motor_Hinten; |
DebugOut.Analog[14] = Motor_Links; |
DebugOut.Analog[15] = Motor_Rechts; |
DebugOut.Analog[16] = Mittelwert_AccHoch; |
/* DebugOut.Analog[16] = motor_rx[0]; |
DebugOut.Analog[17] = motor_rx[1]; |
DebugOut.Analog[18] = motor_rx[2]; |
DebugOut.Analog[19] = motor_rx[3]; |
DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3]; |
DebugOut.Analog[20] /= 14; |
DebugOut.Analog[21] = motor_rx[4]; |
DebugOut.Analog[22] = motor_rx[5]; |
DebugOut.Analog[23] = motor_rx[6]; |
DebugOut.Analog[24] = motor_rx[7]; |
DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
/* |
DebugOut.Analog[17] = IntegralAccNick / 26; |
DebugOut.Analog[18] = IntegralAccRoll / 26; |
DebugOut.Analog[19] = IntegralFehlerNick;// / 26; |
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26; |
DebugOut.Analog[21] = MittelIntegralNick / 26; |
DebugOut.Analog[22] = MittelIntegralRoll / 26; |
DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick); |
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
DebugOut.Analog[25] = IntegralRoll * IntegralFaktor; |
DebugOut.Analog[27] = ausgleichRoll; |
DebugOut.Analog[28] = MesswertRoll; |
DebugOut.Analog[29] = ausgleichRoll; |
DebugOut.Analog[30] = LageKorrekturRoll * 10; |
DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor); |
*/ |
// DebugOut.Analog[9] = MesswertNick; |
// DebugOut.Analog[9] = SollHoehe; |
// DebugOut.Analog[10] = Mess_Integral_Gier / 128; |
// DebugOut.Analog[11] = KompassStartwert; |
// DebugOut.Analog[10] = Parameter_Gyro_I; |
// DebugOut.Analog[10] = EE_Parameter.Gyro_I; |
// DebugOut.Analog[9] = KompassRichtung; |
// DebugOut.Analog[10] = GasMischanteil; |
// DebugOut.Analog[3] = HoeheD * 32; |
// DebugOut.Analog[4] = hoehenregler; |
} |
} |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
//DebugOut.Analog[26] = MesswertNick; |
//DebugOut.Analog[28] = MesswertRoll; |
if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor; |
else MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor; |
990,10 → 970,6 |
else MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor; |
MesswertGier = MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2; |
DebugOut.Analog[25] = IntegralRoll * IntegralFaktor; |
DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor); |
DebugOut.Analog[28] = MesswertRoll; |
// Maximalwerte abfangen |
#define MAX_SENSOR 2048 |
if(MesswertNick > MAX_SENSOR) MesswertNick = MAX_SENSOR; |
1052,7 → 1028,7 |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// + Mischer und PI-Regler |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
DebugOut.Analog[7] = GasMischanteil; |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// Gier-Anteil |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
/branches/MicroMag3_Nick666/trunc/main.c |
---|
254,7 → 254,6 |
} |
timer = SetDelay(100); |
} |
//if(UpdateMotor) DebugOut.Analog[26]++; |
} |
} |
return (1); |
/branches/MicroMag3_Nick666/trunc/mymath.c |
---|
20,7 → 20,7 |
//############################################################################ |
// Arkustangens2 im Gradmaß |
signed int atan2_i(signed int x, signed int y) |
int16_t atan2_i(int16_t x, int16_t y) |
//############################################################################ |
{ |
int16_t angle; |
59,7 → 59,7 |
//############################################################################ |
// Kosinusfunktion im Gradmaß |
signed int cos_i(signed int winkel) |
int16_t cos_i(int16_t winkel) |
//############################################################################ |
{ |
return (sin_i(90-winkel)); |
67,11 → 67,11 |
//############################################################################ |
// Sinusfunktion im Gradmaß |
signed int sin_i(signed int winkel) |
int16_t sin_i(int16_t winkel) |
//############################################################################ |
{ |
short int m,n; |
signed int sinus; |
int8_t m,n; |
int16_t sinus; |
if (winkel < 0) |
{ |
/branches/MicroMag3_Nick666/trunc/timer0.c |
---|
87,17 → 87,27 |
unsigned int SetDelay (unsigned int t) |
{ |
// TIMSK0 &= ~_BV(TOIE0); |
return(CountMilliseconds + t + 1); |
// TIMSK0 |= _BV(TOIE0); |
uint8_t tmp_sreg; |
uint16_t CountMilliseconds_local; |
tmp_sreg = SREG; |
cli(); |
CountMilliseconds_local = CountMilliseconds; |
SREG = tmp_sreg; |
return(CountMilliseconds_local + t + 1); |
} |
// ----------------------------------------------------------------------- |
char CheckDelay(unsigned int t) |
{ |
// TIMSK0 &= ~_BV(TOIE0); |
return(((t - CountMilliseconds) & 0x8000) >> 9); |
// TIMSK0 |= _BV(TOIE0); |
uint8_t tmp_sreg; |
uint16_t CountMilliseconds_local; |
tmp_sreg = SREG; |
cli(); |
CountMilliseconds_local = CountMilliseconds; |
SREG = tmp_sreg; |
return (((t - CountMilliseconds_local) & 0x8000) >> 9); |
} |
// ----------------------------------------------------------------------- |