fmi_adapter_node.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 - for information on the respective copyright owner
2 // see the NOTICE file and/or the repository https://github.com/boschresearch/fmi_adapter.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <cassert>
17 
18 #include <exception>
19 #include <map>
20 #include <string>
21 
22 #include <ros/ros.h>
23 #include <std_msgs/Float64.h>
24 
25 #include "fmi_adapter/FMIAdapter.h"
26 
27 
28 int main(int argc, char** argv) {
29  ros::init(argc, argv, "fmi_adapter_node");
30  ros::NodeHandle n("~");
31 
32  std::string fmuPath;
33  if (!n.getParam("fmu_path", fmuPath)) {
34  ROS_ERROR("Parameter 'fmu_path' not specified!");
35  throw std::runtime_error("Parameter 'fmu_path' not specified!");
36  }
37 
38  double stepSizeAsDouble = 0.0;
39  n.getParam("step_size", stepSizeAsDouble);
40  ros::Duration stepSize(stepSizeAsDouble);
41 
42  fmi_adapter::FMIAdapter adapter(fmuPath, stepSize);
43  for (const std::string & name : adapter.getParameterNames()) {
44  ROS_DEBUG("FMU has parameter '%s'", name.c_str());
45  }
46  adapter.initializeFromROSParameters(n);
47 
48  std::map<std::string, ros::Subscriber> subscribers;
49  for (const std::string& name : adapter.getInputVariableNames()) {
50  std::string rosifiedName = fmi_adapter::FMIAdapter::rosifyName(name);
51  ros::Subscriber subscriber =
52  n.subscribe<std_msgs::Float64>(rosifiedName, 1000, [&adapter, name](const std_msgs::Float64::ConstPtr& msg) {
53  std::string myName = name;
54  adapter.setInputValue(myName, ros::Time::now(), msg->data);
55  });
56  subscribers[name] = subscriber;
57  }
58 
59  std::map<std::string, ros::Publisher> publishers;
60  for (const std::string& name : adapter.getOutputVariableNames()) {
61  std::string rosifiedName = fmi_adapter::FMIAdapter::rosifyName(name);
62  publishers[name] = n.advertise<std_msgs::Float64>(rosifiedName, 1000);
63  }
64 
66 
67  double updatePeriod = 0.01; // Default is 0.01s
68  n.getParam("update_period", updatePeriod);
69 
70  ros::Timer timer = n.createTimer(ros::Duration(updatePeriod), [&](const ros::TimerEvent& event) {
71  if (adapter.getSimulationTime() < event.current_expected) {
72  adapter.doStepsUntil(event.current_expected);
73  } else {
74  ROS_INFO("Simulation time %f is greater than timer's time %f. Is your step size to large?",
75  adapter.getSimulationTime().toSec(), event.current_expected.toSec());
76  }
77  for (const std::string& name : adapter.getOutputVariableNames()) {
78  std_msgs::Float64 msg;
79  msg.data = adapter.getOutputValue(name);
80  publishers[name].publish(msg);
81  }
82  });
83 
84  ros::spin();
85 
86  return 0;
87 }
fmi_adapter::FMIAdapter::getOutputVariableNames
std::vector< std::string > getOutputVariableNames() const
Returns the names of all output variables of the wrapped FMU in the FMI Library's internal representa...
Definition: FMIAdapter.cpp:283
fmi_adapter::FMIAdapter::getParameterNames
std::vector< std::string > getParameterNames() const
Returns the names of all parameters of the wrapped FMU in the FMI Library's internal representation.
Definition: FMIAdapter.cpp:289
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
fmi_adapter::FMIAdapter::exitInitializationMode
void exitInitializationMode(ros::Time simulationTime)
Definition: FMIAdapter.cpp:295
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
ros.h
TimeBase< Time, Duration >::toSec
double toSec() const
main
int main(int argc, char **argv)
Definition: fmi_adapter_node.cpp:28
fmi_adapter::FMIAdapter::getSimulationTime
ros::Time getSimulationTime() const
Returns the current simulation time.
Definition: FMIAdapter.cpp:396
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
fmi_adapter::FMIAdapter::doStepsUntil
ros::Time doStepsUntil(const ros::Time &simulationTime)
Definition: FMIAdapter.cpp:377
ROS_DEBUG
#define ROS_DEBUG(...)
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::TimerEvent
fmi_adapter::FMIAdapter::initializeFromROSParameters
void initializeFromROSParameters(const ros::NodeHandle &handle)
Definition: FMIAdapter.cpp:470
fmi_adapter::FMIAdapter
Definition: FMIAdapter.h:38
ROS_ERROR
#define ROS_ERROR(...)
fmi_adapter::FMIAdapter::getInputVariableNames
std::vector< std::string > getInputVariableNames() const
Returns the names of all input variables of the wrapped FMU in the FMI Library's internal representat...
Definition: FMIAdapter.cpp:277
ros::spin
ROSCPP_DECL void spin()
ROS_INFO
#define ROS_INFO(...)
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
ros::Timer
fmi_adapter::FMIAdapter::rosifyName
static std::string rosifyName(const std::string &name)
Definition: FMIAdapter.cpp:220
fmi_adapter::FMIAdapter::getOutputValue
double getOutputValue(fmi2_import_variable_t *variable) const
Returns the current value of the given output variable.
Definition: FMIAdapter.cpp:424
FMIAdapter.h
ros::NodeHandle
ros::Subscriber
ros::Time::now
static Time now()


fmi_adapter
Author(s): Ralph Lange
autogenerated on Thu Nov 24 2022 03:51:09