icub-client
behavior.h
Go to the documentation of this file.
1 #ifndef BEHAVIOR
2 #define BEHAVIOR
3 
4 #include <string>
5 #include <yarp/os/all.h>
9 
13 class Behavior
14 {
15 private:
16  yarp::os::Mutex* mut;
17  yarp::os::BufferedPort<yarp::os::Bottle> behavior_start_stop_port;
18 
19 protected:
21  yarp::os::ResourceFinder& rf;
22  virtual void run(const yarp::os::Bottle &args) = 0;
23  virtual void close_extra_ports() = 0;
24 
25 public:
26  yarp::os::Port rpc_out_port;
28  yarp::os::BufferedPort<yarp::os::Bottle> sensation_port_in;
29 
30  Behavior(yarp::os::Mutex* _mut, yarp::os::ResourceFinder &_rf, icubclient::ICubClient* _iCub, std::string _behaviorName) : mut(_mut), iCub(_iCub), rf(_rf), behaviorName(_behaviorName){
31  from_sensation_port_name = "None";
32  external_port_name = "None";
33  }
34  Behavior() = delete;
35 
36  virtual ~Behavior() {}
37 
43  void openPorts(std::string port_name_prefix) {
44  if (from_sensation_port_name != "None") {
45  sensation_port_in.open("/" + port_name_prefix +"/" + behaviorName + "/sensation:i");
46  }
47  if (external_port_name != "None") {
48  rpc_out_port.open("/" + port_name_prefix +"/" + behaviorName + "/to_external_module");
49  }
50  behavior_start_stop_port.open("/" + port_name_prefix +"/" + behaviorName + "/start_stop:o");
51  }
52 
58  bool trigger(const yarp::os::Bottle& args) {
59  yDebug() << behaviorName << "::trigger starts";
60  if (mut->try_lock()) {
61  yDebug() << behaviorName << "::trigger mutex locked";
62  yarp::os::Bottle & msg = behavior_start_stop_port.prepare();
63  msg.clear();
64  msg.addString("start");
65  behavior_start_stop_port.write();
66 
67  run(args);
68 
69  msg = behavior_start_stop_port.prepare();
70  msg.clear();
71  msg.addString("stop");
72  behavior_start_stop_port.write();
73  mut->unlock();
74  yDebug() << behaviorName << "::trigger mutex unlocked";
75  yDebug() << behaviorName << "::trigger ends";
76  return true;
77  }
78  yDebug() << behaviorName << " not executed, mutex was closed";
79  yDebug() << behaviorName << "::trigger ends";
80  return false;
81  }
82 
83  virtual void configure() = 0;
84 
88  void interrupt_ports() {
89  sensation_port_in.interrupt();
90  rpc_out_port.interrupt();
91  behavior_start_stop_port.interrupt();
92  }
93 
97  void close_ports() {
99  sensation_port_in.interrupt();
100  sensation_port_in.close();
101  rpc_out_port.interrupt();
102  rpc_out_port.close();
103  behavior_start_stop_port.interrupt();
104  behavior_start_stop_port.close();
105  }
106 
107 };
108 
109 #endif
icubclient::ICubClient * iCub
Definition: behavior.h:20
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
bool trigger(const yarp::os::Bottle &args)
trigger triggers this behavior and blocks the mutex
Definition: behavior.h:58
Behavior()=delete
No default constructor!
virtual void close_extra_ports()=0
virtual ~Behavior()
Definition: behavior.h:36
yarp::os::BufferedPort< yarp::os::Bottle > sensation_port_in
Input port from sensationManager.
Definition: behavior.h:28
yarp::os::Port rpc_out_port
Port to external module.
Definition: behavior.h:26
virtual void configure()=0
virtual void run(const yarp::os::Bottle &args)=0
void openPorts(std::string port_name_prefix)
openPorts open ports defined for a behavior
Definition: behavior.h:43
std::string behaviorName
Definition: behavior.h:27
Behavior(yarp::os::Mutex *_mut, yarp::os::ResourceFinder &_rf, icubclient::ICubClient *_iCub, std::string _behaviorName)
Definition: behavior.h:30
void close_ports()
close_ports close ports defined for a behavior
Definition: behavior.h:97
std::string from_sensation_port_name
Definition: behavior.h:27
yarp::os::ResourceFinder & rf
Definition: behavior.h:21
void interrupt_ports()
interrupt_ports interrupt ports defined for a behavior
Definition: behavior.h:88
std::string external_port_name
Definition: behavior.h:27