Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 472 → Rev 473

/trunk/compass.c
107,6 → 107,7
break;
 
case COMPASS_NCMAG:
UART1_PutString("\r\n Re-Init compass");
if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
else Compass_Device = COMPASS_NONE;
break;
171,7 → 172,8
DebugOut.Analog[26] = MagVector.Z;
break;
case COMPASS_NCMAG:
NCMAG_Update();
if(check_value_counter == 2000) InitNC_MagnetSensor(); // 2 seconds noch change of the compass value
else NCMAG_Update();
DebugOut.Analog[24] = MagRawVector.X;
DebugOut.Analog[25] = MagRawVector.Y;
DebugOut.Analog[26] = MagRawVector.Z;
/trunk/i2c0.c
61,6 → 61,7
#include "timer1.h"
#include "config.h"
#include "led.h"
#include "ncmag.h"
 
 
volatile u8 I2C0_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
214,8 → 215,18
// empty rx and tx buffer
I2C0_TxBufferSize = 0;
I2C0_RxBufferSize = 0;
I2C0_Timeout = SetDelay(2 * I2C0_TIMEOUT);
I2C0_Timeout = SetDelay(5 * I2C0_TIMEOUT);
 
if(I2C_CompassPort == I2C_EXTERN_0)
{
MagRawVector.X = 0;
MagRawVector.Y = 0;
MagRawVector.Z = 0;
AccRawVector.X = 0;
AccRawVector.Y = 0;
AccRawVector.Z = 0;
Compass_Heading = -1;
}
UART1_PutString("ok");
}
 
/trunk/i2c1.c
60,8 → 60,8
#include "timer1.h"
#include "config.h"
#include "led.h"
#include "ncmag.h"
 
 
volatile u8 I2C1_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
volatile u8 I2C1_Error = I2C_ERROR_NOACK; // only one byte!
 
214,6 → 214,17
I2C1_RxBufferSize = 0;
 
I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
if(I2C_CompassPort == I2C_INTERN_1)
{
MagRawVector.X = 0;
MagRawVector.Y = 0;
MagRawVector.Z = 0;
AccRawVector.X = 0;
AccRawVector.Y = 0;
AccRawVector.Z = 0;
Compass_Heading = -1;
}
 
UART1_PutString("ok");
}
/trunk/main.c
82,9 → 82,9
#include "ssc.h"
#include "sdc.h"
#include "uart1.h"
#include "ncmag.h"
 
 
 
#ifdef FOLLOW_ME
u8 TransmitAlsoToFC = 0;
#endif
/trunk/menu.c
75,7 → 75,7
 
 
u8 MenuItem = 0;
u8 MaxMenuItem = 22;
u8 MaxMenuItem = 23;
 
void Menu_Putchar(char c)
{
358,6 → 358,7
LCD_printfxy(0,0,"GyroNick: %4i", FromFlightCtrl.GyroNick);
LCD_printfxy(0,1,"GyroRoll: %4i", FromFlightCtrl.GyroRoll);
LCD_printfxy(0,2,"GyroYaw: %4i", FromFlightCtrl.GyroYaw);
if(FC_is_Calibrated) LCD_printfxy(0,3,"Calibrated ") else LCD_printfxy(0,3,"not calibrated");
break;
case 15:
LCD_printfxy(0,0,"Ubat: %2i.%1i V", FC.BAT_Voltage/10, FC.BAT_Voltage%10);
418,7 → 419,7
LCD_printfxy(0,2,"Y:%5i",MagVector.Y);
LCD_printfxy(0,3,"Z:%5i",MagVector.Z);
LCD_printfxy(8,1,"Field:%3i",EarthMagneticField/5);
if(I2C_CompassPort == I2C_EXTERN_0) LCD_printfxy(12,2,"Extern") else LCD_printfxy(12,2,"Intern");
if(I2C_CompassPort == I2C_EXTERN_0) LCD_printfxy(11,2,"Extern %d",ExtCompassOrientation) else LCD_printfxy(11,2,"Intern");
// LCD_printfxy(8,2,"Dec:%c%i.%1i", sign, abs(GeoMagDec)/10,abs(GeoMagDec)%10);
// LCD_printfxy(8,3,"Inc:%2i", EarthMagneticInclination);
LCD_printfxy(15,3,"(CAL)");
457,8 → 458,23
LCD_printfxy(0,i1+1,"%3d %3d %3d %3d ",BL3_Current(i1*4),BL3_Current(i1*4+1),BL3_Current(i1*4+2),BL3_Current(i1*4+3));
if(Motor[4 + i1 * 4].State == 0) break;
}
 
