quadrotor_bag_builder.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 #include <ros/ros.h>
31 #include <rosbag/bag.h>
32 
33 #include <xpp_msgs/RobotStateJoint.h>
34 #include <xpp_states/convert.h>
35 #include <xpp_states/state.h>
36 
37 
38 using namespace xpp;
39 
40 int main(int argc, char *argv[])
41 {
42  // creates a bag file wherever executable is run (usually ~/.ros/)
43  rosbag::Bag bag;
44  bag.open("quadrotor_traj.bag", rosbag::bagmode::Write);
45 
46  // visualize the state of a drone (only has a base)
47  State3d base;
48 
49  // create a sequence of states for a total duration of T spaced 0.01s apart.
50  double T = 2*M_PI;
51  double dt = 0.01;
52  double t = 1e-6;
53  while (t < T)
54  {
55  // base and foot follow half a sine motion up and down
56  base.lin.p_.z() = 0.7 - 0.5*sin(t); //[m]
57  base.lin.p_.x() = 1.0/T*t; //[m]
58 
59  double roll = 30./180*M_PI*sin(t);
60  base.ang.q = GetQuaternionFromEulerZYX(0.0, 0.0, roll);
61 
62  // save the state message with current time in the bag
63  auto timestamp = ::ros::Time(t);
64 
65  xpp_msgs::RobotStateJoint msg;
66  msg.base = Convert::ToRos(base);
67  bag.write("xpp/joint_quadrotor_des", timestamp, msg);
68 
69  t += dt;
70  }
71 
72  std::string bag_file = bag.getFileName(); // save location before closing
73  bag.close();
74 
75  // plays back the states at desired speeds.
76  // can now also use all the tools from rosbag to pause, speed-up, inspect,
77  // debug the trajectory.
78  // e.g. rosbag -play -r 0.2 /path/to/bag.bag
79  int success = system(("rosbag play " + bag_file).c_str());
80 
81  return 0;
82 }
83 
Quaterniond q
static xpp_msgs::StateLin3d ToRos(const StateLin3d &xpp)
void open(std::string const &filename, uint32_t mode=bagmode::Read)
std::string getFileName() const
static Quaterniond GetQuaternionFromEulerZYX(double yaw, double pitch, double roll)
void close()
int main(int argc, char *argv[])
StateAng3d ang
StateLin3d lin
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
void write(std::string const &topic, ros::MessageEvent< T > const &event)


xpp_examples
Author(s): Alexander W. Winkler
autogenerated on Tue Dec 8 2020 03:10:41