1 #include <industrial_modbus_tcp/Configure.h> 7 std::shared_ptr<ros::NodeHandle>
nh;
13 industrial_modbus_tcp::ConfigureResponse &res)
17 if (req.handlers.empty())
19 res.error =
"Zero handler provided";
23 for (
auto &handler : req.handlers)
25 if (handler.name.empty())
27 res.error =
"Handler name cannot be empty";
31 if (handler.ip_address.empty())
33 res.error =
"Handler '" + handler.name +
"' IP address cannot be empty";
37 if (handler.d_bool.empty() && handler.d_float32.empty() && handler.d_float64.empty() &&
38 handler.d_int16.empty() && handler.d_int32.empty() && handler.d_int64.empty() && handler.d_int8.empty() &&
39 handler.d_uint16.empty() && handler.d_uint32.empty() && handler.d_uint64.empty() && handler.d_uint8.empty())
41 res.error =
"Handler '" + handler.name +
"' contains zero data to be fetched";
47 for (
auto &handler_config : req.handlers)
49 modbus_error_recovery_mode recovery_mode;
50 switch (handler_config.modbus_error_recovery_mode)
53 recovery_mode = MODBUS_ERROR_RECOVERY_NONE;
56 recovery_mode = MODBUS_ERROR_RECOVERY_LINK;
59 recovery_mode = MODBUS_ERROR_RECOVERY_PROTOCOL;
62 recovery_mode = (modbus_error_recovery_mode)(MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
66 std::shared_ptr<Handler> handler;
69 if (handler_config.port == 0)
74 handler_config.ip_address,
82 handler_config.ip_address,
84 handler_config.port));
87 catch (std::runtime_error &e)
89 std_msgs::String error;
90 error.data =
"Handler '" + handler_config.name +
"' error: " + e.what();
92 res.error = error.data;
97 std::vector<std::shared_ptr<DataType>> data;
99 for (
auto v : handler_config.d_bool)
101 std::shared_ptr<DataType>
d = std::make_shared<BoolType>(
nh,
103 std::chrono::microseconds(v.reg.poll_rate_usec),
105 std::dynamic_pointer_cast<
BoolType>(d)->mask_ = v.mask;
106 data.emplace_back(d);
109 for (
auto v : handler_config.d_int8)
111 std::shared_ptr<DataType>
d = std::make_shared<Int8Type>(
nh,
113 std::chrono::microseconds(v.reg.poll_rate_usec),
116 data.emplace_back(d);
119 for (
auto v : handler_config.d_uint8)
121 std::shared_ptr<DataType>
d = std::make_shared<UInt8Type>(
nh,
123 std::chrono::microseconds(v.reg.poll_rate_usec),
126 data.emplace_back(d);
129 for (
auto v : handler_config.d_int16)
131 std::shared_ptr<DataType>
d = std::make_shared<Int16Type>(
nh,
133 std::chrono::microseconds(v.reg.poll_rate_usec),
135 data.emplace_back(d);
138 for (
auto v : handler_config.d_uint16)
140 std::shared_ptr<DataType>
d = std::make_shared<UInt16Type>(
nh,
142 std::chrono::microseconds(v.reg.poll_rate_usec),
144 data.emplace_back(d);
147 for (
auto v : handler_config.d_int32)
149 std::shared_ptr<DataType>
d = std::make_shared<Int32Type>(
nh,
151 std::chrono::microseconds(v.reg.poll_rate_usec),
154 data.emplace_back(d);
157 for (
auto v : handler_config.d_uint32)
159 std::shared_ptr<DataType>
d = std::make_shared<UInt32Type>(
nh,
161 std::chrono::microseconds(v.reg.poll_rate_usec),
164 data.emplace_back(d);
167 for (
auto v : handler_config.d_int64)
169 std::shared_ptr<DataType>
d = std::make_shared<Int64Type>(
nh,
171 std::chrono::microseconds(v.reg.poll_rate_usec),
174 data.emplace_back(d);
177 for (
auto v : handler_config.d_uint64)
179 std::shared_ptr<DataType>
d = std::make_shared<UInt64Type>(
nh,
181 std::chrono::microseconds(v.reg.poll_rate_usec),
184 data.emplace_back(d);
187 for (
auto v : handler_config.d_float32)
189 std::shared_ptr<DataType>
d = std::make_shared<Float32Type>(
nh,
191 std::chrono::microseconds(v.reg.poll_rate_usec),
194 data.emplace_back(d);
197 for (
auto v : handler_config.d_float64)
199 std::shared_ptr<DataType>
d = std::make_shared<Float64Type>(
nh,
201 std::chrono::microseconds(v.reg.poll_rate_usec),
204 data.emplace_back(d);
207 handler->setData(data);
218 nh = std::make_shared<ros::NodeHandle>(
"~");
221 error_pub = std::make_shared<ros::Publisher>();
222 *error_pub =
nh->advertise<std_msgs::String>(base_topic +
"errors", 5);
int main(int argc, char *argv[])
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::shared_ptr< ros::Publisher > error_pub
std::shared_ptr< ros::NodeHandle > nh
bool configureModbusCallback(industrial_modbus_tcp::ConfigureRequest &req, industrial_modbus_tcp::ConfigureResponse &res)
std::vector< std::shared_ptr< Handler > > modbus_handlers
ROSCPP_DECL void waitForShutdown()