ptu46_node.cc
Go to the documentation of this file.
1 
8 #include <string>
9 #include <ros/ros.h>
10 #include <ptu46/ptu46_driver.h>
11 #include <sensor_msgs/JointState.h>
12 
13 using namespace std;
14 
15 namespace PTU46 {
16 
36 class PTU46_Node {
37  public:
38  PTU46_Node(ros::NodeHandle& node_handle);
39  ~PTU46_Node();
40 
41  // Service Control
42  void Connect();
43  bool ok() {
44  return m_pantilt != NULL;
45  }
46  void Disconnect();
47 
48  // Service Execution
49  void spinOnce();
50 
51  // Callback Methods
52  void SetGoal(const sensor_msgs::JointState::ConstPtr& msg);
53 
54  protected:
56  ros::NodeHandle m_node;
57  ros::Publisher m_joint_pub;
58  ros::Subscriber m_joint_sub;
59 };
60 
61 PTU46_Node::PTU46_Node(ros::NodeHandle& node_handle)
62  :m_pantilt(NULL), m_node(node_handle) {
63  // Empty Constructor
64 }
65 
67  Disconnect();
68 }
69 
73  // If we are reconnecting, first make sure to disconnect
74  if (ok()) {
75  Disconnect();
76  }
77 
78 
79  // Publishers : Only publish the most recent reading
80  m_joint_pub = m_node.advertise
81  <sensor_msgs::JointState>("state", 1);
82 
83  // Subscribers : Only subscribe to the most recent instructions
84  m_joint_sub = m_node.subscribe
85  <sensor_msgs::JointState>("cmd", 1, &PTU46_Node::SetGoal, this);
86 
87 // ros::spinOnce();
88 
89  // Query for serial configuration
90  std::string port;
91  m_node.param<std::string>("port", port, PTU46_DEFAULT_PORT);
92  int baud;
93  m_node.param("baud", baud, PTU46_DEFAULT_BAUD);
94 
95  // Connect to the PTU
96  ROS_INFO("Attempting to connect to %s...", port.c_str());
97  m_pantilt = new PTU46(port.c_str(), baud);
98  ROS_ASSERT(m_pantilt != NULL);
99  if (! m_pantilt->isOpen()) {
100  ROS_ERROR("Could not connect to pan/tilt unit [%s]", port.c_str());
101  Disconnect();
102  return;
103  }
104  ROS_INFO("Connected!");
105 
106  m_node.setParam("min_tilt", m_pantilt->GetMin(PTU46_TILT));
107  m_node.setParam("max_tilt", m_pantilt->GetMax(PTU46_TILT));
108  m_node.setParam("min_tilt_speed", m_pantilt->GetMinSpeed(PTU46_TILT));
109  m_node.setParam("max_tilt_speed", m_pantilt->GetMaxSpeed(PTU46_TILT));
110  m_node.setParam("tilt_step", m_pantilt->GetResolution(PTU46_TILT));
111 
112  m_node.setParam("min_pan", m_pantilt->GetMin(PTU46_PAN));
113  m_node.setParam("max_pan", m_pantilt->GetMax(PTU46_PAN));
114  m_node.setParam("min_pan_speed", m_pantilt->GetMinSpeed(PTU46_PAN));
115  m_node.setParam("max_pan_speed", m_pantilt->GetMaxSpeed(PTU46_PAN));
116  m_node.setParam("pan_step", m_pantilt->GetResolution(PTU46_PAN));
117 
118 
119  // Publishers : Only publish the most recent reading
120  m_joint_pub = m_node.advertise
121  <sensor_msgs::JointState>("state", 1);
122 
123  // Subscribers : Only subscribe to the most recent instructions
124  m_joint_sub = m_node.subscribe
125  <sensor_msgs::JointState>("cmd", 1, &PTU46_Node::SetGoal, this);
126 
127 }
128 
131  if (m_pantilt != NULL) {
132  delete m_pantilt; // Closes the connection
133  m_pantilt = NULL; // Marks the service as disconnected
134  }
135 }
136 
138 void PTU46_Node::SetGoal(const sensor_msgs::JointState::ConstPtr& msg) {
139  if (! ok())
140  return;
141 
142  bool set_pan_pos=false, set_tilt_pos=false, set_pan_speed=false, set_tilt_speed=false;
143  double panpos, tiltpos, panspeed, tiltspeed;
144 
145  for (size_t i=0; i<msg->name.size(); i++)
146  {
147  if (msg->name[i]==ros::names::remap("pan"))
148  {
149  if (msg->position.size()>i)
150  {
151  panpos=msg->position[i];
152  set_pan_pos = true;
153  }
154 
155  if (msg->velocity.size()>i)
156  {
157  panspeed=msg->velocity[i];
158  set_pan_speed = true;
159  }
160  }
161 
162 
163  if (msg->name[i]==ros::names::remap("tilt"))
164  {
165  if (msg->position.size()>i)
166  {
167  tiltpos=msg->position[i];
168  set_tilt_pos = true;
169  }
170 
171  if (msg->velocity.size()>i)
172  {
173  tiltspeed=msg->velocity[i];
174  set_tilt_speed = true;
175  }
176  }
177 
178  }
179 
180  if (set_pan_pos)
181  m_pantilt->SetPosition(PTU46_PAN, panpos);
182 
183  if (set_tilt_pos)
184  m_pantilt->SetPosition(PTU46_TILT, tiltpos);
185 
186  if (set_pan_speed)
187  m_pantilt->SetSpeed(PTU46_PAN, panspeed);
188 
189  if (set_tilt_speed)
190  m_pantilt->SetSpeed(PTU46_TILT, tiltspeed);
191 }
192 
198  if (! ok())
199  return;
200 
201  // Read Position & Speed
202  double pan = m_pantilt->GetPosition(PTU46_PAN);
203  double tilt = m_pantilt->GetPosition(PTU46_TILT);
204 
205  double panspeed = m_pantilt->GetSpeed(PTU46_PAN);
206  double tiltspeed = m_pantilt->GetSpeed(PTU46_TILT);
207 
208  // Publish Position & Speed
209  sensor_msgs::JointState joint_state;
210 
211  joint_state.header.stamp = ros::Time::now();
212  joint_state.name.push_back(ros::names::remap("pan"));
213  joint_state.position.push_back(pan);
214  joint_state.velocity.push_back(panspeed);
215  joint_state.name.push_back(ros::names::remap("tilt"));
216  joint_state.position.push_back(tilt);
217  joint_state.velocity.push_back(tiltspeed);
218  m_joint_pub.publish(joint_state);
219 
220 }
221 
222 } // PTU46 namespace
223 
224 int main(int argc, char** argv) {
225  ros::init(argc, argv, "ptu");
226  ros::NodeHandle n("~");
227 
228  // Connect to PTU
229  PTU46::PTU46_Node ptu_node = PTU46::PTU46_Node(n);
230  ptu_node.Connect();
231  if (! ptu_node.ok())
232  return -1;
233 
234  // Query for polling frequency
235  int hz;
236  n.param("hz", hz, PTU46_DEFAULT_HZ);
237  ros::Rate loop_rate(hz);
238 
239  while (ros::ok() && ptu_node.ok()) {
240  // Publish position & velocity
241  ptu_node.spinOnce();
242 
243  // Process a round of subscription messages
244  ros::spinOnce();
245 
246  // This will adjust as needed per iteration
247  loop_rate.sleep();
248  }
249 
250  if (! ptu_node.ok()) {
251  ROS_ERROR("pan/tilt unit disconncted prematurely");
252  return -1;
253  }
254 
255  return 0;
256 }
bool SetSpeed(char type, float speed)
PTU46 Pan Tilt Unit Driver Copyright (C) 2009 Erik Karulf (erik@cse.wustl.edu)
Definition: ptu46_driver.h:58
void SetGoal(const sensor_msgs::JointState::ConstPtr &msg)
Definition: ptu46_node.cc:138
#define PTU46_DEFAULT_HZ
Definition: ptu46_driver.h:16
ros::Subscriber m_joint_sub
Definition: ptu46_node.cc:58
ros::NodeHandle m_node
Definition: ptu46_node.cc:56
float GetPosition(char type)
#define PTU46_PAN
Definition: ptu46_driver.h:19
#define PTU46_TILT
Definition: ptu46_driver.h:20
bool SetPosition(char type, float pos, bool Block=false)
ros::Publisher m_joint_pub
Definition: ptu46_node.cc:57
ptu control header. Defines commands for ptu control.
#define PTU46_DEFAULT_PORT
Definition: ptu46_driver.h:15
#define PTU46_DEFAULT_BAUD
Definition: ptu46_driver.h:13
float GetSpeed(char type)
int main(int argc, char **argv)
Definition: ptu46_node.cc:224


ptu46
Author(s): Erik Karulf, David Lu
autogenerated on Mon Mar 2 2015 01:32:49