2014-06-16 10:24:48 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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"
|
|
|
|
|
#include "TestBench.h"
|
|
|
|
|
|
2014-07-23 15:23:37 -04:00
|
|
|
static constexpr double kSpikeTime = 0.5;
|
|
|
|
|
|
2014-06-26 10:51:26 -04:00
|
|
|
static constexpr double kExpectedBusVoltage = 14.0;
|
|
|
|
|
static constexpr double kExpectedTemperature = 25.0;
|
|
|
|
|
|
2014-06-19 21:43:31 -04:00
|
|
|
static constexpr double kMotorTime = 0.5;
|
|
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
static constexpr double kEncoderSettlingTime = 1.0;
|
2014-07-16 14:13:29 -04:00
|
|
|
static constexpr double kEncoderPositionTolerance = 0.1;
|
|
|
|
|
static constexpr double kEncoderSpeedTolerance = 30.0;
|
|
|
|
|
|
|
|
|
|
static constexpr double kPotentiometerSettlingTime = 1.0;
|
|
|
|
|
static constexpr double kPotentiometerPositionTolerance = 0.1;
|
|
|
|
|
|
|
|
|
|
static constexpr double kCurrentTolerance = 0.1;
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
static constexpr double kVoltageTolerance = 0.1;
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-06-16 10:24:48 -04:00
|
|
|
class CANJaguarTest : public testing::Test {
|
|
|
|
|
protected:
|
2014-07-01 12:02:44 -04:00
|
|
|
CANJaguar *m_jaguar;
|
|
|
|
|
DigitalOutput *m_fakeForwardLimit, *m_fakeReverseLimit;
|
|
|
|
|
AnalogOutput *m_fakePotentiometer;
|
2014-07-23 15:23:37 -04:00
|
|
|
Relay *m_spike;
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
virtual void SetUp() override {
|
2014-07-23 15:23:37 -04:00
|
|
|
m_spike = new Relay(TestBench::kCANJaguarRelayChannel, Relay::kForwardOnly);
|
|
|
|
|
m_spike->Set(Relay::kOn);
|
|
|
|
|
Wait(kSpikeTime);
|
|
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar = new CANJaguar(TestBench::kCANJaguarID);
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
m_fakeForwardLimit = new DigitalOutput(TestBench::kFakeJaguarForwardLimit);
|
|
|
|
|
m_fakeForwardLimit->Set(0);
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
m_fakeReverseLimit = new DigitalOutput(TestBench::kFakeJaguarReverseLimit);
|
|
|
|
|
m_fakeReverseLimit->Set(0);
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
m_fakePotentiometer = new AnalogOutput(TestBench::kFakeJaguarPotentiometer);
|
|
|
|
|
m_fakePotentiometer->SetVoltage(0.0f);
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* The motor might still have momentum from the previous test. */
|
|
|
|
|
Wait(kEncoderSettlingTime);
|
|
|
|
|
}
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
virtual void TearDown() override {
|
2014-07-01 12:02:44 -04:00
|
|
|
delete m_jaguar;
|
|
|
|
|
delete m_fakeForwardLimit;
|
|
|
|
|
delete m_fakeReverseLimit;
|
|
|
|
|
delete m_fakePotentiometer;
|
2014-07-23 15:23:37 -04:00
|
|
|
delete m_spike;
|
2014-07-01 12:02:44 -04:00
|
|
|
}
|
2014-07-16 14:13:29 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calls CANJaguar::Set periodically 50 times to make sure everything is
|
|
|
|
|
* verified. This mimics a real robot program, where Set is presumably
|
|
|
|
|
* called in each iteration of the main loop.
|
|
|
|
|
*/
|
|
|
|
|
void SetJaguar(float totalTime, float value = 0.0f) {
|
|
|
|
|
for(int i = 0; i < 50; i++) {
|
|
|
|
|
m_jaguar->Set(value);
|
|
|
|
|
Wait(totalTime / 50.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-16 10:24:48 -04:00
|
|
|
};
|
|
|
|
|
|
2014-07-18 16:06:04 -04:00
|
|
|
/**
|
2014-07-23 15:23:37 -04:00
|
|
|
* Tests that allocating the same CANJaguar port as an already allocated port
|
|
|
|
|
* causes a ResourceAlreadyAllocated error.
|
2014-07-18 16:06:04 -04:00
|
|
|
*/
|
2014-07-23 15:23:37 -04:00
|
|
|
TEST_F(CANJaguarTest, AlreadyAllocatedError) {
|
2014-07-28 16:25:37 -04:00
|
|
|
std::cout << "The following errors are expected." << std::endl << std::endl;
|
2014-07-23 15:23:37 -04:00
|
|
|
|
|
|
|
|
CANJaguar jaguar(TestBench::kCANJaguarID);
|
|
|
|
|
EXPECT_EQ(wpi_error_value_ResourceAlreadyAllocated, jaguar.GetError().GetCode())
|
|
|
|
|
<< "An error should have been returned";
|
2014-07-18 16:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-23 15:23:37 -04:00
|
|
|
* Test that allocating a CANJaguar with device number 64 causes an
|
|
|
|
|
* out-of-range error.
|
2014-07-18 16:06:04 -04:00
|
|
|
*/
|
2014-07-23 15:23:37 -04:00
|
|
|
TEST_F(CANJaguarTest, 64OutOfRangeError) {
|
2014-07-28 16:25:37 -04:00
|
|
|
std::cout << "The following errors are expected." << std::endl << std::endl;
|
2014-07-23 15:23:37 -04:00
|
|
|
|
|
|
|
|
CANJaguar jaguar(64);
|
|
|
|
|
EXPECT_EQ(wpi_error_value_ChannelIndexOutOfRange, jaguar.GetError().GetCode())
|
|
|
|
|
<< "An error should have been returned";
|
2014-07-18 16:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-23 15:23:37 -04:00
|
|
|
* Test that allocating a CANJaguar with device number 0 causes an out-of-range
|
|
|
|
|
* error.
|
2014-07-18 16:06:04 -04:00
|
|
|
*/
|
2014-07-23 15:23:37 -04:00
|
|
|
TEST_F(CANJaguarTest, 0OutOfRangeError) {
|
2014-07-28 16:25:37 -04:00
|
|
|
std::cout << "The following errors are expected." << std::endl << std::endl;
|
2014-07-23 15:23:37 -04:00
|
|
|
|
|
|
|
|
CANJaguar jaguar(0);
|
|
|
|
|
EXPECT_EQ(wpi_error_value_ChannelIndexOutOfRange, jaguar.GetError().GetCode())
|
|
|
|
|
<< "An error should have been returned";
|
2014-07-18 16:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-26 10:51:26 -04:00
|
|
|
/**
|
|
|
|
|
* Checks the default status data for reasonable values to confirm that we're
|
|
|
|
|
* really getting status data from the Jaguar.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, InitialStatus) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode();
|
2014-06-26 10:51:26 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_NEAR(m_jaguar->GetBusVoltage(), kExpectedBusVoltage, 3.0)
|
|
|
|
|
<< "Bus voltage is not a plausible value.";
|
2014-06-26 10:51:26 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_FLOAT_EQ(m_jaguar->GetOutputVoltage(), 0.0)
|
|
|
|
|
<< "Output voltage is non-zero.";
|
2014-06-26 10:51:26 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_FLOAT_EQ(m_jaguar->GetOutputCurrent(), 0.0)
|
|
|
|
|
<< "Output current is non-zero.";
|
2014-06-26 10:51:26 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_NEAR(m_jaguar->GetTemperature(), kExpectedTemperature, 5.0)
|
|
|
|
|
<< "Temperature is not a plausible value.";
|
2014-06-26 10:51:26 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_EQ(m_jaguar->GetFaults(), 0)
|
|
|
|
|
<< "Jaguar has one or more fault set.";
|
2014-06-26 10:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-24 17:42:25 -04:00
|
|
|
/**
|
|
|
|
|
* Ensure that the jaguar doesn't move when it's disabled
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, Disable) {
|
|
|
|
|
m_jaguar->SetPercentMode(CANJaguar::QuadEncoder, 360);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
m_jaguar->DisableControl();
|
|
|
|
|
|
2014-08-15 11:22:01 -04:00
|
|
|
Wait(kEncoderSettlingTime);
|
|
|
|
|
|
2014-07-24 17:42:25 -04:00
|
|
|
double initialPosition = m_jaguar->GetPosition();
|
2014-07-28 16:25:37 -04:00
|
|
|
|
2014-07-24 17:42:25 -04:00
|
|
|
SetJaguar(kMotorTime, 1.0f);
|
|
|
|
|
m_jaguar->Set(0.0f);
|
|
|
|
|
|
|
|
|
|
double finalPosition = m_jaguar->GetPosition();
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(initialPosition, finalPosition, kEncoderPositionTolerance)
|
|
|
|
|
<< "Jaguar moved while disabled";
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 15:23:37 -04:00
|
|
|
/**
|
|
|
|
|
* Make sure the Jaguar keeps its state after a power cycle by setting a
|
|
|
|
|
* control mode, turning the spike on and off, then checking if the Jaguar
|
|
|
|
|
* behaves like it should in that control mode.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, BrownOut) {
|
|
|
|
|
|
|
|
|
|
/* Set the jaguar to quad encoder position mode */
|
2014-12-01 14:05:26 -05:00
|
|
|
m_jaguar->SetPositionMode(CANJaguar::QuadEncoder, 360, 20.0f, 0.01f, 0.0f);
|
2014-07-23 15:23:37 -04:00
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
SetJaguar(kMotorTime, 0.0);
|
2014-12-01 14:05:26 -05:00
|
|
|
double setpoint = m_jaguar->GetPosition() + 1.0f;
|
2014-07-23 15:23:37 -04:00
|
|
|
|
|
|
|
|
/* Turn the spike off and on again */
|
|
|
|
|
m_spike->Set(Relay::kOff);
|
|
|
|
|
Wait(kSpikeTime);
|
|
|
|
|
m_spike->Set(Relay::kOn);
|
|
|
|
|
Wait(kSpikeTime);
|
|
|
|
|
|
|
|
|
|
/* The jaguar should automatically get set to quad encoder position mode,
|
|
|
|
|
so it should be able to reach a setpoint in a couple seconds. */
|
|
|
|
|
for(int i = 0; i < 10; i++) {
|
|
|
|
|
SetJaguar(1.0f, setpoint);
|
|
|
|
|
|
|
|
|
|
if(std::abs(m_jaguar->GetPosition() - setpoint) <= kEncoderPositionTolerance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(setpoint, m_jaguar->GetPosition(), kEncoderPositionTolerance)
|
|
|
|
|
<< "CAN Jaguar should have resumed PID control after power cycle";
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can set arbitrary setpoints and PID values each each applicable
|
|
|
|
|
* mode and get the same values back.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, SetGet) {
|
|
|
|
|
m_jaguar->DisableControl();
|
|
|
|
|
|
|
|
|
|
m_jaguar->SetSpeedMode(CANJaguar::QuadEncoder, 360, 1, 2, 3);
|
|
|
|
|
m_jaguar->Set(4);
|
|
|
|
|
|
|
|
|
|
EXPECT_FLOAT_EQ(1, m_jaguar->GetP());
|
|
|
|
|
EXPECT_FLOAT_EQ(2, m_jaguar->GetI());
|
|
|
|
|
EXPECT_FLOAT_EQ(3, m_jaguar->GetD());
|
|
|
|
|
EXPECT_FLOAT_EQ(4, m_jaguar->Get());
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:43:31 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can drive the motor in percentage mode and get a position back
|
|
|
|
|
*/
|
2014-07-29 15:39:54 -04:00
|
|
|
TEST_F(CANJaguarTest, PercentModeForwardWorks) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode(CANJaguar::QuadEncoder, 360);
|
|
|
|
|
m_jaguar->EnableControl();
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* The motor might still have momentum from the previous test. */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kEncoderSettlingTime, 0.0f);
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
double initialPosition = m_jaguar->GetPosition();
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* Drive the speed controller briefly to move the encoder */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, 1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->Set(0.0f);
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* The position should have increased */
|
|
|
|
|
EXPECT_GT(m_jaguar->GetPosition(), initialPosition)
|
|
|
|
|
<< "CAN Jaguar position should have increased after the motor moved";
|
2014-06-19 21:43:31 -04:00
|
|
|
}
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2014-06-19 21:43:31 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can drive the motor backwards in percentage mode and get a
|
|
|
|
|
* position back
|
|
|
|
|
*/
|
2014-07-29 15:39:54 -04:00
|
|
|
TEST_F(CANJaguarTest, PercentModeReverseWorks) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode(CANJaguar::QuadEncoder, 360);
|
|
|
|
|
m_jaguar->EnableControl();
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* The motor might still have momentum from the previous test. */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kEncoderSettlingTime, 0.0f);
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
double initialPosition = m_jaguar->GetPosition();
|
2014-06-25 15:42:16 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* Drive the speed controller briefly to move the encoder */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, -1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->Set(0.0f);
|
2014-06-16 10:24:48 -04:00
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
float p = m_jaguar->GetPosition();
|
2014-07-01 12:02:44 -04:00
|
|
|
/* The position should have decreased */
|
2014-07-16 14:13:29 -04:00
|
|
|
EXPECT_LT(p, initialPosition)
|
2014-07-01 12:02:44 -04:00
|
|
|
<< "CAN Jaguar position should have decreased after the motor moved";
|
2014-06-19 21:43:31 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can set an absolute voltage and receive a matching output voltage
|
|
|
|
|
* status.
|
|
|
|
|
*/
|
2014-07-29 15:39:54 -04:00
|
|
|
TEST_F(CANJaguarTest, VoltageModeWorks) {
|
2014-07-16 14:13:29 -04:00
|
|
|
m_jaguar->SetVoltageMode();
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-29 15:39:54 -04:00
|
|
|
float setpoints[] = { M_PI, 8.0f, -10.0f };
|
2014-07-16 14:13:29 -04:00
|
|
|
|
2014-08-05 11:48:47 -04:00
|
|
|
for(unsigned int i = 0; i < sizeof(setpoints)/sizeof(setpoints[0]); i++) {
|
2014-07-29 15:39:54 -04:00
|
|
|
float setpoint = setpoints[i];
|
|
|
|
|
|
|
|
|
|
SetJaguar(kMotorTime, setpoint);
|
|
|
|
|
EXPECT_NEAR(setpoint, m_jaguar->GetOutputVoltage(), kVoltageTolerance);
|
|
|
|
|
}
|
2014-07-16 14:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if we can set a speed in speed control mode and receive a matching
|
|
|
|
|
* speed status.
|
|
|
|
|
*/
|
2014-07-29 15:39:54 -04:00
|
|
|
TEST_F(CANJaguarTest, SpeedModeWorks) {
|
2014-07-16 14:13:29 -04:00
|
|
|
m_jaguar->SetSpeedMode(CANJaguar::QuadEncoder, 360, 0.1f, 0.003f, 0.01f);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2015-04-10 16:25:20 -04:00
|
|
|
constexpr float speed = 50.0f;
|
2014-07-16 14:13:29 -04:00
|
|
|
|
|
|
|
|
SetJaguar(kMotorTime, speed);
|
|
|
|
|
EXPECT_NEAR(speed, m_jaguar->GetSpeed(), kEncoderSpeedTolerance);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:43:31 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can set a position and reach that position with PID control on
|
|
|
|
|
* the Jaguar.
|
|
|
|
|
*/
|
2014-07-29 15:39:54 -04:00
|
|
|
TEST_F(CANJaguarTest, PositionModeWorks) {
|
2014-12-01 14:05:26 -05:00
|
|
|
m_jaguar->SetPositionMode(CANJaguar::QuadEncoder, 360, 15.0f, 0.02f, 0.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
2014-12-01 14:05:26 -05:00
|
|
|
double setpoint = m_jaguar->GetPosition() + 1.0f;
|
2014-07-01 12:02:44 -04:00
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
/* It should get to the setpoint within 10 seconds */
|
|
|
|
|
for(int i = 0; i < 10; i++) {
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(1.0f, setpoint);
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
if(std::abs(m_jaguar->GetPosition() - setpoint) <= kEncoderPositionTolerance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-19 21:43:31 -04:00
|
|
|
|
2014-07-01 12:02:44 -04:00
|
|
|
EXPECT_NEAR(setpoint, m_jaguar->GetPosition(), kEncoderPositionTolerance)
|
|
|
|
|
<< "CAN Jaguar should have reached setpoint with PID control";
|
2014-06-19 21:43:31 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can set a current setpoint with PID control on the Jaguar and get
|
|
|
|
|
* a corresponding output current
|
|
|
|
|
*/
|
2014-10-08 15:00:28 -04:00
|
|
|
TEST_F(CANJaguarTest, DISABLED_CurrentModeWorks) {
|
2014-07-16 14:13:29 -04:00
|
|
|
m_jaguar->SetCurrentMode(10.0, 4.0, 1.0);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-29 15:39:54 -04:00
|
|
|
float setpoints[] = { 1.6f, 2.0f, -1.6f };
|
2014-07-16 14:13:29 -04:00
|
|
|
|
2014-08-05 11:48:47 -04:00
|
|
|
for(unsigned int i = 0; i < sizeof(setpoints)/sizeof(setpoints[0]); i++) {
|
2014-07-29 15:39:54 -04:00
|
|
|
float setpoint = setpoints[i];
|
|
|
|
|
float expectedCurrent = std::abs(setpoints[i]);
|
2014-07-16 14:13:29 -04:00
|
|
|
|
2014-07-29 15:39:54 -04:00
|
|
|
/* It should get to each setpoint within 10 seconds */
|
|
|
|
|
for(int j = 0; j < 10; j++) {
|
2014-08-05 11:48:47 -04:00
|
|
|
SetJaguar(1.0, setpoint);
|
2014-07-16 14:13:29 -04:00
|
|
|
|
2014-07-29 15:39:54 -04:00
|
|
|
if(std::abs(m_jaguar->GetOutputCurrent() - expectedCurrent) <= kCurrentTolerance) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-07-16 14:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-29 15:39:54 -04:00
|
|
|
EXPECT_NEAR(expectedCurrent, m_jaguar->GetOutputCurrent(), kCurrentTolerance);
|
|
|
|
|
}
|
2014-07-16 14:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:43:31 -04:00
|
|
|
/**
|
|
|
|
|
* Test if we can get a position in potentiometer mode, using an analog output
|
|
|
|
|
* as a fake potentiometer.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, FakePotentiometerPosition) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode(CANJaguar::Potentiometer);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
// Set the analog output to 4 different voltages and check if the Jaguar
|
|
|
|
|
// returns corresponding positions.
|
|
|
|
|
for(int i = 0; i <= 3; i++) {
|
|
|
|
|
m_fakePotentiometer->SetVoltage(static_cast<float>(i));
|
|
|
|
|
|
|
|
|
|
SetJaguar(kPotentiometerSettlingTime);
|
|
|
|
|
|
2014-07-23 15:23:37 -04:00
|
|
|
EXPECT_NEAR(m_fakePotentiometer->GetVoltage() / 3.0f, m_jaguar->GetPosition(),
|
|
|
|
|
kPotentiometerPositionTolerance)
|
2014-07-16 14:13:29 -04:00
|
|
|
<< "CAN Jaguar should have returned the potentiometer position set by the analog output";
|
|
|
|
|
}
|
2014-06-19 21:43:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if we can limit the Jaguar to only moving in reverse with a fake
|
|
|
|
|
* limit switch.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, FakeLimitSwitchForwards) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode(CANJaguar::QuadEncoder, 360);
|
|
|
|
|
m_jaguar->ConfigLimitMode(CANJaguar::kLimitMode_SwitchInputsOnly);
|
|
|
|
|
m_fakeForwardLimit->Set(1);
|
|
|
|
|
m_fakeReverseLimit->Set(0);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kEncoderSettlingTime);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* Make sure the limits are recognized by the Jaguar. */
|
|
|
|
|
ASSERT_FALSE(m_jaguar->GetForwardLimitOK());
|
|
|
|
|
ASSERT_TRUE(m_jaguar->GetReverseLimitOK());
|
|
|
|
|
|
|
|
|
|
double initialPosition = m_jaguar->GetPosition();
|
|
|
|
|
|
|
|
|
|
/* Drive the speed controller briefly to move the encoder. If the limit
|
|
|
|
|
switch is recognized, it shouldn't actually move. */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, 1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* The position should be the same, since the limit switch was on. */
|
|
|
|
|
EXPECT_NEAR(initialPosition, m_jaguar->GetPosition(), kEncoderPositionTolerance)
|
|
|
|
|
<< "CAN Jaguar should not have moved with the limit switch pressed";
|
|
|
|
|
|
|
|
|
|
/* Drive the speed controller in the other direction. It should actually
|
|
|
|
|
move, since only the forward switch is activated.*/
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, -1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* The position should have decreased */
|
|
|
|
|
EXPECT_LT(m_jaguar->GetPosition(), initialPosition)
|
|
|
|
|
<< "CAN Jaguar should have moved in reverse while the forward limit was on";
|
2014-06-19 21:43:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if we can limit the Jaguar to only moving forwards with a fake limit
|
|
|
|
|
* switch.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CANJaguarTest, FakeLimitSwitchReverse) {
|
2014-07-01 12:02:44 -04:00
|
|
|
m_jaguar->SetPercentMode(CANJaguar::QuadEncoder, 360);
|
|
|
|
|
m_jaguar->ConfigLimitMode(CANJaguar::kLimitMode_SwitchInputsOnly);
|
|
|
|
|
m_fakeForwardLimit->Set(0);
|
|
|
|
|
m_fakeReverseLimit->Set(1);
|
|
|
|
|
m_jaguar->EnableControl();
|
|
|
|
|
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kEncoderSettlingTime);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* Make sure the limits are recognized by the Jaguar. */
|
|
|
|
|
ASSERT_TRUE(m_jaguar->GetForwardLimitOK());
|
|
|
|
|
ASSERT_FALSE(m_jaguar->GetReverseLimitOK());
|
|
|
|
|
|
|
|
|
|
double initialPosition = m_jaguar->GetPosition();
|
|
|
|
|
|
|
|
|
|
/* Drive the speed controller backwards briefly to move the encoder. If
|
|
|
|
|
the limit switch is recognized, it shouldn't actually move. */
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, -1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* The position should be the same, since the limit switch was on. */
|
|
|
|
|
EXPECT_NEAR(initialPosition, m_jaguar->GetPosition(), kEncoderPositionTolerance)
|
|
|
|
|
<< "CAN Jaguar should not have moved with the limit switch pressed";
|
|
|
|
|
|
|
|
|
|
/* Drive the speed controller in the other direction. It should actually
|
|
|
|
|
move, since only the reverse switch is activated.*/
|
2014-07-16 14:13:29 -04:00
|
|
|
SetJaguar(kMotorTime, 1.0f);
|
2014-07-01 12:02:44 -04:00
|
|
|
|
|
|
|
|
/* The position should have increased */
|
|
|
|
|
EXPECT_GT(m_jaguar->GetPosition(), initialPosition)
|
|
|
|
|
<< "CAN Jaguar should have moved forwards while the reverse limit was on";
|
2014-06-16 10:24:48 -04:00
|
|
|
}
|