pan_tilt_mover_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <trajectory_msgs/JointTrajectory.h>
3 #include <std_msgs/Float64MultiArray.h>
4 
5 
7 
8 void publishTrajectoryMsg(std::vector<double> &head_goal)
9 {
10  trajectory_msgs::JointTrajectory traj;
11  traj.header.stamp = ros::Time::now();
12  traj.joint_names.push_back("head_pan_joint");
13  traj.joint_names.push_back("head_tilt_joint");
14  traj.points.resize(1);
15  traj.points[0].time_from_start = ros::Duration(1.0);
16  std::vector<double> q_goal(2);
17  q_goal[0]=head_goal[0];
18  q_goal[1]=head_goal[1];
19  traj.points[0].positions=q_goal;
20  traj.points[0].velocities.push_back(0.1);
21  traj.points[0].velocities.push_back(0.1);
22  traj_pub.publish(traj);
23 
24 }
25 
26 void publishGroupPosMsg(std::vector<double> &head_goal)
27 {
28  std_msgs::Float64MultiArray grp_pos_msg;
29  grp_pos_msg.data.push_back((float)head_goal[0]);
30  grp_pos_msg.data.push_back((float)head_goal[1]);
31  grp_pos_pub.publish(grp_pos_msg);
32 }
33 
34 int main(int argc, char **argv) {
35  ros::init(argc, argv, "pan_tilt_api");
36  ros::NodeHandle nh;
37  traj_pub = nh.advertise<trajectory_msgs::JointTrajectory>("/pan_tilt_trajectory_controller/command", 5);
38  // grp_pos_pub = nh.advertise<std_msgs::Float64MultiArray>("pan_tilt_controller/command", 5);
39  // ros::Rate r(15); //
40 
41  std::vector<double> head_goal(2); //rads
42  float pan_angle = 0, tilt_angle = 0;
43  float timeout = 0; //secs
44  bool gen_random_angles = false;
45 
46  ros::param::get("~pan_angle", pan_angle);
47  ros::param::get("~tilt_angle", tilt_angle);
48  ros::param::get("~random", gen_random_angles);
49  ros::param::get("~timeout", timeout);
50 
51 
52  if (gen_random_angles)
53  {
54  ROS_INFO("[move_pan_tilt_node]: generating random goal...");
55  srand (time(NULL));
56  pan_angle = (float)((rand()%91)-45);
57  tilt_angle = (float)((rand()%81)-50);
58  head_goal[0] = pan_angle * (M_PI / 180.0);
59  head_goal[1] = tilt_angle * (M_PI / 180.0);
60  }
61  else
62  {
63  head_goal[0] = pan_angle * (M_PI / 180);
64  head_goal[1] = tilt_angle * (M_PI / 180);
65  }
66 
67 
68 
69  ros::Duration max_period(timeout);
70  ros::Duration elapsed;
71  ros::Time start_time = ros::Time::now();
72 //r.sleep();
73  ROS_INFO("[move_pan_tilt_node]: waiting for subscriber");
74  /* if timeout is 0, run forever. else, run until timeout */
75 
76 
77  while (ros::ok())
78  {
79 while (traj_pub.getNumSubscribers()<=0) {
80  ros::spinOnce();
81 ros::Duration(3).sleep();
82 }
83  ROS_INFO("[move_pan_tilt_node]: sending pan-tilt goal [%f,%f](degrees)", pan_angle, tilt_angle);
84  // elapsed = ros::Time::now() - start_time;
85  // publishGroupPosMsg(head_goal);
86  publishTrajectoryMsg(head_goal);
87  ros::spinOnce();
88  // r.sleep();
89  ros::shutdown();
90  }
91 
92  ROS_INFO("[move_pan_tilt_node]: done");
93 }
void publishTrajectoryMsg(std::vector< double > &head_goal)
int main(int argc, char **argv)
ros::Publisher traj_pub
void publish(const boost::shared_ptr< M > &message) const
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void publishGroupPosMsg(std::vector< double > &head_goal)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher grp_pos_pub
uint32_t getNumSubscribers() const
static Time now()
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()


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