lift_arm_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <trajectory_msgs/JointTrajectory.h>
3 #include <std_srvs/Trigger.h>
4 #include <sensor_msgs/JointState.h>
5 #include <control_msgs/GripperCommandActionGoal.h>
6 
7 #define ROTATION1_JOINT_INDX 6
8 #define ROTATION2_JOINT_INDX 7
9 #define SHOULDER1_JOINT_INDX 8
10 #define SHOULDER2_JOINT_INDX 9
11 #define SHOULDER3_JOINT_INDX 10
12 #define WRIST_JOINT_INDX 12
13 
14 /* valid starting positions */
15 #define VALID_START_RAD_GOAL_TOLERANCE 0.174533 //10 deg
16 
17 #define ROTATION1_VALID_START_RAD_LEFT_POS 1.567
18 #define ROTATION2_VALID_START_RAD_LEFT_POS 0.0
19 #define SHOULDER1_VALID_START_RAD_LEFT_POS -1.883
20 #define SHOULDER2_VALID_START_RAD_LEFT_POS 2.366
21 #define SHOULDER3_VALID_START_RAD_LEFT_POS 0.413287
22 #define WRIST_VALID_START_RAD_LEFT_POS 0.0
23 
24 #define ROTATION1_VALID_START_RAD_RIGHT_POS -1.567
25 #define ROTATION2_VALID_START_RAD_RIGHT_POS 0.0
26 #define SHOULDER1_VALID_START_RAD_RIGHT_POS -1.883
27 #define SHOULDER2_VALID_START_RAD_RIGHT_POS 2.366
28 #define SHOULDER3_VALID_START_RAD_RIGHT_POS 0.413287
29 #define WRIST_VALID_START_RAD_RIGHT_POS 0.0
30 
31 /* goal position when arm is folded with gripper */
32 /* on the right side of the robot */
33 #define ROTATION1_RAD_GOAL_LEFT_POS 1.567
34 #define ROTATION2_RAD_GOAL_LEFT_POS 0.0
35 #define SHOULDER1_RAD_GOAL_LEFT_POS -1.5264
36 #define SHOULDER2_RAD_GOAL_LEFT_POS 2.1339
37 #define SHOULDER3_RAD_GOAL_LEFT_POS 0.0257
38 #define WRIST_RAD_GOAL_LEFT_POS 0.0
39 
40 /* goal position when arm is folded with gripper */
41 /* on the left side of the robot */
42 #define ROTATION1_RAD_GOAL_RIGHT_POS -1.567
43 #define ROTATION2_RAD_GOAL_RIGHT_POS 0.0
44 #define SHOULDER1_RAD_GOAL_RIGHT_POS -1.5264
45 #define SHOULDER2_RAD_GOAL_RIGHT_POS 2.1339
46 #define SHOULDER3_RAD_GOAL_RIGHT_POS 0.0257
47 #define WRIST_RAD_GOAL_RIGHT_POS 0.0
48 
49 #define ROTATION1_RAD_GOAL_VEL 0.1
50 #define ROTATION2_RAD_GOAL_VEL 0.1
51 #define SHOULDER1_RAD_GOAL_VEL 0.1
52 #define SHOULDER2_RAD_GOAL_VEL 0.1
53 #define SHOULDER3_RAD_GOAL_VEL 0.1
54 #define WRIST_RAD_GOAL_VEL 0.1
55 
56 #define ROTATION1_JOINT_NAME "rotation1_joint"
57 #define ROTATION2_JOINT_NAME "rotation2_joint"
58 #define SHOULDER1_JOINT_NAME "shoulder1_joint"
59 #define SHOULDER2_JOINT_NAME "shoulder2_joint"
60 #define SHOULDER3_JOINT_NAME "shoulder3_joint"
61 #define WRIST_JOINT_NAME "wrist_joint"
62 
63 enum ArmPose
64 {
68 };
69 struct arm_state
70 {
71  double rotation1_pos_rad = 0;
72  double rotation2_pos_rad = 0;
73  double shoulder1_pos_rad = 0;
74  double shoulder2_pos_rad = 0;
75  double shoulder3_pos_rad = 0;
76  double wrist_pos_rad = 0;
77  bool got_state = false;
78 };
79 
86 
87 /* check that arm is in valid start pos */
88 /* return: arm pose */
90 {
91  //ROS_INFO("real %f: | lower: %f | upper: %f", arm.rotation1_pos_rad, (ROTATION1_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE), (ROTATION1_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE));
94  {
95  /*if (arm.rotation2_pos_rad > ROTATION2_VALID_START_RAD_RIGHT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
96  arm.rotation2_pos_rad < ROTATION2_VALID_START_RAD_RIGHT_POS + VALID_START_RAD_GOAL_TOLERANCE)
97  {
98  if (arm.shoulder1_pos_rad > SHOULDER1_VALID_START_RAD_RIGHT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
99  arm.shoulder1_pos_rad < SHOULDER1_VALID_START_RAD_RIGHT_POS + VALID_START_RAD_GOAL_TOLERANCE)
100  {
101  if (arm.shoulder2_pos_rad > SHOULDER2_VALID_START_RAD_RIGHT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
102  arm.shoulder2_pos_rad < SHOULDER2_VALID_START_RAD_RIGHT_POS + VALID_START_RAD_GOAL_TOLERANCE)
103  {
104  if (arm.shoulder3_pos_rad > SHOULDER3_VALID_START_RAD_RIGHT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
105  arm.shoulder3_pos_rad < SHOULDER3_VALID_START_RAD_RIGHT_POS + VALID_START_RAD_GOAL_TOLERANCE)
106  {
107  if (arm.wrist_pos_rad > WRIST_VALID_START_RAD_RIGHT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
108  arm.wrist_pos_rad < WRIST_VALID_START_RAD_RIGHT_POS + VALID_START_RAD_GOAL_TOLERANCE)*/
110  /* }
111  }
112  }
113  }*/
114  }
117  {
118  /* if (arm.rotation2_pos_rad > ROTATION2_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
119  arm.rotation2_pos_rad < ROTATION2_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE)
120  {
121  if (arm.shoulder1_pos_rad > SHOULDER1_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
122  arm.shoulder1_pos_rad < SHOULDER1_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE)
123  {
124  if (arm.shoulder2_pos_rad > SHOULDER2_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
125  arm.shoulder2_pos_rad < SHOULDER2_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE)
126  {
127  if (arm.shoulder3_pos_rad > SHOULDER3_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
128  arm.shoulder3_pos_rad < SHOULDER3_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE)
129  {
130  if (arm.wrist_pos_rad > WRIST_VALID_START_RAD_LEFT_POS - VALID_START_RAD_GOAL_TOLERANCE &&
131  arm.wrist_pos_rad < WRIST_VALID_START_RAD_LEFT_POS + VALID_START_RAD_GOAL_TOLERANCE)*/
133  /*}
134  }
135  }
136  }*/
137  }
139 }
140 
141 bool liftArmCB(std_srvs::Trigger::Request &req,
142  std_srvs::Trigger::Response &res)
143 {
144 
145 
146  trajectory_msgs::JointTrajectory traj_msg;
147 
148  traj_msg.joint_names.push_back(ROTATION1_JOINT_NAME);
149  traj_msg.joint_names.push_back(ROTATION2_JOINT_NAME);
150  traj_msg.joint_names.push_back(SHOULDER1_JOINT_NAME);
151  traj_msg.joint_names.push_back(SHOULDER2_JOINT_NAME);
152  traj_msg.joint_names.push_back(SHOULDER3_JOINT_NAME);
153  traj_msg.joint_names.push_back(WRIST_JOINT_NAME);
154 
155  trajectory_msgs::JointTrajectoryPoint point;
156 
157  switch (getArmPose())
158  {
160  {
161  res.success = false;
162  res.message = "arm is in invalid start position. move arm to valid start position, and try again";
163  return true;
164  }
166  {
167  point.positions.push_back(ROTATION1_RAD_GOAL_LEFT_POS);
168  point.positions.push_back(ROTATION2_RAD_GOAL_LEFT_POS);
169  point.positions.push_back(SHOULDER1_RAD_GOAL_LEFT_POS);
170  point.positions.push_back(SHOULDER2_RAD_GOAL_LEFT_POS);
171  point.positions.push_back(SHOULDER3_RAD_GOAL_LEFT_POS);
172  point.positions.push_back(WRIST_RAD_GOAL_LEFT_POS);
173  res.message = "gripper to the left";
174  break;
175  }
177  {
178  point.positions.push_back(ROTATION1_RAD_GOAL_RIGHT_POS);
179  point.positions.push_back(ROTATION2_RAD_GOAL_RIGHT_POS);
180  point.positions.push_back(SHOULDER1_RAD_GOAL_RIGHT_POS);
181  point.positions.push_back(SHOULDER2_RAD_GOAL_RIGHT_POS);
182  point.positions.push_back(SHOULDER3_RAD_GOAL_RIGHT_POS);
183  point.positions.push_back(WRIST_RAD_GOAL_RIGHT_POS);
184  res.message = "gripper to the right";
185  break;
186  }
187  }
188 
189  point.velocities.push_back(ROTATION1_RAD_GOAL_VEL);
190  point.velocities.push_back(ROTATION2_RAD_GOAL_VEL);
191  point.velocities.push_back(SHOULDER1_RAD_GOAL_VEL);
192  point.velocities.push_back(SHOULDER2_RAD_GOAL_VEL);
193  point.velocities.push_back(SHOULDER3_RAD_GOAL_VEL);
194  point.velocities.push_back(WRIST_RAD_GOAL_VEL);
195 
196  point.time_from_start = ros::Duration(1);
197  traj_msg.points.push_back(point);
198  traj_msg.header.stamp = ros::Time::now();
199  arm_pub.publish(traj_msg);
200 
201  res.success = true;
202  return true;
203 }
204 
205 void jointsUpdateCB(const sensor_msgs::JointState::ConstPtr& msg)
206 {
207  /* save joints updated state */
208  arm.got_state = true;
209  arm.rotation1_pos_rad = msg->position[ROTATION1_JOINT_INDX];
210  arm.rotation2_pos_rad = msg->position[ROTATION2_JOINT_INDX];
211  arm.shoulder1_pos_rad = msg->position[SHOULDER1_JOINT_INDX];
212  arm.shoulder2_pos_rad = msg->position[SHOULDER2_JOINT_INDX];
213  arm.shoulder3_pos_rad = msg->position[SHOULDER3_JOINT_INDX];
214  arm.wrist_pos_rad = msg->position[WRIST_JOINT_INDX];
215 }
216 
217 bool openGripperCB(std_srvs::Trigger::Request &req,
218  std_srvs::Trigger::Response &res)
219 {
220  control_msgs::GripperCommandActionGoal gripper_msg;
221  gripper_msg.header.stamp = ros::Time::now();
222  gripper_msg.goal.command.position = 0.5;
223  gripper_msg.goal.command.max_effort = 0;
224  gripper_pub.publish(gripper_msg);
225  res.message = "gripper open request was sent";
226  res.success = true;
227  return true;
228 }
229 
230 
231 
232 int main(int argc, char** argv) {
233 
234  ros::init(argc, argv, "lift_arm_node");
235  ros::NodeHandle nh;
236 
237  arm_pub = nh.advertise<trajectory_msgs::JointTrajectory>("arm_trajectory_controller/command", 5);
238  gripper_pub = nh.advertise<control_msgs::GripperCommandActionGoal>("gripper_controller/gripper_cmd/goal", 5);
239  joints_state_sub = nh.subscribe("joint_states", 5, jointsUpdateCB);
240  lift_arm_srv = nh.advertiseService("lift_arm", liftArmCB);
241  open_gripper_srv = nh.advertiseService("open_gripper", openGripperCB);
242 ros::Duration(5).sleep();
243  /* wait for subscribers and publishers to come up */
244  ros::Rate loop_rate(1);
245  while (arm_pub.getNumSubscribers() <= 0 ||
246  gripper_pub.getNumSubscribers() <= 0 ||
247  joints_state_sub.getNumPublishers() <= 0)
248  {
249  ros::spinOnce();
250  loop_rate.sleep();
251  }
252  ROS_INFO("[lift_arm_node]: ready");
253  /* lift arm once on startup */
254  std_srvs::Trigger::Request req;
255  std_srvs::Trigger::Response res;
256  liftArmCB(req, res);
257 
258  ros::spin();
259  return 0;
260 }
261 
#define ROTATION2_JOINT_INDX
bool openGripperCB(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
double rotation2_pos_rad
#define WRIST_JOINT_NAME
#define WRIST_JOINT_INDX
#define ROTATION1_RAD_GOAL_RIGHT_POS
#define WRIST_RAD_GOAL_LEFT_POS
ros::ServiceServer lift_arm_srv
#define ROTATION1_RAD_GOAL_LEFT_POS
void publish(const boost::shared_ptr< M > &message) const
#define ROTATION1_VALID_START_RAD_RIGHT_POS
ros::Subscriber joints_state_sub
double shoulder3_pos_rad
double rotation1_pos_rad
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
#define SHOULDER3_RAD_GOAL_LEFT_POS
bool sleep() const
double shoulder2_pos_rad
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define SHOULDER1_RAD_GOAL_RIGHT_POS
#define ROTATION1_RAD_GOAL_VEL
#define ROTATION1_JOINT_NAME
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
#define SHOULDER3_RAD_GOAL_RIGHT_POS
#define SHOULDER1_JOINT_NAME
#define ROTATION2_JOINT_NAME
ArmPose
#define SHOULDER2_RAD_GOAL_VEL
arm_state arm
#define SHOULDER3_JOINT_NAME
#define ROTATION2_RAD_GOAL_LEFT_POS
#define SHOULDER1_RAD_GOAL_VEL
ArmPose getArmPose()
#define SHOULDER1_RAD_GOAL_LEFT_POS
#define ROTATION1_JOINT_INDX
ros::Publisher gripper_pub
#define ROS_INFO(...)
#define WRIST_RAD_GOAL_RIGHT_POS
#define SHOULDER2_JOINT_NAME
#define SHOULDER2_RAD_GOAL_RIGHT_POS
#define SHOULDER3_RAD_GOAL_VEL
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROTATION2_RAD_GOAL_VEL
ROSCPP_DECL void spin()
int main(int argc, char **argv)
#define SHOULDER1_JOINT_INDX
ros::ServiceServer open_gripper_srv
#define SHOULDER2_RAD_GOAL_LEFT_POS
bool sleep()
#define SHOULDER3_JOINT_INDX
uint32_t getNumSubscribers() const
bool liftArmCB(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
#define VALID_START_RAD_GOAL_TOLERANCE
static Time now()
ros::Publisher arm_pub
double wrist_pos_rad
#define ROTATION2_RAD_GOAL_RIGHT_POS
void jointsUpdateCB(const sensor_msgs::JointState::ConstPtr &msg)
ROSCPP_DECL void spinOnce()
#define ROTATION1_VALID_START_RAD_LEFT_POS
#define SHOULDER2_JOINT_INDX
double shoulder1_pos_rad
#define WRIST_RAD_GOAL_VEL


lift_arm
Author(s):
autogenerated on Wed Jan 3 2018 03:48:16