svenzva_joystick_controller.cpp
Go to the documentation of this file.
1 // Software License Agreement (BSD License)
2 //
3 // Copyright (c) 2017 Svenzva Robotics
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of University of Arizona nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 #include <ros/ros.h>
34 #include <geometry_msgs/Twist.h>
35 #include <sensor_msgs/Joy.h>
36 #include <svenzva_msgs/GripperAction.h>
38 
40 {
41 public:
43 
44 private:
45  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
46 
48 
50  double l_scale_, a_scale_;
54  ros::Rate r;
55  sensor_msgs::Joy last_cmd;
56 };
57 
58 /*
59  * Default mappings are for Xbox 360 gamepad
60  */
62  linear_x(0),
63  linear_y(1),
64  linear_z(4),
65  angular_(2),
66  rate_(20),
67  r(20)
68 {
69  nh_.param("rate", rate_, rate_);
70  nh_.param("axis_linear_x", linear_x, linear_x);
71  nh_.param("axis_linear_y", linear_y, linear_y);
72  nh_.param("axis_linear_z", linear_z, linear_z);
73  nh_.param("axis_angular", angular_, angular_);
74  nh_.param("scale_angular", a_scale_, a_scale_);
75  nh_.param("scale_linear", l_scale_, l_scale_);
76 
77  linear_mode = false;
78  r = ros::Rate(rate_);
79  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("revel/eef_velocity", 1);
80  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 1, &SvenzvaArmJoystick::joyCallback, this);
81 
82 }
83 
84 void SvenzvaArmJoystick::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
85 {
86  geometry_msgs::Twist twist;
87 
88  if(linear_mode){
89 
90  twist.linear.x = l_scale_*joy->axes[linear_x];
91  twist.linear.y = l_scale_*joy->axes[linear_y];
92  twist.linear.z = l_scale_*joy->axes[linear_z];
93  }
94  else{
95  twist.angular.x = a_scale_*joy->axes[linear_x];
96  twist.angular.y = a_scale_*joy->axes[linear_y];
97  twist.angular.z = a_scale_*joy->axes[linear_z];
98  }
99  vel_pub_.publish(twist);
100  last_cmd = *joy;
101 
102 
103  if(joy->axes[2] < 0 || joy->axes[5] < 0){
104  actionlib::SimpleActionClient<svenzva_msgs::GripperAction> gripper_action("/revel/gripper_action", true);
105  gripper_action.waitForServer();
106 
107  svenzva_msgs::GripperGoal goal;
108  if(joy->axes[2] < 0){
109  goal.target_action = goal.CLOSE;
110  goal.target_current = 300;
111  }
112  else if(joy->axes[5] < 0){
113  goal.target_action = goal.OPEN;
114  }
115 
116  gripper_action.sendGoal(goal);
117  }
118 
119  if(joy->buttons[0]){
121  ROS_INFO("Switching modes. Linear mode is now %d", linear_mode);
122  }
123 }
124 
125 
126 int main(int argc, char** argv)
127 {
128  ros::init(argc, argv, "svenzva_joystick_controller");
129  SvenzvaArmJoystick svenzva_joy;
130  actionlib::SimpleActionClient<svenzva_msgs::GripperAction> gripper_action("/revel/gripper_action", true);
131  gripper_action.waitForServer();
132 
133  while(ros::ok()){
134  ros::spinOnce();
135 
136  }
137 }
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void joyCallback(const sensor_msgs::Joy::ConstPtr &joy)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
ROSCPP_DECL void spinOnce()


svenzva_joy
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:29