test_planner_2dof_serial_joints.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
31 #include <planner_cspace_msgs/PlannerStatus.h>
32 #include <sensor_msgs/JointState.h>
33 #include <trajectory_msgs/JointTrajectory.h>
34 
35 #include <gtest/gtest.h>
36 
37 TEST(Planner2DOFSerialJoints, Plan)
38 {
39  ros::NodeHandle nh;
40  ros::Publisher pub_state = nh.advertise<sensor_msgs::JointState>("joint_states", 1, true);
41  ros::Publisher pub_cmd = nh.advertise<trajectory_msgs::JointTrajectory>("trajectory_in", 1, true);
42 
43  trajectory_msgs::JointTrajectory::ConstPtr planned;
44  const auto cb_plan = [&planned](const trajectory_msgs::JointTrajectory::ConstPtr& msg)
45  {
46  planned = msg;
47  };
48  ros::Subscriber sub_plan = nh.subscribe<trajectory_msgs::JointTrajectory>("joint_trajectory", 1, cb_plan);
49 
50  planner_cspace_msgs::PlannerStatus::ConstPtr status;
51  const auto cb_status = [&status](const planner_cspace_msgs::PlannerStatus::ConstPtr& msg)
52  {
53  status = msg;
54  };
55  ros::Subscriber sub_status = nh.subscribe<planner_cspace_msgs::PlannerStatus>(
56  "/planner_2dof_serial_joints/group0/status", 1, cb_status);
57 
58  sensor_msgs::JointState s;
59  s.name.push_back("front");
60  s.position.push_back(-1.57);
61  s.name.push_back("rear");
62  s.position.push_back(0.0);
63 
64  trajectory_msgs::JointTrajectory cmd;
65  cmd.joint_names.push_back("front");
66  cmd.joint_names.push_back("rear");
67  trajectory_msgs::JointTrajectoryPoint p;
68  p.positions.push_back(-4.71);
69  p.positions.push_back(0.0);
70  cmd.points.push_back(p);
71 
72  ros::Rate rate(1);
73  const ros::Time deadline = ros::Time::now() + ros::Duration(10);
74  int cnt = 0;
75  while (ros::ok())
76  {
77  if (ros::Time::now() > deadline)
78  {
79  FAIL() << "Timeout";
80  }
81 
82  pub_state.publish(s);
83  pub_cmd.publish(cmd);
84 
85  ros::spinOnce();
86  rate.sleep();
87  if (planned && status)
88  {
89  cnt++;
90  if (cnt > 5)
91  {
92  break;
93  }
94  }
95  }
96  ASSERT_TRUE(ros::ok());
97 
98  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::DOING, status->status);
99  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::GOING_WELL, status->error);
100 
101  ASSERT_EQ(2u, planned->joint_names.size());
102  ASSERT_EQ("front", planned->joint_names[0]);
103  ASSERT_EQ("rear", planned->joint_names[1]);
104 
105  // collision at front=-3.14, rear=0.0 must be avoided.
106  const float fc = -3.14;
107  const float rc = 0.0;
108  for (int i = 1; i < static_cast<int>(planned->points.size()); ++i)
109  {
110  const trajectory_msgs::JointTrajectoryPoint& p0 = planned->points[i - 1];
111  const trajectory_msgs::JointTrajectoryPoint& p1 = planned->points[i];
112  ASSERT_EQ(2u, p0.positions.size());
113  ASSERT_EQ(2u, p1.positions.size());
114 
115  const float f0 = p0.positions[0];
116  const float r0 = p0.positions[1];
117  const float f1 = p1.positions[0];
118  const float r1 = p1.positions[1];
119 
120  ASSERT_LT(-6.28, f0);
121  ASSERT_GT(0.0, f0);
122  ASSERT_LT(-3.14, r0);
123  ASSERT_GT(3.14, r0);
124  ASSERT_LT(-6.28, f1);
125  ASSERT_GT(0.0, f1);
126  ASSERT_LT(-3.14, r1);
127  ASSERT_GT(3.14, r1);
128 
129  const float front_diff = f1 - f0;
130  const float rear_diff = r1 - r0;
131 
132  const float d =
133  std::abs(
134  rear_diff * fc -
135  front_diff * rc +
136  f1 * r0 -
137  r1 * f0) /
138  std::hypot(front_diff, rear_diff);
139  // std::cerr << d << std::endl;
140  ASSERT_GT(d, 0.15);
141 
142  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::DOING, status->status);
143  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::GOING_WELL, status->error);
144  }
145 }
146 
147 TEST(Planner2DOFSerialJoints, NoPath)
148 {
149  ros::NodeHandle nh;
150  ros::Publisher pub_state = nh.advertise<sensor_msgs::JointState>("joint_states", 1, true);
151  ros::Publisher pub_cmd = nh.advertise<trajectory_msgs::JointTrajectory>("trajectory_in", 1, true);
152 
153  planner_cspace_msgs::PlannerStatus::ConstPtr status;
154  const auto cb_status = [&status](const planner_cspace_msgs::PlannerStatus::ConstPtr& msg)
155  {
156  status = msg;
157  };
158  ros::Subscriber sub_status = nh.subscribe<planner_cspace_msgs::PlannerStatus>(
159  "/planner_2dof_serial_joints/group0/status", 1, cb_status);
160 
161  sensor_msgs::JointState s;
162  s.name.push_back("front");
163  s.position.push_back(-1.57);
164  s.name.push_back("rear");
165  s.position.push_back(0.0);
166 
167  trajectory_msgs::JointTrajectory cmd;
168  cmd.joint_names.push_back("front");
169  cmd.joint_names.push_back("rear");
170  trajectory_msgs::JointTrajectoryPoint p;
171  p.positions.push_back(3.14);
172  p.positions.push_back(0.0); // Collided state
173  cmd.points.push_back(p);
174 
175  ros::Rate rate(1);
176  const ros::Time deadline = ros::Time::now() + ros::Duration(10);
177  int cnt = 0;
178  while (ros::ok())
179  {
180  if (ros::Time::now() > deadline)
181  {
182  FAIL() << "Timeout";
183  }
184 
185  pub_state.publish(s);
186  pub_cmd.publish(cmd);
187 
188  ros::spinOnce();
189  rate.sleep();
190  if (status)
191  {
192  cnt++;
193  if (cnt > 5)
194  {
195  break;
196  }
197  }
198  }
199  ASSERT_TRUE(ros::ok());
200 
201  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::DOING, status->status);
202  ASSERT_EQ(planner_cspace_msgs::PlannerStatus::PATH_NOT_FOUND, status->error);
203 }
204 
205 int main(int argc, char** argv)
206 {
207  testing::InitGoogleTest(&argc, argv);
208  ros::init(argc, argv, "test_planner_2dof_serial_joints");
209 
210  return RUN_ALL_TESTS();
211 }
d
msg
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
string cmd
XmlRpcServer s
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
int main(int argc, char **argv)
static Time now()
ROSCPP_DECL void spinOnce()
TEST(Planner2DOFSerialJoints, Plan)


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 3 2023 02:39:06