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 
28  : api_spec_(api_spec)
29 {
30 }
31 
32 ModbusMsgInStampedPtr ModbusMsgInBuilder::build(const ros::Time& time) const
33 {
34  // Get the minimum and maximum register
35  uint32_t first_index_to_read{register_values_.cbegin()->first};
36  uint32_t last_index_to_read{register_values_.crbegin()->first};
37 
38  // Setup vector
39  RegCont tab_reg(last_index_to_read - first_index_to_read + 1);
40  for(auto reg : register_values_)
41  {
42  tab_reg[reg.first - first_index_to_read] = reg.second;
43  }
44 
45  ModbusMsgInStampedPtr msg { createDefaultModbusMsgIn(first_index_to_read, tab_reg) };
46  msg->header.stamp = time;
47  return msg;
48 }
49 
50 void ModbusMsgInBuilder::setDefaultLayout(std_msgs::MultiArrayLayout* layout,
51  const std_msgs::MultiArrayLayout::_data_offset_type& offset,
52  const RegCont::size_type& size)
53 {
54  if (size > std::numeric_limits<std_msgs::MultiArrayDimension::_size_type>::max())
55  {
56  throw std::invalid_argument("Argument \"size\" must not exceed max value of type \"std_msgs::MultiArrayDimension::_size_type\"");
57  }
58 
59  layout->data_offset = offset;
60  layout->dim.push_back(std_msgs::MultiArrayDimension());
61  layout->dim.back().size = static_cast<std_msgs::MultiArrayDimension::_size_type>(size);
62  layout->dim.back().stride = 1;
63  layout->dim.back().label = "Data in holding register";
64 }
65 
66 ModbusMsgInStampedPtr ModbusMsgInBuilder::createDefaultModbusMsgIn(const std_msgs::MultiArrayLayout::_data_offset_type& offset,
67  const RegCont& holding_register)
68 {
69  ModbusMsgInStampedPtr msg { new ModbusMsgInStamped() };
70  setDefaultLayout(&(msg->holding_registers.layout), offset, holding_register.size());
71  msg->holding_registers.data = holding_register;
72  msg->disconnect.data = false;
73 
74  return msg;
75 }
76 
77 } // namespace prbt_hardware_support
msg
ModbusMsgInStampedPtr build(const ros::Time &time) const
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_
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 Tue Feb 2 2021 03:50:17