rostate_machine.cpp
Go to the documentation of this file.
1 
13 
21 {
22  nh_ = nh;
23  pnh_ = pnh;
24  pnh_.param<std::string>("dot_filepath", dot_filepath_, "");
25  pnh_.param<std::string>("description", description_, "");
26  state_buf_ = boost::circular_buffer<std::string>(2);
27 }
28 
34 {
35  state_machine_ptr_->drawStateMachine(dot_filepath_);
36 }
37 
44 {
45  rostate_machine::Event msg = *event.getMessage();
46  StateInfo old_info = state_machine_ptr_->getStateInfo();
47  bool result = state_machine_ptr_->tryTransition(msg.trigger_event_name);
48  StateInfo info = state_machine_ptr_->getStateInfo();
49  if(!result)
50  {
51  std::string publisher_name = event.getPublisherName();
52  ROS_INFO_STREAM( "from : " << publisher_name << ", failed to transition, current state : "<< info.current_state << ", event_name : " << msg.trigger_event_name);
53  }
54  else
55  {
56  std::string publisher_name = event.getPublisherName();
57  ROS_INFO_STREAM( "from : " << publisher_name << ", succeed to transition, current state : "<< info.current_state << ", event_name : " << msg.trigger_event_name);
58  }
59  return;
60 }
61 
67 {
68  state_machine_ptr_ = std::make_shared<StateMachine>(description_);
69  nh_.param<double>(ros::this_node::getName()+"/publish_rate", publish_rate_, 10);
70  dot_string_pub_ = nh_.advertise<std_msgs::String>(ros::this_node::getName()+"/dot_string",1,true);
71  current_state_pub_ = nh_.advertise<rostate_machine::State>(ros::this_node::getName()+"/"+state_machine_name_+"/current_state",1);
72  boost::thread publish_thread(boost::bind(&RostateMachine::publishCurrentState, this));
73  trigger_event_sub_ = nh_.subscribe(ros::this_node::getName()+"/"+state_machine_name_+"/trigger_event", 10, &RostateMachine::eventCallback,this);
74  return;
75 }
76 
82 {
84  while(ros::ok())
85  {
86  rostate_machine::State state_msg;
87  StateInfo info = state_machine_ptr_->getStateInfo();
88  state_msg.current_state = info.current_state;
89  state_msg.possible_transitions = info.possibe_transitions;
90  state_msg.possible_transition_states = info.possibe_transition_states;
91  state_msg.header.stamp = ros::Time::now();
92  current_state_pub_.publish(state_msg);
93 
94  std_msgs::String dot_string_msg;
95  state_buf_.push_back(state_msg.current_state);
96  if(state_buf_.size() == 1 || state_buf_[0] != state_buf_[1])
97  {
98  dot_string_msg.data = state_machine_ptr_->getDotString();
99  dot_string_pub_.publish(dot_string_msg);
100  }
101  rate.sleep();
102  }
103  return;
104 }
void run()
Run State Machine (Enable Transitions)
std::string description_
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void eventCallback(const ros::MessageEvent< rostate_machine::Event const > &event)
Callback Function for Trigger Event Topic.
ros::NodeHandle pnh_
void publishCurrentState()
Publish Current State Topic.
ROSCPP_DECL const std::string & getName()
boost::circular_buffer< std::string > state_buf_
const std::vector< std::string > possibe_transition_states
Possibe Transition States from the Current State.
Definition: state_machine.h:78
ros::NodeHandle nh_
~RostateMachine()
Destroy the Rostate Machine:: Rostate Machine object.
std::shared_ptr< StateMachine > state_machine_ptr_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::Publisher dot_string_pub_
ROSCPP_DECL bool ok()
ROS Wrapper for the State Machine Class.
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Subscriber trigger_event_sub_
std::string state_machine_name_
ros::Publisher current_state_pub_
bool sleep()
const std::vector< std::string > possibe_transitions
Possibe Transitions from the Current State.
Definition: state_machine.h:83
std::string dot_filepath_
RostateMachine(ros::NodeHandle nh, ros::NodeHandle pnh)
Construct a new Rostate Machine:: Rostate Machine object.
Struct for State Infomation.
Definition: state_machine.h:72
#define ROS_INFO_STREAM(args)
const std::string current_state
Current State.
Definition: state_machine.h:88
static Time now()


rostate_machine
Author(s):
autogenerated on Wed Sep 4 2019 05:19:52