sample_motion.cpp
Go to the documentation of this file.
1 /* Copyright 2018 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jason Peng <jason@ufactory.cc>
6  ============================================================================*/
7 #include "ros/ros.h"
8 #include "std_msgs/Float64.h"
9 
10 #define PUB_RATE 100.0
11 #define ACC_TIME 0.5
12 #define CONST_V_TIME 3.0
13 
14 
15 int main(int argc, char **argv)
16 {
17 
18  ros::init(argc, argv, "angle_control");
19 
20 
22 
23 
24  ros::Publisher angle1_pub = n.advertise<std_msgs::Float64>("/xarm/joint1_position_controller/command", 100);
25 
26  ros::Publisher angle2_pub = n.advertise<std_msgs::Float64>("/xarm/joint2_position_controller/command", 100);
27 
28  ros::Publisher angle3_pub = n.advertise<std_msgs::Float64>("/xarm/joint3_position_controller/command", 100);
29 
30  ros::Publisher angle4_pub = n.advertise<std_msgs::Float64>("/xarm/joint4_position_controller/command", 100);
31 
32  ros::Publisher angle5_pub = n.advertise<std_msgs::Float64>("/xarm/joint5_position_controller/command", 100);
33 
34  ros::Publisher angle6_pub = n.advertise<std_msgs::Float64>("/xarm/joint6_position_controller/command", 100);
35 
36  ros::Publisher angle7_pub = n.advertise<std_msgs::Float64>("/xarm/joint7_position_controller/command", 100);
37 
38 
39  ros::Rate loop_rate(PUB_RATE);
40 
41  unsigned int i = 0;
42  int dir = -1;
43  double angle_cmd = 0.0;
44 
45 
46  std_msgs::Float64 msg; /* Float64 is a struct defined in std_msgs */
47 
48  double delta_s = 0.000628 / (PUB_RATE / 100.0) * 4;
49  double const_v = delta_s * PUB_RATE;
50  double acc = const_v / ACC_TIME;
51  double acc_t = 0.0;
52  double cmd_before_dec = 0.0;
53 
54  if(!n.hasParam("DOF"))
55  {
56  ROS_ERROR("No DOF parameter specified! Could not give the right command");
57  exit(-1);
58  }
59 
60  int robot_dof;
61  n.getParam("DOF",robot_dof);
62 
63  while (ros::ok())
64  {
65 
66  if (i <= ACC_TIME * PUB_RATE /*&& dir == -1*/)
67  {
68  acc_t = 1.0 / PUB_RATE * i;
69  angle_cmd = - 0.5 * (acc) * (acc_t) * (acc_t);
70  }
71 
72  else if (i <= (ACC_TIME + CONST_V_TIME)*PUB_RATE)
73  {
74  angle_cmd = angle_cmd + dir * delta_s;
75  if (i == (ACC_TIME + CONST_V_TIME)*PUB_RATE)
76  cmd_before_dec = angle_cmd;
77  }
78 
79  else
80  {
81  acc_t = 1.0 / PUB_RATE * (i - (ACC_TIME + CONST_V_TIME) * PUB_RATE);
82  angle_cmd = cmd_before_dec - const_v * acc_t + 0.5 * (acc) * (acc_t) * (acc_t);
83  }
84 
85 
86  i = i - dir;
87 
88  if (i == (2 * ACC_TIME + CONST_V_TIME)*PUB_RATE + 1)
89  {
90  dir = 1;
91  }
92  else if (i == 0)
93  {
94  dir = -1;
95  }
96 
97 
98  msg.data = angle_cmd;
99 
100  switch(robot_dof)
101  {
102  case 7:
103  {
104  // Give J2 and J4 same angle_cmd from calculation
105  msg.data = -msg.data;
106  angle4_pub.publish(msg);
107  msg.data = -msg.data;
108 
109  angle2_pub.publish(msg);
110 
111  // Give J1 and J5 twice as angle_cmd from calculation
112  msg.data = msg.data * 2;
113 
114  angle1_pub.publish(msg);
115 
116  angle5_pub.publish(msg);
117 
118  // Other joint command fixed to zero
119  msg.data = 0.0;
120 
121  angle3_pub.publish(msg);
122 
123  angle6_pub.publish(msg);
124 
125  angle7_pub.publish(msg);
126 
127  break;
128  }
129  case 6:
130  {
131  // Give J2 and J3 same angle_cmd from calculation
132  angle3_pub.publish(msg);
133 
134  angle2_pub.publish(msg);
135 
136  // Give J1 and J4 twice as angle_cmd from calculation
137  msg.data = msg.data * 2;
138 
139  angle1_pub.publish(msg);
140 
141  angle4_pub.publish(msg);
142 
143  // Other joint command fixed to zero
144  msg.data = 0.0;
145 
146  angle5_pub.publish(msg);
147 
148  angle6_pub.publish(msg);
149 
150  break;
151  }
152  case 5:
153  {
154  // Give J2 and J3 same angle_cmd from calculation
155  angle3_pub.publish(msg);
156 
157  angle2_pub.publish(msg);
158 
159  // Give J1 and J4 twice as angle_cmd from calculation
160  msg.data = msg.data * 2;
161 
162  angle1_pub.publish(msg);
163 
164  // Other joint command fixed to zero
165  msg.data = 0.0;
166 
167  angle4_pub.publish(msg);
168 
169  angle5_pub.publish(msg);
170 
171  break;
172  }
173 
174  default:
175  {
176  ROS_ERROR("DOF parameter not correct, please check!");
177  exit(-1);
178  }
179  }
180 
181 
182  ros::spinOnce();
183 
184  loop_rate.sleep();
185 
186  }
187 
188  fprintf(stderr, "ROS::OK() false \n" );
189 
190  return 0;
191 }
void publish(const boost::shared_ptr< M > &message) const
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define PUB_RATE
#define CONST_V_TIME
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getParam(const std::string &key, std::string &s) const
#define ACC_TIME
bool hasParam(const std::string &key) const
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


xarm_controller
Author(s):
autogenerated on Sat May 8 2021 02:51:38