-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiTechnicCompass.cpp
64 lines (56 loc) · 1.68 KB
/
HiTechnicCompass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "HiTechnicCompass.h"
#include "DigitalModule.h"
#include "I2C.h"
#include "Utility.h"
#include "WPIStatus.h"
/**
* Constructor.
*
* @param slot The slot of the digital module that the sensor is plugged into.
*/
HiTechnicCompass::HiTechnicCompass(UINT32 slot)
: m_i2c (NULL)
{
DigitalModule *module = DigitalModule::GetInstance(slot);
m_i2c = module->GetI2C(kAddress);
// Verify Sensor
const UINT8 kExpectedManufacturer[] = "HiTechnc";
const UINT8 kExpectedSensorType[] = "Compass ";
if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) )
{
wpi_fatal(CompassManufacturerError);
return;
}
if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) )
{
wpi_fatal(CompassTypeError);
}
}
/**
* Destructor.
*/
HiTechnicCompass::~HiTechnicCompass()
{
delete m_i2c;
m_i2c = NULL;
}
/**
* Get the compass angle in degrees.
*
* The resolution of this reading is 1 degree.
*
* @return Angle of the compass in degrees.
*/
float HiTechnicCompass::GetAngle()
{
UINT16 heading;
m_i2c->Read(kHeadingRegister, sizeof(heading), (UINT8 *)&heading);
// Sensor is little endian... swap bytes
heading = (heading >> 8) | (heading << 8);
return (float)heading;
}