break;
case 23:
LCD_printfxy(0,0,"Ext. Compass" );
if(I2C_CompassPort == I2C_EXTERN_0)
{
u8 tmp;
LCD_printfxy(0,1,"ACC X Y Z");
LCD_printfxy(0,2," %5d %5d %5d",AccRawVector.X,AccRawVector.Y,AccRawVector.Z);
tmp = GetExtCompassOrientation();
LCD_printfxy(0,3,"Orientat.: ");
if(!tmp) LCD_printfxy(11,3,"??") else LCD_printfxy(11,3,"%2d",tmp);
}
else
{
LCD_printfxy(0,1,"Not connected");
}
break;
default:
//MaxMenuItem = MenuItem - 1;
MenuItem = 0;
/trunk/ncmag.c
73,6 → 73,7
u8 NCMAG_IsCalibrated = 0;
 
u8 I2C_CompassPort = 1;
u8 ExtCompassOrientation = 0;
 
u8 *I2C_BufferPnt;
u8 *I2C_ErrorPnt;
89,31 → 90,6
 
u8 NCMAG_SensorType = TYPE_NONE;
 
#define EEPROM_ADR_MAG_CALIBRATION_INTERN 50 // two calibrtion sets for extern and intern sensor
#define EEPROM_ADR_MAG_CALIBRATION_EXTERN 70
 
#define CALIBRATION_VERSION 1
#define MAG_CALIBRATION_COMPATIBLE 0xA2
 
#define NCMAG_MIN_RAWVALUE -2047
#define NCMAG_MAX_RAWVALUE 2047
#define NCMAG_INVALID_DATA -4096
 
typedef struct
{
s16 Range;
s16 Offset;
} __attribute__((packed)) Scaling_t;
 
typedef struct
{
Scaling_t MagX;
Scaling_t MagY;
Scaling_t MagZ;
u8 Version;
u8 crc;
} __attribute__((packed)) Calibration_t;
 
Calibration_t Calibration; // calibration data in RAM
volatile s16vec_t AccRawVector;
volatile s16vec_t MagRawVector;
316,11 → 292,14
u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
EEPROM_Result_t eres;
u8 *pBuff = (u8*)&Calibration;
Calibration.Version = CALIBRATION_VERSION;
 
if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
 
