test_traj.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by qiayuan on 3/21/20.
36 //
37 
38 #include "rm_common/traj_gen.h"
39 #include <ros/ros.h>
40 //#include <rm_msgs/Joint.h>
41 
42 int main(int argc, char** argv)
43 {
44  ros::init(argc, argv, "traj_test");
45 
46  ros::NodeHandle nh;
47  ros::Publisher pub;
48  pub = nh.advertise<rm_msgs::Joint>("test_cmd", 100);
49  rm_msgs::Joint cmd{};
50 
51  RampTraj<double> traj;
52  traj.setLimit(1.);
53  traj.setState(0., 1., 0.);
54  ros::Rate loop_rate(100);
55 
56  if (!traj.calc(2.5))
57  ROS_ERROR("Acc too small");
58  else
59  {
60  pub.publish(cmd);
61  loop_rate.sleep();
62  pub.publish(cmd);
63  loop_rate.sleep();
64  pub.publish(cmd);
65  loop_rate.sleep();
66  pub.publish(cmd);
67  loop_rate.sleep();
68  pub.publish(cmd);
69  loop_rate.sleep();
70  pub.publish(cmd);
71  loop_rate.sleep();
72  pub.publish(cmd);
73  loop_rate.sleep();
74  pub.publish(cmd);
75  loop_rate.sleep();
76 
77  double t = -1.;
78 
79  while (ros::ok() && !traj.isReach(t))
80  {
81  cmd.q_des[0] = traj.getPos(t);
82  cmd.qd_des[0] = traj.getVel(t);
83  cmd.ff[0] = traj.getAcc(t);
84  t += 0.01;
85  pub.publish(cmd);
86 
87  loop_rate.sleep();
88  }
89  }
90  cmd.q_des[0] = 0.;
91  cmd.qd_des[0] = 0.;
92  cmd.ff[0] = 0.;
93 
94  pub.publish(cmd);
95  loop_rate.sleep();
96  pub.publish(cmd);
97  loop_rate.sleep();
98  pub.publish(cmd);
99  loop_rate.sleep();
100  pub.publish(cmd);
101  loop_rate.sleep();
102  pub.publish(cmd);
103  loop_rate.sleep();
104  pub.publish(cmd);
105  loop_rate.sleep();
106  pub.publish(cmd);
107  loop_rate.sleep();
108  pub.publish(cmd);
109  loop_rate.sleep();
110  MinTimeTraj<double> min_traj;
111  min_traj.setLimit(1., 1., 0.01);
112  min_traj.setTarget(1.);
113  double s[3]{};
114  while (ros::ok() && !min_traj.isReach())
115  {
116  s[2] = min_traj.getTau(s[0], s[1]) / 1.;
117  s[1] += 0.01 * s[2];
118  s[0] += 0.01 * s[1];
119 
120  cmd.q_des[0] = s[0];
121  cmd.qd_des[0] = s[1];
122  cmd.ff[0] = s[2];
123  pub.publish(cmd);
124  loop_rate.sleep();
125  }
126  return 0;
127 }
MinTimeTraj::getTau
T getTau(T pos, T vel)
Definition: traj_gen.h:190
MinTimeTraj::setLimit
void setLimit(T max_tau, T inertia, T tolerance)
Definition: traj_gen.h:174
RampTraj::getAcc
T getAcc(T t)
Definition: traj_gen.h:171
ros::Publisher
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
s
XmlRpcServer s
ros.h
main
int main(int argc, char **argv)
Definition: test_traj.cpp:42
traj_gen.h
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
ros::ok
ROSCPP_DECL bool ok()
MinTimeTraj
Definition: traj_gen.h:163
RampTraj::getPos
T getPos(T t)
Definition: traj_gen.h:131
ros::Rate::sleep
bool sleep()
RampTraj::isReach
bool isReach(T t)
Definition: traj_gen.h:102
MinTimeTraj::isReach
bool isReach()
Definition: traj_gen.h:186
RampTraj::setState
void setState(T start, T end, T time_now)
Definition: traj_gen.h:96
RampTraj
Definition: traj_gen.h:45
ROS_ERROR
#define ROS_ERROR(...)
MinTimeTraj::setTarget
void setTarget(T target)
Definition: traj_gen.h:181
ros::Rate
RampTraj::setLimit
void setLimit(T max_acc)
Definition: traj_gen.h:92
cmd
string cmd
t
geometry_msgs::TransformStamped t
ros::NodeHandle
RampTraj::calc
bool calc(T t)
Definition: traj_gen.h:107
RampTraj::getVel
T getVel(T t)
Definition: traj_gen.h:152


rm_common
Author(s):
autogenerated on Sun Apr 6 2025 02:22:01