Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
spoe_process_data.h
Go to the documentation of this file.
1#pragma once
2
3// The pieces that carry SPoE process data between the RT thread and the exchange thread of one
4// drive. The RT thread never waits on either of them.
5//
6// The drive buffers its input frames and returns them in batches, so the inputs travel through a
7// queue and the RT thread takes one frame per cycle. That paces the frames to the RT cycle, which
8// is what lets monitoring see every frame in order. The outputs are latest-wins: a frame the
9// exchange thread did not send yet is replaced by a newer one.
10
11#include <array>
12#include <atomic>
13#include <cstddef>
14#include <cstdint>
15#include <span>
16#include <vector>
17
18namespace mm::comm::spoe {
19
21constexpr std::size_t kInputPopLimit = 500;
22
27 public:
29 void reset(std::size_t frameBytes);
30
32 void write(std::span<const uint8_t> frame);
33
35 std::span<const uint8_t> read();
36
37 private:
38 static constexpr uint8_t kFresh = 0x04;
39 static constexpr uint8_t kIndexMask = 0x03;
40
41 std::array<std::vector<uint8_t>, 3> buffers_;
42 uint8_t writeIndex_ = 0;
43 uint8_t readIndex_ = 1;
44 std::atomic<uint8_t> middle_{2};
45};
46
50 public:
52 void reset(std::size_t frameBytes, std::size_t capacity);
53
55 bool push(std::span<const uint8_t> frame);
56
59 bool pop(std::span<uint8_t> frame);
60
63 std::size_t dropOldest(std::size_t keep);
64
65 std::size_t size() const;
66
67 private:
68 std::vector<uint8_t> storage_;
69 std::size_t frameBytes_ = 0;
70 std::size_t capacity_ = 0;
71 std::atomic<std::size_t> head_{0}; // written by the producer
72 std::atomic<std::size_t> tail_{0}; // written by the consumer
73};
74
86 public:
87 void reset(std::size_t frameBytes);
88
90 std::vector<uint8_t> add(std::span<const uint8_t> reply);
91
93 uint64_t framingLosses() const { return framingLosses_; }
94
95 private:
96 std::size_t frameBytes_ = 0;
97 std::vector<uint8_t> partial_;
98 uint64_t framingLosses_ = 0;
99};
100
101} // namespace mm::comm::spoe
Definition spoe_process_data.h:85
std::vector< uint8_t > add(std::span< const uint8_t > reply)
Adds the input bytes of one reply. Returns the whole frames they complete, joined.
Definition spoe_process_data.cc:89
void reset(std::size_t frameBytes)
Definition spoe_process_data.cc:83
uint64_t framingLosses() const
How many times stale bytes were dropped to restore the framing.
Definition spoe_process_data.h:93
Definition spoe_process_data.h:49
std::size_t dropOldest(std::size_t keep)
Definition spoe_process_data.cc:67
std::size_t size() const
Definition spoe_process_data.cc:79
bool pop(std::span< uint8_t > frame)
Definition spoe_process_data.cc:54
void reset(std::size_t frameBytes, std::size_t capacity)
Sizes the queue and empties it. Not safe while the producer or the consumer runs.
Definition spoe_process_data.cc:33
bool push(std::span< const uint8_t > frame)
Producer side. Returns false, and drops frame, when the queue is full.
Definition spoe_process_data.cc:41
Definition spoe_process_data.h:26
void reset(std::size_t frameBytes)
Sizes every buffer and clears them. Not safe while the writer or the reader runs.
Definition spoe_process_data.cc:8
void write(std::span< const uint8_t > frame)
Writer side. frame must hold the size given to reset.
Definition spoe_process_data.cc:17
std::span< const uint8_t > read()
Reader side. Returns the newest frame, or the previous one again when nothing newer arrived.
Definition spoe_process_data.cc:25
Definition spoe_connection.cc:15
constexpr std::size_t kInputPopLimit
pop_from_pdo_fifo_buffer in the firmware returns at most this many bytes of inputs at once.
Definition spoe_process_data.h:21