ParseIntrusionData.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 #include <sick_safetyscanners/data_processing/ParseIntrusionData.h>
00036 
00037 namespace sick {
00038 namespace data_processing {
00039 
00040 ParseIntrusionData::ParseIntrusionData()
00041 {
00042   m_reader_ptr = std::make_shared<sick::data_processing::ReadWriteHelper>();
00043 }
00044 
00045 datastructure::IntrusionData
00046 ParseIntrusionData::parseUDPSequence(const datastructure::PacketBuffer& buffer,
00047                                      datastructure::Data& data)
00048 {
00049   datastructure::IntrusionData intrusion_data;
00050   if (!checkIfPreconditionsAreMet(data))
00051   {
00052     intrusion_data.setIsEmpty(true);
00053     return intrusion_data;
00054   }
00055 
00056   const uint8_t* data_ptr(buffer.getBuffer().data() +
00057                           data.getDataHeaderPtr()->getIntrusionDataBlockOffset());
00058   setNumScanPoints(data.getDerivedValuesPtr()->getNumberOfBeams());
00059   setDataInIntrusionData(data_ptr, intrusion_data);
00060   return intrusion_data;
00061 }
00062 
00063 bool ParseIntrusionData::checkIfPreconditionsAreMet(const datastructure::Data& data) const
00064 {
00065   if (!checkIfIntrusionDataIsPublished(data))
00066   {
00067     return false;
00068   }
00069   if (!checkIfDataContainsNeededParsedBlocks(data))
00070   {
00071     return false;
00072   }
00073   return true;
00074 }
00075 
00076 bool ParseIntrusionData::checkIfIntrusionDataIsPublished(const datastructure::Data& data) const
00077 {
00078   if (data.getDataHeaderPtr()->getIntrusionDataBlockOffset() == 0 &&
00079       data.getDataHeaderPtr()->getIntrusionDataBlockSize() == 0)
00080   {
00081     return false;
00082   }
00083   return true;
00084 }
00085 
00086 bool ParseIntrusionData::checkIfDataContainsNeededParsedBlocks(
00087   const datastructure::Data& data) const
00088 {
00089   if (data.getDataHeaderPtr()->isEmpty())
00090   {
00091     return false;
00092   }
00093   if (data.getDerivedValuesPtr()->isEmpty())
00094   {
00095     return false;
00096   }
00097   return true;
00098 }
00099 
00100 uint16_t ParseIntrusionData::getNumScanPoints() const
00101 {
00102   return m_num_scan_points;
00103 }
00104 
00105 void ParseIntrusionData::setNumScanPoints(const uint16_t num_scan_points)
00106 {
00107   m_num_scan_points = num_scan_points;
00108 }
00109 
00110 void ParseIntrusionData::setDataInIntrusionData(const uint8_t*& data_ptr,
00111                                                 datastructure::IntrusionData& intrusion_data) const
00112 {
00113   std::vector<sick::datastructure::IntrusionDatum> intrusion_datums;
00114   setDataInIntrusionDatums(data_ptr, intrusion_datums);
00115   intrusion_data.setIntrusionDataVector(intrusion_datums);
00116 }
00117 
00118 void ParseIntrusionData::setDataInIntrusionDatums(
00119   const uint8_t*& data_ptr,
00120   std::vector<sick::datastructure::IntrusionDatum>& intrusion_datums) const
00121 {
00122   uint16_t offset = 0;
00123   // Repeats for 24 CutOffPaths
00124   for (uint8_t i_set = 0; i_set < 24; ++i_set)
00125   {
00126     sick::datastructure::IntrusionDatum datum;
00127     setSizeInIntrusionDatum(offset, data_ptr, datum);
00128     offset += 4;
00129     setFlagsInIntrusionDatum(offset, data_ptr, datum);
00130     offset += datum.getSize();
00131     intrusion_datums.push_back(datum);
00132   }
00133 }
00134 
00135 
00136 uint16_t ParseIntrusionData::setSizeInIntrusionDatum(
00137   const uint16_t offset, const uint8_t*& data_ptr, sick::datastructure::IntrusionDatum& datum) const
00138 {
00139   uint32_t numBytesToRead = m_reader_ptr->readuint32_tLittleEndian(data_ptr, offset);
00140   datum.setSize(numBytesToRead);
00141   return offset;
00142 }
00143 
00144 uint16_t ParseIntrusionData::setFlagsInIntrusionDatum(
00145   const uint16_t offset, const uint8_t*& data_ptr, sick::datastructure::IntrusionDatum& datum) const
00146 {
00147   uint32_t num_read_flags = 0;
00148   std::vector<bool> flags;
00149   for (uint16_t num_read_bytes = 0;
00150        (num_read_bytes < datum.getSize()) && (num_read_flags < m_num_scan_points);
00151        num_read_bytes++)
00152   {
00153     uint8_t bitset = m_reader_ptr->readuint8_tLittleEndian(data_ptr, offset + num_read_bytes);
00154     for (uint32_t i_bit = 0; (i_bit < 8) && (num_read_flags < m_num_scan_points);
00155          i_bit++, num_read_flags++)
00156     {
00157       flags.push_back(static_cast<bool>(bitset & (0x01 << i_bit)));
00158     }
00159   }
00160   datum.setFlagsVector(flags);
00161   return offset;
00162 }
00163 
00164 } // namespace data_processing
00165 } // namespace sick


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