blob: 3c353b3245f3dff448c82a811e1bd5490c4b4dec [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{
29 DigitalModule *module = DigitalModule::GetInstance(moduleNumber);
30 if (module)
31 {
32 m_i2c = module->GetI2C(kAddress);
33
34 // Verify Sensor
35 const UINT8 kExpectedManufacturer[] = "HiTechnc";
36 const UINT8 kExpectedSensorType[] = "Compass ";
37 if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) )
38 {
39 wpi_setWPIError(CompassManufacturerError);
40 return;
41 }
42 if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) )
43 {
44 wpi_setWPIError(CompassTypeError);
45 }
46
47 nUsageReporting::report(nUsageReporting::kResourceType_HiTechnicCompass, moduleNumber - 1);
48 LiveWindow::GetInstance()->AddSensor("HiTechnicCompass", moduleNumber, 0, this);
49 }
50}
51
52/**
53 * Destructor.
54 */
55HiTechnicCompass::~HiTechnicCompass()
56{
57 delete m_i2c;
58 m_i2c = NULL;
59}
60
61/**
62 * Get the compass angle in degrees.
63 *
64 * The resolution of this reading is 1 degree.
65 *
66 * @return Angle of the compass in degrees.
67 */
68float HiTechnicCompass::GetAngle()
69{
70 UINT16 heading = 0;
71 if (m_i2c)
72 {
73 m_i2c->Read(kHeadingRegister, sizeof(heading), (UINT8 *)&heading);
74
75 // Sensor is little endian... swap bytes
76 heading = (heading >> 8) | (heading << 8);
77 }
78 return (float)heading;
79}
80
81void HiTechnicCompass::UpdateTable() {
82 if (m_table != NULL) {
83 m_table->PutNumber("Value", GetAngle());
84 }
85}
86
87void HiTechnicCompass::StartLiveWindowMode() {
88
89}
90
91void HiTechnicCompass::StopLiveWindowMode() {
92
93}
94
95std::string HiTechnicCompass::GetSmartDashboardType() {
96 return "HiTechnicCompass";
97}
98
99void HiTechnicCompass::InitTable(ITable *subTable) {
100 m_table = subTable;
101 UpdateTable();
102}
103
104ITable * HiTechnicCompass::GetTable() {
105 return m_table;
106}
107