Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 739 → Rev 740

/branches/MicroMag3_Nick666/trunc/GPS.c
1,30 → 1,138
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + only for non-profit use
// + www.MikroKopter.com
// + see the File "License.txt" for further Informations
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "main.h"
 
signed int GPS_Nick = 0;
signed int GPS_Roll = 0;
long GpsAktuell_X = 0;
long GpsAktuell_Y = 0;
long GpsZiel_X = 0;
long GpsZiel_Y = 0;
void GPS_Neutral(void)
int16_t GPS_Nick = 0;
int16_t GPS_Roll = 0;
 
uint8_t GPS_P_Factor = 0;
uint8_t GPS_D_Factor = 0;
 
uint8_t flashtimer;
 
typedef struct
{
GpsZiel_X = GpsAktuell_X;
GpsZiel_Y = GpsAktuell_Y;
}
int32_t Northing;
int32_t Easting;
uint8_t Status;
 
void GPS_BerechneZielrichtung(void)
} GPS_Pos_t;
 
// GPS coordinates for hold position
GPS_Pos_t GPSHoldPoint = {0,0, INVALID};
 
 
 
// ---------------------------------------------------------------------------------
 
 
void GPS_Main(void)
{
GPS_Nick = 0;
GPS_Roll = 0;
}
int32_t coscompass, sincompass;
int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0;
int32_t PD_North = 0, PD_East = 0;
int32_t GPSPosDev_North = 0, GPSPosDev_East = 0;
 
// poti2 enables the gps feature
if((Poti3 > 70) && (!Notlandung)) // run GPS function only if Poti 2 is larger than 70 (switch on)
{
switch(GPSInfo.status)
{
case INVALID: // invalid gps data
GPS_Nick = 0;
GPS_Roll = 0;
GRN_OFF;
break;
case PROCESSED: // if gps data are already processed
// downcount timeout
if(GPSTimeout) GPSTimeout--;
// if no new data arrived within timeout set current data invalid
// and therefore disable GPS
else
{
GPS_Nick = 0;
GPS_Roll = 0;
GPSInfo.status = INVALID;
}
break;
case VALID: // new valid data from gps device
// if the gps data quality is sufficient
if (GPSInfo.satfix == SATFIX_3D)
{
GRN_ON;
// if sticks are centered and hold position is valid enable position hold control
if ((MaxStickNick < 20) && (MaxStickRoll < 20) && (GPSHoldPoint.Status == VALID))
{
// Calculate deviation from hold position
GPSPosDev_North = GPSInfo.utmnorth - GPSHoldPoint.Northing;
GPSPosDev_East = GPSInfo.utmeast - GPSHoldPoint.Easting;
 
//Calculate PD-components of the controller (negative sign for compensation)
P_North = -(GPS_P_Factor * GPSPosDev_North)/2048;
D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512;
P_East = -(GPS_P_Factor * GPSPosDev_East)/2048;
D_East = -(GPS_D_Factor * GPSInfo.veleast)/512;
 
// PD-controller
PD_North = P_North + D_North;
PD_East = P_East + D_East;
 
// GPS to pitch and roll settings
 
//A positive pitch angle moves head downwards (flying forward).
//A positive roll angle tilts left side downwards (flying left).
 
// If compass heading is 0 the head of the copter is in north direction.
// A positive pitch angle will fly to north and a positive roll angle will fly to west.
// In case of a positive north deviation/velocity the
// copter should fly to south (negative pitch).
// In case of a positive east position deviation and a positive east velocity the
// copter should fly to west (positive roll).
 
// The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values
// in the fc.c. Therefore a positive north deviation/velocity should result in a positive
// GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll.
 
// rotation transformation to compass heading to match any copter orientation
coscompass = (int32_t)cos_i(KompassValue);
sincompass = (int32_t)sin_i(KompassValue);
 
GPS_Roll = (int16_t)((PD_East * coscompass - PD_North * sincompass) / 1024);
GPS_Nick = -1*(int16_t)((PD_East * sincompass + PD_North * coscompass) / 1024);
 
// limit GPS controls
#define GPS_CTRL_LIMIT 35
if (GPS_Nick > GPS_CTRL_LIMIT) GPS_Nick = GPS_CTRL_LIMIT;
if (GPS_Nick < -GPS_CTRL_LIMIT) GPS_Nick = -GPS_CTRL_LIMIT;
if (GPS_Roll > GPS_CTRL_LIMIT) GPS_Roll = GPS_CTRL_LIMIT;
if (GPS_Roll < -GPS_CTRL_LIMIT) GPS_Roll = -GPS_CTRL_LIMIT;
}
else // MK controlled by user
{
// update hold point to current gps position
GPSHoldPoint.Northing = GPSInfo.utmnorth;
GPSHoldPoint.Easting = GPSInfo.utmeast;
GPSHoldPoint.Status = VALID;
// disable gps control
GPS_Nick = 0;
GPS_Roll = 0;
}
} // eof 3D-FIX
else // no 3D-SATFIX
{ // diable gps control
GPS_Nick = 0;
GPS_Roll = 0;
if (!flashtimer--)
{
GRN_FLASH;
flashtimer=5;
}
}
// set current data as processed to avoid further calculations on the same gps data
GPSInfo.status = PROCESSED;
break;
} // eof GPSInfo.status
}
else GRN_OFF;
return;
}
 
