/branches/MicroMag3_Nick666/V0.69k/compass.c |
---|
0,0 → 1,241 |
/* |
Copyright 2007, Niklas Nold |
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify |
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
either version 3 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
Please note: All the other files for the project "Mikrokopter" by H. Buss are under the license (license_buss.txt) published by www.mikrokopter.de |
*/ |
#include "main.h" |
struct MM3_calib_struct ee_calib EEMEM; // Reservierung im EEPROM |
struct MM3_working_struct MM3; |
struct MM3_calib_struct MM3_calib; |
//############################################################################ |
// Initialisierung |
void init_MM3(void) |
//############################################################################ |
{ |
// SPI-Schnittstelle initialisieren |
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); // Interrupt an, Master, 156 kHz Oszillator |
DDRB |= (1<<PB7)|(1<<PB5); // MOSI, SCK Ausgang |
DDRC |= (1<<PC4)|(1<<PC5); // PC5 (RESET) und PC4 (SSNOT) als Ausgang |
PORTC |= (1<<PC4); // PC4 (SSNOT) auf High -> MM3 passiv |
PORTC &= ~(1<<PC5); // PC5 (RESET) auf Low |
// Init Statemachine |
MM3.AXIS = MM3_X; |
MM3.STATE = MM3_RESET; |
// Kalibrierung aus dem EEprom lesen |
eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
} |
//############################################################################ |
// Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
void timer0_MM3(void) |
//############################################################################ |
{ |
switch (MM3.STATE) |
{ |
case MM3_RESET: |
PORTC &= ~(1<<PC4); // MM3 aktiv |
PORTC |= (1<<PC5); // MM3 Reset |
MM3.STATE = MM3_START_TRANSFER; |
return; |
case MM3_START_TRANSFER: |
PORTC &= ~(1<<PC5); // PC5 auf Low (war ~125 µs auf High) |
if (MM3.AXIS == MM3_X) SPDR = MM3_PERIOD_512 + MM3_X_AXIS; // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus |
else if (MM3.AXIS == MM3_Y) SPDR = MM3_PERIOD_512 + MM3_Y_AXIS; // Micromag Period Select ist 256 (0x30) |
else SPDR = MM3_PERIOD_512 + MM3_Z_AXIS; //if (MM3.AXIS == MM3_Z) |
MM3.DRDY = SetDelay(10); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 512 eigentlich 8 ms) |
MM3.STATE = MM3_WAIT_DRDY; |
return; |
case MM3_WAIT_DRDY: |
if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
return; // Jetzt gehts weiter in SIGNAL (SIG_SPI) |
} |
} |
//############################################################################ |
// SPI byte ready |
SIGNAL (SIG_SPI) |
//############################################################################ |
{ |
static char tmp; |
int value; |
switch (MM3.STATE) |
{ |
case MM3_DRDY: // 1. Byte ist da, zwischenspeichern |
tmp = SPDR; |
SPDR = 0x00; // Übertragung von 2. Byte auslösen |
MM3.STATE = MM3_BYTE2; |
return; |
case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
value = tmp; |
value <<= 8; // 1. Byte an MSB-Stelle rücken |
value |= SPDR; // 2. Byte dranpappen |
if(abs(value) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
switch (MM3.AXIS) |
{ |
case MM3_X: |
MM3.x_axis = value; |
MM3.AXIS = MM3_Y; |
break; |
case MM3_Y: |
MM3.y_axis = value; |
MM3.AXIS = MM3_Z; |
break; |
default: //case MM3_Z: |
MM3.z_axis = value; |
MM3.AXIS = MM3_X; |
} |
PORTC |= (1<<PC4); // MM3 passiv |
MM3.STATE = MM3_RESET; |
} |
} |
//############################################################################ |
// Kompass kalibrieren |
void calib_MM3(void) |
//############################################################################ |
{ |
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; |
GRN_ON; |
ROT_OFF; |
while (measurement) |
{ |
uint8_t 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 (y_axis > y_max) y_max = y_axis; |
else if (y_axis < y_min) y_min = y_axis; |
if (z_axis > z_max) z_max = z_axis; |
else if (z_axis < z_min) z_min = z_axis; |
if (!beeper) |
{ |
ROT_FLASH; |
GRN_FLASH; |
} |
beeper--; |
// Schleife mit 100 Hz |
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--; |
} |
// Wertebereich der Achsen |
MM3_calib.X_range = (x_max - x_min); |
MM3_calib.Y_range = (y_max - y_min); |
MM3_calib.Z_range = (z_max - z_min); |
// Offset der Achsen |
MM3_calib.X_off = (x_max + x_min) /2; |
MM3_calib.Y_off = (y_max + y_min) /2; |
MM3_calib.Z_off = (z_max + z_min) /2; |
// und im EEProm abspeichern |
eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
} |
//############################################################################ |
// Neigungskompensierung und Berechnung der Ausrichtung |
int heading_MM3(void) |
//############################################################################ |
{ |
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; |
// 16bit-Werte lesen |
uint8_t 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; |
int temp = Aktuell_az - acc_neutral.compass; |
// Lage-Berechnung mittels Acc-Messwerte |
tilt = atan2_i(temp,AdWertAccNick*64); |
sin_nick = sin_i(tilt); |
cos_nick = cos_i(tilt); |
tilt = atan2_i(temp,AdWertAccRoll*64); |
sin_roll = sin_i(tilt); |
cos_roll = cos_i(tilt); |
/* |
// Lage-Berechnung mittels Gyro-Integral |
uint16_t div_faktor; |
div_faktor = (uint16_t)EE_Parameter.UserParam3 *8; |
tilt = (IntegralNick /div_faktor); |
sin_nick = sin_i(tilt); |
cos_nick = cos_i(tilt); |
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)) *512) /MM3_calib.X_range; |
Hy = (((int32_t)(mm3_y_axis - MM3_calib.Y_off)) *512) /MM3_calib.Y_range; |
Hz = (((int32_t)(mm3_z_axis - MM3_calib.Z_off)) *512) /MM3_calib.Z_range; |
// Neigungskompensierung |
x_corr = Hx * cos_nick; |
x_corr -= Hz * sin_nick; |
x_corr /= 1024; |
y_corr = Hy * cos_roll; |
y_corr += Hz * sin_roll; |
y_corr /= 16; // atan2_i erwartet y_corr *64. Deshalb /16 und nicht /1024 |
// Winkelberechnung |
heading = atan2_i(x_corr, y_corr); |
// Skalieren von +-180° auf 0-360° |
if (heading < 0) heading = -heading; |
else heading = 360 - heading; |
return (heading); |
} |
/branches/MicroMag3_Nick666/V0.69k/compass.h |
---|
0,0 → 1,59 |
struct MM3_working_struct |
{ |
uint8_t STATE; |
uint8_t AXIS; |
uint16_t DRDY; |
int16_t x_axis; |
int16_t y_axis; |
int16_t z_axis; |
}; |
struct MM3_calib_struct |
{ |
int8_t X_off; |
int8_t Y_off; |
int8_t Z_off; |
int16_t X_range; |
int16_t Y_range; |
int16_t Z_range; |
}; |
extern struct MM3_working_struct MM3; |
extern struct MM3_calib_struct MM3_calib; |
void init_MM3(void); |
void timer0_MM3(void); |
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 |
#define MM3_RESET 0 |
#define MM3_START_TRANSFER 1 |
#define MM3_WAIT_DRDY 2 |
#define MM3_DRDY 3 |
#define MM3_BYTE2 4 |
#define MM3_X 5 |
#define MM3_Y 6 |
#define MM3_Z 7 |
#define MM3_TILT 8 |
#define MM3_IDLE 9 |
/branches/MicroMag3_Nick666/V0.69k/fc.c |
---|
71,9 → 71,12 |
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0; |
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2; |
volatile long Mess_Integral_Hoch = 0; |
volatile int KompassValue = 0; |
volatile int KompassStartwert = 0; |
volatile int KompassRichtung = 0; |
int KompassValue = 0; |
int KompassStartwert = 0; |
int KompassRichtung = 0; |
uint8_t updKompass; |
unsigned int KompassSignalSchlecht = 500; |
unsigned char MAX_GAS,MIN_GAS; |
unsigned char Notlandung = 0; |
158,6 → 161,12 |
acc_neutral.Y = abs(Mittelwert_AccRoll) / ACC_AMPLIFY; |
acc_neutral.Z = Aktuell_az; |
Piep(2); |
while (PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100) //Warten, bis Benutzer den Kopter neu ausgerichtet hat |
Delay_ms_Mess(100); |
acc_neutral.compass = Aktuell_az; |
eeprom_write_block(&acc_neutral,&ee_acc_neutral,sizeof(struct acc_neutral_struct)); |
} |
987,9 → 996,16 |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
//DebugOut.Analog[29] = (MaxStickNick + MaxStickRoll); |
if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)) |
if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
{ |
int w,v,r,fehler,korrektur; |
if (!updKompass--) // Aufruf mit ~20 Hz |
{ |
updKompass = 25; |
KompassValue = heading_MM3(); |
KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
} |
w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln |
v = abs(IntegralRoll /512); |
if(v > w) w = v; // grösste Neigung ermitteln |
1016,15 → 1032,14 |
v = (r * w) / v; // nach Kompass ausrichten |
w = 3 * Parameter_KompassWirkung; |
if(v > w) v = w; // Begrenzen |
else |
if(v < -w) v = -w; |
else if(v < -w) v = -w; |
Mess_Integral_Gier += v; |
} |
if(KompassSignalSchlecht) KompassSignalSchlecht--; |
} |
else KompassSignalSchlecht = 250; // so lange das Signal taub stellen --> ca. 0,5 sek |
} |
} |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
/branches/MicroMag3_Nick666/V0.69k/fc.h |
---|
16,9 → 16,11 |
extern long IntegralAccNick,IntegralAccRoll; |
extern volatile long Mess_Integral_Hoch; |
extern long Integral_Gier,Mess_Integral_Gier,Mess_Integral_Gier2; |
extern volatile int KompassValue; |
extern volatile int KompassStartwert; |
extern volatile int KompassRichtung; |
extern int KompassValue; |
extern int KompassStartwert; |
extern int KompassRichtung; |
extern long ErsatzKompass; |
extern int ErsatzKompassInGrad; // Kompasswert in Grad |
extern int HoehenWert; |
59,6 → 61,7 |
int X; |
int Y; |
float Z; |
int compass; |
}; |
extern struct acc_neutral_struct acc_neutral; |
/branches/MicroMag3_Nick666/V0.69k/flight.pnproj |
---|
1,0 → 0,0 |
<Project name="Flight-Ctrl"><File path="uart.h"></File><File path="main.c"></File><File path="main.h"></File><File path="makefile"></File><File path="uart.c"></File><File path="printf_P.h"></File><File path="printf_P.c"></File><File path="timer0.c"></File><File path="timer0.h"></File><File path="old_macros.h"></File><File path="twimaster.c"></File><File path="version.txt"></File><File path="twimaster.h"></File><File path="rc.c"></File><File path="rc.h"></File><File path="fc.h"></File><File path="fc.c"></File><File path="menu.h"></File><File path="menu.c"></File><File path="_Settings.h"></File><File path="analog.c"></File><File path="analog.h"></File><File path="GPS.c"></File><File path="gps.h"></File><File path="License.txt"></File><File path="eeprom.c"></File><File path="spi.h"></File><File path="spi.c"></File></Project> |
<Project name="Flight-Ctrl"><File path="uart.h"></File><File path="main.c"></File><File path="main.h"></File><File path="makefile"></File><File path="uart.c"></File><File path="printf_P.h"></File><File path="printf_P.c"></File><File path="timer0.c"></File><File path="timer0.h"></File><File path="old_macros.h"></File><File path="twimaster.c"></File><File path="version.txt"></File><File path="twimaster.h"></File><File path="rc.c"></File><File path="rc.h"></File><File path="fc.h"></File><File path="fc.c"></File><File path="menu.h"></File><File path="menu.c"></File><File path="_Settings.h"></File><File path="analog.c"></File><File path="analog.h"></File><File path="GPS.c"></File><File path="gps.h"></File><File path="License.txt"></File><File path="eeprom.c"></File><File path="spi.h"></File><File path="spi.c"></File><File path="mymath.h"></File><File path="compass.c"></File><File path="compass.h"></File><File path="mymath.c"></File></Project> |
/branches/MicroMag3_Nick666/V0.69k/main.c |
---|
147,7 → 147,8 |
rc_sum_init(); |
ADC_Init(); |
i2c_init(); |
SPI_MasterInit(); |
init_MM3(); |
//SPI_MasterInit(); |
sei(); |
181,6 → 182,13 |
//kurze Wartezeit (sonst reagiert die "Kompass kalibrieren?"-Abfrage nicht |
Delay_ms(500); |
//Kompass kalibrieren? |
if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 100 && PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 100) |
{ |
printf("\n\rKalibriere Kompass"); |
calib_MM3(); |
} |
//Neutrallage kalibrieren? |
if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 100 && PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -100) |
{ |
275,7 → 283,7 |
} |
else SPI_StartTransmitPacket(SPI_CMD_VALUE);//# |
*/ |
SPI_StartTransmitPacket();//# |
//SPI_StartTransmitPacket();//# |
SendSPI = 4; |
timer = SetDelay(20); |
282,7 → 290,7 |
} |
//if(UpdateMotor) DebugOut.Analog[26]++; |
} |
if(!SendSPI) { SPI_TransmitByte(); } |
//if(!SendSPI) { SPI_TransmitByte(); } |
} |
return (1); |
} |
/branches/MicroMag3_Nick666/V0.69k/main.h |
---|
76,9 → 76,10 |
#include "rc.h" |
#include "fc.h" |
#include "gps.h" |
#include "spi.h" |
// #include "spi.h" |
#include "compass.h" |
#include "mymath.h" |
#ifndef EEMEM |
#define EEMEM __attribute__ ((section (".eeprom"))) |
#endif |
/branches/MicroMag3_Nick666/V0.69k/makefile |
---|
84,7 → 84,7 |
########################################################################################################## |
# List C source files here. (C dependencies are automatically generated.) |
SRC = main.c uart.c printf_P.c timer0.c analog.c menu.c |
SRC += twimaster.c rc.c fc.c GPS.c spi.c |
SRC += twimaster.c rc.c fc.c GPS.c compass.c mymath.c |
########################################################################################################## |
/branches/MicroMag3_Nick666/V0.69k/menu.c |
---|
27,7 → 27,7 |
void Menu(void) |
{ |
static unsigned char MaxMenue = 11,MenuePunkt=0; |
static unsigned char MaxMenue = 13,MenuePunkt=0; |
if(RemoteTasten & KEY1) { if(MenuePunkt) MenuePunkt--; else MenuePunkt = MaxMenue; LcdClear(); RemotePollDisplayLine = -1; } |
if(RemoteTasten & KEY2) { MenuePunkt++; LcdClear(); RemotePollDisplayLine = -1;} |
125,6 → 125,18 |
LCD_printfxy(0,2,"Gs:%4i Gi:%4i ",ExternControl.Gas,ExternControl.Gier); |
LCD_printfxy(0,3,"Hi:%4i Cf:%4i ",ExternControl.Hight,ExternControl.Config); |
break; |
case 12: |
LCD_printfxy(0,0,"MM3 Off"); |
LCD_printfxy(0,1,"X_Offset: %3i",MM3_calib.X_off); |
LCD_printfxy(0,2,"Y_Offset: %3i",MM3_calib.Y_off); |
LCD_printfxy(0,3,"Z_Offset: %3i",MM3_calib.Z_off); |
break; |
case 13: |
LCD_printfxy(0,0,"MM3 Range"); |
LCD_printfxy(0,1,"X_Range: %4i",MM3_calib.X_range); |
LCD_printfxy(0,2,"Y_Range: %4i",MM3_calib.Y_range); |
LCD_printfxy(0,3,"Z_Range: %4i",MM3_calib.Z_range); |
break; |
default: MaxMenue = MenuePunkt - 1; |
MenuePunkt = 0; |
break; |
/branches/MicroMag3_Nick666/V0.69k/mymath.c |
---|
0,0 → 1,111 |
/* |
Copyright 2007, Niklas Nold |
This program (files math.c and math.h) is free software; you can redistribute it and/or modify |
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
either version 3 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
Please note: All the other files for the project "Mikrokopter" by H. Buss are under the license (license_buss.txt) published by www.mikrokopter.de |
*/ |
#include "main.h" |
const uint8_t pgm_atan[346] PROGMEM = {0,1,2,3,4,4,5,6,7,8,9,10,11,11,12,13,14,15,16,17,17,18,19,20,21,21,22,23,24,24,25,26,27,27,28,29,29,30,31,31,32,33,33,34,35,35,36,36,37,37,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,45,46,46,47,47,48,48,48,49,49,50,50,50,51,51,51,52,52,52,53,53,53,54,54,54,55,55,55,55,56,56,56,57,57,57,57,58,58,58,58,59,59,59,59,60,60,60,60,60,61,61,61,61,62,62,62,62,62,63,63,63,63,63,63,64,64,64,64,64,64,65,65,65,65,65,65,66,66,66,66,66,66,66,67,67,67,67,67,67,67,68,68,68,68,68,68,68,68,69,69,69,69,69,69,69,69,69,70,70,70,70,70,70,70,70,70,71,71,71,71,71,71,71,71,71,71,71,72,72,72,72,72,72,72,72,72,72,72,73,73,73,73,73,73,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74,74,74,74,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79}; |
//############################################################################ |
// Arkustangens2 im Gradmaß |
signed int atan2_i(signed int x, signed int y) |
//############################################################################ |
{ |
int16_t angle; |
uint16_t i; |
int8_t m; |
if (!x && !y) return 0; //atan2 = 0 für x und y = 0 |
if (y < 0) m=-1; |
else m=1; |
if (!x) return (90*m); // atan2 = 90° für x = 0 |
i = abs(y / x); // Berechne i für die Lookup table (Schrittweite atan(x) ist 0,015625 -> y *64) |
if (i<346) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79° |
else if (i>7334) angle = 90; // Grenzwert ist 90° |
else if (i>2444) angle = 89; // 89° bis 80° über Wertebereiche |
else if (i>1465) angle = 88; |
else if (i>1046) angle = 87; |
else if (i>813) angle = 86; |
else if (i>664) angle = 85; |
else if (i>561) angle = 84; |
else if (i>486) angle = 83; |
else if (i>428) angle = 82; |
else if (i>382) angle = 81; |
else angle = 80; // (i>345) |
if (x > 0) return (angle*m); // Quadrant I und IV |
else if ((x < 0) && (m > 0)) return (-angle + 180); // Quadrant II |
else return (angle - 180); // x < 0 && y < 0 Quadrant III |
} |
const uint16_t pgm_sinus_i[91] PROGMEM = {0,18,36,54,71,89,107,125,143,160,178,195,213,230,248,265,282,299,316,333,350,367,384,400,416,433,449,465,481,496,512,527,543,558,573,587,602,616,630,644,658,672,685,698,711,724,737,749,761,773,784,796,807,818,828,839,849,859,868,878,887,896,904,912,920,928,935,943,949,956,962,968,974,979,984,989,994,998,1002,1005,1008,1011,1014,1016,1018,1020,1022,1023,1023,1024,1024}; |
//############################################################################ |
// Kosinusfunktion im Gradmaß |
signed int cos_i(signed int winkel) |
//############################################################################ |
{ |
return (sin_i(90-winkel)); |
} |
//############################################################################ |
// Sinusfunktion im Gradmaß |
signed int sin_i(signed int winkel) |
//############################################################################ |
{ |
short int m,n; |
signed int sinus; |
if (winkel < 0) |
{ |
m = -1; |
winkel = abs(winkel); |
} |
else m = +1; |
// Quadranten auswerten |
if (winkel <= 90) n=1; |
else if ((winkel > 90) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;} |
else if ((winkel > 180) && (winkel <= 270)) {winkel = winkel - 180; n = -1;} |
else {winkel = 360 - winkel; n = -1;} //if ((winkel > 270) && (winkel <= 360)) |
sinus = pgm_read_word(&pgm_sinus_i[winkel]); |
return (sinus*m*n); |
} |
/* |
const uint8_t pgm_asin[201] PROGMEM = {0,0,1,1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9,10,10,10,11,11,11,12,12,12,12,13,13,13,14,14,14,14,15,15,15,16,16,16,17,17,17,17,18,18,18,19,19,19,20,20,20,20,21,21,21,22,22,22,23,23,23,24,24,24,25,25,25,25,26,26,26,27,27,27,28,28,28,29,29,29,30,30,30,31,31,31,32,32,32,33,33,33,34,34,34,35,35,35,36,36,37,37,37,38,38,38,39,39,39,40,40,41,41,41,42,42,42,43,43,44,44,44,45,45,46,46,46,47,47,48,48,49,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,62,62,63,64,64,65,66,66,67,68,68,69,70,71,72,73,74,75,76,77,79,80,82,84,90}; |
//############################################################################ |
// Akurssinusfunktion im Gradmaß |
int8_t asin_i(signed int i) |
//############################################################################ |
{ |
signed char m; |
if (i < 0) {m=-1;i=abs(i);} |
else m=1; |
i %= 200; |
return (pgm_read_byte(&pgm_asin[i]) * m); |
} |
*/ |
/branches/MicroMag3_Nick666/V0.69k/mymath.h |
---|
0,0 → 1,8 |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
#include "main.h" |
extern signed int atan2_i(signed int x, signed int y); |
extern signed int cos_i(signed int winkel); |
extern signed int sin_i(signed int winkel); |
//extern int8_t asin_i(signed int i); |
/branches/MicroMag3_Nick666/V0.69k/timer0.c |
---|
3,7 → 3,6 |
volatile unsigned int CountMilliseconds = 0; |
volatile static unsigned int tim_main; |
volatile unsigned char UpdateMotor = 0; |
volatile unsigned int cntKompass = 0; |
volatile unsigned int beeptime = 0; |
volatile unsigned char SendSPI = 0; |
64,26 → 63,8 |
else PORTC &= ~(1<<7); |
} |
if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
{ |
if(PINC & 0x10) |
{ |
cntKompass++; |
if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) timer0_MM3(); // Kompass auslesen |
} |
else |
{ |
if((cntKompass) && (cntKompass < 362)) |
{ |
cntKompass += cntKompass / 41; |
if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
} |
// if(cntKompass < 10) cntKompass = 10; |
// KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L; |
KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
cntKompass = 0; |
} |
} |
} |
void Timer_Init(void) |
/branches/MicroMag3_Nick666/V0.69k/timer0.h |
---|
11,7 → 11,6 |
extern volatile unsigned int CountMilliseconds; |
extern volatile unsigned char UpdateMotor; |
extern volatile unsigned int beeptime; |
extern volatile unsigned int cntKompass; |
extern int ServoValue; |
extern unsigned int BeepMuster; |
extern volatile unsigned char SendSPI; |