windsonic_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita on 10/11/2010
00036 *********************************************************************/
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 
00040 #include <string>
00041 #include <vector>
00042 
00043 #include <ros/ros.h>
00044 #include <cereal_port/CerealPort.h>
00045 #include <lse_sensor_msgs/Anemometer.h>
00046 
00047 ros::Publisher wind_pub;
00048 std::string frame_id;
00049 
00050 void newDataCallback(std::string * data)
00051 {
00052         data->erase(data->size()-1, 1);
00053         data->erase(0, 1);
00054 
00055         //ROS_INFO("Received <STX>%s<ETX>", data->c_str());
00056         
00057         lse_sensor_msgs::Anemometer wind_msg;
00058         int direction;
00059         float speed;
00060         
00061         int first_comma = data->find_first_of(",", 0);
00062         int second_comma = data->find_first_of(",", first_comma+1);
00063         if(second_comma-first_comma==1)
00064         {
00065                 sscanf(data->c_str(), "Q,,%f,M,00,", &speed);
00066                 direction = 0;
00067         }
00068         else
00069         {
00070                 sscanf(data->c_str(), "Q,%d,%f,M,00,", &direction, &speed);
00071                 if(direction>180) direction -= 360;
00072         }
00073         
00074         wind_msg.header.stamp = ros::Time::now();
00075         wind_msg.header.frame_id = frame_id.c_str();
00076         
00077         wind_msg.min_wind_speed = 0.05;
00078         wind_msg.max_wind_speed = 60.00;
00079         
00080         wind_msg.wind_speed = speed;
00081         wind_msg.wind_direction = direction*0.0174532925;
00082         
00083         wind_pub.publish(wind_msg);
00084 }
00085 
00086 int main(int argc, char** argv)
00087 {
00088         ros::init(argc, argv, "windsonic_node");
00089         ros::NodeHandle n;
00090         ros::NodeHandle pn("~");
00091         
00092         wind_pub = n.advertise<lse_sensor_msgs::Anemometer>("/wind", 10);
00093         
00094         std::string port;
00095         int baudrate;
00096         pn.param<std::string>("port", port, "/dev/ttyUSB0");
00097         pn.param("baudrate", baudrate, 38400);
00098         pn.param<std::string>("frame_id", frame_id, "/base_anemometer");
00099 
00100         ROS_INFO("%s", frame_id.c_str());
00101         
00102         cereal::CerealPort serial_port;
00103 
00104         try{ serial_port.open((char*)port.c_str(), baudrate); }
00105         catch(cereal::Exception& e)
00106         {
00107                 ROS_FATAL("WindSonic -- Failed to open serial port!");
00108                 ROS_BREAK();
00109         }
00110         ROS_INFO("WindoSonic -- Successfully connected to the WindSonic!");
00111         
00112         if( !serial_port.startReadBetweenStream(boost::bind(&newDataCallback, _1), 0x02, 0x03) )
00113         {
00114                 ROS_FATAL("WindSonic -- Failed to start streaming data!");
00115                 ROS_BREAK();
00116         }
00117         ROS_INFO("Windsonic -- Starting to stream data...");
00118 
00119         ros::spin();
00120 
00121         return(0);
00122 }
00123 
00124 // EOF
00125 


windsonic
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:28:30