ik_ros_i.cpp
Go to the documentation of this file.
1 #include "phidgets_ik/ik_ros_i.h"
2 
3 namespace phidgets {
4 
6  : IK(),
7  n_in(0),
8  n_out(0),
9  n_sensors(0),
10  nh_(nh),
11  nh_private_(nh_private),
12  VREF(5.0f)
13 {
14  ROS_INFO("Starting Phidgets IK");
15 
16  initDevice();
17 }
18 
20 {
21  ROS_INFO("Opening device");
22  int serial_num;
23  if (!nh_private_.getParam("serial", serial_num))
24  {
25  serial_num = -1; // default open any device
26  }
27  ROS_INFO("Waiting for IK %d to be attached...", serial_num);
28  int result = openAndWaitForAttachment(serial_num, 10000);
29  if (result)
30  {
31  std::string err = Phidget::getErrorDescription(result);
32  ROS_FATAL("Problem waiting for IK attachment: %s", err.c_str());
33  ros::shutdown();
34  return;
35  }
36 
37  n_in = getInputCount();
40 
41  ROS_INFO("%d inputs, %d outputs, %d sensors", n_in, n_out, n_sensors);
42  for (int i = 0; i < n_in; i++)
43  {
44  char topicname[] = "digital_input00";
45  snprintf(topicname, sizeof(topicname), "digital_input%02d", i);
46  in_pubs_.push_back(nh_.advertise<std_msgs::Bool>(topicname, 1));
47  }
48  for (int i = 0; i < n_out; i++)
49  {
50  char topicname[] = "digital_output00";
51  snprintf(topicname, sizeof(topicname), "digital_output%02d", i);
52  std::shared_ptr<OutputSetter> s(new OutputSetter(this, i));
53  s->subscription = nh_.subscribe(
54  topicname, 1, &OutputSetter::set_msg_callback, s.get());
55  out_subs_.push_back(s);
56  }
57  out_srv_ = nh_.advertiseService("set_digital_output",
59  for (int i = 0; i < n_sensors; i++)
60  {
61  char topicname[] = "analog_input00";
62  snprintf(topicname, sizeof(topicname), "analog_input%02d", i);
63  sensor_pubs_.push_back(nh_.advertise<std_msgs::Float32>(topicname, 1));
64  }
65 }
66 
67 void IKRosI::sensorHandler(int index, int /* sensorValue */)
68 {
69  // get rawsensorvalue and divide by 4096, which according to the
70  // documentation for both the IK888 and IK222 are the maximum sensor value
71  // Multiply by VREF=5.0V to get voltage
72  int rawval = getSensorRawValue(index);
73  std_msgs::Float32 msg;
74  msg.data = VREF * float(rawval) / 4095.0f;
75  if ((static_cast<int>(sensor_pubs_.size()) > index) &&
76  (sensor_pubs_[index]))
77  {
78  sensor_pubs_[index].publish(msg);
79  }
80 }
81 
82 void IKRosI::inputHandler(int index, int inputValue)
83 {
84  std_msgs::Bool msg;
85  msg.data = inputValue != 0;
86  if ((static_cast<int>(in_pubs_.size()) > index) && (in_pubs_[index]))
87  {
88  in_pubs_[index].publish(msg);
89  }
90 }
91 
92 bool IKRosI::set_srv_callback(phidgets_msgs::SetDigitalOutput::Request& req,
93  phidgets_msgs::SetDigitalOutput::Response& res)
94 {
95  ROS_INFO("Setting output %d to %d", req.index, req.state);
96  res.success = setOutputState(req.index, req.state);
97  return true;
98 }
99 
100 void OutputSetter::set_msg_callback(const std_msgs::Bool::ConstPtr& msg)
101 {
102  ROS_INFO("Setting output %d to %d", index_, msg->data);
103  ik_->setOutputState(index_, msg->data);
104 }
105 
106 OutputSetter::OutputSetter(IK* ik, int index) : ik_(ik), index_(index)
107 {
108 }
109 
110 } // namespace phidgets
int openAndWaitForAttachment(int serial_number, int timeout)
std::vector< ros::Publisher > sensor_pubs_
Definition: ik_ros_i.h:37
#define ROS_FATAL(...)
void initDevice()
Definition: ik_ros_i.cpp:19
f
IKRosI(ros::NodeHandle nh, ros::NodeHandle nh_private)
Definition: ik_ros_i.cpp:5
int getSensorCount() const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void inputHandler(int index, int inputValue) override
Definition: ik_ros_i.cpp:82
XmlRpcServer s
ros::NodeHandle nh_
Definition: ik_ros_i.h:41
const float VREF
Definition: ik_ros_i.h:44
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void sensorHandler(int index, int sensorValue) override
Definition: ik_ros_i.cpp:67
std::vector< ros::Publisher > in_pubs_
Definition: ik_ros_i.h:36
OutputSetter(IK *ik, int index)
Definition: ik_ros_i.cpp:106
static std::string getErrorDescription(int errorCode)
std::vector< std::shared_ptr< OutputSetter > > out_subs_
Definition: ik_ros_i.h:39
bool setOutputState(int index, bool state) const
bool set_srv_callback(phidgets_msgs::SetDigitalOutput::Request &req, phidgets_msgs::SetDigitalOutput::Response &res)
Definition: ik_ros_i.cpp:92
#define ROS_INFO(...)
void set_msg_callback(const std_msgs::Bool::ConstPtr &msg)
Definition: ik_ros_i.cpp:100
ros::ServiceServer out_srv_
Definition: ik_ros_i.h:38
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle nh_private_
Definition: ik_ros_i.h:42
bool getParam(const std::string &key, std::string &s) const
int getOutputCount() const
ROSCPP_DECL void shutdown()
int getInputCount() const
int getSensorRawValue(int index) const


phidgets_ik
Author(s): James Sarrett, Russel Howe
autogenerated on Fri Apr 9 2021 02:56:07