HMC5883L - FabLabSeoul/WingProject GitHub Wiki

HMC5883L์€ HoneyWell ์‚ฌ๊ฐ€ ๋งŒ๋“  3์ถ• ์ง€์ž๊ธฐ ์„ผ์„œ๋‹ค.

์ง€์ž๊ธฐ ์„ผ์„œ๋Š” ์ง€๊ตฌ์˜ ์ž๊ธฐ์žฅ์„ ์ด์šฉํ•ด ๋ฐฉํ–ฅ์„ ์ธก์ •ํ•œ๋‹ค. ๋•Œ๋ฌธ์— ์ฃผ์œ„์— ์ „์ž๊ธฐ๊ธฐ๊ฐ€ ์žˆ๊ฑฐ๋‚˜, ์ž๊ธฐ๋ ฅ์„ ์ผ์œผํ‚ค๋Š” ๋ฌผ๊ฑด์ด ์žˆ๋‹ค๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. 3์ถ• ์ง€์ž๊ธฐ ์„ผ์„œ๋Š” x,y,z์ถ•์˜ ์ž๊ธฐ์žฅ์˜ ํฌ๊ธฐ(guass)๋ฅผ ์ธก์ •ํ•œ๋‹ค.

HMC5883L์€ MPU-6050์—์„œ ๊ณ„์‚ฐํ•˜๊ธฐ ํž˜๋“  Yaw๊ฐ์„ ์•Œ์•„์˜ค๋Š” ์šฉ๋„๋กœ ์“ฐ์ธ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ MPU-6050 ์„ผ์„œ์˜ ์˜จ๋„ํŠน์„ฑ์— ๋”ฐ๋ฅธ ๋“œ๋ฆฌํ”„ํŠธ ํŠน์„ฑ์„ ๊ฐ์•ˆํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด MPU-6050์œผ๋กœ๋„ Yaw๊ฐ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

HMC5883L๋„ MPU-6050๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ I2Cํ†ต์‹ ์œผ๋กœ ์ •๋ณด๋ฅผ ์–ป๊ฑฐ๋‚˜ ์“ฐ๊ณ , ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๋กœ ์›ํ•˜๋Š” ์ •๋ณด์— ์ ‘๊ทผํ•œ๋‹ค. ์ง€์ž๊ธฐ ์„ผ์„œ์˜ x,y,z์ถ•์˜ ๋‹จ์œ„๋Š” gauss๋ฅผ ์‚ฌ์šฉํ•˜๋‚˜, ์„ผ์„œ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๋ฐฉํ–ฅ์„ ์•Œ๊ธฐ์œ„ํ•ด์„œ๋Š” ๋‹จ์œ„๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ , atan2(my, mx) ๊ณต์‹์„ ์ด์šฉํ•ด ๋ฐ”๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๊ธ€์€ Stm32 Value line discovery๋ณด๋“œ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๊ธ€์ด๋‹ค.

Stm32 Value line discovery ๋ณด๋“œ๋กœ ์ง€์ž๊ธฐ ์„ผ์„œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ฝ”๋“œ๋Š” STM32\stm32vldiscovery_package\Project\Examples\HMC5883L ๊ฒฝ๋กœ์— ์žˆ๋‹ค.

Arduino๋กœ ์ง€์ž๊ธฐ ์„ผ์„œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ฝ”๋“œ๋Š” Arduino\Adafruit_HMC5883_Unified-master\examples\magsensor ๊ฒฝ๋กœ์— ์žˆ๋‹ค.

HMC5883L ์„ผ์„œ๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ์‹œํ—˜ํ•ด๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” Stm32๋กœ ํ•  ๊ฒฝ์šฐ, HMC5883L ํ”„๋กœ์ ํŠธ๋กœ Stm32๋ฅผ ์‹คํ–‰ํ•˜๊ณ , PB11-SCL, PB10-SDA ํ•€์„ HMC5883L์„ผ์„œ์™€ ์—ฐ๊ฒฐํ•œ๋‹ค. ์‹œ๋ฆฌ์–ผํ†ต์‹ ์œผ๋กœ ์„ผ์„œ ๊ฐ’์„ ๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์—, Processing ํ”„๋กœ์ ํŠธ HMC5883L_test_stm32๋ฅผ ์‹คํ–‰ํ•˜๋ฉด HMC5883L ์˜ ์ง€์ž๊ธฐ์„ผ์„œ x,y,z๋ฅผ ์ฝ์–ด๋“ค์—ฌ 3D๋กœ ๋‚˜์นจ๋ฐ˜์„ ์ถœ๋ ฅํ•œ๋‹ค.

