Advertisement
Guest User

P3-AT Pioneer ROS Sonar Test

a guest
Apr 25th, 2012
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. /*
  2.  * Software License Agreement (BSD License)
  3.  *
  4.  *  Copyright (c) 2010. David Feil-Seifer
  5.  *  All rights reserved.
  6.  *
  7.  *  Redistribution and use in source and binary forms, with or without
  8.  *  modification, are permitted provided that the following conditions
  9.  *  are met:
  10.  *
  11.  *   * Redistributions of source code must retain the above copyright
  12.  *     notice, this list of conditions and the following disclaimer.
  13.  *   * Redistributions in binary form must reproduce the above
  14.  *     copyright notice, this list of conditions and the following
  15.  *     disclaimer in the documentation and/or other materials provided
  16.  *     with the distribution.
  17.  *   * Neither the name of Willow Garage, Inc. nor the names of its
  18.  *     contributors may be used to endorse or promote products derived
  19.  *     from this software without specific prior written permission.
  20.  *
  21.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24.  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25.  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26.  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27.  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29.  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31.  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32.  *  POSSIBILITY OF SUCH DAMAGE.
  33.  */
  34.  
  35. #include <ros/ros.h>
  36. #include <sensor_msgs/JointState.h>
  37. #include <sensor_msgs/Range.h>
  38.  
  39. // For moving the robot on the map
  40. #include <tf/transform_broadcaster.h>
  41. #include <nav_msgs/Odometry.h>
  42.  
  43. int main( int argc, char* argv[] )
  44. {
  45.   ros::init(argc, argv, "p2os_publisher" );
  46.   ros::NodeHandle n;
  47.   ros::NodeHandle n_("~");
  48.  
  49.   ros::Rate loop_rate(10);
  50.  
  51.   ros::Publisher joint_state_publisher = n.advertise<sensor_msgs::JointState>("joint_states",1000);
  52.  
  53.   // Odometry test
  54.   bool publish_odom = true;
  55.   n_.param("publish_odom", publish_odom, publish_odom);
  56.   ros::Publisher odom_pub;
  57.   if(publish_odom) odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
  58.   tf::TransformBroadcaster odom_broadcaster;
  59.  
  60.   // Sonar Range Test
  61.   bool publish_sonar = true;
  62.   n_.param("publish_sonar", publish_sonar, publish_sonar);
  63.   ros::Publisher sonar_pub;
  64.   if(publish_sonar) sonar_pub = n.advertise<sensor_msgs::Range>("Sonar", 1000);;
  65.   sensor_msgs::Range sonar;
  66.   sonar.radiation_type = sensor_msgs::Range::ULTRASOUND;
  67.   sonar.field_of_view = (10.0/180.0) * 3.14;
  68.   sonar.min_range = 0.0;
  69.   sonar.max_range = 10.0;
  70.  
  71.  
  72.   double wheel_rot = 0.0;
  73.  
  74.   double x  = 0.0;
  75.   double y  = 0.0;
  76.   double th = 0.0;
  77.  
  78.   double vx  = 0.2;
  79.   double vy  = 0.0;
  80.   double vth =  vx; // Circle
  81.   double vw  = 1.0;
  82.  
  83.   ros::Time current_time, last_time;
  84.   current_time = ros::Time::now();
  85.   last_time = ros::Time::now();
  86.  
  87.   while( n.ok() )
  88.     {
  89.         current_time = ros::Time::now();
  90.         double dt = (current_time - last_time).toSec();
  91.        
  92.         // Sonar Range Test
  93.         if(publish_sonar)
  94.         {
  95.       for(int i=1;i<=16;i++)
  96.       {
  97.         sonar.header.stamp = ros::Time::now();
  98.         sonar.range        = 1.3;
  99.        
  100.         char buff[64];
  101.         sprintf(buff, "Sonar_f%d", i);
  102.         sonar.header.frame_id = buff;
  103.           sonar_pub.publish(sonar);
  104.          
  105.             ros::spinOnce();
  106.             loop_rate.sleep();
  107.           }
  108.         }
  109.            
  110.         // Wheel joints
  111.     sensor_msgs::JointState js;
  112.       wheel_rot += vw * dt;
  113.      
  114.     // P3-DX
  115.       js.name.push_back(std::string("base_swivel_joint"));
  116.       js.position.push_back(0);
  117.       js.name.push_back(std::string("swivel_hubcap_joint"));
  118.       js.position.push_back(wheel_rot);
  119.       js.name.push_back(std::string("base_left_hubcap_joint"));
  120.       js.position.push_back(wheel_rot);
  121.       js.name.push_back(std::string("base_right_hubcap_joint"));
  122.       js.position.push_back(wheel_rot);
  123.    
  124.       // P3-AT
  125.       js.name.push_back(std::string("base_front_left_hub_joint"));
  126.       js.position.push_back(wheel_rot);
  127.       js.name.push_back(std::string("base_front_right_hub_joint"));
  128.       js.position.push_back(wheel_rot);
  129.       js.name.push_back(std::string("p3at_back_left_hub_joint"));
  130.       js.position.push_back(wheel_rot);
  131.       js.name.push_back(std::string("p3at_back_right_hub_joint"));
  132.       js.position.push_back(wheel_rot);
  133.  
  134.     js.header.frame_id="base_link";
  135.     js.header.stamp = ros::Time::now();
  136.         joint_state_publisher.publish(js);
  137.        
  138.        
  139.     // Odometry given the velocities of the robot
  140.     if(publish_odom)
  141.     {
  142.       double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
  143.       double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
  144.       double delta_th = vth * dt;
  145.  
  146.       x  += delta_x;
  147.       y  += delta_y;
  148.       th += delta_th;
  149.  
  150.       // since all odometry is 6DOF we'll need a quaternion created from yaw
  151.       geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
  152.  
  153.       // first, we'll publish the transform over tf
  154.       geometry_msgs::TransformStamped odom_trans;
  155.       odom_trans.header.stamp = current_time;
  156.       odom_trans.header.frame_id = "odom";
  157.       odom_trans.child_frame_id = "base_link";
  158.  
  159.       odom_trans.transform.translation.x = x;
  160.       odom_trans.transform.translation.y = y;
  161.       odom_trans.transform.translation.z = 0.0;
  162.       odom_trans.transform.rotation = odom_quat;
  163.  
  164.       // send the transform
  165.       odom_broadcaster.sendTransform(odom_trans);
  166.  
  167.       // next, we'll publish the odometry message over ROS
  168.       nav_msgs::Odometry odom;
  169.       odom.header.stamp = current_time;
  170.       odom.header.frame_id = "odom";
  171.  
  172.       // set the position
  173.       odom.pose.pose.position.x = x;
  174.       odom.pose.pose.position.y = y;
  175.       odom.pose.pose.position.z = 0.0;
  176.       odom.pose.pose.orientation = odom_quat;
  177.  
  178.       // set the velocity
  179.       odom.child_frame_id = "base_link";
  180.       odom.twist.twist.linear.x = vx;
  181.       odom.twist.twist.linear.y = vy;
  182.       odom.twist.twist.angular.z = vth;
  183.  
  184.       // publish the message
  185.       odom_pub.publish(odom);
  186.     }
  187.        
  188.         ros::spinOnce();
  189.         loop_rate.sleep();
  190.       last_time = current_time;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement