DvlSim.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, LABUST, UNIZG-FER
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the LABUST nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *  Author: Dula Nad
00035  *  Created: 01.02.2013.
00036  *********************************************************************/
00037 #include <labust/tools/conversions.hpp>
00038 
00039 #include <nav_msgs/Odometry.h>
00040 #include <geometry_msgs/TwistStamped.h>
00041 #include <std_msgs/Float32.h>
00042 #include <ros/ros.h>
00043 #include <tf/transform_datatypes.h>
00044 
00045 struct DvlSim
00046 {
00047         DvlSim():
00048                 maxBottomLock(100),
00049                 maxDepth(100),
00050                 dvl_pub(1)
00051         {
00052                 ros::NodeHandle nh,ph("~");
00053 
00054                 ph.param("MaxBottomLock",maxBottomLock, maxBottomLock);
00055                 ph.param("MaxDepth", maxDepth, maxDepth);
00056                 ph.param("DvlPub", dvl_pub, dvl_pub);
00057 
00058                 odom = nh.subscribe<nav_msgs::Odometry>("meas_odom",1,&DvlSim::onOdom, this);
00059                 dvl_nu = nh.advertise<geometry_msgs::TwistStamped>("dvl",1);
00060                 dvl_ned = nh.advertise<geometry_msgs::TwistStamped>("dvl_ned",1);
00061                 altitude_pub = nh.advertise<std_msgs::Float32>("altitude",1);
00062 
00063                 last_pos[0] = last_pos[1] = last_pos[2] = 0;
00064         }
00065 
00066         void onOdom(const typename nav_msgs::Odometry::ConstPtr& msg)
00067         {
00068                 static int i=0;
00069                 i=++i%dvl_pub;
00070 
00071                 if (i == 0)
00072                 {
00073                         double dT = (ros::Time::now() - lastTime).toSec();
00074                         lastTime = ros::Time::now();
00075                         geometry_msgs::TwistStamped::Ptr dvl(new geometry_msgs::TwistStamped());
00076                         dvl->header.stamp = ros::Time::now();
00077                         dvl->header.frame_id = msg->header.frame_id;
00078                         double pos[3],v[3];
00079                         labust::tools::pointToVector(msg->pose.pose.position, pos);
00080                         for (int i=0; i<3; ++i)
00081                         {
00082                                 v[i] = (pos[i] - last_pos[i])/dT;
00083                                 last_pos[i] = pos[i];
00084                         }
00085                         labust::tools::vectorToPoint(v, dvl->twist.linear);
00086                         dvl_ned.publish(dvl);
00087 
00088                         dvl.reset(new geometry_msgs::TwistStamped());
00089                         dvl->header.stamp = ros::Time::now();
00090                         dvl->header.frame_id = msg->child_frame_id;
00091                         //Calculate body-fixed speeds
00092                         tf::Quaternion q;
00093                         tf::quaternionMsgToTF(msg->pose.pose.orientation, q);
00094                         tf::Transform t;
00095                         t.setRotation(q);
00096                         tf::Vector3 nu = t.getBasis().transpose() * tf::Vector3(v[0],v[1],v[2]);
00097                         dvl->twist.linear.x = nu.x();
00098                         dvl->twist.linear.y = nu.y();
00099                         dvl->twist.linear.z = nu.z();
00100                         dvl_nu.publish(dvl);
00101 
00102                         if ((maxDepth - msg->pose.pose.position.z) < maxBottomLock)
00103                         {
00104                                 std_msgs::Float32::Ptr altitude(new std_msgs::Float32());
00105                                 altitude->data = maxDepth - msg->pose.pose.position.z;
00106                                 altitude_pub.publish(altitude);
00107                         }
00108                 }
00109         }
00110 
00111 private:
00112         ros::Subscriber odom;
00113         ros::Publisher dvl_nu, altitude_pub, dvl_ned;
00114         ros::Time lastTime;
00115         double last_pos[3];
00116         int dvl_pub;
00117         double maxBottomLock, maxDepth;
00118 };
00119 
00120 int main(int argc, char* argv[])
00121 {
00122         ros::init(argc,argv,"dvl_sim");
00123         ros::NodeHandle nh;
00124         DvlSim dvl;
00125         ros::spin();
00126         return 0;
00127 }
00128 
00129 


labust_sim
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:33