kinematics_benchmark.cc
Go to the documentation of this file.
1 
2 
3 #include <boost/program_options.hpp>
4 
5 #include "rdl_benchmark/Timer.h"
7 #include <rdl_dynamics/Model.h>
9 
10 struct Samples
11 {
12  std::vector<RobotDynamics::Math::VectorNd, Eigen::aligned_allocator<RobotDynamics::Math::VectorNd>> q_vec;
13  std::vector<RobotDynamics::Math::VectorNd, Eigen::aligned_allocator<RobotDynamics::Math::VectorNd>> qdot_vec;
14  std::vector<RobotDynamics::Math::VectorNd, Eigen::aligned_allocator<RobotDynamics::Math::VectorNd>> qddot_vec;
15  std::vector<RobotDynamics::Math::VectorNd, Eigen::aligned_allocator<RobotDynamics::Math::VectorNd>> tau_vec;
16 };
17 
18 Samples fillRandom(unsigned int q_size, unsigned int qdot_size, unsigned int num_samples)
19 {
20  Samples samples;
21  RobotDynamics::Math::VectorNd q = RobotDynamics::Math::VectorNd::Zero(q_size);
22  RobotDynamics::Math::VectorNd qdot = RobotDynamics::Math::VectorNd::Zero(qdot_size);
23  RobotDynamics::Math::VectorNd qddot = RobotDynamics::Math::VectorNd::Zero(qdot_size);
24  RobotDynamics::Math::VectorNd tau = RobotDynamics::Math::VectorNd::Zero(qdot_size);
25 
26  for(unsigned int i = 0; i < num_samples; i++)
27  {
28  for(unsigned int j = 0; j < q_size; j++)
29  {
30  q[j] = (static_cast<double>(rand()) / static_cast<double>(RAND_MAX)) * 2. - 1.;
31  }
32 
33  for(unsigned int j = 0; j < qdot_size; j++)
34  {
35  qdot[j] = (static_cast<double>(rand()) / static_cast<double>(RAND_MAX)) * 2. - 1.;
36  qddot[j] = (static_cast<double>(rand()) / static_cast<double>(RAND_MAX)) * 2. - 1.;
37  tau[j] = (static_cast<double>(rand()) / static_cast<double>(RAND_MAX)) * 2. - 1.;
38  }
39 
40  samples.q_vec.push_back(q);
41  samples.qdot_vec.push_back(qdot);
42  samples.qddot_vec.push_back(qddot);
43  samples.tau_vec.push_back(tau);
44  }
45 
46  return samples;
47 }
48 
49 int main(int argc, char *argv[])
50 {
51  std::string urdf_file;
52  unsigned int num_samples;
53 
54  boost::program_options::options_description description("Command line options kinematics benchmark");
55 
56  description.add_options()("help,h", "Display help options")("urdf", boost::program_options::value<std::string>(&urdf_file),
57  "Path to the urdf")("samples", boost::program_options::value<unsigned int>(&num_samples),
58  "Number of benchmark samples to run");
59 
60  boost::program_options::variables_map command_line_args;
61  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), command_line_args);
62  boost::program_options::notify(command_line_args);
63 
64  if (command_line_args.count("help"))
65  {
66  std::cout << description << std::endl;
67  return 0;
68  }
69 
70  if (command_line_args.count("urdf"))
71  {
72  urdf_file = command_line_args["urdf"].as<std::string>();
73  }
74 
75  num_samples = command_line_args.count("samples") ? command_line_args["samples"].as<unsigned int>() : 10000; // default 10,000 samples
76 
78  if(!RobotDynamics::Urdf::urdfReadFromFile(urdf_file, model))
79  {
80  std::cerr << "Error reading urdf file" << std::endl;
81  return 1;
82  }
83 
84  Samples samples = fillRandom(model->q_size, model->qdot_size, num_samples);
85 
86  double duration = 0.;
87  TimerInfo tinfo;
88  timer_start(&tinfo);
89 
90  for(unsigned int i = 0; i < num_samples; i++)
91  {
92  RobotDynamics::updateKinematics(*model, samples.q_vec[i], samples.qdot_vec[i], samples.qddot_vec[i]);
93  }
94 
95  duration = timer_stop(&tinfo);
96 
97  std::cout << "Kinematics for " << num_samples << " took " << duration << "s. Average calculation is " << duration/num_samples << std::endl;
98 
99  timer_start(&tinfo);
100 
101  for(unsigned int i = 0; i < num_samples; i++)
102  {
103  RobotDynamics::updateKinematicsParallel(*model, samples.q_vec[i], samples.qdot_vec[i], samples.qddot_vec[i]);
104  }
105 
106  duration = timer_stop(&tinfo);
107 
108  std::cout << "Parallel kinematics for " << num_samples << " took " << duration << "s. Average calculation is " << duration/num_samples << std::endl;
109 
110  timer_start(&tinfo);
111 
112  for(unsigned int i = 0; i < num_samples; i++)
113  {
114  RobotDynamics::updateKinematicsCustom(*model, &samples.q_vec[i], &samples.qdot_vec[i], nullptr);
115  }
116 
117  duration = timer_stop(&tinfo);
118 
119  std::cout << "Kinematics custom of only q/qdot for " << num_samples << " took " << duration << "s. Average calculation is " << duration/num_samples << std::endl;
120 
121  timer_start(&tinfo);
122 
123  for(unsigned int i = 0; i < num_samples; i++)
124  {
125  RobotDynamics::updateKinematicsCustomParallel(*model, &samples.q_vec[i], &samples.qdot_vec[i], &samples.qddot_vec[i]);
126  }
127 
128  duration = timer_stop(&tinfo);
129 
130  std::cout << "Parallel kinematics custom of only q/qdot for " << num_samples << " took " << duration << "s. Average calculation is " << duration/num_samples << std::endl;
131 
132  return 0;
133 }
Samples fillRandom(unsigned int q_size, unsigned int qdot_size, unsigned int num_samples)
std::shared_ptr< Model > ModelPtr
std::vector< RobotDynamics::Math::VectorNd, Eigen::aligned_allocator< RobotDynamics::Math::VectorNd > > tau_vec
double timer_stop(TimerInfo *timer)
Definition: Timer.h:27
void updateKinematicsParallel(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &QDDot)
Eigen::VectorXd VectorNd
void updateKinematics(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &QDDot)
void timer_start(TimerInfo *timer)
Definition: Timer.h:18
std::vector< RobotDynamics::Math::VectorNd, Eigen::aligned_allocator< RobotDynamics::Math::VectorNd > > q_vec
void updateKinematicsCustom(Model &model, const Math::VectorNd *Q, const Math::VectorNd *QDot=nullptr, const Math::VectorNd *QDDot=nullptr)
std::vector< RobotDynamics::Math::VectorNd, Eigen::aligned_allocator< RobotDynamics::Math::VectorNd > > qddot_vec
bool urdfReadFromFile(const char *filename, ModelPtr model, bool floating_base, bool verbose=false)
Definition: Timer.h:6
std::vector< RobotDynamics::Math::VectorNd, Eigen::aligned_allocator< RobotDynamics::Math::VectorNd > > qdot_vec
void updateKinematicsCustomParallel(Model &model, const Math::VectorNd *Q, const Math::VectorNd *QDot=nullptr, const Math::VectorNd *QDDot=nullptr)
int main(int argc, char *argv[])


rdl_benchmark
Author(s):
autogenerated on Tue Apr 20 2021 02:25:39