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