mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Merge "Added a C++ built-in accelerometer class"
This commit is contained in:
committed by
Gerrit Code Review
commit
be106b3527
30
wpilibc/wpilibC++/include/BuiltInAccelerometer.h
Normal file
30
wpilibc/wpilibC++/include/BuiltInAccelerometer.h
Normal 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;
|
||||
};
|
||||
53
wpilibc/wpilibC++/lib/BuiltInAccelerometer.cpp
Normal file
53
wpilibc/wpilibC++/lib/BuiltInAccelerometer.cpp
Normal 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();
|
||||
}
|
||||
23
wpilibc/wpilibC++IntegrationTests/src/AccelerometerTest.cpp
Normal file
23
wpilibc/wpilibC++IntegrationTests/src/AccelerometerTest.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user