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
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:73
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:305
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
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:102
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:37
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
A single value (typically within a Bottle).
Definition: Value.h:47
A class for a Matrix.
Definition: Matrix.h:46