trajectory_player_unit.cpp
Go to the documentation of this file.
1 
29 #include <gtest/gtest.h>
31 
34 
35 void CheckTrajectory(const tesseract_common::JointTrajectory& trajectory, int first, int last)
36 {
37  using namespace tesseract_visualization;
38  using namespace tesseract_common;
39 
40  TrajectoryPlayer player;
41  player.setTrajectory(trajectory);
42 
43  EXPECT_NEAR(player.trajectoryDurationBegin(), first, 1e-5);
44  EXPECT_NEAR(player.trajectoryDurationEnd(), last, 1e-5);
45  EXPECT_NEAR(player.currentDuration(), first, 1e-5);
46 
47  // Advance by index
48  for (long i = first; i <= last; ++i)
49  {
50  JointState s = player.setCurrentDurationByIndex(i - first);
51  EXPECT_NEAR(s.time, static_cast<double>(i), 1e-5);
52  EXPECT_NEAR(s.position(0), static_cast<double>(i), 1e-5);
53  }
54 
55  // Advance by time
56  for (long i = first; i <= last; ++i)
57  {
58  JointState s = player.setCurrentDuration(static_cast<double>(i));
59  EXPECT_NEAR(s.time, static_cast<double>(i), 1e-5);
60  EXPECT_NEAR(s.position(0), static_cast<double>(i), 1e-5);
61  }
62 
63  {
64  JointState s = player.setCurrentDurationByIndex((last - first) + 1);
65  EXPECT_NEAR(s.time, last, 1e-5);
66  EXPECT_NEAR(s.position(0), last, 1e-5);
67  }
68 
69  {
70  JointState s = player.setCurrentDuration(last + 1);
71  EXPECT_NEAR(s.time, last, 1e-5);
72  EXPECT_NEAR(s.position(0), last, 1e-5);
73  EXPECT_TRUE(player.isFinished());
74  }
75 
76  {
77  JointState s = player.setCurrentDuration(last + 2);
78  EXPECT_NEAR(s.time, last, 1e-5);
79  EXPECT_NEAR(s.position(0), last, 1e-5);
80  EXPECT_TRUE(player.isFinished());
81  player.setCurrentDuration(first);
82  EXPECT_FALSE(player.isFinished());
83  }
84 
85  {
87  EXPECT_NEAR(s.time, first, 1e-5);
88  EXPECT_NEAR(s.position(0), first, 1e-5);
89  }
90 
91  {
92  JointState s = player.setCurrentDuration(first - 1);
93  EXPECT_NEAR(s.time, first, 1e-5);
94  EXPECT_NEAR(s.position(0), first, 1e-5);
95  EXPECT_FALSE(player.isFinished());
96  }
97 }
98 
99 TEST(TesseracTrajectoryPlayerUnit, TrajectoryUntimedTest) // NOLINT
100 {
101  using namespace tesseract_visualization;
102  using namespace tesseract_common;
103 
104  std::vector<std::string> joint_names = { "joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6" };
105  JointTrajectory trajectory;
106  int first = 0;
107  int last = 9;
108  double auto_dt = 0.1; // Time step assigned by interpolator to untimed trajectory
109 
110  // Define trajectory without timing
111  for (long i = first; i <= last; ++i)
112  {
113  Eigen::VectorXd p = Eigen::VectorXd::Zero(6);
114  p(0) = static_cast<double>(i);
115  trajectory.push_back(JointState(joint_names, p));
116  }
117 
118  TrajectoryPlayer player;
119  player.setTrajectory(trajectory);
120 
121  EXPECT_NEAR(player.trajectoryDurationBegin(), first, 1e-5);
122  EXPECT_NEAR(player.trajectoryDurationEnd(), last * auto_dt, 1e-5);
123  EXPECT_NEAR(player.currentDuration(), first, 1e-5);
124 
125  {
126  JointState s = player.setCurrentDuration(0.5);
127  EXPECT_NEAR(s.position(0), 0.5 / auto_dt, 1e-5);
128  }
129 
130  {
132  EXPECT_NEAR(s.time, 6 * auto_dt, 1e-5);
133  }
134 }
135 
136 TEST(TesseracTrajectoryPlayerUnit, TrajectoryTimedTest) // NOLINT
137 {
138  using namespace tesseract_visualization;
139  using namespace tesseract_common;
140 
141  std::vector<std::string> joint_names = { "joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6" };
142  JointTrajectory trajectory;
143  int first = 0;
144  int last = 9;
145 
146  // Define trajectory with timing
147  trajectory.clear();
148  for (long i = first; i <= last; ++i)
149  {
150  Eigen::VectorXd p = Eigen::VectorXd::Zero(6);
151  p(0) = static_cast<double>(i);
152  trajectory.push_back(JointState(joint_names, p));
153  trajectory.back().time = static_cast<double>(i);
154  }
155 
156  CheckTrajectory(trajectory, first, last);
157 }
158 
159 TEST(TesseracTrajectoryPlayerUnit, TrajectoryNonzeroStartTest) // NOLINT
160 {
161  using namespace tesseract_visualization;
162  using namespace tesseract_common;
163 
164  std::vector<std::string> joint_names = { "joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6" };
165  JointTrajectory trajectory;
166  int first = 5;
167  int last = 14;
168 
169  // Define trajectory with timing and a non-zero begin
170  for (long i = first; i <= last; ++i)
171  {
172  Eigen::VectorXd p = Eigen::VectorXd::Zero(6);
173  p(0) = static_cast<double>(i);
174  trajectory.push_back(JointState(joint_names, p));
175  trajectory.back().time = static_cast<double>(i);
176  }
177 
178  CheckTrajectory(trajectory, first, last);
179 }
180 
181 TEST(TesseracTrajectoryInterpolatorUnit, TrajectoryInterpolatorTest) // NOLINT
182 {
183  using namespace tesseract_visualization;
184  using namespace tesseract_common;
185 
186  std::vector<std::string> joint_names = { "joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6" };
187  JointTrajectory trajectory;
188  double time_scale = 0.5;
189 
190  // Define trajectory
191  for (long i = 0; i < 10; ++i)
192  {
193  Eigen::VectorXd p = Eigen::VectorXd::Zero(6);
194  p(0) = static_cast<double>(i);
195  trajectory.push_back(JointState(joint_names, p));
196  trajectory.back().time = static_cast<double>(i) * time_scale;
197  }
198 
199  TrajectoryInterpolator interpolator(trajectory);
200 
201  EXPECT_EQ(interpolator.getStateCount(), 10);
202 
203  for (long i = 0; i < 19; ++i)
204  {
205  JointState s = interpolator.getState(static_cast<double>(i) * 0.5 * time_scale);
206  EXPECT_NEAR(s.time, static_cast<double>(i) * 0.5 * time_scale, 1e-5);
207  EXPECT_NEAR(s.position(0), static_cast<double>(i) * 0.5, 1e-5);
208  }
209 
210  // Test above max duration
211  JointState s = interpolator.getState(10);
212  EXPECT_NEAR(s.time, 9 * time_scale, 1e-5);
213  EXPECT_NEAR(s.position(0), 9, 1e-5);
214 
215  // Test get instruction duration
216  for (long i = 0; i < 10; ++i)
217  {
218  double duration = interpolator.getStateDuration(i);
219  EXPECT_NEAR(duration, static_cast<double>(i) * time_scale, 1e-5);
220  }
221 }
222 
223 int main(int argc, char** argv)
224 {
225  testing::InitGoogleTest(&argc, argv);
226 
227  return RUN_ALL_TESTS();
228 }
tesseract_visualization::TrajectoryInterpolator::getState
tesseract_common::JointState getState(double request_duration) const
Definition: trajectory_interpolator.cpp:101
tesseract_common
tesseract_common::JointTrajectory::push_back
void push_back(const value_type &&x)
duration
std::chrono::system_clock::duration duration
tesseract_visualization::TrajectoryPlayer::trajectoryDurationEnd
double trajectoryDurationEnd() const
Get the trajectory duration at the end state.
Definition: trajectory_player.cpp:136
tesseract_visualization::TrajectoryPlayer::setCurrentDuration
tesseract_common::JointState setCurrentDuration(double duration)
Set the current time for the player by duration.
Definition: trajectory_player.cpp:76
main
int main(int argc, char **argv)
Definition: trajectory_player_unit.cpp:223
tesseract_common::JointTrajectory::back
reference back()
tesseract_visualization::TrajectoryPlayer
Enables the ability to play a trajectory provided by the set program.
Definition: trajectory_player.h:42
tesseract_visualization::TrajectoryPlayer::currentDuration
double currentDuration() const
Get the current duration populated by the last call to getNext()
Definition: trajectory_player.cpp:132
tesseract_common::JointTrajectory::clear
void clear()
tesseract_visualization::TrajectoryInterpolator
Definition: trajectory_interpolator.h:42
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
trajectory_interpolator.h
Trajectory interpolator class.
TEST
TEST(TesseracTrajectoryPlayerUnit, TrajectoryUntimedTest)
Definition: trajectory_player_unit.cpp:99
EXPECT_TRUE
#define EXPECT_TRUE(args)
tesseract_common::JointState
tesseract_visualization::TrajectoryPlayer::setTrajectory
void setTrajectory(const tesseract_common::JointTrajectory &trajectory)
Set the the trajectory for the trajectory player.
Definition: trajectory_player.cpp:40
EXPECT_NEAR
#define EXPECT_NEAR(a, b, prec)
tesseract_visualization::TrajectoryPlayer::setCurrentDurationByIndex
tesseract_common::JointState setCurrentDurationByIndex(long index)
Set the current time for the player by index of the input trajectoy.
Definition: trajectory_player.cpp:55
tesseract_common::JointTrajectory
tesseract_visualization::TrajectoryPlayer::isFinished
bool isFinished() const
Check if the player has the reached the end of the trajectory.
Definition: trajectory_player.cpp:138
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_visualization::TrajectoryInterpolator::getStateCount
long getStateCount() const
Definition: trajectory_interpolator.cpp:143
tesseract_visualization::TrajectoryInterpolator::getStateDuration
double getStateDuration(long index) const
Definition: trajectory_interpolator.cpp:131
tesseract_common::JointState::time
double time
trajectory_player.h
Trajectory player class.
tesseract_common::JointState::position
Eigen::VectorXd position
macros.h
tesseract_visualization::TrajectoryPlayer::trajectoryDurationBegin
double trajectoryDurationBegin() const
Get the trajectory duration at the begin state.
Definition: trajectory_player.cpp:134
CheckTrajectory
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH TESSERACT_COMMON_IGNORE_WARNINGS_POP void CheckTrajectory(const tesseract_common::JointTrajectory &trajectory, int first, int last)
Definition: trajectory_player_unit.cpp:35
EXPECT_EQ
#define EXPECT_EQ(a, b)
EXPECT_FALSE
#define EXPECT_FALSE(args)
tesseract_visualization
Definition: fwd.h:4


tesseract_visualization
Author(s): Levi Armstrong
autogenerated on Wed Apr 9 2025 03:03:25