Calibration.Version = CALIBRATION_VERSION;
else
{
address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
Calibration.Version = CALIBRATION_VERSION + ExtCompassOrientation * 16;
}
for(i = 0; i<(sizeof(Calibration)-1); i++)
{
crc += pBuff[i];
349,7 → 328,7
}
crc = ~crc;
if(Calibration.crc != crc) return(0); // crc mismatch
if(Calibration.Version == CALIBRATION_VERSION) return(1);
if((Calibration.Version & 0x0f) == CALIBRATION_VERSION) return(1);
}
return(0);
}
495,24 → 474,59
{ // if number of bytes are matching
if(RxBufferSize == sizeof(MagRawVector) )
{ // byte order from big to little endian
s16 raw;
s16 raw, X = 0, Y = 0, Z = 0;
raw = pRxBuffer[0]<<8;
raw+= pRxBuffer[1];
if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw;
raw = pRxBuffer[2]<<8;
raw+= pRxBuffer[3];
if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
{
if(NCMAG_SensorType == TYPE_LSM303DLM) MagRawVector.Z = raw; // here Z and Y are exchanged
else MagRawVector.Y = raw;
if(NCMAG_SensorType == TYPE_LSM303DLM) Z = raw; // here Z and Y are exchanged
else Y = raw;
}
raw = pRxBuffer[4]<<8;
raw+= pRxBuffer[5];
if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
{
if(NCMAG_SensorType == TYPE_LSM303DLM) MagRawVector.Y = raw; // here Z and Y are exchanged
else MagRawVector.Z = raw;
if(NCMAG_SensorType == TYPE_LSM303DLM) Y = raw; // here Z and Y are exchanged
else Z = raw;
}
switch(ExtCompassOrientation)
{
case 0:
case 1:
default:
MagRawVector.X = X;
MagRawVector.Y = Y;
MagRawVector.Z = Z;
break;
case 2:
MagRawVector.X = -X;
MagRawVector.Y = Y;
MagRawVector.Z = -Z;
break;
case 3:
MagRawVector.X = -Z;
MagRawVector.Y = Y;
MagRawVector.Z = X;
break;
case 4:
MagRawVector.X = Z;
MagRawVector.Y = Y;
MagRawVector.Z = -X;
break;
case 5:
MagRawVector.X = X;
MagRawVector.Y = -Z;
MagRawVector.Z = Y;
break;
case 6:
MagRawVector.X = -X;
MagRawVector.Y = -Z;
MagRawVector.Z = -Y;
break;
}
}
if(Compass_CalState || !NCMAG_IsCalibrated)
{ // mark out data invalid
533,18 → 547,32
// rx data handler for acceleration raw data
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
static s32 filter_z;
if(RxBufferSize == sizeof(AccRawVector) )
{
memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
}
// DebugOut.Analog[16] = AccRawVector.X;
// DebugOut.Analog[17] = AccRawVector.Y;
filter_z = (filter_z * 7 + AccRawVector.Z) / 8;
}
 
// DebugOut.Analog[18] = filter_z;
// DebugOut.Analog[19] = AccRawVector.Z;
u8 GetExtCompassOrientation(void)
{
if(I2C_CompassPort != I2C_EXTERN_0) return(0);
if(abs(FromFlightCtrl.AngleNick) > 300) return(0); // MK tilted
if(abs(FromFlightCtrl.AngleRoll) > 300) return(0);
 
if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
else
if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
else
if(AccRawVector.X > 3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn
else
if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn
else
if(AccRawVector.Y > 3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn
else
if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten - 'front' nach vorn
return(0);
}
 
// rx data handler for reading magnetic sensor configuration
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
611,7 → 639,7
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2C_LockBufferFunc(50))
{
u8 TxBytes = 0;
I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
619,7 → 647,7
TxBytes += sizeof(AccConfig);
if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2C_WaitForEndOfTransmissionFunc(50))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
709,10 → 737,10
}
 
//----------------------------------------------------------------
void NCMAG_GetAccVector(void)
void NCMAG_GetAccVector(u8 timeout)
{
// try to catch the I2C buffer within 0 ms
if(I2C_LockBufferFunc(0))
if(I2C_LockBufferFunc(timeout))
{
u16 TxBytes = 0;
// set register pointer
719,6 → 747,9
I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
// initiate transmission
I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
//DebugOut.Analog[16] = AccRawVector.X;
//DebugOut.Analog[17] = AccRawVector.Y;
//DebugOut.Analog[18] = AccRawVector.Z;
}
}
 
755,7 → 786,7
u8 NCMAG_Init_ACCSensor(void)
{
AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_50HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN;
AccConfig.ctrl_2 = 0;//ACC_CRTL2_FILTER32;
AccConfig.ctrl_2 = 0;
AccConfig.ctrl_3 = 0x00;
AccConfig.ctrl_4 = ACC_CTRL4_BDU | ACC_CTRL4_FS_8G;
AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF;
785,7 → 816,7
}
else
{
// static u8 s = 0;
static u8 s = 0;
// check for new calibration state
Compass_UpdateCalState();
if(Compass_CalState) NCMAG_Calibrate();
799,12 → 830,16
break;
case TYPE_LSM303DLH:
case TYPE_LSM303DLM:
NCMAG_GetMagVector();
delay = 20;
/* if(s){ NCMAG_GetMagVector(); s = 0;}
else { NCMAG_GetAccVector(); s = 1;}
delay = 10;
*/
if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector();
else
{
if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor();
NCMAG_GetAccVector(0);
delay = 3;
s = 50;
};
 
break;
}
if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration
965,7 → 1000,8
retval = 0;
do
{
retval = NCMAG_GetIdentification();
// retval = NCMAG_GetIdentification();
retval = NCMAG_GetAccConfig(); // only the sensor with ACC is supported
if(retval) break; // break loop on success
// UART1_PutString("+");
repeat++;
980,6 → 1016,30
else
{
UART1_PutString(" external sensor ");
NCMAG_Init_ACCSensor();
 
for(repeat = 0; repeat < 100; repeat++)
{
NCMAG_GetAccVector(10); // only the sensor with ACC is supported
ExtCompassOrientation = GetExtCompassOrientation();
if(ExtCompassOrientation) break;
}
//DebugOut.Analog[19] = repeat;
 
if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)");
else
{
NCMag_CalibrationRead(I2C_CompassPort);
sprintf(msg, "with orientation: %d ",ExtCompassOrientation );
UART1_PutString(msg);
if(ExtCompassOrientation != Calibration.Version / 16)
{
sprintf(msg, "\n\r! Warining: calibrated orientation was %d !",Calibration.Version / 16);
UART1_PutString(msg);
}
else UART1_PutString("ok");
}
 
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
/trunk/ncmag.h
6,7 → 6,35
 
extern s16vec_t ACC_Vector;
extern volatile s16vec_t MagRawVector;
extern volatile s16vec_t AccRawVector;
 
#define EEPROM_ADR_MAG_CALIBRATION_INTERN 50 // two calibrtion sets for extern and intern sensor
#define EEPROM_ADR_MAG_CALIBRATION_EXTERN 70
 
#define CALIBRATION_VERSION 1
#define MAG_CALIBRATION_COMPATIBLE 0xA2
 
#define NCMAG_MIN_RAWVALUE -2047
#define NCMAG_MAX_RAWVALUE 2047
#define NCMAG_INVALID_DATA -4096
 
typedef struct
{
s16 Range;
s16 Offset;
} __attribute__((packed)) Scaling_t;
 
typedef struct
{
Scaling_t MagX;
Scaling_t MagY;
Scaling_t MagZ;
u8 Version;
u8 crc;
} __attribute__((packed)) Calibration_t;
 
extern Calibration_t Calibration; // calibration data in RAM
 
#define I2C_EXTERN_0 0
#define I2C_INTERN_1 1
 
15,7 → 43,9
extern u8 NCMAG_Present;
extern u8 NCMAG_IsCalibrated;
extern u8 I2C_CompassPort;
extern u8 ExtCompassOrientation;
 
 
extern u8 *I2C_BufferPnt;
extern u8 *I2C_ErrorPnt;
 
/trunk/spi_slave.c
70,6 → 70,7
#include "params.h"
#include "stdlib.h"
#include "settings.h"
#include "ncmag.h"
 
#define SPI_RXSYNCBYTE1 0xAA
#define SPI_RXSYNCBYTE2 0x83
585,7 → 586,15
}
else
{
if(FCCalibActive) if(--FCCalibActive == 0) FC_is_Calibrated = 1;
if(FCCalibActive)
{
if(--FCCalibActive == 0)
{
FC_is_Calibrated = 1;
ExtCompassOrientation = GetExtCompassOrientation();
if(ExtCompassOrientation != Calibration.Version / 16) NCMAG_IsCalibrated = 0;
}
}
}
if(FC.StatusFlags & FC_STATUS_START)
{
797,7 → 806,7
}while (!CheckDelay(timeout));
UART1_PutString(".");
repeat++;
FCCalibActive = 1;
// FCCalibActive = 1;
}while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
// if we got it
if (FC_Version.Major != 0xFF)
/trunk/uart1.c
775,28 → 775,28
{
// +++++++++++++++++++++++++++++++++++++++++++
tmp1 = abs(GPSData.Position.Latitude)/10000000L;
i += sprintf(&array[i],"A,%02d",tmp1); // Status: A = Okay V = Warnung
i += sprintf(&array[i],"A,%02d",(int)tmp1); // Status: A = Okay V = Warnung
 
tmp1 = abs(GPSData.Position.Latitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",tmp2);
i += sprintf(&array[i],"%02d",(int)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",tmp2);
i += sprintf(&array[i],".%05d,",(int)tmp2);
if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
else i += sprintf(&array[i],"S,");
// +++++++++++++++++++++++++++++++++++++++++++
tmp1 = abs(GPSData.Position.Longitude)/10000000L;
i += sprintf(&array[i],"%03d",tmp1);
i += sprintf(&array[i],"%03d",(int)tmp1);
 
tmp1 = abs(GPSData.Position.Longitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",tmp2);
i += sprintf(&array[i],"%02d",(int)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",tmp2);
i += sprintf(&array[i],".%05d,",(int)tmp2);
if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
else i += sprintf(&array[i],"W,");
// +++++++++++++++++++++++++++++++++++++++++++