iri_joystick_driver_node.cpp
Go to the documentation of this file.
00001 #include "iri_joystick_driver_node.h"
00002 #include <iridrivers/exceptions.h>
00003 #include <iridrivers/genius_g_12x.h>
00004 #include <iridrivers/joystick_exceptions.h>
00005 #include <sensor_msgs/Joy.h>
00006 
00007 IriJoystickDriverNode::IriJoystickDriverNode(ros::NodeHandle &nh) :
00008     iri_base_driver::IriBaseNodeDriver<IriJoystickDriver>(nh),
00009     joy_mov_pub_(),
00010     joy_mov_pub_name_("joy"),
00011     total_axes_events_(0),
00012     total_button_events_(0),
00013     total_joy_msg_sent_(0)
00014 {
00015     try
00016     {
00017         driver_.openDriver();
00018         driver_.startDriver();
00019     }
00020     catch (CJoystickException &e)
00021     {
00022         ROS_ERROR("Driver Node dixit: '%s'",e.what().c_str());
00023     }
00024     catch (CException &e)
00025     {
00026         ROS_FATAL("Unexpected error: '%s'",e.what().c_str());
00027     }
00028 
00029     // Start publishing in the topic
00030     joy_mov_pub_ = node_handle_.advertise<sensor_msgs::Joy>(joy_mov_pub_name_,1);
00031     ROS_DEBUG("Publishing topic active at: '%s'",joy_mov_pub_name_.c_str());
00032 }
00033 
00034 void
00035 IriJoystickDriverNode::wrapper_move_joystick_callback(void * param, unsigned int axis_id, float value)
00036 {
00037     IriJoystickDriverNode * self = (IriJoystickDriverNode *) param;
00038 
00039     self->move_joystick_callback(axis_id, value);
00040 }
00041 
00042 void
00043 IriJoystickDriverNode::move_joystick_callback(unsigned int axis_id, float value)
00044 {
00045     sensor_msgs::Joy joy_msg;
00046 
00047     ROS_DEBUG("Joystick movement. Axis: '%d' - Value: '%f' ", axis_id, value);
00048     total_axes_events_++;
00049 
00050     joy_msg.axes.resize(2);
00051     joy_msg.axes[1] = 0.0;
00052     joy_msg.axes[2] = 0.0;
00053 
00054     joy_msg.axes[axis_id] = value;
00055     publish_joy_msg(joy_msg);
00056 }
00057 
00058 void
00059 IriJoystickDriverNode::publish_joy_msg(const sensor_msgs::Joy & msg)
00060 {
00061     joy_mov_pub_.publish(msg);
00062     total_joy_msg_sent_++;
00063 }
00064 
00065 void
00066 IriJoystickDriverNode::wrapper_button_callback(void * param, unsigned int button_id, bool level)
00067 {
00068     IriJoystickDriverNode * self = (IriJoystickDriverNode *) param;
00069 
00070     self->button_callback(button_id, level);
00071 }
00072 
00073 void
00074 IriJoystickDriverNode::button_callback(unsigned int button_id, bool level)
00075 {
00076     sensor_msgs::Joy joy_msg;
00077     int total_buttons = driver_.get_num_buttons();
00078 
00079     ROS_DEBUG("Button pressed: '%i'", button_id+1);
00080     total_button_events_++;
00081 
00082     // We do not use the 0 for a button id, so conversion to position 
00083     // in the array to button id is: pos +1
00084     joy_msg.buttons.resize(total_buttons+1);
00085 
00086     for (int i = 0; i < total_buttons + 1; i++)
00087         joy_msg.buttons[i] = 0;
00088 
00089     // level is not always 1 in callback, so please check
00090     if (! level)
00091         return;
00092 
00093     joy_msg.buttons[button_id+1] = 1;
00094     publish_joy_msg(joy_msg);
00095 }
00096 
00097 void
00098 IriJoystickDriverNode::mainNodeThread()
00099 {
00100 }
00101 
00102 void
00103 IriJoystickDriverNode::postNodeOpenHook(void)
00104 {
00105     int total_buttons = driver_.get_num_buttons();
00106 
00107     // Enable joystick axes. Need to be every time the joystick is make
00108     // and open since it restart the callbacks.
00109     driver_.enable_position_change_callback(y_axis,
00110                                          wrapper_move_joystick_callback,
00111                                          this);
00112     driver_.enable_position_change_callback(x_axis,
00113                                          wrapper_move_joystick_callback,
00114                                          this);
00115 
00116     for (int i = 0; i < total_buttons; i++)
00117         driver_.enable_button_callback(i, wrapper_button_callback, this);
00118 }
00119 
00120 void
00121 IriJoystickDriverNode::joystick_check(diagnostic_updater::DiagnosticStatusWrapper &stat)
00122 {
00123     // TODO I: posible check if fail to open via variable in
00124     // node_driver if fail to open the device.
00125     stat.summary(0, "OK");
00126 
00127     // TODO II: implement get_device in driver node
00128     stat.add("Physical device",                driver_.get_device());
00129     stat.add("Buttons in device",              driver_.get_num_buttons());
00130     stat.add("Axes in device",                 driver_.get_num_axes());
00131     stat.add("Publishing movements topic",     joy_mov_pub_.getTopic());
00132     stat.add("Subscribers to movements topic", joy_mov_pub_.getNumSubscribers());
00133     stat.add("Total axes events generated",    total_axes_events_);
00134     stat.add("Total buttons events generated", total_button_events_);
00135     stat.add("Total joy msg published",        total_joy_msg_sent_);
00136 }
00137 
00138 void
00139 IriJoystickDriverNode::addNodeDiagnostics(void)
00140 {
00141   diagnostic_.add("General joystick check", this, &IriJoystickDriverNode::joystick_check);
00142 }
00143 
00144 void
00145 IriJoystickDriverNode::addNodeOpenedTests(void)
00146 {
00147 }
00148 
00149 void
00150 IriJoystickDriverNode::addNodeStoppedTests(void)
00151 {
00152 }
00153 
00154 void
00155 IriJoystickDriverNode::addNodeRunningTests(void)
00156 {
00157 }
00158 
00159 void
00160 IriJoystickDriverNode::reconfigureNodeHook(int level)
00161 {
00162 }
00163 
00164 IriJoystickDriverNode::~IriJoystickDriverNode()
00165 {
00166     driver_.stopDriver();
00167     driver_.closeDriver();
00168 }
00169 
00170 /* main function */
00171 int main(int argc,char *argv[])
00172 {
00173   return driver_base::main<IriJoystickDriverNode>(argc,argv,"iri_joystick_driver_node");
00174 }


iri_joystick
Author(s): Jose Luis Rivero
autogenerated on Fri Dec 6 2013 23:38:45