Merge "Added a C++ built-in accelerometer class"

This commit is contained in:
Jonathan Leitschuh (WPI)
2014-07-21 06:11:59 -07:00
committed by Gerrit Code Review
3 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2014. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "SensorBase.h"
/**
* Built-in accelerometer.
*
* This class allows access to the RoboRIO's internal accelerometer.
*/
class BuiltInAccelerometer : public SensorBase
{
public:
enum Range
{
kRange_2G = 0x00,
kRange_4G = 0x01,
kRange_8G = 0x02,
};
BuiltInAccelerometer(Range range = kRange_2G);
virtual ~BuiltInAccelerometer();
virtual double GetX() const;
virtual double GetY() const;
virtual double GetZ() const;
};

View File

@@ -0,0 +1,53 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2014. 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 "BuiltInAccelerometer.h"
#include "HAL/HAL.hpp"
/**
* Constructor.
* @param range The range the accelerometer will measure
*/
BuiltInAccelerometer::BuiltInAccelerometer(Range range)
{
setAccelerometerActive(false);
setAccelerometerRange((AccelerometerRange)range);
setAccelerometerActive(true);
HALReport(HALUsageReporting::kResourceType_Accelerometer, 0, 0, "Built-in accelerometer");
}
/**
* Destructor.
*/
BuiltInAccelerometer::~BuiltInAccelerometer()
{
setAccelerometerActive(false);
}
/**
* @return The acceleration of the RoboRIO along the X axis in g-forces
*/
double BuiltInAccelerometer::GetX() const
{
return getAccelerometerX();
}
/**
* @return The acceleration of the RoboRIO along the Y axis in g-forces
*/
double BuiltInAccelerometer::GetY() const
{
return getAccelerometerY();
}
/**
* @return The acceleration of the RoboRIO along the Z axis in g-forces
*/
double BuiltInAccelerometer::GetZ() const
{
return getAccelerometerZ();
}

View File

@@ -0,0 +1,23 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2014. 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 the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "WPILib.h"
#include "gtest/gtest.h"
static constexpr double accelerationTolerance = 0.1;
/**
* There's not much we can automatically test with the on-board accelerometer,
* but checking for gravity is probably good enough to tell that it's working.
*/
TEST(AccelerometerTest, Accelerometer) {
BuiltInAccelerometer accelerometer;
ASSERT_NEAR(0.0, accelerometer.GetX(), accelerationTolerance);
ASSERT_NEAR(0.0, accelerometer.GetY(), accelerationTolerance);
ASSERT_NEAR(1.0, accelerometer.GetZ(), accelerationTolerance);
}