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  serial_num = -1; //defalut open any device
25  open(serial_num);
26 
27  ROS_INFO("Waiting for IK %d to be attached...", serial_num);
28  int result = waitForAttachment(10000);
29  if(result)
30  {
31  const char *err;
32  CPhidget_getErrorDescription(result, &err);
33  ROS_FATAL("Problem waiting for IK attachment: %s", err);
34  ros::shutdown();
35  return;
36  }
37 
38  CPhidgetInterfaceKit_getInputCount(ik_handle_, &n_in);
39  CPhidgetInterfaceKit_getOutputCount(ik_handle_, &n_out);
40  CPhidgetInterfaceKit_getSensorCount(ik_handle_, &n_sensors);
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  char topicname[] = "digital_input00";
44  snprintf(topicname, sizeof(topicname), "digital_input%02d", i);
45  in_pubs_.push_back(nh_.advertise<std_msgs::Bool>(topicname, 1));
46  }
47  for (int i = 0; i < n_out; i++) {
48  char topicname[] = "digital_output00";
49  snprintf(topicname, sizeof(topicname), "digital_output%02d", i);
51  s->subscription = nh_.subscribe(topicname, 1, &OutputSetter::set_msg_callback, s);
52  out_subs_.push_back(s);
53  }
54  out_srv_ = nh_.advertiseService("set_digital_output", &IKRosI::set_srv_callback, this);
55  for (int i = 0; i < n_sensors; i++) {
56  char topicname[] = "analog_input00";
57  snprintf(topicname, sizeof(topicname), "analog_input%02d", i);
58  sensor_pubs_.push_back(nh_.advertise<std_msgs::Float32>(topicname, 1));
59  }
60 }
61 
62 void IKRosI::sensorHandler(int index, int sensorValue)
63 {
64  //get rawsensorvalue and divide by 4096, which according to the documentation
65  //for both the IK888 and IK222 are the maximum sensor value
66  //Multiply by VREF=5.0V to get voltage
67  int rawval = 0;
68  CPhidgetInterfaceKit_getSensorRawValue(ik_handle_, index, &rawval);
69  std_msgs::Float32 msg;
70  msg.data = VREF*float(rawval)/4095.0f;
71  if ((static_cast<int>(sensor_pubs_.size()) > index) && (sensor_pubs_[index])) {
72  sensor_pubs_[index].publish(msg);
73  }
74 }
75 
76 void IKRosI::inputHandler(int index, int inputValue)
77 {
78  std_msgs::Bool msg;
79  msg.data = inputValue != 0;
80  if ((static_cast<int>(in_pubs_.size()) > index) && (in_pubs_[index])) {
81  in_pubs_[index].publish(msg);
82  }
83 }
84 
85 bool IKRosI::set_srv_callback(phidgets_ik::SetDigitalOutput::Request& req,
86  phidgets_ik::SetDigitalOutput::Response &res)
87 {
88  ROS_INFO("Setting output %d to %d", req.index, req.state);
89  res.success = !CPhidgetInterfaceKit_setOutputState(ik_handle_, req.index, req.state);
90  return true;
91 }
92 
93 void OutputSetter::set_msg_callback(const std_msgs::Bool::ConstPtr& msg)
94 {
95  ROS_INFO("Setting output %d to %d", index, msg->data);
96  CPhidgetInterfaceKit_setOutputState(ik_handle_, index, msg->data);
97 }
98 
99 OutputSetter::OutputSetter(CPhidgetInterfaceKitHandle ik_handle, int index)
100 {
101  this->ik_handle_ = ik_handle;
102  this->index = index;
103 }
104 
105 } // namespace phidgets
std::vector< ros::Publisher > sensor_pubs_
Definition: ik_ros_i.h:38
#define ROS_FATAL(...)
bool set_srv_callback(phidgets_ik::SetDigitalOutput::Request &req, phidgets_ik::SetDigitalOutput::Response &res)
Definition: ik_ros_i.cpp:85
void initDevice()
Definition: ik_ros_i.cpp:19
f
IKRosI(ros::NodeHandle nh, ros::NodeHandle nh_private)
Definition: ik_ros_i.cpp:5
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
XmlRpcServer s
ros::NodeHandle nh_
Definition: ik_ros_i.h:44
const float VREF
Definition: ik_ros_i.h:47
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
int open(int serial_number)
OutputSetter(CPhidgetInterfaceKitHandle ik_handle, int index)
Definition: ik_ros_i.cpp:99
std::vector< ros::Publisher > in_pubs_
Definition: ik_ros_i.h:37
int waitForAttachment(int timeout)
#define ROS_INFO(...)
std::vector< boost::shared_ptr< OutputSetter > > out_subs_
Definition: ik_ros_i.h:40
virtual void set_msg_callback(const std_msgs::Bool::ConstPtr &msg)
Definition: ik_ros_i.cpp:93
ros::ServiceServer out_srv_
Definition: ik_ros_i.h:39
void sensorHandler(int index, int sensorValue)
Definition: ik_ros_i.cpp:62
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle nh_private_
Definition: ik_ros_i.h:45
void inputHandler(int index, int inputValue)
Definition: ik_ros_i.cpp:76
CPhidgetInterfaceKitHandle ik_handle_
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()


phidgets_ik
Author(s): James Sarrett
autogenerated on Tue May 7 2019 03:19:28