netft_utils_sim.cpp
Go to the documentation of this file.
1 
2 /*
3 Copyright (c) 2016, Los Alamos National Security, LLC
4 All rights reserved.
5 Copyright 2016. Los Alamos National Security, LLC. This software was produced under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which is operated by Los Alamos National Security, LLC for the U.S. Department of Energy. The U.S. Government has rights to use, reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to produce derivative works, such modified software should be clearly marked, so as not to confuse it with the version available from LANL.
6 
7 Additionally, redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 3. Neither the name of Los Alamos National Security, LLC, Los Alamos National Laboratory, LANL, the U.S. Government, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 
12 THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 
14 Author: Alex von Sternberg
15 */
16 
17 #include "ros/ros.h"
18 #include "geometry_msgs/WrenchStamped.h"
19 #include "netft_utils/StartSim.h"
20 #include "netft_utils/StopSim.h"
21 #include <math.h>
22 
27 // SIM vars
28 enum SimType
29 {
32  TYPE_EXP = 2,
34 };
35 enum Dim {DIM_X = 0,
36  DIM_Y = 1,
37  DIM_Z = 2,
38  DIM_RX = 3,
39  DIM_RY = 4,
40  DIM_RZ = 5};
41 
42 geometry_msgs::WrenchStamped simWrench; // Wrench containing the simulated data
43 bool toSim = false; // True if we should output simulated data
44 SimType simType; // Defines the simulation profile type
45 Dim simDim; // Dimension to simulate FT in
46 double simStart; // Time that the simulation started
47 double simSlope; // Parameter to tweak how fast force increases
48 double maxForce = 0.0; // Max force to be simulated
49 double upSlope = true; // True if the triangle wave is on up slope
50 
51 void setWrench(double x, double y, double z, double rx, double ry, double rz)
52 {
53  simWrench.wrench.force.x = x;
54  simWrench.wrench.force.y = y;
55  simWrench.wrench.force.z = z;
56  simWrench.wrench.torque.x = rx;
57  simWrench.wrench.torque.y = ry;
58  simWrench.wrench.torque.z = rz;
59 }
60 
61 void simData()
62 {
63  if(toSim)
64  {
65  double ft = 0.0;
66  double deltaT = ros::Time::now().toSec() - simStart;
67  switch (simType)
68  {
69  case TYPE_ZEROS:
70  break;
71  case TYPE_LINEAR:
72  {
73  if((deltaT * simSlope) < maxForce)
74  ft = deltaT * simSlope;
75  else
76  ft = maxForce;
77  }
78  break;
79  case TYPE_EXP:
80  {
81  if((pow(deltaT,2) * simSlope) < maxForce)
82  ft = pow(deltaT,2) * simSlope;
83  else
84  ft = maxForce;
85  }
86  break;
87  case TYPE_TRIANGLE:
88  {
89  if((deltaT * simSlope) < maxForce && upSlope)
90  {
91  ft = deltaT * simSlope;
92  }
93  else if((deltaT * simSlope) >= maxForce && upSlope)
94  {
95  ft = maxForce;
96  upSlope = false;
97  }
98  else if(2*maxForce - deltaT*simSlope > 0.0)
99  {
100  ft = 2*maxForce - deltaT*simSlope;
101  }
102  else
103  {
104  ft = 0.0;
105  }
106  }
107  break;
108  default:
109  ROS_ERROR("Sim type invalid. Stopping sim.");
110  toSim = false;
111  break;
112  }
113  switch (simDim)
114  {
115  case DIM_X:
116  setWrench(ft, 0.0, 0.0, 0.0, 0.0, 0.0);
117  break;
118  case DIM_Y:
119  setWrench(0.0, ft, 0.0, 0.0, 0.0, 0.0);
120  break;
121  case DIM_Z:
122  setWrench(0.0, 0.0, ft, 0.0, 0.0, 0.0);
123  break;
124  case DIM_RX:
125  setWrench(0.0, 0.0, 0.0, ft, 0.0, 0.0);
126  break;
127  case DIM_RY:
128  setWrench(0.0, 0.0, 0.0, 0.0, ft, 0.0);
129  break;
130  case DIM_RZ:
131  setWrench(0.0, 0.0, 0.0, 0.0, 0.0, ft);
132  break;
133  default:
134  ROS_ERROR("Dimension invalid. Stopping sim.");
135  toSim = false;
136  break;
137  }
138  }
139  else
140  {
141  upSlope = true;
142  setWrench(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
143  }
144 }
145 
146 bool startSim(netft_utils::StartSim::Request &req, netft_utils::StartSim::Response &res)
147 {
149  simDim = (Dim)req.simDim;
150  simType = (SimType)req.simType;
151  simSlope = req.simSlope;
152  maxForce = req.maxForce;
153  toSim = true;
154  return true;
155 }
156 
157 bool stopSim(netft_utils::StopSim::Request &req, netft_utils::StopSim::Response &res)
158 {
159  toSim = false;
160  return true;
161 }
162 
163 int main(int argc, char **argv)
164 {
165  //Node name
166  ros::init(argc, argv, "netft_utils_sim");
167 
168  //Access main ROS system
169  ros::NodeHandle n;
170 
171  //Publish on the /netft_transformed_data topic. Queue up to 100000 data points
172  ros::Publisher netft_data_pub = n.advertise<geometry_msgs::WrenchStamped>("netft_data", 100000);
173 
174  //Advertise bias and threshold services
175  ros::ServiceServer start_service = n.advertiseService("start_sim", startSim);
176  ros::ServiceServer stop_service = n.advertiseService("stop_sim", stopSim);
177 
178  ros::Rate loop_rate(400);
179 
180  //Initialize variables
181  upSlope = true;
182 
183  while ( ros::ok() )
184  {
185  simData();
186 
187  // Publish transformed dat
188  netft_data_pub.publish( simWrench );
189 
190  loop_rate.sleep();
191  ros::spinOnce();
192  }
193 
194  return 0;
195 }
int main(int argc, char **argv)
SimType simType
void publish(const boost::shared_ptr< M > &message) const
geometry_msgs::WrenchStamped simWrench
bool toSim
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
double maxForce
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool startSim(netft_utils::StartSim::Request &req, netft_utils::StartSim::Response &res)
Dim simDim
double simSlope
ROSCPP_DECL bool ok()
void simData()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
void setWrench(double x, double y, double z, double rx, double ry, double rz)
static Time now()
ROSCPP_DECL void spinOnce()
double upSlope
#define ROS_ERROR(...)
double simStart
bool stopSim(netft_utils::StopSim::Request &req, netft_utils::StopSim::Response &res)


netft_utils
Author(s): Alex von Sternberg , Derek King, Andy Zelenak
autogenerated on Tue Mar 2 2021 03:15:08