mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
Adds resource tracking to CANJaguar in C++
Change-Id: I0d562af5e9f4f50f79d61db15ff25eaf4dae00d5
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "ErrorBase.h"
|
||||
#include "MotorSafety.h"
|
||||
#include "Resource.h"
|
||||
#include "MotorSafetyHelper.h"
|
||||
#include "PIDOutput.h"
|
||||
#include "SpeedController.h"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user