blob: ef9e80cd9a491ce4506c9e4d6380ad3d91d7ec63 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "HiTechnicCompass.h"
8#include "DigitalModule.h"
9#include "I2C.h"
10#include "NetworkCommunication/UsageReporting.h"
11#include "WPIErrors.h"
12#include "LiveWindow/LiveWindow.h"
13
14const UINT8 HiTechnicCompass::kAddress;
15const UINT8 HiTechnicCompass::kManufacturerBaseRegister;
16const UINT8 HiTechnicCompass::kManufacturerSize;
17const UINT8 HiTechnicCompass::kSensorTypeBaseRegister;
18const UINT8 HiTechnicCompass::kSensorTypeSize;
19const UINT8 HiTechnicCompass::kHeadingRegister;
20
21/**
22 * Constructor.
23 *
24 * @param moduleNumber The digital module that the sensor is plugged into (1 or 2).
25 */
26HiTechnicCompass::HiTechnicCompass(UINT8 moduleNumber)
27 : m_i2c (NULL)
28{
jerrym37afdca2013-03-03 01:17:57 +000029 m_table = NULL;
jerrymf1579332013-02-07 01:56:28 +000030 DigitalModule *module = DigitalModule::GetInstance(moduleNumber);
31 if (module)
32 {
33 m_i2c = module->GetI2C(kAddress);
34
35 // Verify Sensor
36 const UINT8 kExpectedManufacturer[] = "HiTechnc";
37 const UINT8 kExpectedSensorType[] = "Compass ";
38 if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) )
39 {
40 wpi_setWPIError(CompassManufacturerError);
41 return;
42 }
43 if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) )
44 {
45 wpi_setWPIError(CompassTypeError);
46 }
47
48 nUsageReporting::report(nUsageReporting::kResourceType_HiTechnicCompass, moduleNumber - 1);
49 LiveWindow::GetInstance()->AddSensor("HiTechnicCompass", moduleNumber, 0, this);
50 }
51}
52
53/**
54 * Destructor.
55 */
56HiTechnicCompass::~HiTechnicCompass()
57{
58 delete m_i2c;
59 m_i2c = NULL;
60}
61
62/**
63 * Get the compass angle in degrees.
64 *
65 * The resolution of this reading is 1 degree.
66 *
67 * @return Angle of the compass in degrees.
68 */
69float HiTechnicCompass::GetAngle()
70{
71 UINT16 heading = 0;
72 if (m_i2c)
73 {
74 m_i2c->Read(kHeadingRegister, sizeof(heading), (UINT8 *)&heading);
75
76 // Sensor is little endian... swap bytes
77 heading = (heading >> 8) | (heading << 8);
78 }
79 return (float)heading;
80}
81
82void HiTechnicCompass::UpdateTable() {
83 if (m_table != NULL) {
84 m_table->PutNumber("Value", GetAngle());
85 }
86}
87
88void HiTechnicCompass::StartLiveWindowMode() {
89
90}
91
92void HiTechnicCompass::StopLiveWindowMode() {
93
94}
95
96std::string HiTechnicCompass::GetSmartDashboardType() {
97 return "HiTechnicCompass";
98}
99
100void HiTechnicCompass::InitTable(ITable *subTable) {
101 m_table = subTable;
102 UpdateTable();
103}
104
105ITable * HiTechnicCompass::GetTable() {
106 return m_table;
107}
108