|
| 1 | +/* |
| 2 | + * Copyright 2012 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | +#include <math.h> |
| 18 | +#include <ros/ros.h> |
| 19 | +#include <ros/subscribe_options.h> |
| 20 | +#include <boost/thread.hpp> |
| 21 | +#include <boost/algorithm/string.hpp> |
| 22 | +#include <sensor_msgs/JointState.h> |
| 23 | +#include <osrf_msgs/JointCommands.h> |
| 24 | + |
| 25 | +ros::Publisher pub_joint_commands_; |
| 26 | +osrf_msgs::JointCommands jointcommands; |
| 27 | + |
| 28 | +void SetJointStates(const sensor_msgs::JointState::ConstPtr &_js) |
| 29 | +{ |
| 30 | + static ros::Time startTime = ros::Time::now(); |
| 31 | + { |
| 32 | + // for testing round trip time |
| 33 | + jointcommands.header.stamp = _js->header.stamp; |
| 34 | + |
| 35 | + // assign sinusoidal joint angle targets |
| 36 | + for (unsigned int i = 0; i < jointcommands.name.size(); i++) |
| 37 | + jointcommands.position[i] = |
| 38 | + 3.2* sin((ros::Time::now() - startTime).toSec()); |
| 39 | + |
| 40 | + pub_joint_commands_.publish(jointcommands); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +int main(int argc, char** argv) |
| 45 | +{ |
| 46 | + ros::init(argc, argv, "pub_joint_command_test"); |
| 47 | + |
| 48 | + ros::NodeHandle* rosnode = new ros::NodeHandle(); |
| 49 | + |
| 50 | + // Waits for simulation time update. |
| 51 | + ros::Time last_ros_time_; |
| 52 | + bool wait = true; |
| 53 | + while (wait) |
| 54 | + { |
| 55 | + last_ros_time_ = ros::Time::now(); |
| 56 | + if (last_ros_time_.toSec() > 0) |
| 57 | + wait = false; |
| 58 | + } |
| 59 | + |
| 60 | + // must match those inside AtlasPlugin |
| 61 | + jointcommands.name.push_back("atlas::back_lbz"); |
| 62 | + jointcommands.name.push_back("atlas::back_mby"); |
| 63 | + jointcommands.name.push_back("atlas::back_ubx"); |
| 64 | + jointcommands.name.push_back("atlas::neck_ay"); |
| 65 | + jointcommands.name.push_back("atlas::l_leg_uhz"); |
| 66 | + jointcommands.name.push_back("atlas::l_leg_mhx"); |
| 67 | + jointcommands.name.push_back("atlas::l_leg_lhy"); |
| 68 | + jointcommands.name.push_back("atlas::l_leg_kny"); |
| 69 | + jointcommands.name.push_back("atlas::l_leg_uay"); |
| 70 | + jointcommands.name.push_back("atlas::l_leg_lax"); |
| 71 | + jointcommands.name.push_back("atlas::r_leg_uhz"); |
| 72 | + jointcommands.name.push_back("atlas::r_leg_mhx"); |
| 73 | + jointcommands.name.push_back("atlas::r_leg_lhy"); |
| 74 | + jointcommands.name.push_back("atlas::r_leg_kny"); |
| 75 | + jointcommands.name.push_back("atlas::r_leg_uay"); |
| 76 | + jointcommands.name.push_back("atlas::r_leg_lax"); |
| 77 | + jointcommands.name.push_back("atlas::l_arm_usy"); |
| 78 | + jointcommands.name.push_back("atlas::l_arm_shx"); |
| 79 | + jointcommands.name.push_back("atlas::l_arm_ely"); |
| 80 | + jointcommands.name.push_back("atlas::l_arm_elx"); |
| 81 | + jointcommands.name.push_back("atlas::l_arm_uwy"); |
| 82 | + jointcommands.name.push_back("atlas::l_arm_mwx"); |
| 83 | + jointcommands.name.push_back("atlas::r_arm_usy"); |
| 84 | + jointcommands.name.push_back("atlas::r_arm_shx"); |
| 85 | + jointcommands.name.push_back("atlas::r_arm_ely"); |
| 86 | + jointcommands.name.push_back("atlas::r_arm_elx"); |
| 87 | + jointcommands.name.push_back("atlas::r_arm_uwy"); |
| 88 | + jointcommands.name.push_back("atlas::r_arm_mwx"); |
| 89 | + |
| 90 | + unsigned int n = jointcommands.name.size(); |
| 91 | + jointcommands.position.resize(n); |
| 92 | + jointcommands.velocity.resize(n); |
| 93 | + jointcommands.effort.resize(n); |
| 94 | + jointcommands.kp_position.resize(n); |
| 95 | + jointcommands.ki_position.resize(n); |
| 96 | + jointcommands.kd_position.resize(n); |
| 97 | + jointcommands.kp_velocity.resize(n); |
| 98 | + jointcommands.i_effort_min.resize(n); |
| 99 | + jointcommands.i_effort_max.resize(n); |
| 100 | + |
| 101 | + for (unsigned int i = 0; i < n; i++) |
| 102 | + { |
| 103 | + std::vector<std::string> pieces; |
| 104 | + boost::split(pieces, jointcommands.name[i], boost::is_any_of(":")); |
| 105 | + |
| 106 | + rosnode->getParam("atlas_controller/gains/" + pieces[2] + "/p", |
| 107 | + jointcommands.kp_position[i]); |
| 108 | + |
| 109 | + rosnode->getParam("atlas_controller/gains/" + pieces[2] + "/i", |
| 110 | + jointcommands.ki_position[i]); |
| 111 | + |
| 112 | + rosnode->getParam("atlas_controller/gains/" + pieces[2] + "/d", |
| 113 | + jointcommands.kd_position[i]); |
| 114 | + |
| 115 | + rosnode->getParam("atlas_controller/gains/" + pieces[2] + "/i_clamp", |
| 116 | + jointcommands.i_effort_min[i]); |
| 117 | + jointcommands.i_effort_min[i] = -jointcommands.i_effort_min[i]; |
| 118 | + |
| 119 | + rosnode->getParam("atlas_controller/gains/" + pieces[2] + "/i_clamp", |
| 120 | + jointcommands.i_effort_max[i]); |
| 121 | + |
| 122 | + jointcommands.velocity[i] = 0; |
| 123 | + jointcommands.effort[i] = 0; |
| 124 | + jointcommands.kp_velocity[i] = 0; |
| 125 | + } |
| 126 | + |
| 127 | + // ros topic subscriptions |
| 128 | + ros::SubscribeOptions jointStatesSo = |
| 129 | + ros::SubscribeOptions::create<sensor_msgs::JointState>( |
| 130 | + "/atlas/joint_states", 1, SetJointStates, |
| 131 | + ros::VoidPtr(), rosnode->getCallbackQueue()); |
| 132 | + |
| 133 | + // Because TCP causes bursty communication with high jitter, |
| 134 | + // declare a preference on UDP connections for receiving |
| 135 | + // joint states, which we want to get at a high rate. |
| 136 | + // Note that we'll still accept TCP connections for this topic |
| 137 | + // (e.g., from rospy nodes, which don't support UDP); |
| 138 | + // we just prefer UDP. |
| 139 | + jointStatesSo.transport_hints = ros::TransportHints().unreliable(); |
| 140 | + |
| 141 | + ros::Subscriber subJointStates = rosnode->subscribe(jointStatesSo); |
| 142 | + // ros::Subscriber subJointStates = |
| 143 | + // rosnode->subscribe("/atlas/joint_states", 1000, SetJointStates); |
| 144 | + |
| 145 | + pub_joint_commands_ = |
| 146 | + rosnode->advertise<osrf_msgs::JointCommands>( |
| 147 | + "/atlas/joint_commands", 1, true); |
| 148 | + |
| 149 | + ros::spin(); |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
0 commit comments