Stm32 Serial Debugging๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด Stm32๋กœ ๋ฐ”๋กœ ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ ์ˆ˜์žˆ๋‹ค.

์•„๋‘์ด๋…ธ๋กœ HMC5883L ์„ผ์„œ๋ฅผ ์‹œํ—˜ํ•  ๊ฒฝ์šฐ, ์•„๋‘์ด๋…ธ ํ”„๋กœ์ ํŠธ magsensor๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ด ๋•Œ HMC5883L๊ณผ I2Cํ†ต์‹ ํ•  ํฌํŠธ A4-SDA, A5-SCL์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜์—ˆ๋‹ค๋ฉด, ์ง€์ž๊ธฐ ์„ผ์„œ์ •๋ณด๊ฐ€ ์‹œ๋ฆฌ์–ผ์„ ํ†ตํ•ด PC๋กœ ์ „๋‹ฌ๋˜๊ฒŒ ๋œ๋‹ค. ๊ทธ ํ›„ Processing ํ”„๋กœ์ ํŠธ HMC5883L_test_arduino๋ฅผ ์—ด์–ด ์‹คํ–‰ํ•˜๋ฉด 3D ๋‚˜์นจ๋ฐ˜์ด ์ถœ๋ ฅ๋œ๋‹ค.

GY-271 ๋ณด๋“œ๋Š” SCL, SDA ํฌํŠธ์— ํ’€์—…์ €ํ•ญ์ด ๋ถ™์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋”ฐ๋กœ ๋‹ฌ์•„์ค„ ํ•„์š” ์—†๋‹ค. ๋‚˜๋จธ์ง€ ๋ณด๋“œ๋“ค์€ ํšŒ๋กœ๋„๋ฅผ ํ™•์ธํ•˜๊ณ  ํ’€์—…์ €ํ•ญ์„ ๋‹ฌ์ž.

Stm32 ์†Œ์Šค๋Š” https://harinadha.wordpress.com/2012/05/24/hmc5883llib/ ๋ฅผ ์ด์šฉํ–ˆ๋‹ค. MPU-6050 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•œ ์‚ฌ๋žŒ๊ณผ ๋™์ผํ•˜๋‹ค. ์ด ์ฝ”๋“œ์˜ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋Š” while()๋ฌธ์œผ๋กœ ACK๊ฐ€ ์˜ฌ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋งŒ์•ฝ ์„ผ์„œ์˜ I2C๋ผ์ธ์ด ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋Š์–ด์งˆ ๊ฒฝ์šฐ, ๋งค์ธ ํ”„๋กœ๊ทธ๋žจ์€ ๋ฉˆ์ถฐ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ์ฝ”๋“œ๋Š” HMC5883L์„ผ์„œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์“ฐ์ด๊ณ , ์‹ค์ œ ํ๋ธŒ๋ฅผ ๋‚ ๋ฆด ๋•Œ๋Š” ์“ฐ์ง€ ์•Š์„ ๊ณ„ํš์ด๋‹ค. (multiwii์˜ baseflight ํ”„๋กœ์ ํŠธ๋Š” ๋ฌดํ•œ์ • ๋Œ€๊ธฐํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ด๋ฒคํŠธ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.)

HMC5883L ์ดˆ๊ธฐํ™”

