start_request_serialization.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #include <string>
17 #include <cassert>
18 
19 #ifdef _WIN32
20 #include <Windows.h>
21 #endif
22 
23 #include <boost/crc.hpp>
24 
30 
31 using namespace std;
32 
34 {
35 namespace data_conversion_layer
36 {
37 namespace start_request
38 {
39 static constexpr uint64_t RESERVED{ 0 };
40 
41 static const uint32_t OPCODE{ 0x35 };
42 } // namespace start_request
43 
45 {
46  boost::crc_32_type crc;
47  crc.process_bytes(&data.at(0), data.size());
48  return static_cast<uint32_t>(crc.checksum());
49 }
50 
51 uint8_t getEnableByte(uint8_t nr_ubscribers)
52 {
53  uint8_t base{ 0b00001000 };
54  for (int i = 0; i < nr_ubscribers; i++)
55  {
56  base = base | (base >> 1);
57  }
58  return base;
59 }
60 
62  const uint32_t& seq_number)
63 {
64  std::ostringstream os;
65 
66  raw_processing::write(os, seq_number);
69 
71 #ifdef __linux__
72  const uint32_t host_ip_big_endian = htobe32(msg.host_ip_);
73 #endif
74 
75 #ifdef _WIN32
76  const uint32_t host_ip_big_endian = _byteswap_ulong(msg.host_ip_);
77 #endif
78 
79  raw_processing::write(os, host_ip_big_endian);
80 
83 
90  uint8_t nr_ubscribers = static_cast<uint8_t>(msg.master_device_settings_.nrSubscribers());
91 
92  const uint8_t device_enabled{ getEnableByte(nr_ubscribers) }; // hard coded for one subscriber
93  const uint8_t intensity_enabled{ static_cast<uint8_t>(
94  msg.master_device_settings_.intensitiesEnabled() ? 0b00001000 : 0b00000000) };
95  const uint8_t point_in_safety_enabled{ 0 };
96  const uint8_t active_zone_set_enabled{ getEnableByte(nr_ubscribers) };
97  const uint8_t io_pin_data_enabled{ 0b00001000 };
98  const uint8_t scan_counter_enabled{ getEnableByte(nr_ubscribers) }; // hard coded for one subscriber
99  const uint8_t speed_encoder_enabled{ 0 };
100  const uint8_t diagnostics_enabled{ static_cast<uint8_t>(
101  msg.master_device_settings_.diagnosticsEnabled() ? 0b00001000 : 0b00000000) };
102 
103  raw_processing::write(os, device_enabled);
104  raw_processing::write(os, intensity_enabled);
105  raw_processing::write(os, point_in_safety_enabled);
106  raw_processing::write(os, active_zone_set_enabled);
107  raw_processing::write(os, io_pin_data_enabled);
108  raw_processing::write(os, scan_counter_enabled);
109  raw_processing::write(os, speed_encoder_enabled);
110  raw_processing::write(os, diagnostics_enabled);
111 
112  const auto start = msg.master_.scanRange().start().value();
113  auto end = msg.master_.scanRange().end().value();
114  const auto resolution = msg.master_.resolution().value();
115 
116  /* In order to get all the data points we want, the scanner needs a value
117  that is strictly greater than the end point */
118  if ((end - start) % resolution == 0)
119  {
120  end++;
121  }
123  raw_processing::write(os, end);
124  raw_processing::write(os, resolution);
125 
126  PSENSCAN_DEBUG("StartRequestSerialization",
127  "Serializing start request with angle_start={} angle_end={} resolution={} tenths of degree.",
128  start,
129  end,
130  resolution);
131 
132  for (int i = 0; i < nr_ubscribers; i++) // Note: This refers to the scanner type subscriber, *not* a ros subscriber
133  {
135  raw_processing::write(os, end);
136  raw_processing::write(os, resolution);
137  }
138 
139  const std::string raw_data_as_str{ os.str() };
140  const data_conversion_layer::RawData raw_data(raw_data_as_str.cbegin(), raw_data_as_str.cend());
141 
142  std::ostringstream os_crc;
143  raw_processing::write(os_crc, calculateCRC(raw_data));
144 
145  std::string raw_data_with_crc_str(os_crc.str() + os.str());
146  data_conversion_layer::RawData raw_data_with_crc{ raw_data_with_crc_str.cbegin(), raw_data_with_crc_str.cend() };
147 
148  assert(raw_data_with_crc.size() == 58 && "Message data of start request has not the size expceted by protocol");
149 
150  return raw_data_with_crc;
151 }
152 
153 } // namespace data_conversion_layer
154 } // namespace psen_scan_v2_standalone
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::LaserScanSettings::scanRange
constexpr const ScanRange & scanRange() const
Definition: start_request.h:111
psen_scan_v2_standalone::data_conversion_layer::start_request::OPCODE
static const uint32_t OPCODE
Definition: start_request_serialization.cpp:41
start_request_serialization.h
start_request.h
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::master_device_settings_
const DeviceSettings master_device_settings_
Definition: start_request.h:99
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::DeviceSettings::diagnosticsEnabled
constexpr bool diagnosticsEnabled() const
Definition: start_request.h:130
psen_scan_v2_standalone::ScanRangeTemplated::start
const util::TenthOfDegree & start() const
Definition: scan_range.h:94
PSENSCAN_DEBUG
#define PSENSCAN_DEBUG(name,...)
Definition: logging.h:63
psen_scan_v2_standalone::data_conversion_layer::raw_processing::write
void write(std::ostringstream &os, const T &data)
Definition: raw_processing.h:47
psen_scan_v2_standalone::data_conversion_layer::calculateCRC
uint32_t calculateCRC(const data_conversion_layer::RawData &data)
Definition: start_request_serialization.cpp:44
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::DeviceSettings::nrSubscribers
constexpr uint8_t nrSubscribers() const
Definition: start_request.h:140
psen_scan_v2_standalone::util::TenthOfDegree::value
constexpr int16_t value() const
Definition: tenth_of_degree.h:49
psen_scan_v2_standalone::ScanRangeTemplated::end
const util::TenthOfDegree & end() const
Definition: scan_range.h:100
psen_scan_v2_standalone::data_conversion_layer::start_request::RESERVED
static constexpr uint64_t RESERVED
Definition: start_request_serialization.cpp:39
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::host_udp_port_data_
const uint16_t host_udp_port_data_
Definition: start_request.h:97
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::DeviceSettings::intensitiesEnabled
constexpr bool intensitiesEnabled() const
Definition: start_request.h:135
raw_scanner_data.h
psen_scan_v2_standalone::data_conversion_layer::scanner_reply::serialize
RawData serialize(const Message &reply)
Definition: scanner_reply_serialization_deserialization.cpp:55
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::host_ip_
const uint32_t host_ip_
network byte order = big endian
Definition: start_request.h:96
raw_processing.h
psen_scan_v2_standalone::data_conversion_layer::getEnableByte
uint8_t getEnableByte(uint8_t nr_ubscribers)
Definition: start_request_serialization.cpp:51
start
ROSCPP_DECL void start()
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::master_
const LaserScanSettings master_
Definition: start_request.h:100
psen_scan_v2_standalone::data_conversion_layer::start_request::Message::LaserScanSettings::resolution
constexpr util::TenthOfDegree resolution() const
Definition: start_request.h:116
std
logging.h
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
psen_scan_v2_standalone::data_conversion_layer::RawData
std::vector< char > RawData
Definition: raw_scanner_data.h:25
psen_scan_v2_standalone::data_conversion_layer::start_request::Message
Higher level data type representing a scanner start request.
Definition: start_request.h:42


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:12