2014-06-07 17:37:51 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2014-2016. All Rights Reserved. */
|
2014-06-07 17:37:51 -04:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "gtest/gtest.h"
|
2014-06-07 17:37:51 -04:00
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "Encoder.h"
|
|
|
|
|
#include "Jaguar.h"
|
|
|
|
|
#include "PIDController.h"
|
|
|
|
|
#include "Talon.h"
|
|
|
|
|
#include "TestBench.h"
|
|
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "Victor.h"
|
|
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
enum MotorEncoderTestType { TEST_VICTOR, TEST_JAGUAR, TEST_TALON };
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
std::ostream& operator<<(std::ostream& os, MotorEncoderTestType const& type) {
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (type) {
|
|
|
|
|
case TEST_VICTOR:
|
|
|
|
|
os << "Victor";
|
|
|
|
|
break;
|
|
|
|
|
case TEST_JAGUAR:
|
|
|
|
|
os << "Jaguar";
|
|
|
|
|
break;
|
|
|
|
|
case TEST_TALON:
|
|
|
|
|
os << "Talon";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return os;
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr double kMotorTime = 0.5;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A fixture that includes a PWM speed controller and an encoder connected to
|
|
|
|
|
* the same motor.
|
|
|
|
|
*/
|
|
|
|
|
class MotorEncoderTest : public testing::TestWithParam<MotorEncoderTestType> {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2016-05-20 17:30:37 -07:00
|
|
|
SpeedController* m_speedController;
|
|
|
|
|
Encoder* m_encoder;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetUp() override {
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (GetParam()) {
|
|
|
|
|
case TEST_VICTOR:
|
|
|
|
|
m_speedController = new Victor(TestBench::kVictorChannel);
|
|
|
|
|
m_encoder = new Encoder(TestBench::kVictorEncoderChannelA,
|
|
|
|
|
TestBench::kVictorEncoderChannelB);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TEST_JAGUAR:
|
|
|
|
|
m_speedController = new Jaguar(TestBench::kJaguarChannel);
|
|
|
|
|
m_encoder = new Encoder(TestBench::kJaguarEncoderChannelA,
|
|
|
|
|
TestBench::kJaguarEncoderChannelB);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TEST_TALON:
|
|
|
|
|
m_speedController = new Talon(TestBench::kTalonChannel);
|
|
|
|
|
m_encoder = new Encoder(TestBench::kTalonEncoderChannelA,
|
|
|
|
|
TestBench::kTalonEncoderChannelB);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void TearDown() override {
|
2015-06-25 15:07:55 -04:00
|
|
|
delete m_speedController;
|
|
|
|
|
delete m_encoder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
|
m_speedController->Set(0.0f);
|
|
|
|
|
m_encoder->Reset();
|
|
|
|
|
}
|
2014-06-07 17:37:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if the encoder value increments after the motor drives forward
|
|
|
|
|
*/
|
|
|
|
|
TEST_P(MotorEncoderTest, Increment) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Drive the speed controller briefly to move the encoder */
|
2016-01-28 14:25:39 -05:00
|
|
|
m_speedController->Set(0.2f);
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kMotorTime);
|
|
|
|
|
m_speedController->Set(0.0);
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* The encoder should be positive now */
|
|
|
|
|
EXPECT_GT(m_encoder->Get(), 0)
|
|
|
|
|
<< "Encoder should have incremented after the motor moved";
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if the encoder value decrements after the motor drives backwards
|
|
|
|
|
*/
|
|
|
|
|
TEST_P(MotorEncoderTest, Decrement) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Drive the speed controller briefly to move the encoder */
|
2016-01-28 14:25:39 -05:00
|
|
|
m_speedController->Set(-0.2f);
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kMotorTime);
|
|
|
|
|
m_speedController->Set(0.0f);
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* The encoder should be positive now */
|
|
|
|
|
EXPECT_LT(m_encoder->Get(), 0.0f)
|
|
|
|
|
<< "Encoder should have decremented after the motor moved";
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if motor speeds are clamped to [-1,1]
|
|
|
|
|
*/
|
|
|
|
|
TEST_P(MotorEncoderTest, ClampSpeed) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_speedController->Set(2.0f);
|
|
|
|
|
Wait(kMotorTime);
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FLOAT_EQ(1.0f, m_speedController->Get());
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_speedController->Set(-2.0f);
|
|
|
|
|
Wait(kMotorTime);
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FLOAT_EQ(-1.0f, m_speedController->Get());
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-14 20:37:52 -07:00
|
|
|
* Test if position PID loop works
|
2014-06-07 17:37:51 -04:00
|
|
|
*/
|
2015-07-14 20:37:52 -07:00
|
|
|
TEST_P(MotorEncoderTest, PositionPIDController) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2016-01-28 14:25:39 -05:00
|
|
|
double goal = 1000;
|
2015-07-14 20:37:52 -07:00
|
|
|
m_encoder->SetPIDSourceType(PIDSourceType::kDisplacement);
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDController pid(0.001f, 0.0005f, 0.0f, m_encoder, m_speedController);
|
2016-01-28 14:25:39 -05:00
|
|
|
pid.SetAbsoluteTolerance(50.0f);
|
|
|
|
|
pid.SetOutputRange(-0.2f, 0.2f);
|
|
|
|
|
pid.SetSetpoint(goal);
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* 10 seconds should be plenty time to get to the setpoint */
|
|
|
|
|
pid.Enable();
|
|
|
|
|
Wait(10.0);
|
|
|
|
|
pid.Disable();
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
RecordProperty("PIDError", pid.GetError());
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
EXPECT_TRUE(pid.OnTarget())
|
|
|
|
|
<< "PID loop did not converge within 10 seconds. Goal was: " << goal
|
|
|
|
|
<< " Error was: " << pid.GetError();
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 20:37:52 -07:00
|
|
|
/**
|
|
|
|
|
* Test if velocity PID loop works
|
|
|
|
|
*/
|
|
|
|
|
TEST_P(MotorEncoderTest, VelocityPIDController) {
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
|
|
m_encoder->SetPIDSourceType(PIDSourceType::kRate);
|
2015-07-16 14:45:07 -04:00
|
|
|
PIDController pid(1e-5, 0.0f, 3e-5, 8e-5, m_encoder, m_speedController);
|
2016-01-28 14:25:39 -05:00
|
|
|
pid.SetAbsoluteTolerance(200.0f);
|
|
|
|
|
pid.SetToleranceBuffer(50);
|
2015-07-14 20:37:52 -07:00
|
|
|
pid.SetOutputRange(-0.3f, 0.3f);
|
2016-01-28 14:25:39 -05:00
|
|
|
pid.SetSetpoint(600);
|
2015-07-14 20:37:52 -07:00
|
|
|
|
|
|
|
|
/* 10 seconds should be plenty time to get to the setpoint */
|
|
|
|
|
pid.Enable();
|
|
|
|
|
Wait(10.0);
|
|
|
|
|
pid.Disable();
|
2016-01-28 14:25:39 -05:00
|
|
|
RecordProperty("PIDError", pid.GetError());
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
EXPECT_TRUE(pid.OnTarget())
|
|
|
|
|
<< "PID loop did not converge within 10 seconds. Goal was: " << 600
|
|
|
|
|
<< " Error was: " << pid.GetError();
|
2015-07-14 20:37:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
/**
|
|
|
|
|
* Test resetting encoders
|
|
|
|
|
*/
|
|
|
|
|
TEST_P(MotorEncoderTest, Reset) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-08-15 11:22:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_EQ(0, m_encoder->Get()) << "Encoder did not reset to 0";
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(Test, MotorEncoderTest,
|
2015-06-25 15:07:55 -04:00
|
|
|
testing::Values(TEST_VICTOR, TEST_JAGUAR, TEST_TALON));
|