HMC5883L_I2C_Init()ํ•จ์ˆ˜์—์„œ๋Š” HMC5883L๊ณผ ํ†ต์‹ ์„ ์œ„ํ•œ I2CํฌํŠธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.(PB10-SCL, PB11-SDA) ๊ทธ๋ฆฌ๊ณ  SerialSetup()ํ•จ์ˆ˜์—์„œ ์„ผ์„œ๋กœ๋ถ€ํ„ฐ ์–ป์–ด ์˜จ ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๋ฆฌ์–ผํ†ต์‹  ํฌํŠธ PA10-Rx, PA9-Tx๋ฅผ ์„ค์ •ํ•œ๋‹ค. InitSysTick()ํ•จ์ˆ˜๋Š” GetTickCount()ํ•จ์ˆ˜๋ฅผ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.

HMC5883L_Initialize() ํ•จ์ˆ˜์—์„œ +-1.3 Gauss ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•˜๊ณ , ์‹ฑ๊ธ€๋ชจ๋“œ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ํ•œ๋‹ค. ์ด ์˜ต์…˜๋“ค์€ ๋ชจ๋‘ ๊ธฐ๋ณธ์„ค์ •์‚ฌํ•ญ์ด๋‹ค.

#define HMC5883L_I2C                  I2C2
#define HMC5883L_I2C_RCC_Periph       RCC_APB1Periph_I2C2
#define HMC5883L_I2C_Port             GPIOB
#define HMC5883L_I2C_SCL_Pin          GPIO_Pin_10
#define HMC5883L_I2C_SDA_Pin          GPIO_Pin_11
#define HMC5883L_I2C_RCC_Port         RCC_APB2Periph_GPIOB
#define HMC5883L_I2C_Speed            100000

void setup()
{
	InitSysTick();
	SerialSetup();
	
	HMC5883L_I2C_Init();
	HMC5883L_Initialize();	
}

/**
 * @brief  Initializes the I2C peripheral used to drive the HMC5883L
 * @param  None
 * @retval None
 */
void HMC5883L_I2C_Init()
{
    I2C_InitTypeDef I2C_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable I2C and GPIO clocks */
    RCC_APB1PeriphClockCmd(HMC5883L_I2C_RCC_Periph, ENABLE);
    RCC_APB2PeriphClockCmd(HMC5883L_I2C_RCC_Port, ENABLE);

    /* Configure I2C pins: SCL and SDA */
    GPIO_InitStructure.GPIO_Pin = HMC5883L_I2C_SCL_Pin | HMC5883L_I2C_SDA_Pin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_Init(HMC5883L_I2C_Port, &GPIO_InitStructure);

    /* I2C configuration */
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_OwnAddress1 = HMC5883L_DEFAULT_ADDRESS; // HMC5883L 7-bit adress = 0x1E;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = HMC5883L_I2C_Speed;

    /* Apply I2C configuration after enabling it */
    I2C_Init(HMC5883L_I2C, &I2C_InitStructure);

    I2C_Cmd(HMC5883L_I2C, ENABLE);
}

/** Power on and prepare for general usage.
 * This will prepare the magnetometer with default settings, ready for single-
 * use mode (very low power requirements). Default settings include 8-sample
 * averaging, 15 Hz data output rate, normal measurement bias, a,d 1090 gain (in
 * terms of LSB/Gauss). Be sure to adjust any settings you need specifically
 * after initialization, especially the gain settings if you happen to be seeing
 * a lot of -4096 values (see the datasheet for mor information).
 */
void HMC5883L_Initialize()
{
    // write CONFIG_A register

    uint8_t tmp = (HMC5883L_AVERAGING_8 << (HMC5883L_CRA_AVERAGE_BIT - HMC5883L_CRA_AVERAGE_LENGTH + 1))
            | (HMC5883L_RATE_15 << (HMC5883L_CRA_RATE_BIT - HMC5883L_CRA_RATE_LENGTH + 1))
            | (HMC5883L_BIAS_NORMAL << (HMC5883L_CRA_BIAS_BIT - HMC5883L_CRA_BIAS_LENGTH + 1));
    HMC5883L_I2C_ByteWrite(HMC5883L_DEFAULT_ADDRESS, &tmp, HMC5883L_RA_CONFIG_A);

    // write CONFIG_B register
    HMC5883L_SetGain(HMC5883L_GAIN_1090);

    // write MODE register
    HMC5883L_SetMode(HMC5883L_MODE_SINGLE);
}

 void SerialSetup(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// RCC Configuration
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	

	// Port ์„ค์ •
	// PA9  - Tx
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// PA10  - Rx
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// UART Port ์„ค์ •
	USART_InitStructure.USART_BaudRate    = 19200;
	USART_InitStructure.USART_WordLength   = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits    = USART_StopBits_1;
	USART_InitStructure.USART_Parity    = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode    = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART1, &USART_InitStructure);
	USART_Cmd(USART1, ENABLE);
}

