Skip to content

Commit

Permalink
Adding some tests for setting the speed scaling factor
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauch committed Jan 15, 2025
1 parent d141556 commit 2abc954
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ bool JointTrajectoryController::set_scaling_factor(const double scaling_factor)
return false;
}

if (scaling_factor_ != scaling_factor_)
if (scaling_factor != scaling_factor_)
{
RCLCPP_INFO(
get_node()->get_logger().get_child("speed_scaling"), "New scaling factor will be %f",
Expand Down
59 changes: 59 additions & 0 deletions joint_trajectory_controller/test/test_trajectory_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,3 +2209,62 @@ TEST_F(TrajectoryControllerTest, incorrect_initialization_using_interface_parame
state_interface_types_ = {"velocity"};
EXPECT_EQ(SetUpTrajectoryControllerLocal(), controller_interface::return_type::ERROR);
}

TEST_F(TrajectoryControllerTest, setting_scaling_factor_works_correctly)
{
rclcpp::executors::MultiThreadedExecutor executor;
std::vector<rclcpp::Parameter> params = {};
SetUpAndActivateTrajectoryController(executor, params);
auto speed_scaling_pub = node_->create_publisher<control_msgs::msg::SpeedScalingFactor>(
controller_name_ + "/speed_scaling_input", rclcpp::SystemDefaultsQoS());
subscribeToState(executor);

control_msgs::msg::SpeedScalingFactor msg;
msg.factor = 0.765;
speed_scaling_pub->publish(msg);
traj_controller_->wait_for_trajectory(executor);

updateController();

// Spin to receive latest state
executor.spin_some();
auto state = getState();
EXPECT_EQ(state->speed_scaling_factor, 0.765);

// 0.0 should work as an edge case
msg.factor = 0.0;
speed_scaling_pub->publish(msg);
traj_controller_->wait_for_trajectory(executor);
updateController();
executor.spin_some();
state = getState();
EXPECT_EQ(state->speed_scaling_factor, 0.0);

// Sending a negative value will be ignored
msg.factor = 0.45;
speed_scaling_pub->publish(msg);
traj_controller_->wait_for_trajectory(executor);
msg.factor = -0.12;
speed_scaling_pub->publish(msg);
traj_controller_->wait_for_trajectory(executor);
updateController();
executor.spin_some();
state = getState();
EXPECT_EQ(state->speed_scaling_factor, 0.45);
}

TEST_F(TrajectoryControllerTest, scaling_factor_from_param)
{
double initial_factor = 0.123;
rclcpp::executors::MultiThreadedExecutor executor;
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("scaling_factor_initial_default", initial_factor),
};
SetUpAndActivateTrajectoryController(executor, params);
subscribeToState(executor);
updateController();
// Spin to receive latest state
executor.spin_some();
auto state = getState();
EXPECT_EQ(state->speed_scaling_factor, initial_factor);
}

0 comments on commit 2abc954

Please sign in to comment.