analog_inputs_ros_i.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Open Source Robotics Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <functional>
31 #include <memory>
32 #include <mutex>
33 
34 #include <ros/ros.h>
35 #include <std_msgs/Float64.h>
36 
39 
40 namespace phidgets {
41 
43  ros::NodeHandle nh_private)
44  : nh_(nh), nh_private_(nh_private)
45 {
46  ROS_INFO("Starting Phidgets AnalogInputs");
47 
48  int serial_num;
49  if (!nh_private_.getParam("serial", serial_num))
50  {
51  serial_num = -1; // default open any device
52  }
53  int hub_port;
54  if (!nh_private.getParam("hub_port", hub_port))
55  {
56  hub_port = 0; // only used if the device is on a VINT hub_port
57  }
58  bool is_hub_port_device;
59  if (!nh_private.getParam("is_hub_port_device", is_hub_port_device))
60  {
61  // only used if the device is on a VINT hub_port
62  is_hub_port_device = false;
63  }
64  int data_interval_ms;
65  if (!nh_private.getParam("data_interval_ms", data_interval_ms))
66  {
67  data_interval_ms = 250;
68  }
69  if (!nh_private.getParam("publish_rate", publish_rate_))
70  {
71  publish_rate_ = 0;
72  }
73  if (nh_private.getParam("server_name", server_name_) &&
74  nh_private.getParam("server_ip", server_ip_))
75  {
76  PhidgetNet_addServer(server_name_.c_str(), server_ip_.c_str(), 5661, "",
77  0);
78 
79  ROS_INFO("Using phidget server %s at IP %s", server_name_.c_str(),
80  server_ip_.c_str());
81  }
82 
83  ROS_INFO("Connecting to Phidgets AnalogInputs serial %d, hub port %d ...",
84  serial_num, hub_port);
85 
86  // We take the mutex here and don't unlock until the end of the constructor
87  // to prevent a callback from trying to use the publisher before we are
88  // finished setting up.
89  std::lock_guard<std::mutex> lock(ai_mutex_);
90 
91  int n_in;
92  try
93  {
94  ais_ = std::make_unique<AnalogInputs>(
95  serial_num, hub_port, is_hub_port_device,
97  std::placeholders::_1, std::placeholders::_2));
98 
99  n_in = ais_->getInputCount();
100  ROS_INFO("Connected %d inputs", n_in);
101  val_to_pubs_.resize(n_in);
102  for (int i = 0; i < n_in; i++)
103  {
104  char topicname[] = "analog_input00";
105  snprintf(topicname, sizeof(topicname), "analog_input%02d", i);
106  val_to_pubs_[i].pub =
107  nh_.advertise<std_msgs::Float64>(topicname, 1);
108 
109  ais_->setDataInterval(i, data_interval_ms);
110 
111  val_to_pubs_[i].last_val = ais_->getSensorValue(i);
112  }
113  } catch (const Phidget22Error& err)
114  {
115  ROS_ERROR("AnalogInputs: %s", err.what());
116  throw;
117  }
118 
119  if (publish_rate_ > 0)
120  {
123  } else
124  {
125  // If we are *not* publishing periodically, then we are event driven and
126  // will only publish when something changes (where "changes" is defined
127  // by the libphidget22 library). In that case, make sure to publish
128  // once at the beginning to make sure there is *some* data.
129  for (int i = 0; i < n_in; ++i)
130  {
131  publishLatest(i);
132  }
133  }
134 }
135 
137 {
138  double VREF = 5.0;
139  // get rawsensorvalue and divide by 4096, which according to the
140  // documentation for both the IK888 and IK222 are the maximum sensor value
141  // Multiply by VREF=5.0V to get voltage
142  std_msgs::Float64 msg;
143  msg.data = VREF * val_to_pubs_[index].last_val / 4095.0;
144  val_to_pubs_[index].pub.publish(msg);
145 }
146 
148 {
149  std::lock_guard<std::mutex> lock(ai_mutex_);
150  for (int i = 0; i < static_cast<int>(val_to_pubs_.size()); ++i)
151  {
152  publishLatest(i);
153  }
154 }
155 
156 void AnalogInputsRosI::sensorChangeCallback(int index, double sensor_value)
157 {
158  if (static_cast<int>(val_to_pubs_.size()) > index)
159  {
160  std::lock_guard<std::mutex> lock(ai_mutex_);
161  val_to_pubs_[index].last_val = sensor_value;
162 
163  if (publish_rate_ <= 0)
164  {
165  publishLatest(index);
166  }
167  }
168 }
169 
170 } // namespace phidgets
phidgets
phidgets::AnalogInputsRosI::timerCallback
void timerCallback(const ros::TimerEvent &event)
Definition: analog_inputs_ros_i.cpp:147
phidgets::AnalogInputsRosI::publish_rate_
int publish_rate_
Definition: analog_inputs_ros_i.h:62
phidgets::AnalogInputsRosI::ai_mutex_
std::mutex ai_mutex_
Definition: analog_inputs_ros_i.h:55
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
ros.h
phidgets::AnalogInputsRosI::server_ip_
std::string server_ip_
Definition: analog_inputs_ros_i.h:64
analog_inputs_ros_i.h
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
analog_inputs.h
phidgets::AnalogInputsRosI::timer_
ros::Timer timer_
Definition: analog_inputs_ros_i.h:61
phidgets::AnalogInputsRosI::AnalogInputsRosI
AnalogInputsRosI(ros::NodeHandle nh, ros::NodeHandle nh_private)
Definition: analog_inputs_ros_i.cpp:42
phidgets::Phidget22Error
ros::TimerEvent
phidgets::Phidget22Error::what
const char * what() const noexcept
phidgets::AnalogInputsRosI::nh_private_
ros::NodeHandle nh_private_
Definition: analog_inputs_ros_i.h:59
phidgets::AnalogInputsRosI::nh_
ros::NodeHandle nh_
Definition: analog_inputs_ros_i.h:58
phidgets::AnalogInputsRosI::sensorChangeCallback
void sensorChangeCallback(int index, double sensor_value)
Definition: analog_inputs_ros_i.cpp:156
ROS_ERROR
#define ROS_ERROR(...)
phidgets::AnalogInputsRosI::server_name_
std::string server_name_
Definition: analog_inputs_ros_i.h:63
phidgets::AnalogInputsRosI::val_to_pubs_
std::vector< ValToPub > val_to_pubs_
Definition: analog_inputs_ros_i.h:56
ROS_INFO
#define ROS_INFO(...)
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
phidgets::AnalogInputsRosI::publishLatest
void publishLatest(int index)
Definition: analog_inputs_ros_i.cpp:136
phidgets::AnalogInputsRosI::ais_
std::unique_ptr< AnalogInputs > ais_
Definition: analog_inputs_ros_i.h:54
ros::NodeHandle


phidgets_analog_inputs
Author(s): Chris Lalancette
autogenerated on Sun May 11 2025 02:20:29