/branches/MicroMag3_Nick666/trunc/compass.c
182,7 → 182,6
int heading_MM3(void)
//############################################################################
{
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;
208,6 → 207,7
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);
/branches/MicroMag3_Nick666/trunc/fc.c
200,7 → 200,6
HoeheD = 0;
Mess_Integral_Hoch = 0;
KompassStartwert = KompassValue;
GPS_Neutral();
beeptime = 50;
Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
432,7 → 431,7
Mittelwert();
 
GRN_ON;
//GRN_ON;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Gaswert ermitteln
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
496,7 → 495,7
{
if(++delay_neutral > 200) // nicht sofort
{
GRN_OFF;
//GRN_OFF;
MotorenEin = 0;
delay_neutral = 0;
modell_fliegt = 0;
912,12 → 911,26
KompassStartwert = KompassValue;
NeueKompassRichtungMerken = 0;
}
Mess_Integral_Gier += (KompassRichtung *Parameter_KompassWirkung); // nach Kompass ausrichten
Mess_Integral_Gier += (KompassRichtung * Parameter_KompassWirkung); // nach Kompass ausrichten
}
else beeptime = 50;
}
}
 
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// GPS
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)
{
GPS_P_Factor = Parameter_UserParam5;
GPS_D_Factor = Parameter_UserParam6;
GPS_Main(); // updates GPS_Pitch and GPS_Roll on new GPS data
}
else
{
GPS_Nick = 0;
GPS_Roll = 0;
}
 
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Debugwerte zuordnen
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
942,9 → 955,10
DebugOut.Analog[14] = Motor_Links;
DebugOut.Analog[15] = Motor_Rechts;
DebugOut.Analog[16] = Mittelwert_AccHoch;
 
