Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/*
2
 * HMC.cpp - Interface a Honeywell HMC5843 magnetometer to an AVR via i2c
3
 * Version 0.1 - http://www.timzaman.com/
4
 * Copyright (c) 2011 Tim Zaman
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
17
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include "HMC.h"
30
#include "WProgram.h"
31
#include <Wire.h>
32
 
33
/* PUBLIC METHODS */
34
 
35
HMC5843::HMC5843()
36
{
37
}
38
 
39
void HMC5843::init()
40
{
41
}
42
 
43
// This can be called at 100ms intervals to get new data
44
void HMC5843::getValues(int *x, int *y, int *z)
45
{
46
        Wire.begin();       //Initiate the Wire library and join the I2C bus as a master
47
        int regb=0x01;
48
        int regbdata=0x40;
49
        int outputData[6];
50
        int i;
51
    double angle;
52
 
53
 
54
    Wire.beginTransmission(HMC5883_WriteAddress);
55
    Wire.send(regb);
56
    Wire.send(regbdata);
57
    Wire.endTransmission();
58
 
59
    delay(1000);
60
    Wire.beginTransmission(HMC5883_WriteAddress); //Initiate a transmission with HMC5883 (Write address).
61
    Wire.send(HMC5883_ModeRegisterAddress);       //Place the Mode Register Address in send-buffer.
62
    Wire.send(HMC5883_ContinuousModeCommand);     //Place the command for Continuous operation Mode in send-buffer.
63
    Wire.endTransmission();                       //Send the send-buffer to HMC5883 and end the I2C transmission.
64
    delay(100);
65
 
66
 
67
    Wire.beginTransmission(HMC5883_WriteAddress);  //Initiate a transmission with HMC5883 (Write address).
68
    Wire.requestFrom(HMC5883_WriteAddress,6);      //Request 6 bytes of data from the address specified.
69
 
70
    delay(500);
71
 
72
 
73
    //Read the value of magnetic components X,Y and Z
74
 
75
    if(6 <= Wire.available()) // If the number of bytes available for reading be <=6.
76
    {
77
        for(i=0;i<6;i++)
78
        {
79
            outputData[i]=Wire.receive();  //Store the data in outputData buffer
80
        }
81
    }
82
 
83
    *x=outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
84
    *z=outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
85
    *y=outputData[4] << 8 | outputData[5]; //Combine MSB and LSB of Y Data output register
86
}
87
 
88
void HMC5843::getAngle(int *a)
89
{
90
        int fx,fy,fz;
91
        getValues(&fx,&fy,&fz);
92
        *a= atan((double)fy/(double)fx)*(360/PI); // angle in degrees
93
}
94
 
95
 
96
 
97
// Set the default object
98
HMC5843 HMC = HMC5843();
99
 
100
 
101
 
102
 
103
 
104
 
105
 
106
 
107
 
108