node.cpp
Go to the documentation of this file.
1 /*
2  * flir_ptu_driver ROS package
3  * Copyright (C) 2014 Mike Purvis (mpurvis@clearpathrobotics.com)
4  *
5  * PTU ROS Package
6  * Copyright (C) 2009 Erik Karulf (erik@cse.wustl.edu)
7  *
8  * Author: Toby Collett (University of Auckland)
9  * Date: 2003-02-10
10  *
11  * Player - One Hell of a Robot Server
12  * Copyright (C) 2000 Brian Gerkey & Kasper Stoy
13  * gerkey@usc.edu kaspers@robotics.usc.edu
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  */
30 
33 #include <flir_ptu_driver/driver.h>
34 #include <ros/ros.h>
35 #include <sensor_msgs/JointState.h>
36 #include <serial/serial.h>
37 #include <std_msgs/Bool.h>
38 
39 #include <string>
40 
41 namespace flir_ptu_driver
42 {
43 
44 class Node
45 {
46 public:
47  explicit Node(ros::NodeHandle& node_handle);
48  ~Node();
49 
50  // Service Control
51  void connect();
52  bool ok()
53  {
54  return m_pantilt != NULL;
55  }
56  void disconnect();
57 
58  // Service Execution
59  void spinCallback(const ros::TimerEvent&);
60 
61  // Callback Methods
62  void cmdCallback(const sensor_msgs::JointState::ConstPtr& msg);
63  void resetCallback(const std_msgs::Bool::ConstPtr& msg);
64 
66 
67 protected:
74 
76  std::string m_joint_name_prefix;
78 };
79 
81  : m_pantilt(NULL), m_node(node_handle)
82 {
84  m_updater->setHardwareID("none");
85  m_updater->add("PTU Status", this, &Node::produce_diagnostics);
86 
87  ros::param::param<std::string>("~joint_name_prefix", m_joint_name_prefix, "ptu_");
88 }
89 
91 {
92  disconnect();
93  delete m_updater;
94 }
95 
99 {
100  // If we are reconnecting, first make sure to disconnect
101  if (ok())
102  {
103  disconnect();
104  }
105 
106  // Query for serial configuration
107  std::string port;
108  int32_t baud;
109  bool limit;
110  ros::param::param<std::string>("~port", port, PTU_DEFAULT_PORT);
111  ros::param::param<bool>("~limits_enabled", limit, true);
112  ros::param::param<int32_t>("~baud", baud, PTU_DEFAULT_BAUD);
113  ros::param::param<double>("~default_velocity", default_velocity_, PTU_DEFAULT_VEL);
114 
115  // Connect to the PTU
116  ROS_INFO_STREAM("Attempting to connect to FLIR PTU on " << port);
117 
118  try
119  {
120  m_ser.setPort(port);
121  m_ser.setBaudrate(baud);
122  serial::Timeout to = serial::Timeout(200, 200, 0, 200, 0);
123  m_ser.setTimeout(to);
124  m_ser.open();
125  }
126  catch (serial::IOException& e)
127  {
128  ROS_ERROR_STREAM("Unable to open port " << port);
129  return;
130  }
131 
132  ROS_INFO_STREAM("FLIR PTU serial port opened, now initializing.");
133 
134  m_pantilt = new PTU(&m_ser);
135 
136  if (!m_pantilt->initialize())
137  {
138  ROS_ERROR_STREAM("Could not initialize FLIR PTU on " << port);
139  disconnect();
140  return;
141  }
142 
143  if (!limit)
144  {
146  ROS_INFO("FLIR PTU limits disabled.");
147  }
148 
149  ROS_INFO("FLIR PTU initialized.");
150 
151  m_node.setParam("min_tilt", m_pantilt->getMin(PTU_TILT));
152  m_node.setParam("max_tilt", m_pantilt->getMax(PTU_TILT));
153  m_node.setParam("min_tilt_speed", m_pantilt->getMinSpeed(PTU_TILT));
154  m_node.setParam("max_tilt_speed", m_pantilt->getMaxSpeed(PTU_TILT));
156 
157  m_node.setParam("min_pan", m_pantilt->getMin(PTU_PAN));
158  m_node.setParam("max_pan", m_pantilt->getMax(PTU_PAN));
159  m_node.setParam("min_pan_speed", m_pantilt->getMinSpeed(PTU_PAN));
160  m_node.setParam("max_pan_speed", m_pantilt->getMaxSpeed(PTU_PAN));
162 
163  // Publishers : Only publish the most recent reading
165  <sensor_msgs::JointState>("state", 1);
166 
167  // Subscribers : Only subscribe to the most recent instructions
169  <sensor_msgs::JointState>("cmd", 1, &Node::cmdCallback, this);
170 
172  <std_msgs::Bool>("reset", 1, &Node::resetCallback, this);
173 }
174 
177 {
178  if (m_pantilt != NULL)
179  {
180  delete m_pantilt; // Closes the connection
181  m_pantilt = NULL; // Marks the service as disconnected
182  }
183 }
184 
186 void Node::resetCallback(const std_msgs::Bool::ConstPtr& msg)
187 {
188  ROS_INFO("Resetting the PTU");
189  m_pantilt->home();
190 }
191 
193 void Node::cmdCallback(const sensor_msgs::JointState::ConstPtr& msg)
194 {
195  ROS_DEBUG("PTU command callback.");
196  if (!ok()) return;
197 
198  if (msg->position.size() != 2)
199  {
200  ROS_ERROR("JointState command to PTU has wrong number of position elements.");
201  return;
202  }
203 
204  double pan = msg->position[0];
205  double tilt = msg->position[1];
206  double panspeed, tiltspeed;
207 
208  if (msg->velocity.size() == 2)
209  {
210  panspeed = msg->velocity[0];
211  tiltspeed = msg->velocity[1];
212  }
213  else
214  {
215  ROS_WARN_ONCE("JointState command to PTU has wrong number of velocity elements; using default velocity.");
216  panspeed = default_velocity_;
217  tiltspeed = default_velocity_;
218  }
219 
222  m_pantilt->setSpeed(PTU_PAN, panspeed);
223  m_pantilt->setSpeed(PTU_TILT, tiltspeed);
224 }
225 
227 {
228  stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "All normal.");
229  stat.add("PTU Mode", m_pantilt->getMode() == PTU_POSITION ? "Position" : "Velocity");
230 }
231 
232 
238 {
239  if (!ok()) return;
240 
241  // Read Position & Speed
242  double pan = m_pantilt->getPosition(PTU_PAN);
243  double tilt = m_pantilt->getPosition(PTU_TILT);
244 
245  double panspeed = m_pantilt->getSpeed(PTU_PAN);
246  double tiltspeed = m_pantilt->getSpeed(PTU_TILT);
247 
248  // Publish Position & Speed
249  sensor_msgs::JointState joint_state;
250  joint_state.header.stamp = ros::Time::now();
251  joint_state.name.resize(2);
252  joint_state.position.resize(2);
253  joint_state.velocity.resize(2);
254  joint_state.name[0] = m_joint_name_prefix + "pan";
255  joint_state.position[0] = pan;
256  joint_state.velocity[0] = panspeed;
257  joint_state.name[1] = m_joint_name_prefix + "tilt";
258  joint_state.position[1] = tilt;
259  joint_state.velocity[1] = tiltspeed;
260  m_joint_pub.publish(joint_state);
261 
262  m_updater->update();
263 }
264 
265 } // namespace flir_ptu_driver
266 
267 int main(int argc, char** argv)
268 {
269  ros::init(argc, argv, "ptu");
270  ros::NodeHandle n;
271 
272  while (ros::ok())
273  {
274  // Connect to PTU
275  flir_ptu_driver::Node ptu_node(n);
276  ptu_node.connect();
277 
278  // Set up polling callback
279  int hz;
280  ros::param::param<int>("~hz", hz, PTU_DEFAULT_HZ);
281  ros::Timer spin_timer = n.createTimer(ros::Duration(1 / hz),
283 
284  // Spin until there's a problem or we're in shutdown
285  ros::spin();
286 
287  if (!ptu_node.ok())
288  {
289  ROS_ERROR("FLIR PTU disconnected, attempting reconnection.");
290  ros::Duration(1.0).sleep();
291  }
292  }
293 
294  return 0;
295 }
msg
#define PTU_DEFAULT_VEL
Definition: driver.h:39
void produce_diagnostics(diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition: node.cpp:226
float getMax(char type)
Definition: driver.h:114
void resetCallback(const std_msgs::Bool::ConstPtr &msg)
Definition: node.cpp:186
float getPosition(char type)
Definition: driver.cpp:183
bool setPosition(char type, float pos, bool Block=false)
Definition: driver.cpp:200
void publish(const boost::shared_ptr< M > &message) const
diagnostic_updater::Updater * m_updater
Definition: node.cpp:68
void summary(unsigned char lvl, const std::string s)
ros::Subscriber m_reset_sub
Definition: node.cpp:73
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void setHardwareID(const std::string &hwid)
bool sleep() const
void setTimeout(Timeout &timeout)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setBaudrate(uint32_t baudrate)
float getMaxSpeed(char type)
Definition: driver.h:131
void add(const std::string &name, TaskFunction f)
#define PTU_DEFAULT_PORT
Definition: driver.h:37
float getResolution(char type)
Definition: driver.h:97
ROSCPP_DECL void spin(Spinner &spinner)
serial::Serial m_ser
Definition: node.cpp:75
void cmdCallback(const sensor_msgs::JointState::ConstPtr &msg)
Definition: node.cpp:193
#define PTU_POSITION
Definition: driver.h:49
#define PTU_PAN
Definition: driver.h:42
float getMinSpeed(char type)
Definition: driver.h:123
std::string m_joint_name_prefix
Definition: node.cpp:76
ros::Subscriber m_joint_sub
Definition: node.cpp:72
#define ROS_WARN_ONCE(...)
#define ROS_INFO(...)
double default_velocity_
Definition: node.cpp:77
ROSCPP_DECL bool ok()
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ros::Publisher m_joint_pub
Definition: node.cpp:71
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
Node(ros::NodeHandle &node_handle)
Definition: node.cpp:80
void spinCallback(const ros::TimerEvent &)
Definition: node.cpp:237
ros::NodeHandle m_node
Definition: node.cpp:70
void setPort(const std::string &port)
#define ROS_INFO_STREAM(args)
float getSpeed(char type)
Definition: driver.cpp:239
float getMin(char type)
Definition: driver.h:106
int main(int argc, char **argv)
Definition: node.cpp:267
static Time now()
void add(const std::string &key, const T &val)
#define ROS_ERROR_STREAM(args)
bool setSpeed(char type, float speed)
Definition: driver.cpp:257
bool disableLimits()
Definition: driver.cpp:70
#define PTU_DEFAULT_BAUD
Definition: driver.h:35
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
#define PTU_DEFAULT_HZ
Definition: driver.h:38
#define PTU_TILT
Definition: driver.h:43
#define ROS_DEBUG(...)


flir_ptu_driver
Author(s): Erik Karulf , David V. Lu , Nick Hawes
autogenerated on Sun Mar 28 2021 02:27:16