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); |