|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, Yujin Robot. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * * Neither the name of Yujin Robot nor the names of its |
| 14 | + * contributors may be used to endorse or promote products derived from |
| 15 | + * this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Marcus Liebhardt |
| 32 | + * |
| 33 | + * This work has been inspired by Nate Koenig's Gazebo plugin for the iRobot Create. |
| 34 | + */ |
| 35 | + |
| 36 | +#ifndef GAZEBO_ROS_KOBUKI_H |
| 37 | +#define GAZEBO_ROS_KOBUKI_H |
| 38 | + |
| 39 | +#include <cmath> |
| 40 | +#include <cstring> |
| 41 | +#include <string> |
| 42 | +#include <boost/bind.hpp> |
| 43 | +#include <boost/thread.hpp> |
| 44 | +#include <boost/shared_ptr.hpp> |
| 45 | +#include <gazebo/gazebo.hh> |
| 46 | +#include <gazebo/common/common.hh> |
| 47 | +#include <gazebo/common/Time.hh> |
| 48 | +#include <gazebo/math/gzmath.hh> |
| 49 | +#include <gazebo/physics/physics.hh> |
| 50 | +#include <gazebo/sensors/sensors.hh> |
| 51 | +#include <gazebo_plugins/gazebo_ros_utils.h> |
| 52 | +#include <ros/ros.h> |
| 53 | +#include <std_msgs/Empty.h> |
| 54 | +#include <std_msgs/Float64.h> |
| 55 | +#include <sensor_msgs/Imu.h> |
| 56 | +#include <sensor_msgs/JointState.h> |
| 57 | +#include <nav_msgs/Odometry.h> |
| 58 | +#include <geometry_msgs/Twist.h> |
| 59 | +#include <geometry_msgs/TransformStamped.h> |
| 60 | +#include <tf/transform_broadcaster.h> |
| 61 | +#include <tf/LinearMath/Quaternion.h> |
| 62 | +#include <kobuki_msgs/MotorPower.h> |
| 63 | +#include <kobuki_msgs/CliffEvent.h> |
| 64 | +#include <kobuki_msgs/BumperEvent.h> |
| 65 | + |
| 66 | +namespace gazebo |
| 67 | +{ |
| 68 | + |
| 69 | +enum {LEFT= 0, RIGHT=1}; |
| 70 | + |
| 71 | +class GazeboRosKobuki : public ModelPlugin |
| 72 | +{ |
| 73 | +public: |
| 74 | + /// Constructor |
| 75 | + GazeboRosKobuki(); |
| 76 | + /// Destructor |
| 77 | + ~GazeboRosKobuki(); |
| 78 | + /// Called when plugin is loaded |
| 79 | + void Load(physics::ModelPtr parent, sdf::ElementPtr sdf); |
| 80 | + /// Called by the world update start event |
| 81 | + void OnUpdate(); |
| 82 | + |
| 83 | +private: |
| 84 | + /* |
| 85 | + * Methods |
| 86 | + */ |
| 87 | + /// Callback for incoming velocity commands |
| 88 | + void cmdVelCB(const geometry_msgs::TwistConstPtr &msg); |
| 89 | + /// Callback for incoming velocity commands |
| 90 | + void motorPowerCB(const kobuki_msgs::MotorPowerPtr &msg); |
| 91 | + /// Callback for resetting the odometry data |
| 92 | + void resetOdomCB(const std_msgs::EmptyConstPtr &msg); |
| 93 | + /// Spin method for the spinner thread |
| 94 | + void spin(); |
| 95 | + // void OnContact(const std::string &name, const physics::Contact &contact); necessary? |
| 96 | + |
| 97 | + |
| 98 | + // internal functions for load |
| 99 | + void prepareMotorPower(); |
| 100 | + bool prepareJointState(); |
| 101 | + void preparePublishTf(); |
| 102 | + bool prepareWheelAndTorque(); |
| 103 | + void prepareOdom(); |
| 104 | + bool prepareVelocityCommand(); |
| 105 | + bool prepareCliffSensor(); |
| 106 | + bool prepareBumper(); |
| 107 | + bool prepareIMU(); |
| 108 | + void setupRosApi(std::string& model_name); |
| 109 | + |
| 110 | + // internal functions for update |
| 111 | + void updateJointState(); |
| 112 | + void updateOdometry(common::Time& step_time); |
| 113 | + void updateIMU(); |
| 114 | + void propagateVelocityCommands(); |
| 115 | + void updateCliffSensor(); |
| 116 | + void updateBumper(); |
| 117 | + void calculateBumps(double relative_contact_angle); |
| 118 | + |
| 119 | + |
| 120 | + /* |
| 121 | + * Parameters |
| 122 | + */ |
| 123 | + /// ROS node handles (relative & private) |
| 124 | + ros::NodeHandle nh_, nh_priv_; |
| 125 | + /// node name |
| 126 | + std::string node_name_; |
| 127 | + |
| 128 | + /// TF Prefix |
| 129 | + std::string tf_prefix_; |
| 130 | + /// extra thread for triggering ROS callbacks |
| 131 | +// boost::shared_ptr<boost::thread> ros_spinner_thread_; necessary? |
| 132 | + /// flag for shutting down the spinner thread |
| 133 | + bool shutdown_requested_; |
| 134 | + /// pointer to the model |
| 135 | + physics::ModelPtr model_; |
| 136 | + /// pointer to the gazebo ros node |
| 137 | + GazeboRosPtr gazebo_ros_; |
| 138 | + sdf::ElementPtr sdf_; |
| 139 | + /// pointer to simulated world |
| 140 | + physics::WorldPtr world_; |
| 141 | + /// pointer to the update event connection (triggers the OnUpdate callback when event update event is received) |
| 142 | + event::ConnectionPtr update_connection_; |
| 143 | + /// Simulation time on previous update |
| 144 | + common::Time prev_update_time_; |
| 145 | + /// ROS subscriber for motor power commands |
| 146 | + ros::Subscriber motor_power_sub_; |
| 147 | + /// Flag indicating if the motors are turned on or not |
| 148 | + bool motors_enabled_; |
| 149 | + /// Pointers to Gazebo's joints |
| 150 | + physics::JointPtr joints_[2]; |
| 151 | + /// Left wheel's joint name |
| 152 | + std::string left_wheel_joint_name_; |
| 153 | + /// Right wheel's joint name |
| 154 | + std::string right_wheel_joint_name_; |
| 155 | + /// ROS publisher for joint state messages |
| 156 | + ros::Publisher joint_state_pub_; |
| 157 | + /// ROS message for joint sates |
| 158 | + sensor_msgs::JointState joint_state_; |
| 159 | + /// ROS subscriber for velocity commands |
| 160 | + ros::Subscriber cmd_vel_sub_; |
| 161 | + /// Simulation time of the last velocity command (used for time out) |
| 162 | + common::Time last_cmd_vel_time_; |
| 163 | + /// Time out for velocity commands in seconds |
| 164 | + double cmd_vel_timeout_; |
| 165 | + /// Speeds of the wheels |
| 166 | + double wheel_speed_cmd_[2]; |
| 167 | + /// Max. torque applied to the wheels |
| 168 | + double torque_; |
| 169 | + /// Separation between the wheels |
| 170 | + double wheel_sep_; |
| 171 | + /// Diameter of the wheels |
| 172 | + double wheel_diam_; |
| 173 | + /// Vector for pose |
| 174 | + double odom_pose_[3]; |
| 175 | + /// Vector for velocity |
| 176 | + double odom_vel_[3]; |
| 177 | + /// Pointer to pose covariance matrix |
| 178 | + double *pose_cov_[36]; |
| 179 | + /// Pointer to twist covariance matrix |
| 180 | + double *twist_cov_[36]; |
| 181 | + /// ROS publisher for odometry messages |
| 182 | + ros::Publisher odom_pub_; |
| 183 | + /// ROS message for odometry data |
| 184 | + nav_msgs::Odometry odom_; |
| 185 | + /// Flag for (not) publish tf transform for odom -> robot |
| 186 | + bool publish_tf_; |
| 187 | + /// TF transform publisher for the odom frame |
| 188 | + tf::TransformBroadcaster tf_broadcaster_; |
| 189 | + /// TF transform for the odom frame |
| 190 | + geometry_msgs::TransformStamped odom_tf_; |
| 191 | + /// Pointer to left cliff sensor |
| 192 | + sensors::RaySensorPtr cliff_sensor_left_; |
| 193 | + /// Pointer to frontal cliff sensor |
| 194 | + sensors::RaySensorPtr cliff_sensor_center_; |
| 195 | + /// Pointer to left right sensor |
| 196 | + sensors::RaySensorPtr cliff_sensor_right_; |
| 197 | + /// ROS publisher for cliff detection events |
| 198 | + ros::Publisher cliff_event_pub_; |
| 199 | + /// Kobuki ROS message for cliff event |
| 200 | + kobuki_msgs::CliffEvent cliff_event_; |
| 201 | + /// Cliff flag for the left sensor |
| 202 | + bool cliff_detected_left_; |
| 203 | + /// Cliff flag for the center sensor |
| 204 | + bool cliff_detected_center_; |
| 205 | + /// Cliff flag for the right sensor |
| 206 | + bool cliff_detected_right_; |
| 207 | + /// measured distance in meter for detecting a cliff |
| 208 | + float cliff_detection_threshold_; |
| 209 | + /// Maximum distance to floor |
| 210 | + int floot_dist_; |
| 211 | + /// Pointer to bumper sensor simulating Kobuki's left, centre and right bumper sensors |
| 212 | + sensors::ContactSensorPtr bumper_; |
| 213 | + /// ROS publisher for bumper events |
| 214 | + ros::Publisher bumper_event_pub_; |
| 215 | + /// Kobuki ROS message for bumper event |
| 216 | + kobuki_msgs::BumperEvent bumper_event_; |
| 217 | + /// Flag for left bumper's last state |
| 218 | + bool bumper_left_was_pressed_; |
| 219 | + /// Flag for center bumper's last state |
| 220 | + bool bumper_center_was_pressed_; |
| 221 | + /// Flag for right bumper's last state |
| 222 | + bool bumper_right_was_pressed_; |
| 223 | + /// Flag for left bumper's current state |
| 224 | + bool bumper_left_is_pressed_; |
| 225 | + /// Flag for left bumper's current state |
| 226 | + bool bumper_center_is_pressed_; |
| 227 | + /// Flag for left bumper's current state |
| 228 | + bool bumper_right_is_pressed_; |
| 229 | + /// Pointer to IMU sensor model |
| 230 | + sensors::ImuSensorPtr imu_; |
| 231 | + /// Storage for the angular velocity reported by the IMU |
| 232 | + math::Vector3 vel_angular_; |
| 233 | + /// ROS publisher for IMU data |
| 234 | + ros::Publisher imu_pub_; |
| 235 | + /// ROS message for publishing IMU data |
| 236 | + sensor_msgs::Imu imu_msg_; |
| 237 | + /// ROS subscriber for reseting the odometry data |
| 238 | + ros::Subscriber odom_reset_sub_; |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | +}; |
| 243 | + |
| 244 | +} // namespace gazebo |
| 245 | + |
| 246 | +#endif /* GAZEBO_ROS_KOBUKI_H */ |
0 commit comments