Skip to content

Commit

Permalink
Create a function to reuse for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christophfroehlich committed Nov 27, 2023
1 parent 10d6aef commit 6b99228
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 63 deletions.
65 changes: 3 additions & 62 deletions joint_trajectory_controller/test/test_trajectory_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include <thread>
#include <vector>

#include "gmock/gmock.h"

#include "builtin_interfaces/msg/duration.hpp"
#include "builtin_interfaces/msg/time.hpp"
#include "controller_interface/controller_interface.hpp"
Expand Down Expand Up @@ -103,77 +101,20 @@ TEST_P(TrajectoryControllerTestParameterized, check_interface_names)
const auto state = traj_controller_->get_node()->configure();
ASSERT_EQ(state.id(), State::PRIMARY_STATE_INACTIVE);

std::vector<std::string> state_interface_names;
state_interface_names.reserve(joint_names_.size() * state_interface_types_.size());
for (const auto & joint : joint_names_)
{
for (const auto & interface : state_interface_types_)
{
state_interface_names.push_back(joint + "/" + interface);
}
}
auto state_interfaces = traj_controller_->state_interface_configuration();
EXPECT_EQ(state_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(state_interfaces.names.size(), joint_names_.size() * state_interface_types_.size());
ASSERT_THAT(state_interfaces.names, testing::UnorderedElementsAreArray(state_interface_names));

std::vector<std::string> command_interface_names;
command_interface_names.reserve(joint_names_.size() * command_interface_types_.size());
for (const auto & joint : joint_names_)
{
for (const auto & interface : command_interface_types_)
{
command_interface_names.push_back(joint + "/" + interface);
}
}
auto command_interfaces = traj_controller_->command_interface_configuration();
EXPECT_EQ(
command_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(command_interfaces.names.size(), joint_names_.size() * command_interface_types_.size());
ASSERT_THAT(
command_interfaces.names, testing::UnorderedElementsAreArray(command_interface_names));
compare_joints(joint_names_, joint_names_);
}

TEST_P(TrajectoryControllerTestParameterized, check_interface_names_with_command_joints)
{
rclcpp::executors::MultiThreadedExecutor executor;
// set command_joints parameter
// set command_joints parameter different than joint_names_
const rclcpp::Parameter command_joint_names_param("command_joints", command_joint_names_);
SetUpTrajectoryController(executor, {command_joint_names_param});

const auto state = traj_controller_->get_node()->configure();
ASSERT_EQ(state.id(), State::PRIMARY_STATE_INACTIVE);

std::vector<std::string> state_interface_names;
state_interface_names.reserve(joint_names_.size() * state_interface_types_.size());
for (const auto & joint : joint_names_)
{
for (const auto & interface : state_interface_types_)
{
state_interface_names.push_back(joint + "/" + interface);
}
}
auto state_interfaces = traj_controller_->state_interface_configuration();
EXPECT_EQ(state_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(state_interfaces.names.size(), joint_names_.size() * state_interface_types_.size());
ASSERT_THAT(state_interfaces.names, testing::UnorderedElementsAreArray(state_interface_names));

std::vector<std::string> command_interface_names;
command_interface_names.reserve(command_joint_names_.size() * command_interface_types_.size());
for (const auto & joint : command_joint_names_)
{
for (const auto & interface : command_interface_types_)
{
command_interface_names.push_back(joint + "/" + interface);
}
}
auto command_interfaces = traj_controller_->command_interface_configuration();
EXPECT_EQ(
command_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(
command_interfaces.names.size(), command_joint_names_.size() * command_interface_types_.size());
ASSERT_THAT(
command_interfaces.names, testing::UnorderedElementsAreArray(command_interface_names));
compare_joints(joint_names_, command_joint_names_);
}

TEST_P(TrajectoryControllerTestParameterized, activate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <utility>
#include <vector>

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "hardware_interface/types/hardware_interface_type_values.hpp"
#include "joint_trajectory_controller/joint_trajectory_controller.hpp"
Expand Down Expand Up @@ -623,6 +623,47 @@ class TrajectoryControllerTest : public ::testing::Test
}
}

/**
* @brief compares the joint names and interface types of the controller with the given ones
*/
void compare_joints(
std::vector<std::string> state_joint_names, std::vector<std::string> command_joint_names)
{
std::vector<std::string> state_interface_names;
state_interface_names.reserve(state_joint_names.size() * state_interface_types_.size());
for (const auto & joint : state_joint_names)
{
for (const auto & interface : state_interface_types_)
{
state_interface_names.push_back(joint + "/" + interface);
}
}
auto state_interfaces = traj_controller_->state_interface_configuration();
EXPECT_EQ(
state_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(
state_interfaces.names.size(), state_joint_names.size() * state_interface_types_.size());
ASSERT_THAT(state_interfaces.names, testing::UnorderedElementsAreArray(state_interface_names));

std::vector<std::string> command_interface_names;
command_interface_names.reserve(command_joint_names.size() * command_interface_types_.size());
for (const auto & joint : command_joint_names)
{
for (const auto & interface : command_interface_types_)
{
command_interface_names.push_back(joint + "/" + interface);
}
}
auto command_interfaces = traj_controller_->command_interface_configuration();
EXPECT_EQ(
command_interfaces.type, controller_interface::interface_configuration_type::INDIVIDUAL);
EXPECT_EQ(
command_interfaces.names.size(),
command_joint_names.size() * command_interface_types_.size());
ASSERT_THAT(
command_interfaces.names, testing::UnorderedElementsAreArray(command_interface_names));
}

std::string controller_name_;

std::vector<std::string> joint_names_;
Expand Down

0 comments on commit 6b99228

Please sign in to comment.