diff --git a/modules/catkin_ws/src/PC/CMakeLists.txt b/modules/catkin_ws/src/PC/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..226a1708bbe4590a0718d7ad03df250cde025d8d --- /dev/null +++ b/modules/catkin_ws/src/PC/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.9) +project(PC) + +set(CMAKE_CXX_STANDARD 14) + +find_package(Boost REQUIRED COMPONENTS regex system) + +set(SOURCE_FILES + src/main.cpp + src/Logging.cpp + include/Logging.h) + +add_executable(PC ${SOURCE_FILES}) + +target_link_libraries(PC ${Boost_LIBRARIES}) \ No newline at end of file diff --git a/modules/catkin_ws/src/PC/include/Logging.h b/modules/catkin_ws/src/PC/include/Logging.h new file mode 100644 index 0000000000000000000000000000000000000000..edfcc572331776a773dbf2a3b1f6d35461dcd4d5 --- /dev/null +++ b/modules/catkin_ws/src/PC/include/Logging.h @@ -0,0 +1,31 @@ +// +// Created by philipp on 22.03.18. +// + +#ifndef PC_LOGGING_H +#define PC_LOGGING_H + +#include <boost/asio.hpp> + +namespace pc +{ + +class Logging +{ +public: + explicit Logging(std::uint16_t port) + : port(port) + {} + + void start(); + void stop(); + +private: + std::uint16_t port; + std::atomic<bool> running{false}; + boost::asio::io_service ioService; +}; + +} + +#endif //PC_LOGGING_H diff --git a/modules/catkin_ws/src/PC/src/Logging.cpp b/modules/catkin_ws/src/PC/src/Logging.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c5153e8faaaded45bb54687b239c26a543c3f71 --- /dev/null +++ b/modules/catkin_ws/src/PC/src/Logging.cpp @@ -0,0 +1,58 @@ +// +// Created by philipp on 22.03.18. +// + +#include <iostream> +#include "../include/Logging.h" + +namespace pc +{ + +void Logging::start() +{ + using boost::asio::ip::tcp; + + if (running) + return; + + running = true; + tcp::endpoint endpoint{tcp::v4(), port}; + tcp::acceptor acceptor{ioService, endpoint}; + + while (running) + { + try + { + tcp::iostream stream; + boost::system::error_code ec; + acceptor.accept(*stream.rdbuf(), ec); + if (ec) + continue; + + while (running) + { + std::string msg; + stream >> msg; + std::cout << msg << std::endl; + if (stream.error()) + { + if (stream.error() != boost::asio::error::eof) + std::cerr << "LOGGING ERROR: " << stream.error().message() << "\n"; + + break; + } + } + } + catch (const std::exception & e) + { + std::cerr << "LOGGING ERROR: " << e.what() << "\n"; + } + } +} + +void Logging::stop() +{ + running = false; +} + +} \ No newline at end of file diff --git a/modules/catkin_ws/src/PC/src/main.cpp b/modules/catkin_ws/src/PC/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3594c9a7b8e3ecc385ca834fe8df14ffc690aea2 --- /dev/null +++ b/modules/catkin_ws/src/PC/src/main.cpp @@ -0,0 +1,8 @@ +#include "../include/Logging.h" + +int main() +{ + pc::Logging logging{10207}; + logging.start(); + return 0; +} \ No newline at end of file diff --git a/modules/catkin_ws/src/car/include/logging/Logging.h b/modules/catkin_ws/src/car/include/logging/Logging.h index 0c0f969ceed7e362596775f3deba84932d187b6f..a6d7e7f0131e046bea76ecb439595e43cb4ef393 100644 --- a/modules/catkin_ws/src/car/include/logging/Logging.h +++ b/modules/catkin_ws/src/car/include/logging/Logging.h @@ -8,6 +8,8 @@ #include <nodelet/nodelet.h> #include <ros/ros.h> +#include <boost/asio.hpp> + #include "car/camDataMsg.h" #include "car/ccDataMsg.h" #include "car/environmentDataMsg.h" @@ -35,6 +37,9 @@ private: ros::NodeHandle nh_; std::string name_; + std::string host{"127.0.0.1"}; + std::string port{"10207"}; + ros::Subscriber camData; ros::Subscriber ccData; ros::Subscriber environmentData; @@ -45,6 +50,8 @@ private: ros::Subscriber stmData; ros::Subscriber ussData; + void logMessage(const std::string & str); + void camDataCallback(const camDataMsg::ConstPtr & inMsg); void ccDataCallback(const ccDataMsg::ConstPtr & inMsg); void environmentDataCallback(const environmentDataMsg::ConstPtr & inMsg); diff --git a/modules/catkin_ws/src/car/src/logging/Logging.cpp b/modules/catkin_ws/src/car/src/logging/Logging.cpp index 688eacfdee0d3c23348927686752dca0ffac2429..cef2c2ff100fd77a05520196532293d74307ec4c 100644 --- a/modules/catkin_ws/src/car/src/logging/Logging.cpp +++ b/modules/catkin_ws/src/car/src/logging/Logging.cpp @@ -7,8 +7,7 @@ #include "logging/Logging.h" -PLUGINLIB_EXPORT_CLASS(car::Logging, nodelet::Nodelet -); +PLUGINLIB_EXPORT_CLASS(car::Logging, nodelet::Nodelet); namespace car { @@ -40,6 +39,27 @@ void Logging::onInit() NODELET_INFO("Logging::onInit -- END"); } +void Logging::logMessage(const std::string & msg) +{ + try + { + using boost::asio::ip::tcp; + tcp::iostream stream; + stream.expires_from_now(boost::posix_time::seconds(10)); + stream.connect(host, port); + if (stream.error()) + { + NODELET_ERROR_STREAM("Logging: Could not connect to PC!\n" << stream.error().message()); + return; + } + stream << msg; + } + catch (const std::exception & e) + { + NODELET_ERROR_STREAM("Logging: Could not connect to PC!\n" << e.what()); + } +} + void Logging::ccDataCallback(const ccDataMsg::ConstPtr & inMsg) { std::cout << "Logging received new cc data ( ).\n"; @@ -53,6 +73,7 @@ void Logging::camDataCallback(const camDataMsg::ConstPtr & inMsg) void Logging::environmentDataCallback(const environmentDataMsg::ConstPtr & inMsg) { std::cout << "Logging received new environment data ( ).\n"; + logMessage(std::string{"Environment [distance]: "} + std::to_string(inMsg->distance)); } void Logging::laneDataCallback(const laneDataMsg::ConstPtr & inMsg)