b21_teleop.cpp
Go to the documentation of this file.
00001 #include <string>
00002 #include <ros/ros.h>
00003 #include <sensor_msgs/Joy.h>
00004 #include <sensor_msgs/JointState.h>
00005 #include <geometry_msgs/Twist.h>
00006 
00007 ros::Publisher pub, pub2;
00008 
00009 double tmin, tmax, pmin, pmax;
00010 double tsmin, tsmax, psmin, psmax;
00011 float pcur, tcur;
00012 bool mode = true;
00013 
00014 double max_speed = 0.500; // m/second
00015 double max_turn = 60.0*M_PI/180.0; // rad/second
00016 
00017 void posRcvd(const sensor_msgs::JointState::ConstPtr& msg) {
00018     pcur = msg->position[0];
00019     tcur = msg->position[1];
00020 }
00021 
00022 void joyRcvd(const sensor_msgs::Joy::ConstPtr& msg) {
00023     float x = msg->axes[0], y = msg->axes[1];
00024     float pan, pvel, tilt, tvel;
00025     float dead = 0.25;
00026     if (msg->buttons[1]) {
00027         mode = !mode;
00028     }
00029     if (mode) {
00030         if (msg->buttons[0]) {
00031             pan = 0;
00032             tilt = 0;
00033             pvel = psmax;
00034             tvel = tsmax;
00035         } else {
00036             if (x<-dead)
00037                 pan = pmin;
00038             else if (x>dead)
00039                 pan = pmax;
00040             else
00041                 pan = pcur;
00042 
00043             if (y<-dead)
00044                 tilt = tmin;
00045             else if (y>dead)
00046                 tilt = tmax;
00047             else
00048                 tilt = tcur;
00049 
00050             float xx = x*x, yy = y*y;
00051             pvel = xx*(psmax-psmin)+psmin;
00052             tvel = yy*(tsmax-tsmin)+tsmin;
00053         }
00054         sensor_msgs::JointState joint_cmd;
00055         joint_cmd.header.stamp = ros::Time::now();
00056         joint_cmd.name.resize(2);
00057         joint_cmd.position.resize(2);
00058         joint_cmd.velocity.resize(2);
00059         joint_cmd.name[0] ="head_pan_joint";
00060         joint_cmd.position[0] = pan;
00061         joint_cmd.velocity[0] = pvel;
00062         joint_cmd.name[1] ="head_tilt_joint";
00063         joint_cmd.position[1] = tilt;
00064         joint_cmd.velocity[1] = tvel;
00065         pub.publish(joint_cmd) ;
00066     } else {
00067                 geometry_msgs::Twist cmdvel;
00068                 float speed, turn;
00069                 if(x<dead && x>-dead)
00070                         turn = 0;
00071                 else
00072                         turn = max_turn * x;
00073                 if(y<dead && y>-dead)
00074                         speed = 0;
00075                 else
00076                         speed = max_speed * y;
00077                 cmdvel.linear.x = speed;
00078         cmdvel.angular.z = turn;
00079                 pub2.publish(cmdvel);
00080     }
00081 }
00082 
00083 int main(int argc, char** argv) {
00084     ros::init(argc, argv, "b21_teleop");
00085     ros::NodeHandle n;
00086 
00087     int hz = 30;
00088     ros::Rate loop_rate(hz);
00089     pub = n.advertise<sensor_msgs::JointState>("/ptu/cmd", 1);
00090     pub2 = n.advertise<geometry_msgs::Twist>("/b21/cmd_vel", 1);
00091     n.param("/ptu/max_pan", pmax, 90.);
00092     n.param("/ptu/min_pan", pmin, -90.);
00093     n.param("/ptu/max_tilt", tmax, 30.);
00094     n.param("/ptu/min_tilt", tmin, -30.);
00095     n.param("/ptu/min_tilt_speed", tsmin, 4.);
00096     n.param("/ptu/max_tilt_speed", tsmax, 140.);
00097     n.param("/ptu/min_pan_speed", psmin, 4.);
00098     n.param("/ptu/max_pan_speed", psmax, 140.);
00099     pcur = tcur = 0;
00100 
00101     ros::Subscriber sub =
00102         n.subscribe<sensor_msgs::Joy>("joy", 1, joyRcvd);
00103     ros::Subscriber sub2=
00104         n.subscribe<sensor_msgs::JointState>("/ptu/state", 1, posRcvd);
00105 
00106     while (ros::ok()) {
00107 
00108         // Process a round of subscription messages
00109         ros::spinOnce();
00110 
00111         // This will adjust as needed per iteration
00112         loop_rate.sleep();
00113     }
00114 
00115     return 0;
00116 }


b21_teleop
Author(s): David V. Lu!!
autogenerated on Fri Aug 28 2015 12:58:53