45 #include <rclcpp/rclcpp.hpp>
46 #include <std_srvs/srv/trigger.hpp>
47 #include <std_msgs/msg/int8.hpp>
48 #include <std_msgs/msg/float64.hpp>
49 #include <std_msgs/msg/float64_multi_array.hpp>
50 #include <control_msgs/msg/joint_jog.hpp>
51 #include <geometry_msgs/msg/twist_stamped.hpp>
52 #include <trajectory_msgs/msg/joint_trajectory.hpp>
53 #include <sensor_msgs/msg/joint_state.hpp>
54 #include <moveit_msgs/srv/change_drift_dimensions.hpp>
55 #include <moveit_msgs/srv/change_control_dimensions.hpp>
58 #include <gtest/gtest.h>
66 static const rclcpp::Logger
LOGGER = rclcpp::get_logger(
"moveit_servo.servo_launch_test_common.hpp");
81 :
node_(std::make_shared<
rclcpp::Node>(
"servo_testing"))
82 ,
executor_(std::make_shared<
rclcpp::executors::SingleThreadedExecutor>())
88 RCLCPP_FATAL(LOGGER,
"Failed to load the servo parameters");
94 auto test_parameters = std::make_shared<struct TestParameters>();
96 test_parameters->publish_period = 1.0 / test_parameters->publish_hz;
97 test_parameters->timeout_iterations = 50 * test_parameters->publish_hz;
98 test_parameters->servo_node_name =
"/servo_node";
125 if (topic_name.at(0) ==
'~')
140 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
143 RCLCPP_INFO(LOGGER,
"client_servo_start_ service not available, waiting again...");
144 rclcpp::sleep_for(std::chrono::milliseconds(500));
153 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
156 RCLCPP_INFO(LOGGER,
"client_servo_stop_ service not available, waiting again...");
157 rclcpp::sleep_for(std::chrono::milliseconds(500));
163 [
this](
const std_msgs::msg::Int8::ConstSharedPtr& msg) {
return statusCB(msg); });
174 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
177 RCLCPP_INFO(LOGGER,
"client_servo_pause_ service not available, waiting again...");
178 rclcpp::sleep_for(std::chrono::milliseconds(500));
190 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
193 RCLCPP_INFO(LOGGER,
"client_servo_unpause_ service not available, waiting again...");
194 rclcpp::sleep_for(std::chrono::milliseconds(500));
207 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
210 RCLCPP_INFO(LOGGER,
"client_change_control_dims_ service not available, waiting again...");
211 rclcpp::sleep_for(std::chrono::milliseconds(500));
224 RCLCPP_ERROR(LOGGER,
"Interrupted while waiting for the service. Exiting.");
227 RCLCPP_INFO(LOGGER,
"client_change_drift_dims_ service not available, waiting again...");
228 rclcpp::sleep_for(std::chrono::milliseconds(500));
237 [
this](
const std_msgs::msg::Float64::ConstSharedPtr& msg) {
return collisionScaleCB(msg); });
243 if (command_type ==
"trajectory_msgs/JointTrajectory")
247 [
this](
const trajectory_msgs::msg::JointTrajectory::ConstSharedPtr& msg) {
return trajectoryCommandCB(msg); });
250 else if (command_type ==
"std_msgs/Float64MultiArray")
254 [
this](
const std_msgs::msg::Float64MultiArray::ConstSharedPtr& msg) {
return arrayCommandCB(msg); });
259 RCLCPP_ERROR(LOGGER,
"Invalid command type for Servo output command");
268 [
this](
const sensor_msgs::msg::JointState::ConstSharedPtr& msg) {
return jointStateCB(msg); });
272 void statusCB(
const std_msgs::msg::Int8::ConstSharedPtr& msg)
288 void jointStateCB(
const sensor_msgs::msg::JointState::ConstSharedPtr& msg)
402 auto time_start =
node_->now();
403 auto start_result =
client_servo_start_->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());
404 if (!start_result.get()->success)
406 RCLCPP_ERROR(LOGGER,
"Error returned form service call to start servo");
409 RCLCPP_INFO_STREAM(LOGGER,
"Wait for start servo: " << (
node_->now() - time_start).seconds());
413 time_start =
node_->now();
415 size_t iterations = 0;
418 publish_loop_rate.sleep();
421 RCLCPP_INFO_STREAM(LOGGER,
"Wait for status num increasing: " << (
node_->now() - time_start).seconds());
425 RCLCPP_ERROR(LOGGER,
"Timeout waiting for status num increasing");
434 auto time_start =
node_->now();
435 auto stop_result =
client_servo_stop_->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());
436 if (!stop_result.get()->success)
438 RCLCPP_ERROR(LOGGER,
"Error returned form service call to stop servo");
bool setupUnpauseClient()
bool setupControlDimsClient()
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr client_servo_stop_
void arrayCommandCB(const std_msgs::msg::Float64MultiArray::ConstSharedPtr &msg)
std_msgs::msg::Float64MultiArray::ConstSharedPtr latest_array_cmd_
rclcpp::Executor::SharedPtr executor_
bool setupDriftDimsClient()
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr client_servo_pause_
std::string resolveServoTopicName(std::string topic_name)
void trajectoryCommandCB(const trajectory_msgs::msg::JointTrajectory::ConstSharedPtr &msg)
rclcpp::Client< moveit_msgs::srv::ChangeDriftDimensions >::SharedPtr client_change_drift_dims_
rclcpp::Publisher< control_msgs::msg::JointJog >::SharedPtr pub_joint_cmd_
void jointStateCB(const sensor_msgs::msg::JointState::ConstSharedPtr &msg)
sensor_msgs::msg::JointState::ConstSharedPtr latest_joint_state_
std_msgs::msg::Float64MultiArray getLatestArrayCommand()
rclcpp::Subscription< std_msgs::msg::Float64 >::SharedPtr sub_collision_scale_
bool setupJointStateSub()
std::mutex latest_state_mutex_
double latest_collision_scale_
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr client_servo_start_
void watchForStatus(StatusCode code_to_watch_for)
sensor_msgs::msg::JointState getLatestJointState()
size_t num_collision_scale_
trajectory_msgs::msg::JointTrajectory getLatestTrajCommand()
void resetNumJointState()
rclcpp::Subscription< sensor_msgs::msg::JointState >::SharedPtr sub_joint_state_
StatusCode status_tracking_code_
size_t getNumJointState()
rclcpp::Subscription< std_msgs::msg::Float64MultiArray >::SharedPtr sub_array_cmd_output_
rclcpp::Subscription< std_msgs::msg::Int8 >::SharedPtr sub_servo_status_
void collisionScaleCB(const std_msgs::msg::Float64::ConstSharedPtr &msg)
void statusCB(const std_msgs::msg::Int8::ConstSharedPtr &msg)
rclcpp::Publisher< geometry_msgs::msg::TwistStamped >::SharedPtr pub_twist_cmd_
void resetNumCollisionScale()
rclcpp::Node::SharedPtr node_
bool setupCommandSub(const std::string &command_type)
StatusCode latest_status_
std::thread executor_thread_
bool setupCollisionScaleSub()
size_t getNumCollisionScale()
rclcpp::Client< moveit_msgs::srv::ChangeControlDimensions >::SharedPtr client_change_control_dims_
std::shared_ptr< const struct TestParameters > test_parameters_
double getLatestCollisionScale()
trajectory_msgs::msg::JointTrajectory::ConstSharedPtr latest_traj_cmd_
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr client_servo_unpause_
std::shared_ptr< const moveit_servo::ServoParameters > servo_parameters_
rclcpp::Subscription< trajectory_msgs::msg::JointTrajectory >::SharedPtr sub_trajectory_cmd_output_
StatusCode getLatestStatus()
const rclcpp::Logger LOGGER
std::string servo_node_name
double timeout_iterations
static SharedConstPtr makeServoParameters(const rclcpp::Node::SharedPtr &node, const std::string &ns="moveit_servo", bool dynamic_parameters=true)