Rev 19 |
Rev 21 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*#######################################################################################
MK3Mag 3D-Magnet sensor
!!! THIS IS NOT FREE SOFTWARE !!!
#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 05.2008 Holger Buss
// + Thanks to Ilja Fähnrich (P_Latzhalter)
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
// + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// + from this software without specific prior written permission.
// + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// + for non-commercial use (directly or indirectly)
// + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// + with our written permission
// + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
// + In case of doubt please contact: info@MikroKopter.de
// + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
// + clearly linked as origin
// + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <avr/interrupt.h>
#include <math.h>
#include <stdlib.h>
#include "main.h"
#include "timer0.h"
#include "twislave.h"
#include "led.h"
#include "analog.h"
#include "uart.h"
int16_t RawMagnet1a
, RawMagnet1b
; // raw AD-Data
int16_t RawMagnet2a
, RawMagnet2b
;
int16_t RawMagnet3a
, RawMagnet3b
;
typedef struct
{
int16_t Range
;
int16_t Offset
;
} Scaling_t
;
typedef struct
{
Scaling_t X
;
Scaling_t Y
;
Scaling_t Z
;
} Calibration_t
;
Calibration_t eeCalibration EEMEM
; // calibration data in EEProm
Calibration_t Calibration
; // calibration data in RAM
int16_t UncalMagnetX
, UncalMagnetY
, UncalMagnetZ
; // sensor signal difference without Scaling
int16_t MagnetX
, MagnetY
, MagnetZ
; // rescaled magnetic field readings
uint8_t PC_Connected
= 0;
int16_t Heading
;
void CalcFields
(void)
{
UncalMagnetX
= (1 * UncalMagnetX
+ (RawMagnet1a
- RawMagnet1b
)) / 2;
UncalMagnetY
= (1 * UncalMagnetY
+ (RawMagnet3a
- RawMagnet3b
)) / 2;
UncalMagnetZ
= (1 * UncalMagnetZ
+ (RawMagnet2a
- RawMagnet2b
)) / 2;
MagnetX
= (1024L * (int32_t)(UncalMagnetX
- Calibration.
X.
Offset)) / (Calibration.
X.
Range);
MagnetY
= (1024L * (int32_t)(UncalMagnetY
- Calibration.
Y.
Offset)) / (Calibration.
Y.
Range);
MagnetZ
= (1024L * (int32_t)(UncalMagnetZ
- Calibration.
Z.
Offset)) / (Calibration.
Z.
Range);
}
void CalcHeading
(void)
{
double nick_rad
, roll_rad
, Hx
, Hy
, Cx
, Cy
, Cz
;
int16_t heading
= -1;
Cx
= MagnetX
;
Cy
= MagnetY
;
Cz
= MagnetZ
;
if(ExternData.
Orientation == 1)
{
Cx
= MagnetX
;
Cy
= -MagnetY
;
Cz
= MagnetZ
;
}
// calculate nick and roll angle in rad
nick_rad
= ((double)ExternData.
Attitude[NICK
]) * M_PI
/ (double)(1800.0);
roll_rad
= ((double)ExternData.
Attitude[ROLL
]) * M_PI
/ (double)(1800.0);
// calculate attitude correction
Hx
= Cx
* (double)cos(nick_rad
) - Cz
* (double)sin(nick_rad
);
Hy
= Cy
* (double)cos(roll_rad
) + Cz
* (double)sin(roll_rad
);
// calculate Heading
heading
= (int16_t)((180.0 * atan2(Hy
, Hx
)) / M_PI
);
// atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
// but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
if (heading
< 0) heading
= -heading
;
else heading
= 360 - heading
;
if(abs(heading
) < 361) Heading
= heading
;
else (Heading
= -1);
}
void Calibrate
(void)
{
uint8_t cal
;
static int16_t Xmin
= 0, Xmax
= 0, Ymin
= 0, Ymax
= 0, Zmin
= 0, Zmax
= 0;
// check both sources of communication for calibration request
if(I2C_WriteCal.
CalByte) cal
= I2C_WriteCal.
CalByte;
else cal
= ExternData.
CalState;
// calibration state machine
switch(cal
)
{
case 0: // no calibration
LED_GRN_ON
;
break;
case 1: // 1st step of calibration
// initialize ranges
// used to change the orientation of the MK3MAG in the horizontal plane
LED_GRN_OFF
;
Xmin
= 10000;
Xmax
= -10000;
Ymin
= 10000;
Ymax
= -10000;
Zmin
= 10000;
Zmax
= -10000;
break;
case 2: // 2nd step of calibration
// find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
LED_GRN_ON
;
if(UncalMagnetX
< Xmin
) Xmin
= UncalMagnetX
;
if(UncalMagnetX
> Xmax
) Xmax
= UncalMagnetX
;
if(UncalMagnetY
< Ymin
) Ymin
= UncalMagnetY
;
if(UncalMagnetY
> Ymax
) Ymax
= UncalMagnetY
;
break;
case 3: // 3rd step of calibration
// used to change the orietation of the MK3MAG vertical to the horizontal plane
LED_GRN_OFF
;
break;
case 4:
// find Min and Max of the Z-Sensor
LED_GRN_ON
;
if(UncalMagnetZ
< Zmin
) Zmin
= UncalMagnetZ
;
if(UncalMagnetZ
> Zmax
) Zmax
= UncalMagnetZ
;
break;
case 5:
LED_GRN_OFF
; // Save values
Calibration.
X.
Range = Xmax
- Xmin
;
Calibration.
X.
Offset = (Xmin
+ Xmax
) / 2;
Calibration.
Y.
Range = Ymax
- Ymin
;
Calibration.
Y.
Offset = (Ymin
+ Ymax
) / 2;
Calibration.
Z.
Range = Zmax
- Zmin
;
Calibration.
Z.
Offset = (Zmin
+ Zmax
) / 2;
if((Calibration.
X.
Range > 150) && (Calibration.
Y.
Range > 150) && (Calibration.
Z.
Range > 150))
{
eeprom_write_block
(&Calibration
, &eeCalibration
, sizeof(Calibration_t
));
Delay_ms
(2000);
}
LED_GRN_ON
;
break;
default:
LED_GRN_ON
;
break;
}
}
void SetDebugValues
(void)
{
DebugOut.
Analog[0] = MagnetX
;
DebugOut.
Analog[1] = MagnetY
;
DebugOut.
Analog[2] = MagnetZ
;
DebugOut.
Analog[3] = UncalMagnetX
;
DebugOut.
Analog[4] = UncalMagnetY
;
DebugOut.
Analog[5] = UncalMagnetZ
;
DebugOut.
Analog[6] = ExternData.
Attitude[NICK
];
DebugOut.
Analog[7] = ExternData.
Attitude[ROLL
];
DebugOut.
Analog[8] = Calibration.
X.
Offset;
DebugOut.
Analog[9] = Calibration.
X.
Range;
DebugOut.
Analog[10] = Calibration.
Y.
Offset;
DebugOut.
Analog[11] = Calibration.
Y.
Range;
DebugOut.
Analog[12] = Calibration.
Z.
Offset;
DebugOut.
Analog[13] = Calibration.
Z.
Range;
DebugOut.
Analog[14] = ExternData.
CalState;
DebugOut.
Analog[15] = Heading
;
DebugOut.
Analog[16] = ExternData.
UserParam[0];
DebugOut.
Analog[17] = ExternData.
UserParam[1];
}
int main
(void)
{
Led_Init
();
LED_GRN_ON
;
TIMER0_Init
();
USART0_Init
();
ADC_Init
();
I2C_Init
();
sei
(); //Globale Interrupts Einschalten
Debug_Timer
= SetDelay
(100); // Sendeintervall
// read calibration info from eeprom
eeprom_read_block
(&Calibration
, &eeCalibration
, sizeof(Calibration_t
));
ExternData.
Orientation = 0;
ExternData.
CalState = 0;
I2C_WriteCal.
CalByte = 0;
// main loop
while (1)
{
FLIP_LOW
;
Delay_ms
(2);
RawMagnet1a
= ADC_GetValue
(ADC0
);
RawMagnet2a
= -ADC_GetValue
(ADC1
);
RawMagnet3a
= ADC_GetValue
(ADC7
);
Delay_ms
(1);
FLIP_HIGH
;
Delay_ms
(2);
RawMagnet1b
= ADC_GetValue
(ADC0
);
RawMagnet2b
= -ADC_GetValue
(ADC1
);
RawMagnet3b
= ADC_GetValue
(ADC7
);
Delay_ms
(1);
CalcFields
();
if(ExternData.
CalState || I2C_WriteCal.
CalByte) Calibrate
();
else CalcHeading
();
// check data from USART
USART0_ProcessRxData
();
if(PC_Connected
)
{
USART0_EnableTXD
();
USART0_TransmitTxData
();
PC_Connected
--;
}
else
{
USART0_DisableTXD
();
}
} // while(1)
}