ps3.cpp
Go to the documentation of this file.
00001 #include "evarobot_teleop/ps3.h"
00002 #include <signal.h>
00003 
00004 int i_error_code = 0;
00005 
00006 IMJoystick::IMJoystick()
00007 {
00008         this->max_lin = 1.0;
00009         this->max_ang = 3.0;
00010         this->deadman = false;
00011         
00012         this->max_x = 0.2;
00013         this->max_z = 0.5;
00014 }
00015 
00016 void ProduceDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &stat)
00017 {
00018     if(i_error_code<0)
00019     {
00020         stat.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "%s", GetErrorDescription(i_error_code).c_str());
00021         i_error_code = 0;
00022     }
00023     else
00024     {
00025         stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "No collision!");
00026     }
00027 }
00028 
00029 void IMJoystick::init()
00030 {
00031         this->deadman_button = 10;
00032         this->axr_leftwards = 2;
00033         this->axr_upwards = 3;
00034         this->axl_leftwards = 0;
00035         this->axl_upwards = 1;
00036 
00037         this->triangle_button = 12;
00038         this->cross_button = 14;
00039         this->square_button = 15;
00040         this->circle_button = 13;
00041         
00042         this->vel_msg.linear.x = 0.0;
00043         this->vel_msg.angular.z = 0.0;
00044         
00045         this->client = this->n.serviceClient<im_msgs::SetRGB>("evarobot_rgb/SetRGB");
00046                 
00047         this->vel_pub = this->n.advertise<geometry_msgs::Twist>("cmd_vel", 1);
00048         this->joy_sub_ = this->n.subscribe("joy", 10, &IMJoystick::CallbackJoy, this);
00049         
00050         this->n.param("evarobot_ps3/timeout", this->d_timeout, 1.0);
00051         ROS_DEBUG("EvarobotTeleop: timeout: %f", this->d_timeout);
00052                 
00053         this->last_time = ros::Time::now();     
00054 }
00055 
00056 void IMJoystick::CallbackJoy(const sensor_msgs::Joy::ConstPtr& joy_msg)
00057 {
00058         deadman = (((unsigned int)deadman_button < joy_msg->buttons.size()) && joy_msg->buttons[deadman_button]);
00059 
00060         this->last_time = ros::Time::now();     
00061 
00062         ROS_DEBUG("EvarobotTeleop: DEADMAN %d", joy_msg->buttons[deadman_button]);
00063         
00064         if (!deadman)
00065         {
00066                 ROS_DEBUG("EvarobotTeleop: deadman is false");
00067                 this->vel_msg.linear.x = 0.0;
00068                 this->vel_msg.angular.z = 0.0;
00069                 return;
00070         }
00071 
00072         if(((unsigned int)triangle_button < joy_msg->buttons.size()) && joy_msg->buttons[triangle_button])
00073                 this->max_x += 0.1;
00074         
00075         if(((unsigned int)cross_button < joy_msg->buttons.size()) && joy_msg->buttons[cross_button])
00076                                         this->max_x -= 0.1;
00077 
00078         if(this->max_x < 0.0)
00079                 max_x = 0.1;
00080 
00081         if(this->max_x > this->max_lin)
00082                 max_x = this->max_lin;
00083 
00084 
00085         if(((unsigned int)square_button < joy_msg->buttons.size()) && joy_msg->buttons[square_button])
00086                 this->max_z += 0.1;
00087 
00088         if(((unsigned int)circle_button < joy_msg->buttons.size()) && joy_msg->buttons[circle_button])
00089                 this->max_z -= 0.1;
00090 
00091         if(this->max_z < 0.0)
00092                 max_z = 0.1;
00093 
00094         if(this->max_z > this->max_ang)
00095                 max_z = this->max_ang;
00096 
00097 
00098 
00099         ROS_DEBUG("EvarobotTeleop: linear %f <-- %d", joy_msg->axes[axr_upwards], axr_upwards);
00100         ROS_DEBUG("EvarobotTeleop: angular %f <-- %d", joy_msg->axes[axl_leftwards], axl_leftwards);
00101 
00102         this->vel_msg.linear.x = this->max_x * joy_msg->axes[axr_upwards];
00103         this->vel_msg.angular.z = this->max_z * joy_msg->axes[axl_leftwards];
00104 }
00105 
00106 void IMJoystick::PublishVel()
00107 {
00108         this->CheckTimeout();
00109         this->vel_pub.publish(this->vel_msg);
00110 }
00111 
00112 void IMJoystick::CallRGBService()
00113 {
00114         im_msgs::SetRGB srv;
00115 
00116         srv.request.times = -1;
00117         srv.request.mode = 3;
00118         srv.request.frequency = -1;
00119         srv.request.color = 0;
00120 
00121         if(this->client.call(srv) == 0)
00122         {
00123                 //ROS_ERROR("Failed to call service evarobot_rgb/SetRGB");
00124                 ROS_INFO(GetErrorDescription(-123).c_str());
00125         i_error_code = -123;
00126         }
00127 }
00128 
00129 void IMJoystick::CheckTimeout()
00130 {
00131         ros::Duration duration = ros::Time::now() - this->last_time;
00132         if(duration.toSec() > d_timeout)
00133         {
00134                 this->vel_msg.linear.x = 0.0;
00135                 this->vel_msg.angular.z = 0.0;
00136                 //ROS_ERROR("Timeout Error [%f]", duration.toSec());
00137                 ROS_INFO(GetErrorDescription(-123).c_str());
00138         i_error_code = -124;
00139         }
00140 }
00141 
00142 void IMJoystick::TurnOffRGB()
00143 {
00144         im_msgs::SetRGB srv;
00145 
00146         srv.request.times = -1;
00147         srv.request.mode = 0;
00148         srv.request.frequency = 1;
00149         srv.request.color = 0;
00150         if(this->client.call(srv) == 0)
00151         {
00152                 //ROS_ERROR("Failed to call service evarobot_rgb/SetRGB");
00153                 ROS_INFO(GetErrorDescription(-123).c_str());
00154         i_error_code = -123;
00155         }
00156 }
00157 
00158 bool g_b_terminate = false;
00159 
00160 void sighandler(int sig)
00161 {
00162         g_b_terminate = true;
00163 }
00164 
00165 int main(int argc, char **argv)
00166 {
00167         ros::init(argc, argv, "evarobot_ps3");
00168         
00169         IMJoystick ps3;
00170         ps3.init();
00171         ps3.CallRGBService();
00172         
00173         ros::Rate loop_rate(10.0);
00174         
00175         signal(SIGINT, &sighandler);
00176         // ROS PARAMS
00177     double d_min_freq = 0.2;
00178         double d_max_freq = 10.0;
00179 
00180         // Diagnostics
00181         diagnostic_updater::Updater updater;
00182         updater.setHardwareID("EvarobotEIO");
00183         updater.add("eio", &ProduceDiagnostics);
00184 
00185         diagnostic_updater::HeaderlessTopicDiagnostic pub_freq("eio", updater,
00186             diagnostic_updater::FrequencyStatusParam(&d_min_freq, &d_max_freq, 0.1, 10));
00187             
00188   while (!g_b_terminate)
00189   {
00190                 ps3.PublishVel();
00191                 updater.update();
00192                 ros::spinOnce();
00193                 loop_rate.sleep();
00194   }
00195   ps3.TurnOffRGB();  
00196         return 0;
00197 }


evarobot_teleop
Author(s): Mehmet Akcakoca
autogenerated on Fri Feb 12 2016 01:15:33