modbus_msg_in_builder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
19 
20 #include <stdexcept>
21 
22 #include <std_msgs/MultiArrayDimension.h>
23 
24 namespace prbt_hardware_support
25 {
26 ModbusMsgInBuilder::ModbusMsgInBuilder(const ModbusApiSpec& api_spec) : api_spec_(api_spec)
27 {
28 }
29 
30 ModbusMsgInStampedPtr ModbusMsgInBuilder::build(const ros::Time& time) const
31 {
32  // Get the minimum and maximum register
33  uint32_t first_index_to_read{ register_values_.cbegin()->first };
34  uint32_t last_index_to_read{ register_values_.crbegin()->first };
35 
36  // Setup vector
37  RegCont tab_reg(last_index_to_read - first_index_to_read + 1);
38  for (auto reg : register_values_)
39  {
40  tab_reg[reg.first - first_index_to_read] = reg.second;
41  }
42 
43  ModbusMsgInStampedPtr msg{ createDefaultModbusMsgIn(first_index_to_read, tab_reg) };
44  msg->header.stamp = time;
45  return msg;
46 }
47 
48 void ModbusMsgInBuilder::setDefaultLayout(std_msgs::MultiArrayLayout* layout,
49  const std_msgs::MultiArrayLayout::_data_offset_type& offset,
50  const RegCont::size_type& size)
51 {
52  if (size > std::numeric_limits<std_msgs::MultiArrayDimension::_size_type>::max())
53  {
54  throw std::invalid_argument("Argument \"size\" must not exceed max value of type "
55  "\"std_msgs::MultiArrayDimension::_size_type\"");
56  }
57 
58  layout->data_offset = offset;
59  layout->dim.push_back(std_msgs::MultiArrayDimension());
60  layout->dim.back().size = static_cast<std_msgs::MultiArrayDimension::_size_type>(size);
61  layout->dim.back().stride = 1;
62  layout->dim.back().label = "Data in holding register";
63 }
64 
66  const std_msgs::MultiArrayLayout::_data_offset_type& offset, const RegCont& holding_register)
67 {
68  ModbusMsgInStampedPtr msg{ new ModbusMsgInStamped() };
69  setDefaultLayout(&(msg->holding_registers.layout), offset, holding_register.size());
70  msg->holding_registers.data = holding_register;
71  msg->disconnect.data = false;
72 
73  return msg;
74 }
75 
76 } // namespace prbt_hardware_support
msg
std::vector< uint16_t > RegCont
Convenience data type defining the data type for a collection of registers.
ModbusMsgInBuilder(const ModbusApiSpec &api_spec)
static ModbusMsgInStampedPtr createDefaultModbusMsgIn(const std_msgs::MultiArrayLayout::_data_offset_type &offset, const RegCont &holding_register)
Creates a standard ModbusMsgIn which contains default values for all essential elements of the messag...
std::map< unsigned int, uint16_t > register_values_
ModbusMsgInStampedPtr build(const ros::Time &time) const
static void setDefaultLayout(std_msgs::MultiArrayLayout *layout, const std_msgs::MultiArrayLayout::_data_offset_type &offset, const RegCont::size_type &size)
Specifies the meaning of the holding registers.


prbt_hardware_support
Author(s):
autogenerated on Mon Feb 28 2022 23:14:34