SMACC
Loading...
Searching...
No Matches
smacc_runtime_test_node.cpp
Go to the documentation of this file.
1#include <ros/ros.h>
2#include <rosgraph_msgs/Log.h>
3#include <smacc_msgs/SmaccTransitionLogEntry.h>
4#include <boost/signals2.hpp>
5#include <memory>
6#include <vector>
7
11
12//---------------------------------------------------------------------------------------------------
14{
15public:
17
18 virtual void init(XmlRpc::XmlRpcValue& initXmlRpcValue)
19 {
20 }
21
22 virtual void update()
23 {
24 }
25};
26//---------------------------------------------------------------------------------------------------
27
29{
30public:
31 ros::NodeHandle nh_;
32 ros::Subscriber rosoutSub_;
33 ros::Subscriber smaccTransitionSub_;
34
35
36 std::vector<std::shared_ptr<TestPolicy>> testPolicies;
37 boost::signals2::signal<void(const rosgraph_msgs::Log&)> onRosOutMsg;
38 boost::signals2::signal<void(const smacc_msgs::SmaccTransitionLogEntry&)> onSmaccTransition;
39
40 template <typename T>
41 std::shared_ptr<TestPolicy> tryTestPolicyFactory(std::string matchname, XmlRpc::XmlRpcValue& initXmlRpcValue)
42 {
43 std::shared_ptr<TestPolicy> policy = nullptr;
44 ROS_INFO_STREAM(initXmlRpcValue);
45
46 auto type = (std::string)initXmlRpcValue["type"];
47
48 if (type == matchname)
49 {
50 auto specificPolicy = std::make_shared<T>();
51 policy = std::dynamic_pointer_cast<TestPolicy>(specificPolicy);
52 testPolicies.push_back(policy);
53
54 policy->owner_ = this;
55 policy->init(initXmlRpcValue);
56 }
57
58 return policy;
59 }
60
61 void init()
62 {
63 ROS_INFO("[SmaccTestruntimeNode] subscribing rosout");
64
65 ros::NodeHandle private_nh("~");
66 std::string stateMachineROSParamWs;
67 if (private_nh.getParam("state_machine_rosparam_ws", stateMachineROSParamWs))
68 {
69 ROS_INFO_STREAM("state machine param ws: " << stateMachineROSParamWs);
70 }
71
72 rosoutSub_ = nh_.subscribe("/rosout", 1, &SmaccTestRuntimeNode::onRosOutCallback, this);
73 smaccTransitionSub_ = nh_.subscribe(stateMachineROSParamWs + "/smacc/transition_log", 1, &SmaccTestRuntimeNode::onSmaccTransitionCallback, this);
74
75 XmlRpc::XmlRpcValue successSwitchParam;
76 private_nh.getParam("success_switch", successSwitchParam);
77 ROS_INFO_STREAM("[SmaccTestruntimeNode] success switch" << successSwitchParam);
78 ROS_ASSERT(successSwitchParam.getType() == XmlRpc::XmlRpcValue::TypeArray);
79
80 for (int32_t i = 0; i < successSwitchParam.size(); ++i)
81 {
82 auto initXmlRpcValue = successSwitchParam[i];
83 if (tryTestPolicyFactory<ReachedStateSuccessTestPolicy>("state_reached", initXmlRpcValue) != nullptr)
84 continue;
85 }
86
87 XmlRpc::XmlRpcValue failureSwitchParam;
88 private_nh.getParam("failure_switch", failureSwitchParam);
89 ROS_INFO_STREAM("[SmaccTestruntimeNode] failure switch" << failureSwitchParam);
90 ROS_ASSERT(failureSwitchParam.getType() == XmlRpc::XmlRpcValue::TypeArray);
91
92 for (int32_t i = 0; i < failureSwitchParam.size(); ++i)
93 {
94 auto initXmlRpcValue = failureSwitchParam[i];
95 if (tryTestPolicyFactory<TimeoutFailureTestPolicy>("timeout", initXmlRpcValue) != nullptr)
96 continue;
97 }
98 }
99 void success(std::string msg)
100 {
101 ROS_INFO_STREAM("SmaccRuntimeTestNode policy thrown success exit: " << msg);
102 exit(-1);
103 }
104
105 void failure(std::string msg)
106 {
107 ROS_FATAL_STREAM("SmaccRuntimeTestNode policy thrown failure exit: " << msg);
108 exit(-1);
109 }
110
111 void update()
112 {
113 for (auto& policy : testPolicies)
114 {
115 policy->update();
116 }
117 }
118
119 void onRosOutCallback(const rosgraph_msgs::Log& msg)
120 {
121 this->onRosOutMsg(msg);
122 }
123
124 void onSmaccTransitionCallback(const smacc_msgs::SmaccTransitionLogEntry& msg)
125 {
126 this->onSmaccTransition(msg);
127 }
128};
129
130//---------------------------------------------------------------------------------------------------
132{
133 ros::Duration timeout_;
134 ros::Time startTime_;
135
136 virtual void init(XmlRpc::XmlRpcValue& initXmlRpcValue) override
137 {
138 ROS_INFO("[TimeoutFailureTestPolicy] initializating");
139 startTime_ = ros::Time::now();
140 timeout_ = ros::Duration((double)initXmlRpcValue["duration"]);
141
142 ROS_INFO_STREAM("[TimeoutFailureTestPolicy] duration: " << timeout_);
143 }
144
145 virtual void update()
146 {
147 auto now = ros::Time::now();
148
149 auto elapsed = now - startTime_;
150
151 if (elapsed > timeout_)
152 {
153 this->owner_->failure(std::string("timeout failure: ") + std::to_string(timeout_.toSec()));
154 }
155 }
156};
157//---------------------------------------------------------------------------------------------------
159{
160 std::string targetStateName_;
161 virtual void init(XmlRpc::XmlRpcValue& initXmlRpcValue) override
162 {
163 ROS_INFO("[ReachedStateSuccessTestPolicy] initializating");
164 targetStateName_ = ((std::string)initXmlRpcValue["state_name"]);
165 ROS_INFO_STREAM("[ReachedStateSuccessTestPolicy] success state: " << targetStateName_);
166 this->owner_->onSmaccTransition.connect(
167 [=](auto& msg)
168 {
169 ROS_INFO_STREAM("[ReachedStateSuccessTestPolicy] received state: " << msg.transition.destiny_state_name);
170
171 if(msg.transition.destiny_state_name == targetStateName_)
172 {
173 this->owner_->success(std::string("success destiny state ") + targetStateName_);
174 }
175 });
176 }
177
178 virtual void update()
179 {
180
181 }
182};
183//---------------------------------------------------------------------------------------------------
184
185int main(int argc, char** argv)
186{
187 ros::init(argc, argv, "smacc_runtime_test_node");
188
189 SmaccTestRuntimeNode testNode;
190 testNode.init();
191
192 ros::Rate r(20);
193
194 while (ros::ok())
195 {
196 testNode.update();
197 ros::spinOnce();
198 r.sleep();
199 }
200 // ros::Subscriber sub = nh.subscribe();
201}
virtual void init(XmlRpc::XmlRpcValue &initXmlRpcValue) override
std::vector< std::shared_ptr< TestPolicy > > testPolicies
void onSmaccTransitionCallback(const smacc_msgs::SmaccTransitionLogEntry &msg)
void success(std::string msg)
void failure(std::string msg)
std::shared_ptr< TestPolicy > tryTestPolicyFactory(std::string matchname, XmlRpc::XmlRpcValue &initXmlRpcValue)
boost::signals2::signal< void(const rosgraph_msgs::Log &)> onRosOutMsg
void onRosOutCallback(const rosgraph_msgs::Log &msg)
boost::signals2::signal< void(const smacc_msgs::SmaccTransitionLogEntry &)> onSmaccTransition
virtual void init(XmlRpc::XmlRpcValue &initXmlRpcValue)
SmaccTestRuntimeNode * owner_
virtual void update()
virtual void init(XmlRpc::XmlRpcValue &initXmlRpcValue) override
int main(int argc, char **argv)