YARP
Yet Another Robot Platform
PriorityCarrier.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef PRIORITYCARRIER_INC
7 #define PRIORITYCARRIER_INC
8 
9 #include <cmath>
11 #include <yarp/os/Election.h>
13 #include <yarp/os/Semaphore.h>
14 #include <yarp/os/Time.h>
15 #include <yarp/os/PeriodicThread.h>
16 #include <yarp/os/BufferedPort.h>
17 
18 #include <yarp/sig/Vector.h>
19 #include <yarp/sig/Matrix.h>
20 
21 #define STIMUL_THRESHOLD 1.0
22 #define WITH_PRIORITY_DEBUG
23 
24 class PriorityCarrier;
25 
30  public yarp::os::PeerRecord<PriorityCarrier>
31 {
32 public:
33  virtual ~PriorityGroup() {}
35  PriorityCarrier *source);
36  bool recalculate(double t);
37 
38 public:
39  yarp::sig::Matrix InvA; // the inverse of matrix (I-A) in the equation y(t) = [(I-A)^(-1) * B] .*x(t)
40  yarp::sig::Matrix B; // matrix of biases B in the equation y(t) = [(I-A)^(-1) * B] .*x(t)
41  yarp::sig::Matrix Y; // matrix y(t)
42  yarp::sig::Matrix X; // matrix x(t)
43  //yarp::os::Semaphore semDebug; // this semaphor is used only when debug mode is active
44  // to control the access to matrices from debug thread
45 };
46 
47 
48 
49 #ifdef WITH_PRIORITY_DEBUG
52 {
53 public:
55  ~PriorityDebugThread() override;
56  void run() override;
57  bool threadInit() override;
58  void threadRelease() override;
59 
60 public:
61  int count;
63  std::string debugPortName;
65 };
66 #endif //WITH_PRIORITY_DEBUG
67 
68 
76 {
77 
78 #ifdef WITH_PRIORITY_DEBUG
79  friend class PriorityDebugThread;
80 #endif //WITH_PRIORITY_DEBUG
81 
82 public:
84 #ifdef WITH_PRIORITY_DEBUG
85  : debugger(this)
86 #endif //WITH_PRIORITY_DEBUG
87  {
88  group = 0/*NULL*/;
90  timeResting = 0;
91  stimulation = 0;
93  isVirtual = false;
94  isActive = false;
95  bias = 0;
96  yi = 0; // used in debug
97  }
98 
99  virtual ~PriorityCarrier() {
100  if (portName!="") {
101  // let peer carriers know I'm gone.
102  getPeers().remove(portName,this);
103  }
104  }
105 
106  Carrier *create() const override {
107  return new PriorityCarrier();
108  }
109 
110  std::string getName() const override {
111  return "priority";
112  }
113 
114  std::string toString() const override {
115  return "priority_carrier";
116  }
117 
118  bool isResting(double priority) {
119  return ((timeResting > 0) &&
120  ((priority < 0) || (priority >= STIMUL_THRESHOLD)));
121  }
122 
123 
124  void stimulate(double t) {
125 
127  // if it is in active state, stimulates.
129  {
131  // respecting priority ceiling
133  {
135  isActive = true;
136  }
137  // updating arrival time
138  timeArrival = t;
139  }
140  }
141 
142  double getActualStimulation(double t);
143 
144  double getActualInput(double t);
145 
146  bool configure(yarp::os::ConnectionState& proto) override;
147 
148  bool acceptIncomingData(yarp::os::ConnectionReader& reader) override;
149 
150  void setCarrierParams(const yarp::os::Property& params) override {
151  yarp::os::Property property = params;
152  timeConstant = property.check("tc", yarp::os::Value(timeConstant)).asFloat64();
153  timeResting = property.check("tr", yarp::os::Value(timeResting)).asFloat64();
154  stimulation = property.check("st", yarp::os::Value(stimulation)).asFloat64();
155  bias = property.check("bs", yarp::os::Value(bias)).asFloat64();
156  isVirtual = property.check("virtual", yarp::os::Value(isVirtual)).asBool();
157  if(property.check("ex")) {
158  excitation = property.findGroup("ex");
159  }
160  }
161 
162  void getCarrierParams(yarp::os::Property& params) const override {
163  params.put("tc", timeConstant);
164  params.put("tr", timeResting);
165  params.put("st", stimulation);
166  params.put("bs", bias);
167  params.put("virtual", (int)isVirtual);
168  params.put("ex", excitation.toString());
169  }
170 
171 
172 
173 public:
174  double timeConstant; // priority reset time (tc > 0)
175  double timeResting; // priority resting time (tr > 0)
176  double stimulation; // stimulation value of the message (s > 0)
177  double temporalStimulation; // current priority of the message S(t)
178  double timeArrival; // arrival time of the message
179  bool isVirtual; // a virtual link does not carry any data
180  bool isActive; // true if port is in active state X(t)
181  double bias; // bias value for excitation
182  yarp::os::Bottle excitation; // a list of exitatory signals as (name, value)
183  std::string sourceName;
184 
185  double yi; // this is set in the recalculate() for the debug purpose
186 
187 private:
188  std::string portName;
189  PriorityGroup *group;
190 
192 
193  static yarp::os::ElectionOf<PriorityGroup>& getPeers();
194 
195 #ifdef WITH_PRIORITY_DEBUG
196 private:
197  PriorityDebugThread debugger;
198 #endif //WITH_PRIORITY_DEBUG
199 };
200 
201 #endif // PRIORITYCARRIER_INC
float t
contains the definition of a Matrix type
#define STIMUL_THRESHOLD
contains the definition of a Vector type
Allow priority-based message selection.
std::string sourceName
bool configure(yarp::os::ConnectionState &proto) override
Give carrier a shot at looking at how the connection is set up.
void getCarrierParams(yarp::os::Property &params) const override
Get carrier configuration and deliver it by port administrative commands.
std::string toString() const override
Get name of carrier.
double getActualInput(double t)
void setCarrierParams(const yarp::os::Property &params) override
Configure carrier from port administrative commands.
double temporalStimulation
void stimulate(double t)
Carrier * create() const override
Factory method.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool isResting(double priority)
bool acceptIncomingData(yarp::os::ConnectionReader &reader) override
Determine whether incoming data should be accepted.
yarp::os::Bottle excitation
virtual ~PriorityCarrier()
double getActualStimulation(double t)
yarp::os::BufferedPort< yarp::sig::Vector > debugPort
~PriorityDebugThread() override
void threadRelease() override
Release method.
void run() override
Loop function.
PriorityCarrier * pcarrier
bool threadInit() override
Initialization method.
PriorityDebugThread(PriorityCarrier *carrier)
Class PriorityDebugThread.
std::string debugPortName
Manager for priority-aware inputs to a given port.
yarp::sig::Matrix X
virtual ~PriorityGroup()
virtual bool acceptIncomingData(yarp::os::ConnectionReader &reader, PriorityCarrier *source)
bool recalculate(double t)
Class PriorityGroup.
yarp::sig::Matrix Y
yarp::sig::Matrix InvA
yarp::sig::Matrix B
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:302
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
An interface for reading from a network connection.
The basic state of a connection - route, streams in use, etc.
void remove(const std::string &key, typename PR::peer_type *entity)
Definition: Election.h:99
A starter class for implementing simple modifying carriers.
An abstraction for a periodic thread.
A class for storing options and configuration information.
Definition: Property.h:34
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A single value (typically within a Bottle).
Definition: Value.h:45
A class for a Matrix.
Definition: Matrix.h:43