svenzva_3axis_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 <std_msgs/Int32.h>
35 #include <geometry_msgs/Twist.h>
36 #include <sensor_msgs/Joy.h>
37 #include <svenzva_msgs/GripperAction.h>
39 
40 /*
41  * This handles the joystick / velocity interface for the
42  * Svenzva Robotics custom 3axis joystick with mode button slecetor controller.
43  *
44  */
45 
47 
49 {
50 public:
52  sensor_msgs::Joy cur_cmd;
53  sensor_msgs::Joy last_cmd;
54  bool valid;
56  int mode;
57  int linear_x;
58 
59 private:
60  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
61 
63 
65  double l_scale_, a_scale_;
70 };
71 
73  linear_x(0),
74  linear_y(1),
75  linear_z(4),
76  angular_x(4),
77  angular_y(4),
78  angular_z(4),
79  mode_button(0),
80  rate_(20),
81  r(20)
82 {
83  nh_.param("rate", rate_, rate_);
84  nh_.param("axis_linear_x", linear_x, linear_x);
85  nh_.param("axis_linear_y", linear_y, linear_y);
86  nh_.param("axis_linear_z", linear_z, linear_z);
87  nh_.param("axis_angular_x", angular_x, angular_x);
88  nh_.param("axis_angular_y", angular_y, angular_y);
89  nh_.param("axis_angular_z", angular_z, angular_z);
90  nh_.param("mode_button", mode_button, mode_button);
91  nh_.param("scale_angular", a_scale_, a_scale_);
92  nh_.param("scale_linear", l_scale_, l_scale_);
93 
94  valid = false;
95  r = ros::Rate(rate_);
96  mode_pub_ = nh_.advertise<std_msgs::Int32>("revel/eef_velocity_mode", 1);
97  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("revel/eef_velocity", 1);
98  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 1, &SvenzvaArmJoystick::joyCallback, this);
99  mode = 0;
100 }
101 
102 void SvenzvaArmJoystick::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
103 {
104  geometry_msgs::Twist twist;
105  std_msgs::Int32 mode_cmd;
106  mode_cmd.data = mode;
107  if(mode == 0){
108  twist.linear.x = l_scale_*joy->axes[linear_x];
109  twist.linear.y = l_scale_*joy->axes[linear_y];
110  twist.linear.z = l_scale_*joy->axes[linear_z];
111  }
112  if(mode == 1){
113  twist.angular.x = a_scale_*joy->axes[linear_x];
114  twist.angular.y = a_scale_*joy->axes[linear_y];
115  twist.angular.z = a_scale_*joy->axes[linear_z];
116  }
117  vel_pub_.publish(twist);
118  mode_pub_.publish(mode_cmd);
119  last_cmd = cur_cmd;
120  cur_cmd = *joy;
121  valid = true;
122 }
123 
124 
125 int main(int argc, char** argv)
126 {
127  ros::init(argc, argv, "svenzva_3axis_controller");
128  SvenzvaArmJoystick svenzva_joy;
129 
130  gripperClient gripper_action("/revel/gripper_action", true);
131  gripper_action.waitForServer();
132  bool gripper_open = true;
133 
134  while(!svenzva_joy.valid){
135  ros::Duration(0.5).sleep();
136  ros::spinOnce();
137  }
138 
139  int last_mode = 0;
140  while(ros::ok()){
141  ros::spinOnce();
142  if(svenzva_joy.cur_cmd.buttons[svenzva_joy.mode_button] == 0 && last_mode == 1){
143  svenzva_joy.mode += 1;
144  if (svenzva_joy.mode > 2)
145  svenzva_joy.mode = 0;
146  }
147 
148  if(svenzva_joy.mode == 2){
149 
150  svenzva_msgs::GripperGoal goal;
151 
152  int user_target_goal = 0;
153  if(svenzva_joy.cur_cmd.axes[svenzva_joy.linear_x] > 0.95)
154  user_target_goal = 1;
155  else if (svenzva_joy.cur_cmd.axes[svenzva_joy.linear_x] < -0.95)
156  user_target_goal = -1;
157 
158 
159  if(user_target_goal < 0){
160  goal.target_action = goal.CLOSE;
161  goal.target_current = 200;
162  gripper_open = false;
163  gripper_action.sendGoalAndWait(goal);
164  ros::Duration(0.25).sleep();
165 
166  }
167  else if(user_target_goal > 0){
168  goal.target_action = goal.OPEN;
169  gripper_open = true;
170  gripper_action.sendGoalAndWait(goal);
171  ros::Duration(0.25).sleep();
172  }
173  }
174 
175  last_mode = svenzva_joy.cur_cmd.buttons[svenzva_joy.mode_button];
176  ros::Rate(15).sleep();
177  }
178 
179 
180 }
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())
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void joyCallback(const sensor_msgs::Joy::ConstPtr &joy)
actionlib::SimpleActionClient< svenzva_msgs::GripperAction > gripperClient
int main(int argc, char **argv)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
SimpleClientGoalState sendGoalAndWait(const Goal &goal, const ros::Duration &execute_timeout=ros::Duration(0, 0), const ros::Duration &preempt_timeout=ros::Duration(0, 0))
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
ROSCPP_DECL void spinOnce()


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