microphone.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include <stdr_robot/sensors/microphone.h>
00023 
00024 namespace stdr_robot {
00025   
00034   SoundSensor::SoundSensor(
00035     const nav_msgs::OccupancyGrid& map,
00036     const stdr_msgs::SoundSensorMsg& msg, 
00037     const std::string& name,
00038     ros::NodeHandle& n)
00039     : Sensor(map, name, n, msg.pose, msg.frame_id, msg.frequency)
00040   {
00041     _description = msg;
00042 
00043     _publisher = n.advertise<stdr_msgs::SoundSensorMeasurementMsg>
00044       ( _namespace + "/" + msg.frame_id, 1 );
00045       
00046     sound_sources_subscriber_ = n.subscribe(
00047       "stdr_server/sound_sources_list", 
00048       1, 
00049       &SoundSensor::receiveSoundSources,
00050       this);
00051   }
00052   
00057   SoundSensor::~SoundSensor(void)
00058   {
00059     
00060   }
00061 
00066   void SoundSensor::updateSensorCallback() 
00067   {
00068     if (sound_sources_.sound_sources.size() == 0) return;    
00069 
00070     stdr_msgs::SoundSensorMeasurementMsg measuredSourcesMsg;
00071 
00072     measuredSourcesMsg.header.frame_id = _description.frame_id;
00073     measuredSourcesMsg.sound_dbs = 0; 
00074     
00075     float max_range = _description.maxRange;
00076     float sensor_th = tf::getYaw(_sensorTransform.getRotation());
00077     float min_angle = sensor_th - _description.angleSpan / 2.0;
00078     float max_angle = sensor_th + _description.angleSpan / 2.0;
00079     
00081     for(unsigned int i = 0 ; i < sound_sources_.sound_sources.size() ; i++)
00082     {
00084       float sensor_x = _sensorTransform.getOrigin().x();
00085       float sensor_y = _sensorTransform.getOrigin().y();
00086       float dist = sqrt(
00087         pow(sensor_x - sound_sources_.sound_sources[i].pose.x, 2) +
00088         pow(sensor_y - sound_sources_.sound_sources[i].pose.y, 2)
00089       );
00090       if(dist > max_range)
00091       {
00092         continue;
00093       }
00094       
00096       float ang = atan2(
00097         sound_sources_.sound_sources[i].pose.y - sensor_y,
00098         sound_sources_.sound_sources[i].pose.x - sensor_x);
00099       
00100       if(!stdr_robot::angCheck(ang, min_angle, max_angle))
00101       {
00102         continue;
00103       }
00104       
00105       if(dist > 0.5)
00106       {
00107         measuredSourcesMsg.sound_dbs += sound_sources_.sound_sources[i].dbs *
00108           pow(0.5, 2) / pow(dist, 2);
00109       }
00110       else
00111       {
00112         measuredSourcesMsg.sound_dbs += sound_sources_.sound_sources[i].dbs;
00113       }
00114     }
00115     
00116     measuredSourcesMsg.header.stamp = ros::Time::now();
00117     measuredSourcesMsg.header.frame_id = 
00118       _namespace + "_" + _description.frame_id;
00119     _publisher.publish( measuredSourcesMsg );
00120   }
00121   
00125   void SoundSensor::receiveSoundSources(const stdr_msgs::SoundSourceVector& msg)
00126   {
00127     sound_sources_ = msg;
00128   }
00129 
00130 }  // namespace stdr_robot


stdr_robot
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Wed Sep 2 2015 03:36:25