yarp-devices
InterpolatedPositionBuffer.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __INTERPOLATED_POSITION_BUFFER_HPP__
4 #define __INTERPOLATED_POSITION_BUFFER_HPP__
5 
6 #include <cstdint>
7 
8 #include <deque>
9 #include <mutex>
10 #include <vector>
11 
12 namespace roboticslab
13 {
14 
23 {
24 public:
26  InterpolatedPositionBuffer(double samplingPeriod, double interpolationPeriod);
27 
29  virtual ~InterpolatedPositionBuffer() = default;
30 
32  void setInitial(int initialTarget);
33 
35  int getPeriodMs() const;
36 
38  virtual std::uint16_t getBufferSize() const = 0;
39 
41  std::uint16_t getBufferConfig() const;
42 
44  virtual std::int16_t getSubMode() const = 0;
45 
47  void addSetpoint(int target);
48 
50  std::vector<std::uint64_t> popBatch(bool fullBuffer);
51 
53  int getPrevTarget() const;
54 
56  bool isQueueReady() const;
57 
59  bool isQueueEmpty() const;
60 
62  void clearQueue();
63 
64 protected:
65  using ip_record = std::pair<int, double>; // position (internal units), timestamp (seconds)
66 
68  std::uint8_t getIntegrityCounter() const;
69 
71  virtual std::size_t getOffset() const;
72 
74  std::uint16_t getSampledTime(double currentTimestamp);
75 
77  double getMeanVelocity(const ip_record & earliest, const ip_record & latest) const;
78 
80  virtual std::uint64_t makeDataRecord(const ip_record & previous, const ip_record & current, const ip_record & next) = 0;
81 
82  const double samplingPeriod; // [s]
83 
84 private:
85  const std::uint16_t fixedSamples;
86  std::uint8_t integrityCounter;
87  ip_record prevTarget;
88  double initialTimestamp;
89  int sampleCount;
90  std::deque<ip_record> pendingTargets;
91  mutable std::mutex queueMutex;
92 };
93 
99 {
100 public:
102 
103  std::uint16_t getBufferSize() const override;
104  std::int16_t getSubMode() const override;
105 
106 protected:
107  std::uint64_t makeDataRecord(const ip_record & previous, const ip_record & current, const ip_record & next) override;
108 };
109 
118 {
119 public:
121 
122  std::uint16_t getBufferSize() const override;
123  std::int16_t getSubMode() const override;
124 
125 protected:
126  std::size_t getOffset() const override;
127  std::uint64_t makeDataRecord(const ip_record & previous, const ip_record & current, const ip_record & next) override;
128 };
129 
130 } // namespace roboticslab
131 
132 #endif // __INTERPOLATED_POSITION_BUFFER_HPP__
Base class for a PT/PVT buffer of setpoints.
Definition: InterpolatedPositionBuffer.hpp:23
std::vector< std::uint64_t > popBatch(bool fullBuffer)
Generate next batch of setpoints popped from the front of the queue.
Definition: InterpolatedPositionBuffer.cpp:54
virtual std::uint16_t getBufferSize() const =0
Get PT/PVT buffer size.
double getMeanVelocity(const ip_record &earliest, const ip_record &latest) const
Compute mean velocity (internal units) between two setpoints.
Definition: InterpolatedPositionBuffer.cpp:154
std::uint16_t getSampledTime(double currentTimestamp)
Obtain time samples (internal units) for current segment, update internal counters.
Definition: InterpolatedPositionBuffer.cpp:138
virtual std::size_t getOffset() const
Determine how many points should be left in the queue on each non-final batch update.
Definition: InterpolatedPositionBuffer.cpp:133
bool isQueueEmpty() const
Report whether there are no more points in the queue.
Definition: InterpolatedPositionBuffer.cpp:116
void addSetpoint(int target)
Place a new setpoint (internal units) at the end of the queue.
Definition: InterpolatedPositionBuffer.cpp:48
void setInitial(int initialTarget)
Store initial position (internal units).
Definition: InterpolatedPositionBuffer.cpp:32
int getPrevTarget() const
Retrieve last point loaded into the buffer (internal units).
Definition: InterpolatedPositionBuffer.cpp:103
int getPeriodMs() const
Get PT/PVT period if fixed (synchronous), zero otherwise (asynchronous).
bool isQueueReady() const
Report whether there are enough points in the queue to fill the buffer.
Definition: InterpolatedPositionBuffer.cpp:109
virtual std::uint64_t makeDataRecord(const ip_record &previous, const ip_record &current, const ip_record &next)=0
Generate interpolation data record given three contiguous position target (object 60C1h).
std::uint16_t getBufferConfig() const
Get buffer configuration (object 2074h).
Definition: InterpolatedPositionBuffer.cpp:40
virtual std::int16_t getSubMode() const =0
Generate interpolation submode register value (object 60C0h).
virtual ~InterpolatedPositionBuffer()=default
Virtual destructor.
void clearQueue()
Clear internal queue.
Definition: InterpolatedPositionBuffer.cpp:122
InterpolatedPositionBuffer(double samplingPeriod, double interpolationPeriod)
Constructor, sets internal invariable parameters.
Definition: InterpolatedPositionBuffer.cpp:23
std::uint8_t getIntegrityCounter() const
Retrieve current integrity counter value.
Definition: InterpolatedPositionBuffer.cpp:128
Implementation of a PT buffer (linear interpolation).
Definition: InterpolatedPositionBuffer.hpp:99
std::uint64_t makeDataRecord(const ip_record &previous, const ip_record &current, const ip_record &next) override
Generate interpolation data record given three contiguous position target (object 60C1h).
Definition: InterpolatedPositionBuffer.cpp:180
std::uint16_t getBufferSize() const override
Get PT/PVT buffer size.
Definition: InterpolatedPositionBuffer.cpp:170
std::int16_t getSubMode() const override
Generate interpolation submode register value (object 60C0h).
Definition: InterpolatedPositionBuffer.cpp:175
Implementation of a PVT buffer (cubic interpolation).
Definition: InterpolatedPositionBuffer.hpp:118
std::int16_t getSubMode() const override
Generate interpolation submode register value (object 60C0h).
Definition: InterpolatedPositionBuffer.cpp:200
std::uint16_t getBufferSize() const override
Get PT/PVT buffer size.
Definition: InterpolatedPositionBuffer.cpp:195
std::uint64_t makeDataRecord(const ip_record &previous, const ip_record &current, const ip_record &next) override
Generate interpolation data record given three contiguous position target (object 60C1h).
Definition: InterpolatedPositionBuffer.cpp:210
std::size_t getOffset() const override
Determine how many points should be left in the queue on each non-final batch update.
Definition: InterpolatedPositionBuffer.cpp:205
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6