DebugOut.Analog[17] = Parameter_UserParam5;
DebugOut.Analog[18] = Parameter_UserParam6;
/*
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;
/branches/MicroMag3_Nick666/trunc/fc.h
39,6 → 39,8
void DefaultKonstanten1(void);
void DefaultKonstanten2(void);
 
extern int MaxStickNick,MaxStickRoll;
extern unsigned char Notlandung;
extern unsigned char h,m,s;
extern volatile unsigned char Timeout ;
extern volatile int DiffNick,DiffRoll;
/branches/MicroMag3_Nick666/trunc/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="compass.c"></File><File path="compass.h"></File><File path="mymath.c"></File><File path="mymath.h"></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="compass.c"></File><File path="compass.h"></File><File path="mymath.c"></File><File path="mymath.h"></File><File path="ubx.c"></File><File path="ubx.h"></File></Project>
/branches/MicroMag3_Nick666/trunc/gps.h
1,14 → 1,15
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Nick;
extern signed int GPS_Roll;
#ifndef _GPS_H
#define _GPS_H
 
void GPS_Neutral(void);
void GPS_BerechneZielrichtung(void);
#include <inttypes.h>
 
extern int16_t GPS_Nick;
extern int16_t GPS_Roll;
 
extern uint8_t GPS_P_Factor;
extern uint8_t GPS_D_Factor;
 
extern void GPS_Main(void);
 
#endif //_GPS_H
 
/branches/MicroMag3_Nick666/trunc/main.h
75,8 → 75,8
#include "gps.h"
#include "compass.h"
#include "mymath.h"
#include "ubx.h"
 
 
#ifndef EEMEM
#define EEMEM __attribute__ ((section (".eeprom")))
#endif
/branches/MicroMag3_Nick666/trunc/makefile
75,7 → 75,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
SRC += twimaster.c rc.c fc.c gps.c ubx.c
SRC += compass.c mymath.c
##########################################################################################################
 
/branches/MicroMag3_Nick666/trunc/uart.c
101,7 → 101,10
static unsigned char UartState = 0;
unsigned char SioTmp;
 
SioTmp = UDR;
SioTmp = UDR;
if(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) ubx_parser(SioTmp); // GPS UBX-Protokoll Parser
if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0;
if(buf_ptr == 0)
{
/branches/MicroMag3_Nick666/trunc/ubx.c
0,0 → 1,234
#include "main.h"
 
// ubx protocol parser state machine
#define UBXSTATE_IDLE 0
#define UBXSTATE_SYNC1 1
#define UBXSTATE_SYNC2 2
#define UBXSTATE_CLASS 3
#define UBXSTATE_LEN1 4
#define UBXSTATE_LEN2 5
#define UBXSTATE_DATA 6
#define UBXSTATE_CKA 7
#define UBXSTATE_CKB 8
 
// ublox protocoll identifier
#define UBX_CLASS_NAV 0x01
#define UBX_ID_POSLLH 0x02
#define UBX_ID_POSUTM 0x08
#define UBX_ID_STATUS 0x03
#define UBX_ID_VELNED 0x12
#define UBX_SYNC1_CHAR 0xB5
#define UBX_SYNC2_CHAR 0x62
 
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
uint8_t GPSfix; // GPSfix Type, range 0..6
uint8_t Flags; // Navigation Status Flags
uint8_t DiffS; // Differential Status
uint8_t res; // reserved
uint32_t TTFF; // Time to first fix (millisecond time tag)
uint32_t MSSS; // Milliseconds since Startup / Reset
uint8_t Status;
} GPS_STATUS_t;
 
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t LON; // 1e-07 deg Longitude
int32_t LAT; // 1e-07 deg Latitude
int32_t HEIGHT; // mm Height above Ellipsoid
int32_t HMSL; // mm Height above mean sea level
uint32_t Hacc; // mm Horizontal Accuracy Estimate
uint32_t Vacc; // mm Vertical Accuracy Estimate
uint8_t Status;
} GPS_POSLLH_t;
 
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t EAST; // cm UTM Easting
int32_t NORTH; // cm UTM Nording
int32_t ALT; // cm altitude
uint8_t ZONE; // UTM zone number
uint8_t HEM; // Hemisphere Indicator (0=North, 1=South)
uint8_t Status;
} GPS_POSUTM_t;
 
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t VEL_N; // cm/s NED north velocity
int32_t VEL_E; // cm/s NED east velocity
int32_t VEL_D; // cm/s NED down velocity
uint32_t Speed; // cm/s Speed (3-D)
uint32_t GSpeed; // cm/s Ground Speed (2-D)
int32_t Heading; // 1e-05 deg Heading 2-D
uint32_t SAcc; // cm/s Speed Accuracy Estimate
uint32_t CAcc; // deg Course / Heading Accuracy Estimate
uint8_t Status;
} GPS_VELNED_t;
 
GPS_STATUS_t GpsStatus = {0,0,0,0,0,0,0, INVALID};
GPS_POSLLH_t GpsPosLlh = {0,0,0,0,0,0,0, INVALID};
GPS_POSUTM_t GpsPosUtm = {0,0,0,0,0,0, INVALID};
GPS_VELNED_t GpsVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
GPS_INFO_t GPSInfo = {0,0,0,0,0,0,0,0,0,0, INVALID};
 
volatile uint8_t GPSTimeout = 0;
 
void UpdateGPSInfo (void)
{
if (GpsStatus.Status == VALID) // valid packet
{
GPSInfo.satfix = GpsStatus.GPSfix;
GpsStatus.Status = PROCESSED; // never update old data
}
if (GpsPosLlh.Status == VALID) // valid packet
{
GPSInfo.longitude = GpsPosLlh.LON;
GPSInfo.latitude = GpsPosLlh.LAT;
GPSInfo.altitude = GpsPosLlh.HEIGHT;
GpsPosLlh.Status = PROCESSED; // never update old data
}
if (GpsPosUtm.Status == VALID) // valid packet
{
GPSInfo.utmeast = GpsPosUtm.EAST;
GPSInfo.utmnorth = GpsPosUtm.NORTH;
GPSInfo.utmalt = GpsPosUtm.ALT;
GpsPosUtm.Status = PROCESSED; // never update old data
}
if (GpsVelNed.Status == VALID) // valid packet
{
GPSInfo.veleast = GpsVelNed.VEL_E;
GPSInfo.velnorth = GpsVelNed.VEL_N;
GPSInfo.veltop = -GpsVelNed.VEL_D;
GpsVelNed.Status = PROCESSED; // never update old data
}
if (GpsStatus.Status != INVALID)
{
GPSInfo.status = VALID; // set valid if data are updated
}
}
 
 
// this function should be called within the UART RX ISR
void ubx_parser(uint8_t c)
{
static uint8_t ubxstate = UBXSTATE_IDLE;
static uint8_t cka, ckb;
static uint16_t msglen;
static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
 
switch(ubxstate)
{
case UBXSTATE_IDLE: // check 1st sync byte
if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1;
else ubxstate = UBXSTATE_IDLE; // out of synchronization
break;
 
case UBXSTATE_SYNC1: // check 2nd sync byte
if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2;
else ubxstate = UBXSTATE_IDLE; // out of synchronization
break;
 
case UBXSTATE_SYNC2: // check msg class to be NAV
if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS;
else ubxstate = UBXSTATE_IDLE; // unsupported message class
break;
 
case UBXSTATE_CLASS: // check message identifier
switch(c)
{
case UBX_ID_POSUTM: // utm position
ubxP = (int8_t *)&GpsPosUtm; // data start pointer
ubxEp = (int8_t *)(&GpsPosUtm + sizeof(GPS_POSUTM_t)); // data end pointer
ubxSp = (int8_t *)&GpsPosUtm.Status; // status pointer
break;
 
case UBX_ID_POSLLH: // geodetic position
ubxP = (int8_t *)&GpsPosLlh; // data start pointer
ubxEp = (int8_t *)(&GpsPosLlh + sizeof(GPS_POSLLH_t)); // data end pointer
ubxSp = (int8_t *)&GpsPosLlh.Status; // status pointer
break;
 
case UBX_ID_STATUS: // receiver status
ubxP = (int8_t *)&GpsStatus; // data start pointer
ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer
ubxSp = (int8_t *)&GpsStatus.Status; // status pointer
break;
 
case UBX_ID_VELNED: // velocity vector in tangent plane
ubxP = (int8_t *)&GpsVelNed; // data start pointer
ubxEp = (int8_t *)(&GpsVelNed + sizeof(GPS_VELNED_t)); // data end pointer
ubxSp = (int8_t *)&GpsVelNed.Status; // status pointer
break;
 
default: // unsupported identifier
ubxstate = UBXSTATE_IDLE;
break;
}
if (ubxstate != UBXSTATE_IDLE)
{
ubxstate = UBXSTATE_LEN1;
cka = UBX_CLASS_NAV + c;
ckb = UBX_CLASS_NAV + cka;
}
break;
 
case UBXSTATE_LEN1: // 1st message length byte
msglen = c;
cka += c;
ckb += cka;
ubxstate = UBXSTATE_LEN2;
break;
 
case UBXSTATE_LEN2: // 2nd message length byte
msglen += ((uint16_t)c)<<8;
cka += c;
ckb += cka;
// if the old data are not processed so far then break parsing now
// to avoid writing new data in ISR during reading by another function
if ( *ubxSp == VALID ) ubxstate = UBXSTATE_IDLE;
else // data invalid or allready processd
{
*ubxSp = INVALID;
ubxstate = UBXSTATE_DATA;
}
break;
 
case UBXSTATE_DATA:
if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left
cka += c;
ckb += cka;
if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read
break;
 
case UBXSTATE_CKA:
if (c == cka) ubxstate = UBXSTATE_CKB;
else
{
*ubxSp = INVALID;
ubxstate = UBXSTATE_IDLE;
}
break;
 
case UBXSTATE_CKB:
if (c == ckb)
{
*ubxSp = VALID; // new data are valid
ROT_FLASH;
UpdateGPSInfo(); //update GPS info respectively
GPSTimeout = 255;
}
else
{ // if checksum not fit then set data invalid
*ubxSp = INVALID;
}
ubxstate = UBXSTATE_IDLE; // ready to parse new data
break;
 
default: // unknown ubx state
ubxstate = UBXSTATE_IDLE;
break;
}
 
}
 
 
/branches/MicroMag3_Nick666/trunc/ubx.h
0,0 → 1,53
#ifndef _UBX_H
#define _UBX_H
 
#include <inttypes.h>
 
#define INVALID 0x00
#define VALID 0x01
#define PROCESSED 0x02
 
 
#define SATFIX_NONE 0x00
#define SATFIX_DEADRECKOING 0x01
#define SATFIX_2D 0x02
#define SATFIX_3D 0x03
#define SATFIX_GPS_DEADRECKOING 0x04
#define SATFIX_TIMEONLY 0x05
 
 
/* enable the UBX protocol at the gps receiver with the following messages enabled
01-02 NAV - POSLLH
01-03 NAV - STATUS
01-08 NAV - POSUTM
01-12 NAV - VELNED */
 
typedef struct
{
uint8_t satfix; // type of satfix
int32_t utmnorth; // in cm (+ = north)
int32_t utmeast; // in cm (+ = east)
int32_t utmalt; // in cm (+ = top)
int32_t velnorth; // in cm/s
int32_t veleast; // in cm/s
int32_t veltop; // in cm/s
int32_t longitude; // in 1e-07 deg
int32_t latitude; // in 1e-07 deg
int32_t altitude; // in mm
uint8_t status; // status of data: invalid | valid
} GPS_INFO_t;
 
 
//here you will find the current gps info
extern GPS_INFO_t GPSInfo; // measured position (last gps record)
 
// this variable should be decremted by the application
extern volatile uint8_t GPSTimeout; // is reset to 255 if a new UBX msg was received
 
// initialized the upx parser
extern void ubx_init(void);
 
// this function should be called within the UART RX ISR
extern void ubx_parser(uint8_t c);
 
#endif //_UBX_H