Adds resource tracking to CANJaguar in C++

Change-Id: I0d562af5e9f4f50f79d61db15ff25eaf4dae00d5
This commit is contained in:
Jonathan Leitschuh
2014-07-18 16:06:04 -04:00
parent d8a5ced015
commit f27e16735f
3 changed files with 48 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
#include "ErrorBase.h"
#include "MotorSafety.h"
#include "Resource.h"
#include "MotorSafetyHelper.h"
#include "PIDOutput.h"
#include "SpeedController.h"

View File

@@ -32,6 +32,8 @@ static const uint32_t kFullMessageIDMask = (CAN_MSGID_API_M | CAN_MSGID_MFR_M |
static const int32_t kReceiveStatusAttempts = 50;
static Resource *allocated = NULL;
static int32_t sendMessageHelper(uint32_t messageID, const uint8_t *data, uint8_t dataSize, int32_t period)
{
static const uint32_t kTrustedMessages[] = {
@@ -236,6 +238,16 @@ CANJaguar::CANJaguar(uint8_t deviceNumber)
, m_maxOutputVoltage (kApproxBusVoltage)
, m_safetyHelper (NULL)
{
char buf[64];
snprintf(buf, 64, "CANJaguar device number %d", m_deviceNumber);
Resource::CreateResourceObject(&allocated, 63);
if (allocated->Allocate(m_deviceNumber-1, buf) == ~0ul)
{
CloneError(allocated);
return;
}
SetPercentMode();
InitCANJaguar();
ConfigMaxOutputVoltage(kApproxBusVoltage);
@@ -243,6 +255,8 @@ CANJaguar::CANJaguar(uint8_t deviceNumber)
CANJaguar::~CANJaguar()
{
allocated->Free(m_deviceNumber-1);
int32_t status;
// Disable periodic setpoints

View File

@@ -67,6 +67,39 @@ protected:
}
};
/**
* Tests that allocating the same CANJaguar port as an already allocated port throws a resource already allocated error code.
*/
TEST_F(CANJaguarTest, DuplicateAllocationCausesError) {
std::cout<<"The following error is an expected part of the test system"<<std::endl;
CANJaguar *m_jaguar = new CANJaguar(TestBench::kCANJaguarID);
EXPECT_EQ(wpi_error_value_ResourceAlreadyAllocated, m_jaguar->GetError().GetCode()) << "An error should have been returned";
//See WPIErrors.h for error code comparison
delete m_jaguar;
}
/**
* Tests that allocating a CANJaguar device beyond the range of devices throws a module index out of range error code.
*/
TEST_F(CANJaguarTest, OutOfRangeAllocationCausesError) {
std::cout<<"The following error is an expected part of the test system"<<std::endl;
CANJaguar *m_jaguar = new CANJaguar(64);
EXPECT_EQ( wpi_error_value_ModuleIndexOutOfRange, m_jaguar->GetError().GetCode()) << "An error should have been returned";
//See WPIErrors.h for error code comparison
delete m_jaguar; //This will also return an error
}
/**
* Tests that allocating a negative CANJaguar device throws a module index out of range error code.
*/
TEST_F(CANJaguarTest, OutOfRangeNegativeAllocationCausesError) {
std::cout<<"The following error is an expected part of the test system"<<std::endl;
CANJaguar *m_jaguar = new CANJaguar(0);
EXPECT_EQ(wpi_error_value_ModuleIndexOutOfRange, m_jaguar->GetError().GetCode()) << "An error should have been returned";
//See WPIErrors.h for error code comparison
delete m_jaguar; //This will also return an error
}
/**
* Checks the default status data for reasonable values to confirm that we're
* really getting status data from the Jaguar.