joystick_control.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/Joy.h>
00003 #include <corobot_srvs/MoveArm.h>
00004 #include <corobot_msgs/MotorCommand.h>
00005 #include <corobot_msgs/takepic.h>
00006 #include <corobot_msgs/PanTilt.h>
00007 #include <corobot_msgs/velocityValue.h>
00008 
00009 #include <math.h>
00010 
00011 using namespace corobot_msgs;
00012 using namespace corobot_srvs;
00013 
00014 ros::ServiceClient moveArm_client;
00015 ros::ServiceClient moveWrist_client; 
00016 ros::ServiceClient moveGripper_client; 
00017 ros::ServiceClient resetArm_client; 
00018 
00019 ros::Publisher driveControl_pub,takepic_pub,pan_tilt_control;
00020 
00021 int speed_left, speed_right, speed_value;
00022 bool turningLeft, turningRight;
00023 int pan_value,tilt_value;
00024 double orx;
00025 double ory,orz;
00026 int gripper_state; //0 = open, 1 = closed
00027 int save_image_state = 0;
00028 
00029 void velocityCallback(const velocityValue::ConstPtr& msg)
00030 {
00031         speed_value = msg->velocity;
00032 }
00033 void joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
00034 {
00035 //********************************************
00036 //Motor control
00037 
00038         speed_left = joy->axes[1] * 100;
00039         speed_right = joy->axes[1] * 100;
00040         speed_left += -joy->axes[0] * 100;
00041         speed_right += joy->axes[0] * 100;
00042 
00043         if(speed_right >100)
00044                 speed_right = 100;
00045         if(speed_right < -100)
00046                 speed_right = -100;
00047         if(speed_left >100)
00048                 speed_left = 100;
00049         if(speed_left < -100)
00050                 speed_left = -100;
00051 
00052     corobot_msgs::MotorCommand msg;
00053     msg.leftSpeed = speed_left;
00054     msg.rightSpeed = speed_right;
00055     msg.secondsDuration = 3;
00056     msg.acceleration = 50;
00057     driveControl_pub.publish(msg);
00058 
00059   if(joy->buttons[8]) // STOP
00060   {
00061         corobot_msgs::MotorCommand msg;
00062         msg.leftSpeed = 0;
00063         msg.rightSpeed = 0;
00064         msg.secondsDuration = 0;
00065         msg.acceleration = 50;
00066         driveControl_pub.publish(msg);
00067   }
00068 //*********************************************************
00069 //Take picture
00070  if(joy->buttons[11]) // right Stick click Take Picture
00071   {
00072     if(save_image_state == 0)
00073     {
00074         takepic msg;
00075         msg.camera_index = 1;
00076         msg.take = true;
00077         takepic_pub.publish(msg);
00078         save_image_state = 1;
00079     }
00080   }
00081   else
00082         save_image_state = 0;
00083 
00084 //********************************************************
00085 // Pan Tilt Control     
00086   if(joy->axes[2]>0.5) // Pan control
00087   {
00088         if(pan_value > -70)
00089         {
00090         pan_value -= 5;
00091                 PanTilt msg;
00092                 msg.pan = pan_value;
00093                 msg.tilt = tilt_value;
00094                 msg.reset = 0;
00095                 pan_tilt_control.publish(msg);
00096         }
00097   }
00098 
00099   if(joy->axes[2]<-0.5) // Pan control
00100   {
00101         if(pan_value < 70)
00102         {
00103         pan_value += 5;
00104                 PanTilt msg;
00105                 msg.pan = pan_value;
00106                 msg.tilt = tilt_value;
00107                 msg.reset = 0;
00108                 pan_tilt_control.publish(msg);
00109         }
00110   }
00111 
00112   if(joy->axes[3]<-0.5) // Tilt control
00113   {
00114         if(tilt_value > -30)
00115         {
00116         tilt_value -= 5;
00117                 PanTilt msg;
00118                 msg.pan = pan_value;
00119                 msg.tilt = tilt_value;
00120                 msg.reset = 0;
00121                 pan_tilt_control.publish(msg);
00122         }
00123   }
00124 
00125   if(joy->axes[3]>0.5) // Tilt control
00126   {
00127         if(tilt_value < 30)
00128         {
00129         tilt_value += 5;
00130                 PanTilt msg;
00131                 msg.pan = pan_value;
00132                 msg.tilt = tilt_value;
00133                 msg.reset = 0;
00134                 pan_tilt_control.publish(msg);
00135         }
00136   }
00137 
00138  if(joy->buttons[9]) // PTZ reset
00139   {
00140     tilt_value = 0;
00141     pan_value = 0;
00142     PanTilt msg;
00143     msg.pan = pan_value;
00144     msg.tilt = tilt_value;
00145     msg.reset = 0;
00146     pan_tilt_control.publish(msg);
00147   }
00148 
00149 //*****************************************************
00150 
00151 //*****************************************************
00152 //Arm control
00153 if(joy->axes[4]>0.5) // Shoulder control
00154   {
00155         if(ory > 0.7)
00156         {
00157         ory -= M_PI/8;
00158 
00159                 corobot_srvs::MoveArm srv1;
00160 
00161                 srv1.request.gripper = 0;
00162                 srv1.request.wristOrientation = orx;
00163                 srv1.request.shoulderOrientation = ory;
00164                 srv1.request.elbowOrientation = orz;
00165 
00166                 moveArm_client.call(srv1);
00167         }
00168   }
00169 
00170   if(joy->axes[4]<-0.5) // Shoulder control
00171   {
00172     
00173         if( ory < 2)
00174         {
00175                 ory += M_PI/8;
00176 
00177                 corobot_srvs::MoveArm srv1;
00178 
00179                 srv1.request.gripper = 0;
00180                 srv1.request.wristOrientation = orx;
00181                 srv1.request.shoulderOrientation = ory;
00182                 srv1.request.elbowOrientation = orz;
00183 
00184                 moveArm_client.call(srv1);
00185         }
00186   }
00187 
00188   if(joy->axes[5]<-0.5) // Elbow control
00189   {
00190     
00191     if( orz > 0.8)
00192     {
00193                 orz -= M_PI/8;
00194                 corobot_srvs::MoveArm srv1;
00195 
00196                 srv1.request.gripper = 0;
00197                 srv1.request.wristOrientation = orx;
00198                 srv1.request.shoulderOrientation = ory;
00199                 srv1.request.elbowOrientation = orz;
00200 
00201                 moveArm_client.call(srv1);
00202         }
00203   }
00204 
00205   if(joy->axes[5]>0.5) // Elbow control
00206   {
00207     
00208     if( orz < 2.5)
00209     {
00210                 orz += M_PI/8;
00211 
00212            corobot_srvs::MoveArm srv1;
00213 
00214                 srv1.request.gripper = 0;
00215                 srv1.request.wristOrientation = orx;
00216                 srv1.request.shoulderOrientation = ory;
00217                 srv1.request.elbowOrientation = orz;
00218 
00219                 moveArm_client.call(srv1);
00220         }
00221   }
00222 
00223  if(joy->buttons[7]) // arm reset
00224   {
00225     ory = orz = M_PI/2;
00226 
00227    corobot_srvs::MoveArm srv1;
00228 
00229     srv1.request.gripper = 0;
00230     srv1.request.wristOrientation = orx;
00231     srv1.request.shoulderOrientation = ory;
00232     srv1.request.elbowOrientation = orz;
00233 
00234     resetArm_client.call(srv1);
00235   }
00236 //******************************************
00237 //wrist control
00238  if(joy->buttons[4]) // Wrist Left
00239   {
00240     
00241     if(orx < 4.5)
00242     {
00243           orx += 0.5;
00244 
00245            corobot_srvs::MoveArm srv1;
00246 
00247                 srv1.request.gripper = 0;
00248                 srv1.request.wristOrientation = orx;
00249                 srv1.request.shoulderOrientation = ory;
00250                 srv1.request.elbowOrientation = orz;
00251 
00252                 moveWrist_client.call(srv1);
00253         }
00254   }
00255 
00256  if(joy->buttons[5]) // Wrist Right
00257   {
00258     if(orx > 0.1)
00259     {
00260           orx -= 0.5;
00261 
00262    corobot_srvs::MoveArm srv1;
00263 
00264     srv1.request.gripper = 0;
00265     srv1.request.wristOrientation = orx;
00266     srv1.request.shoulderOrientation = ory;
00267     srv1.request.elbowOrientation = orz;
00268 
00269     moveWrist_client.call(srv1);
00270         }
00271   }
00272 //****************************************************
00273 //gripper control
00274   if(joy->buttons[6])
00275   {
00276    corobot_srvs::MoveArm srv1;
00277 
00278     if(gripper_state == 0)
00279         gripper_state = 1;
00280     else if(gripper_state == 1)
00281         gripper_state = 0;
00282     srv1.request.gripper = gripper_state;
00283     srv1.request.wristOrientation = orx;
00284     srv1.request.shoulderOrientation = ory;
00285     srv1.request.elbowOrientation = orz;
00286 
00287     moveGripper_client.call(srv1);
00288   }
00289 
00290 //*****************************************************
00291   //if(joy->axes[1]>0)
00292   //{twist.linear.x = speed;twist.angular.z = 1;} // Go Foward
00293 
00294   //if(joy->axes[1]<0)
00295   //{twist.linear.x = -1*speed;twist.angular.z = -1;} // Go Backward
00296 
00297   //if((joy->buttons[0]==1)&&(joy->buttons[1]==0)&&(joy->buttons[2]==0)&&(joy->buttons[3]==0)&&(joy->buttons[4]==0))
00298   //{twist.linear.x = 0;twist.angular.z = 0;}//stop
00299 
00300   //if((joy->buttons[0]==0)&&(joy->buttons[1]==0)&&(joy->buttons[2]==0)&&(joy->buttons[3]==1)&&(joy->buttons[4]==0)&&(joy->axes[1]==0))
00301   //{twist.linear.x = 0;twist.angular.z = 1;}//turn left (constant speed)
00302 
00303   //if((joy->buttons[0]==0)&&(joy->buttons[1]==0)&&(joy->buttons[2]==0)&&(joy->buttons[3]==0)&&(joy->buttons[4]==1)&&(joy->axes[1]==0))
00304   //{twist.linear.x = 0;twist.angular.z = -1;}//turn right
00305  
00306   //if((joy->buttons[0]==0)&&(joy->buttons[1]==1)&&(joy->buttons[2]==0)&&(joy->buttons[3]==0)&&(joy->buttons[4]==0)&&(joy->axes[1]==0))
00307   //{speed = speed - 0.05;} //decrease speed
00308 
00309   //if((joy->buttons[0]==0)&&(joy->buttons[1]==0)&&(joy->buttons[2]==1)&&(joy->buttons[3]==0)&&(joy->buttons[4]==0)&&(joy->axes[1]==0))
00310   //{speed = speed + 0.05;} //decrease speed
00311 
00312 }
00313 
00314 
00315 int main(int argc, char** argv)
00316 {
00317   
00318   pan_value = 0;
00319   tilt_value = 0;
00320   orx = 2.3;//initial value for wrist
00321   ory = M_PI/2;
00322   orz = M_PI/2;
00323   gripper_state = 0;
00324   speed_left = 0;
00325   speed_right = 0;
00326   speed_value = 75;
00327   turningLeft = false;
00328   turningRight = false;
00329 
00330   ros::init(argc, argv, "corobot_joystick");
00331   
00332   ros::NodeHandle n;
00333  
00334   /*ros::Subscriber sub = n.subscribe<joy::Joy>("joy", 1000, joyCallback);*/
00335 
00336   moveArm_client = n.serviceClient<MoveArm>("move_arm");
00337   moveWrist_client = n.serviceClient<MoveArm>("move_wrist");
00338   moveGripper_client = n.serviceClient<MoveArm>("move_gripper");
00339   resetArm_client = n.serviceClient<MoveArm>("reset_arm"); 
00340 
00341   driveControl_pub = n.advertise<corobot_msgs::MotorCommand>("PhidgetMotor", 100);
00342   takepic_pub = n.advertise<takepic>("takepicture",100);
00343   pan_tilt_control = n.advertise<PanTilt>("pantilt",10);
00344 
00345   ros::Subscriber sub = n.subscribe<sensor_msgs::Joy>("joy", 1000, joyCallback);
00346   ros::Subscriber velocity = n.subscribe<velocityValue>("velocityValue", 1000, velocityCallback);
00347 
00348   ros::spin();
00349 }


corobot_joystick
Author(s): Gang Li/GangLi@coroware.com
autogenerated on Tue Jan 7 2014 11:39:37