revel_cartesian_vel.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 Svenzva Robotics LLC 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 
34 #include <ros/ros.h>
35 #include <ros/package.h>
36 #include <stdlib.h>
38 
39 #include <kdl/frames.hpp>
40 #include <kdl/chainfksolverpos_recursive.hpp>
41 #include <kdl/chainidsolver_recursive_newton_euler.hpp>
42 #include <kdl/chainiksolvervel_pinv.hpp>
43 
44 #include <sensor_msgs/JointState.h>
45 #include <svenzva_msgs/SvenzvaJointAction.h>
46 #include <geometry_msgs/Twist.h>
47 
48 /*
49  * A CPP implementation of a cartesian controller for Revel robot.
50  *
51  * This file is generally depreciated and has been replaced with a python implementation
52  * that has direct access to the communications layer of the robot.
53  */
54 
55 
56 sensor_msgs::JointState joint_states;
59 int mNumJnts = 6;
60 // Get some joint pos, vel, acc values
65 svenzva_msgs::SvenzvaJointGoal goal;
66 bool vel_updated = false;
67 
68 
69 void js_cb(const sensor_msgs::JointState::ConstPtr& msg){
70  joint_states = *msg;
71 }
72 
73 /*
74  * Inverse velocity kinematics
75  */
76 
77 void cart_vel_cb(const geometry_msgs::TwistConstPtr& msg){
78  if(msg->linear.x == 0 && msg->linear.y == 0 && msg->linear.z == 0)
79  return;
80 
81  for (unsigned int i = 0; i < mNumJnts; i++) {
82  jnt_q(i) = joint_states.position[i];
83  jnt_qd(i) = 0.0;
84  jnt_qdd(i) = 0.0;
85  }
86 
87  KDL::ChainIkSolverVel_pinv vel_solver = KDL::ChainIkSolverVel_pinv(chain,0.00001,150);
88 
89  //Find an output joint velocity qdot_out, given a starting joint pose q_init and a desired cartesian velocity v_in
90  KDL::Vector trans(msg->linear.x, msg->linear.y, -1 * msg->linear.z);
91  KDL::Vector rot(msg->angular.x, msg->angular.y, -1 * msg->angular.z);
92  KDL::Twist vel(trans, rot);
93  KDL::JntArray qdot_out(mNumJnts);
94 
95  //Rather than using joint velocity (which requires a context switch in motor EEPROM)
96  //get joint positions that satisfy the trans and rotation given.
97  //TODO: switch to velocity, and extrapolate position so as to send position trajectories on a constant, but incremental basis
98  //so as to behave as one would expect a velocity command to
99  vel_solver.CartToJnt( jnt_q, vel, qdot_out);
100 
101  svenzva_msgs::SvenzvaJointGoal goal_prime;
102  ros::spinOnce();
103 
104  for( int i=0; i < mNumJnts; i++){
105  //ROS_INFO("Joint %d: %f", i+1, qdot_out(i));
106  ROS_INFO("Joint at %f, moving to %f", joint_states.position[i], joint_states.position[i] + qdot_out(i));
107  goal_prime.positions.push_back(joint_states.position[i] + qdot_out(i));
108  }
109  goal = goal_prime;
110  vel_updated = true;
111 
112  if(false){
113  //send position trajectory to driver
114  //j_ac.sendGoal(goal);
115  }
116 
117 }
118 
119 int main(int argc, char** argv){
120  ros::init(argc, argv, "revel_cartesian_vel_manager");
121 
122  ros::NodeHandle n;
123  int rate = 10;
124  //n.param<int>("~publish_rate", rate, 20);
125  ros::Subscriber sub = n.subscribe("/revel/eef_velocity", 1, cart_vel_cb);
126  ros::Subscriber js_sub = n.subscribe("joint_states", 2, js_cb);
127  ros::Rate update_rate = ros::Rate(rate);
128  std::string path = ros::package::getPath("svenzva_description");
129  std::string full_path = path + "/robots/svenzva_arm.urdf";
130 
131  actionlib::SimpleActionClient<svenzva_msgs::SvenzvaJointAction> j_ac("svenzva_joint_action", true);
132 
133  //setup kinematic model
134  ROS_INFO("Loading model from %s", full_path.c_str());
135 
136  kdl_parser::treeFromFile(full_path, my_tree);
137 
138  if (!kdl_parser::treeFromFile(full_path, my_tree)){
139  ROS_ERROR("Failed to construct kdl tree");
140  return false;
141  }
142  my_tree.getChain("base_link", "link_6", chain);
143  ROS_INFO("Kinematic chain expects %d joints", chain.getNrOfJoints());
144 
145  ROS_INFO("Waiting for svenzva_joint_action server to start.");
146  j_ac.waitForServer();
147 
148 
149  while(ros::ok()){
150  ros::spinOnce();
151  //hacky use of private action server?
152  if(vel_updated)
153  j_ac.sendGoal(goal);
154  vel_updated = false;
155  update_rate.sleep();
156  }
157 
158  return 0;
159 
160 }
161 
KDL::JntArray jnt_qdd(mNumJnts)
KDL::JntArray jnt_q(mNumJnts)
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int mNumJnts
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
virtual int CartToJnt(const JntArray &q_in, const Twist &v_in, JntArray &qdot_out)
void js_cb(const sensor_msgs::JointState::ConstPtr &msg)
svenzva_msgs::SvenzvaJointGoal goal
KDL::JntArray jnt_qd(mNumJnts)
bool vel_updated
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
unsigned int getNrOfJoints() const
KDL_PARSER_PUBLIC bool treeFromFile(const std::string &file, KDL::Tree &tree)
void cart_vel_cb(const geometry_msgs::TwistConstPtr &msg)
ROSLIB_DECL std::string getPath(const std::string &package_name)
bool sleep()
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
KDL::Chain chain
int main(int argc, char **argv)
KDL::Tree my_tree
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
sensor_msgs::JointState joint_states
KDL::JntArray jnt_taugc(mNumJnts)


svenzva_drivers
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:27