uos_rotunit_teleop_ps3joy.cpp
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (C) 2015 University of Osnabrück, Germany
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  *
00019  * uos_rotunit_teleop_ps3joy.cpp
00020  *
00021  *  Created on: 05.03.2015
00022  *      Author: Sebastian Pütz <spuetz@uos.de>
00023  */
00024 
00025 #include "uos_rotunit_teleop_ps3joy.h"
00026 
00027 RotunitTeleopPS3Joy::RotunitTeleopPS3Joy(ros::NodeHandle &nh)
00028   : nh_(nh),
00029     ac("rotunit_snapshotter", true),
00030     vel_(0)
00031 {
00032   ROS_INFO("Waiting for the uos_rotunit_snapshotter action server to start.");
00033   ac.waitForServer(); 
00034   ROS_INFO("Connected to uos_rotunit_snapshotter server.");
00035 
00036   nh_.param("acc", acc_, 0.01);           // acceleration
00037   nh_.param("scan_vel", scan_vel_, 0.6);  // velocity while scanning
00038   nh_.param("max_vel", max_vel_, 1.3);    // maximum velocity
00039   nh_.param("timeout", timeout_, 30.0);    // waiting time for a snapshot to be finished
00040   vel_pub_ = nh_.advertise<geometry_msgs::Twist>("rot_vel", 1);
00041   joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 15,  &RotunitTeleopPS3Joy::PS3Callback, this);
00042   rot_vel_client_ = nh_.serviceClient<uos_rotunit_driver::RotVelSrv>("rotunit_velocity");
00043 }
00044 
00045 void RotunitTeleopPS3Joy::PS3Callback(const sensor_msgs::Joy::ConstPtr &joy){
00046 
00047   uos_rotunit_driver::RotVelSrv rot_vel_srv;
00048   
00049   // handle uos_rotunit velocity 
00050   // via ps3 buttons rear right 1 and 2
00051   if(joy->buttons[PS3_BUTTON_REAR_RIGHT_2])
00052     vel_ += acc_;
00053   if(joy->buttons[PS3_BUTTON_REAR_RIGHT_1])
00054     vel_ -= acc_;
00055   
00056   // ensure maxima
00057   if(vel_ > max_vel_)
00058     vel_ = max_vel_;
00059   if(vel_ < -max_vel_) 
00060     vel_ = -max_vel_;
00061 
00062   rot_vel_srv.request.twist.angular.z = vel_;
00063   rot_vel_client_.call(rot_vel_srv);
00064   
00065   // handle uos_rotunit_snapshotter scan command
00066   if(joy->buttons[PS3_BUTTON_ACTION_TRIANGLE]){
00067     sound_client_.say("Starting scan");
00068 
00069     // set scan velocity and wait for 3 seconds
00070     rot_vel_srv.request.twist.angular.z = scan_vel_;
00071     rot_vel_client_.call(rot_vel_srv);
00072     ROS_INFO("start snapshot in 3 seconds...");
00073     ros::Duration(3.0).sleep();
00074     
00075     // call snapshotter action server
00076     ROS_INFO("start snapshot...");
00077     uos_rotunit_snapshotter::RotunitSnapshotGoal goal;
00078     goal.angle = 2 * M_PI;
00079     ac.sendGoal(goal);
00080     bool finished_before_timeout =
00081       ac.waitForResult(ros::Duration(timeout_));
00082 
00083     if (finished_before_timeout){
00084       actionlib::SimpleClientGoalState state = ac.getState();
00085       ROS_INFO("Snapshot action finished with the state: %s",state.toString().c_str());
00086       sound_client_.say("Finished scan");
00087     }
00088     else{
00089       ROS_INFO("Snapshot action did not finish before the timeout of %f seconds.", timeout_);
00090       sound_client_.say("Scan didn't finish in time");
00091     }
00092 
00093     // set standard velocity and call velocity service
00094     rot_vel_srv.request.twist.angular.z = vel_;
00095     rot_vel_client_.call(rot_vel_srv);
00096   }
00097 }
00098 
00099 int main(int args, char**argv){
00100   ros::init(args, argv, "uos_rotunit_teleop_ps3joy");
00101   ros::NodeHandle nh;
00102   RotunitTeleopPS3Joy teleop(nh);
00103   ros::spin();
00104   return EXIT_SUCCESS;
00105 }


uos_rotunit_teleop
Author(s):
autogenerated on Thu Jun 6 2019 19:07:33