HMC5883L ์„ผ์„œ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

์ดํ•ดํ•  ์ˆ˜ ์—†๋Š” ์ฝ”๋“œ๊ฐ€ ๋ช‡ ๊ตฐ๋ฐ ์žˆ์ง€๋งŒ, ๋„˜์–ด๊ฐ€๊ณ  HMC5883L_I2C_BufferRead()ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. MPU-6050์„ผ์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์ฝ์–ด ์˜ฌ HMC5883L ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๋ฅผ ๋จผ์ €๋ณด๋‚ด๊ณ , ์—ฌ์„ฏ ๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ Readํ•ด์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๋Š” HMC5883L_RA_DATAX_H์—์„œ ๋ถ€ํ„ฐ ์—ฌ์„ฏ ๋ฒˆ ์ฝ์–ด์˜จ๋‹ค. ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๋Š” HAL_HMC5883L.h ํ—ค๋”ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ ๋˜์–ด ์žˆ๋‹ค.

Y์ถ•๊ณผ Z์ถ• ๋ ˆ์ง€์Šคํ„ฐ์ฃผ์†Œ๊ฐ€ ์„œ๋กœ ๋ฐ”๊ผˆ์ง€๋งŒ, ์—ฌ์„ฏ ๋ฒˆ์„ ์ˆœ์„œ๋Œ€๋กœ ์ฝ๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ณด๊ฐ€ ๋น ์ง€์ง€๋Š” ์•Š๋Š”๋‹ค.

#define HMC5883L_RA_CONFIG_A        0x00
#define HMC5883L_RA_CONFIG_B        0x01
#define HMC5883L_RA_MODE            0x02
#define HMC5883L_RA_DATAX_H         0x03
#define HMC5883L_RA_DATAX_L         0x04
#define HMC5883L_RA_DATAY_H         0x05
#define HMC5883L_RA_DATAY_L         0x06
#define HMC5883L_RA_DATAZ_H         0x07
#define HMC5883L_RA_DATAZ_L         0x08
#define HMC5883L_RA_STATUS          0x09
#define HMC5883L_RA_ID_A            0x0A
#define HMC5883L_RA_ID_B            0x0B
#define HMC5883L_RA_ID_C            0x0C

์‹ค์ œ ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

int16_t  MagneticHeading[3]={0};
HMC5883L_GetHeading(MagneticHeading);

/** Get 3-axis heading measurements.
 * In the event the ADC reading overflows or underflows for the given channel,
 * or if there is a math overflow during the bias measurement, this data
 * register will contain the value -4096. This register value will clear when
 * after the next valid measurement is made. Note that this method automatically
 * clears the appropriate bit in the MODE register if Single mode is active.
 * @param x 16-bit signed integer container for X,Y,Z-axis heading
 * @see HMC5883L_RA_DATAX_H
 */
void HMC5883L_GetHeading(s16* Mag)
{
    int i=0;
    uint8_t tmp;
    uint8_t tmpbuff[6] = { 0 };
    HMC5883L_I2C_BufferRead(HMC5883L_DEFAULT_ADDRESS, tmpbuff, HMC5883L_RA_DATAX_H, 6);

    tmp = HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1);

    if (HMC5883Lmode == HMC5883L_MODE_SINGLE)
        HMC5883L_I2C_ByteWrite(HMC5883L_DEFAULT_ADDRESS, &tmp, HMC5883L_RA_MODE);
    for (i = 0; i < 3; i++)
        Mag[i] = ((s16) ((u16) tmpbuff[2 * i] << 8) + tmpbuff[2 * i + 1]);
}

