SickSafetyscanners.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 
00024 // -- END LICENSE BLOCK ------------------------------------------------
00025 
00026 //----------------------------------------------------------------------
00033 //----------------------------------------------------------------------
00034 
00035 
00036 #include "sick_safetyscanners/SickSafetyscanners.h"
00037 
00038 namespace sick {
00039 
00040 SickSafetyscanners::SickSafetyscanners(
00041   packetReceivedCallbackFunction newPacketReceivedCallbackFunction,
00042   sick::datastructure::CommSettings* settings)
00043   : m_newPacketReceivedCallbackFunction(newPacketReceivedCallbackFunction)
00044 {
00045   ROS_INFO("Starting SickSafetyscanners");
00046   m_io_service_ptr       = std::make_shared<boost::asio::io_service>();
00047   m_async_udp_client_ptr = std::make_shared<sick::communication::AsyncUDPClient>(
00048     boost::bind(&SickSafetyscanners::processUDPPacket, this, _1),
00049     boost::ref(*m_io_service_ptr),
00050     settings->getHostUdpPort());
00051   settings->setHostUdpPort(
00052     m_async_udp_client_ptr
00053       ->get_local_port()); // Store which port was used, needed for data request from the laser
00054   m_packet_merger_ptr = std::make_shared<sick::data_processing::UDPPacketMerger>();
00055   ROS_INFO("Started SickSafetyscanners");
00056 }
00057 
00058 SickSafetyscanners::~SickSafetyscanners()
00059 {
00060   m_io_service_ptr->stop();
00061   m_udp_client_thread_ptr->join();
00062   m_udp_client_thread_ptr.reset();
00063 }
00064 
00065 bool SickSafetyscanners::run()
00066 {
00067   m_udp_client_thread_ptr.reset(
00068     new boost::thread(boost::bind(&SickSafetyscanners::UDPClientThread, this)));
00069 
00070   m_async_udp_client_ptr->runService();
00071   return true;
00072 }
00073 
00074 bool SickSafetyscanners::UDPClientThread()
00075 {
00076   ROS_INFO("Enter io thread");
00077   m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(boost::ref(*m_io_service_ptr));
00078   m_io_service_ptr->run();
00079   ROS_INFO("Exit io thread");
00080   return true;
00081 }
00082 
00083 
00084 void SickSafetyscanners::processTCPPacket(const sick::datastructure::PacketBuffer& buffer)
00085 {
00086   // Not needed for current functionality, inplace for possible future developments
00087 }
00088 
00089 void SickSafetyscanners::changeSensorSettings(const datastructure::CommSettings& settings)
00090 {
00091   startTCPConnection(settings);
00092 
00093   changeCommSettingsInColaSession(settings);
00094 
00095   stopTCPConnection();
00096 }
00097 
00098 void SickSafetyscanners::requestTypeCode(const datastructure::CommSettings& settings,
00099                                          sick::datastructure::TypeCode& type_code)
00100 {
00101   startTCPConnection(settings);
00102 
00103   requestTypeCodeInColaSession(type_code);
00104 
00105   stopTCPConnection();
00106 }
00107 
00108 void SickSafetyscanners::requestFieldData(const datastructure::CommSettings& settings,
00109                                           std::vector<sick::datastructure::FieldData>& field_data)
00110 {
00111   startTCPConnection(settings);
00112 
00113   requestFieldDataInColaSession(field_data);
00114 
00115   stopTCPConnection();
00116 }
00117 
00118 void SickSafetyscanners::requestMonitoringCases(
00119   const datastructure::CommSettings& settings,
00120   std::vector<sick::datastructure::MonitoringCaseData>& monitoring_cases)
00121 {
00122   startTCPConnection(settings);
00123 
00124   requestMonitoringCaseDataInColaSession(monitoring_cases);
00125 
00126   stopTCPConnection();
00127 }
00128 
00129 void SickSafetyscanners::requestDeviceName(const datastructure::CommSettings& settings,
00130                                            std::string& device_name)
00131 {
00132   startTCPConnection(settings);
00133 
00134   requestDeviceNameInColaSession(device_name);
00135 
00136   stopTCPConnection();
00137 }
00138 
00139 void SickSafetyscanners::requestPersistentConfig(const datastructure::CommSettings& settings,
00140                                                  sick::datastructure::ConfigData& config_data)
00141 {
00142   startTCPConnection(settings);
00143 
00144   requestPersistentConfigInColaSession(config_data);
00145 
00146   stopTCPConnection();
00147 }
00148 
00149 void SickSafetyscanners::startTCPConnection(const sick::datastructure::CommSettings& settings)
00150 {
00151   std::shared_ptr<sick::communication::AsyncTCPClient> async_tcp_client =
00152     std::make_shared<sick::communication::AsyncTCPClient>(
00153       boost::bind(&SickSafetyscanners::processTCPPacket, this, _1),
00154       boost::ref(*m_io_service_ptr),
00155       settings.getSensorIp(),
00156       settings.getSensorTcpPort());
00157   async_tcp_client->doConnect();
00158 
00159   m_session_ptr.reset();
00160   m_session_ptr = std::make_shared<sick::cola2::Cola2Session>(async_tcp_client);
00161 
00162   m_session_ptr->open();
00163 }
00164 
00165 void SickSafetyscanners::changeCommSettingsInColaSession(
00166   const datastructure::CommSettings& settings)
00167 {
00168   sick::cola2::Cola2Session::CommandPtr command_ptr =
00169     std::make_shared<sick::cola2::ChangeCommSettingsCommand>(boost::ref(*m_session_ptr), settings);
00170   m_session_ptr->executeCommand(command_ptr);
00171 }
00172 
00173 void SickSafetyscanners::requestFieldDataInColaSession(
00174   std::vector<sick::datastructure::FieldData>& fields)
00175 {
00176   sick::datastructure::ConfigData config_data;
00177 
00178   /*sick::cola2::Cola2Session::CommandPtr command_ptr =
00179     std::make_shared<sick::cola2::MeasurementPersistentConfigVariableCommand>(
00180       boost::ref(*m_session_ptr), pers_config_data);
00181   m_session_ptr->executeCommand(command_ptr);
00182 */
00183   sick::cola2::Cola2Session::CommandPtr command_ptr =
00184     std::make_shared<sick::cola2::MeasurementCurrentConfigVariableCommand>(
00185       boost::ref(*m_session_ptr), config_data);
00186   m_session_ptr->executeCommand(command_ptr);
00187   /*
00188     command_ptr = std::make_shared<sick::cola2::MonitoringCaseTableHeaderVariableCommand>(
00189       boost::ref(*m_session_ptr), common_field_data);
00190     m_session_ptr->executeCommand(command_ptr);
00191   */
00192   for (int i = 0; i < 128; i++)
00193   {
00194     sick::datastructure::FieldData field_data;
00195 
00196     command_ptr = std::make_shared<sick::cola2::FieldHeaderVariableCommand>(
00197       boost::ref(*m_session_ptr), field_data, i);
00198     m_session_ptr->executeCommand(command_ptr);
00199 
00200     if (field_data.getIsValid())
00201     {
00202       command_ptr = std::make_shared<sick::cola2::FieldGeometryVariableCommand>(
00203         boost::ref(*m_session_ptr), field_data, i);
00204       m_session_ptr->executeCommand(command_ptr);
00205 
00206       field_data.setStartAngleDegrees(config_data.getStartAngle());
00207       field_data.setAngularBeamResolutionDegrees(config_data.getAngularBeamResolution());
00208 
00209       fields.push_back(field_data);
00210     }
00211     else if (i > 0) // index 0 is reserved for contour data
00212     {
00213       break; // skip other requests after first invalid
00214     }
00215   }
00216 }
00217 
00218 void SickSafetyscanners::requestMonitoringCaseDataInColaSession(
00219   std::vector<sick::datastructure::MonitoringCaseData>& monitoring_cases)
00220 {
00221   sick::cola2::Cola2Session::CommandPtr command_ptr;
00222   for (int i = 0; i < 254; i++)
00223   {
00224     sick::datastructure::MonitoringCaseData monitoring_case_data;
00225 
00226     command_ptr = std::make_shared<sick::cola2::MonitoringCaseVariableCommand>(
00227       boost::ref(*m_session_ptr), monitoring_case_data, i);
00228     m_session_ptr->executeCommand(command_ptr);
00229     if (monitoring_case_data.getIsValid())
00230     {
00231       monitoring_cases.push_back(monitoring_case_data);
00232     }
00233     else
00234     {
00235       break; // skip other requests after first invalid
00236     }
00237   }
00238 }
00239 
00240 void SickSafetyscanners::requestDeviceNameInColaSession(std::string& device_name)
00241 {
00242   sick::cola2::Cola2Session::CommandPtr command_ptr =
00243     std::make_shared<sick::cola2::DeviceNameVariableCommand>(boost::ref(*m_session_ptr),
00244                                                              device_name);
00245   m_session_ptr->executeCommand(command_ptr);
00246   ROS_INFO("Device name: %s", device_name.c_str());
00247 }
00248 
00249 void SickSafetyscanners::requestTypeCodeInColaSession(sick::datastructure::TypeCode& type_code)
00250 {
00251   sick::cola2::Cola2Session::CommandPtr command_ptr =
00252     std::make_shared<sick::cola2::TypeCodeVariableCommand>(boost::ref(*m_session_ptr), type_code);
00253   m_session_ptr->executeCommand(command_ptr);
00254 }
00255 
00256 void SickSafetyscanners::requestPersistentConfigInColaSession(
00257   sick::datastructure::ConfigData& config_data)
00258 {
00259   sick::cola2::Cola2Session::CommandPtr command_ptr =
00260     std::make_shared<sick::cola2::MeasurementPersistentConfigVariableCommand>(
00261       boost::ref(*m_session_ptr), config_data);
00262   m_session_ptr->executeCommand(command_ptr);
00263 }
00264 
00265 void SickSafetyscanners::stopTCPConnection()
00266 {
00267   m_session_ptr->close();
00268   m_session_ptr->doDisconnect();
00269 }
00270 
00271 
00272 void SickSafetyscanners::processUDPPacket(const sick::datastructure::PacketBuffer& buffer)
00273 {
00274   if (m_packet_merger_ptr->addUDPPacket(buffer))
00275   {
00276     sick::datastructure::PacketBuffer deployedBuffer =
00277       m_packet_merger_ptr->getDeployedPacketBuffer();
00278     sick::datastructure::Data data;
00279     sick::data_processing::ParseData data_parser;
00280     data_parser.parseUDPSequence(deployedBuffer, data);
00281 
00282     m_newPacketReceivedCallbackFunction(data);
00283   }
00284 }
00285 
00286 } // namespace sick


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Tue May 7 2019 03:27:36