emc_system
engine.cpp
Go to the documentation of this file.
1 #include "emc/engine.h"
2 #include "emc/communication.h"
3 
4 #include <ros/init.h>
5 #include <ros/rate.h>
6 
7 namespace emc
8 {
9 
10 // ----------------------------------------------------------------------------------------------------
11 
12 Engine::Engine() : io_(nullptr), state_(-1), user_data_(nullptr), loop_freq_(0), has_error_(false)
13 {
14  // Register the special 'null' event
15  events.push_back("NO_EVENT");
16  event_to_int[""] = 0;
17 }
18 
19 // ----------------------------------------------------------------------------------------------------
20 
22 {
23  if (io_)
24  delete io_;
25 }
26 
27 // ----------------------------------------------------------------------------------------------------
28 
30 {
31  if (has_error_)
32  return;
33 
34  if (state_ < 0)
35  {
36  addError("Initial state not specified");
37  return;
38  }
39 
40  if (loop_freq_ <= 0)
41  {
42  addError("Loop frequency not set.");
43  return;
44  }
45 
46 // comm_->init();
47  if (io_)
48  delete io_;
49 
50  io_ = new IO;
51 
52  ros::Rate r(loop_freq_);
53  while(ros::ok())
54  {
55  FSMInterface fsm;
56 
58  s.func(fsm, *io_, user_data_);
59 
60  if (!ros::ok())
61  break;
62 
63  int event_id = getEvent(fsm.event().c_str());
64  if (event_id < 0)
65  {
66  addError("Unknown event '" + fsm.event() + "' raised in state '" + stateToString(state_) + "'");
67  break;
68  }
69 
70  if (event_id > 0)
71  {
72  std::map<int, int> ::const_iterator it = s.transitions.find(event_id);
73  if (it == s.transitions.end())
74  {
75  addError("Cannot deal with event '" + eventToString(event_id) + "'' in state '" + stateToString(state_) + "'");
76  break;
77  }
78 
79  state_ = it->second;
80  }
81 
82  r.sleep();
83  }
84 }
85 
86 } // end namespace emc
87 
emc::Engine::loop_freq_
double loop_freq_
Definition: engine.h:102
emc::Engine::state_
int state_
Definition: engine.h:81
emc::Engine::getEvent
int getEvent(const char *event)
Definition: engine.h:134
emc::FSMInterface::event
const std::string & event() const
Definition: data.h:47
emc::Engine::addError
void addError(const std::string &err)
Definition: engine.h:166
emc::FSMInterface
Definition: data.h:35
emc::Engine::has_error_
bool has_error_
Definition: engine.h:164
emc::Engine::events
std::vector< std::string > events
Definition: engine.h:96
std::map::find
T find(T... args)
emc::Engine::stateToString
const std::string & stateToString(int state_id)
Definition: engine.h:129
emc::Engine::event_to_int
std::map< std::string, int > event_to_int
Definition: engine.h:92
std::vector::push_back
T push_back(T... args)
emc::Engine::Engine
Engine()
Definition: engine.cpp:12
std::string::c_str
T c_str(T... args)
emc::Engine::user_data_
void * user_data_
Definition: engine.h:100
emc::Engine::StateDetail
Definition: engine.h:83
emc::Engine::run
void run()
Definition: engine.cpp:29
emc::IO
Definition: io.h:18
std::map< int, int >
emc::Engine::StateDetail::transitions
std::map< int, int > transitions
Definition: engine.h:88
communication.h
emc::Engine::~Engine
~Engine()
Definition: engine.cpp:21
emc::Engine::state_details
std::vector< StateDetail > state_details
Definition: engine.h:98
emc::Engine::eventToString
const std::string & eventToString(int event_id)
Definition: engine.h:159
emc
Definition: bumper.h:4
engine.h
std::map::end
T end(T... args)
emc::Engine::StateDetail::func
state_function func
Definition: engine.h:89
emc::Engine::io_
IO * io_
Definition: engine.h:79