HMC5883L.h ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์„ผ์„œ์ •๋ณด๋ฅผ ์ด๋ ‡๊ฒŒ ๊ฐ€์ ธ์˜ค๊ณ  ๋๋‚œ๋‹ค. ์ด ๊ฐ’์€ gauss ๋‹จ์œ„์ด์ง€๋งŒ, ์‹ค์ œ ๊ฐ’์€ ์•„๋‹ˆ๊ณ , Scale Factor๋กœ ๋‚˜๋ˆ ์ค˜์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ์„ผ์„œ๊ฐ€ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ๋ฐฉํ–ฅ๊ฐ๋งŒ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด gauss ํ˜•๋ณ€ํ™˜ ์—†์ด ๋ฐ”๋กœ atan2(my, mx)๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ ๊ฐ์€ ์‹ค์ œ ๋ฐฉํ–ฅ์„ ๊ตฌํ•˜๊ธฐ์œ„ํ•ด์„œ ํŽธ๊ฐ(declination)์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์ค˜์•ผ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ž๊ธฐ์žฅ์ด ๊ฐ€๋ฅดํ‚ค๋Š” ๋ถ๊ทน์€ ์‹ค์ œ ๋ถ๊ทนํ•˜๊ณ  ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Main ํ•จ์ˆ˜

HMC5883L_GetHeading()ํ•จ์ˆ˜๋กœ ์„ผ์„œ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ๋ฐ”๋กœ SendSerialMagnetometer()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‹œ๋ฆฌ์–ผํ†ต์‹ ์œผ๋กœ ๋ณด๋‚ธ๋‹ค. x,z,y ์ˆœ์„œ๋Œ€๋กœ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์ด๋Š” HMC5883L ์„ผ์„œ์˜ ๋ ˆ์ง€์Šคํ„ฐ ์ฃผ์†Œ ์ˆœ์„œ ๋•Œ๋ฌธ์ด๋‹ค.

์„ผ์„œ๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์„ ๊ตฌํ•  ๋•Œ, atan2(my, mx)๋กœ ๊ณ„์‚ฐํ•˜๋Š” ์ด์œ ๋Š” MEMS ์„ผ์„œ๋ฅผ ์ด์šฉํ•œ ๋ฌด์ธํ•ญ๊ณต๊ธฐ์šฉ ์ž์„ธ์ธก์ •์žฅ์น˜์˜ ํŠน์„ฑ์— ๋Œ€ํ•œ ์—ฐ๊ตฌ.pdf ๋ฌธ์„œ์— ์ž˜ ๋‚˜์™€ ์žˆ๋‹ค. STM32\pdf\MEMS ์„ผ์„œ๋ฅผ ์ด์šฉํ•œ ๋ฌด์ธํ•ญ๊ณต๊ธฐ์šฉ.pdf ๊ฒฝ๋กœ์— ์žˆ๋‹ค.

int main()
{
	int curTick=0, oldTick=0;
	int16_t  MagneticHeading[3]={0};
	
	setup();
	
	if( HMC5883L_TestConnection())
	{
		 // connection success
	}
	else
	{
		 // connection failed
		return 1;
	}
	
	while(1)
	{
		curTick = GetTickCount();

		if (curTick - oldTick > 50)
		{
			HMC5883L_GetHeading(MagneticHeading);
			
			SendSerialMagnetometer(MagneticHeading);
		
			oldTick = curTick;
		}
	}
}

// ์ง€์ž๊ธฐ ์„ผ์„œ ์ •๋ณด๋ฅผ ์‹œ๋ฆฌ์–ผํ†ต์‹ ์œผ๋กœ ๋ณด๋‚ธ๋‹ค.
void SendSerialMagnetometer( int16_t magnetometer[3] )
{
	int16_t mag_x = magnetometer[0];
	int16_t mag_y = magnetometer[2];
	int16_t mag_z = magnetometer[1];	
	
	print_byte( 'S' );
	print_short( mag_x );
	print_short( mag_y );
	print_short( mag_z );	
}


๋ ˆํผ๋Ÿฐ์Šค