42 void Write(std::ostream &out)
const {
43 const uint32_t count =
static_cast<uint32_t
>(
ticks.size());
52 bool Read(std::istream &in, uint32_t size) {
56 uint16_t record_size = 0u;
61 for (uint32_t i = 0u; i < count; ++i) {
69 ticks.push_back(tick);
123 void Write(std::ostream &out)
const {
124 std::ostringstream payload;
127 std::ostringstream record;
129 if (state.random_engine) {
132 if (state.lidar_horizontal_angle) {
135 if (state.imu_history) {
136 std::ostringstream value;
137 WriteVector(value, state.imu_history->older_location);
138 WriteVector(value, state.imu_history->last_location);
142 if (state.radar_previous_location) {
143 std::ostringstream value;
144 WriteVector(value, *state.radar_previous_location);
147 if (state.imu_local_rotation) {
148 std::ostringstream value;
149 for (
double axis : *state.imu_local_rotation) {
159 bool Read(std::istream &in, uint32_t size) {
166 for (uint32_t i = 0u; i < count; ++i) {
171 case kRandomEngine: {
172 uint32_t engine = 0u;
173 if (!value.Read(engine)) {
176 state.random_engine = engine;
179 case kLidarHorizontalAngle: {
181 if (!value.Read(angle)) {
184 state.lidar_horizontal_angle = angle;
189 if (!ReadVector(value, history.older_location) ||
190 !ReadVector(value, history.last_location) ||
191 !value.Read(history.last_delta_seconds)) {
194 state.imu_history = history;
197 case kRadarPreviousLocation: {
198 std::array<double, 3> location;
199 if (!ReadVector(value, location)) {
202 state.radar_previous_location = location;
205 case kImuLocalRotation: {
206 std::array<double, 4> rotation;
207 for (double &axis : rotation) {
208 if (!value.Read(axis)) {
212 state.imu_local_rotation = rotation;
223 states.push_back(std::move(state));
230 template <
typename T>
231 static std::string
Bytes(
const T &value) {
232 std::ostringstream out;
237 static void WriteVector(std::ostream &out,
const std::array<double, 3> &vector) {
238 for (
double value : vector) {
244 for (
double &value : vector) {
245 if (!reader.
Read(value)) {
255 std::minstd_rand copy = engine;
258 const uint64_t next = copy();
259 return static_cast<uint32_t
>((next * uint64_t{1899818559u}) % uint64_t{2147483647u});
265 if ((state == 0u) || (state >= std::minstd_rand::modulus)) {
Reads the fields of one packet (or one record inside it) without ever consuming more than its declare...
bool ReadFixedListHeader(uint16_t min_record_size, uint32_t &count, uint16_t &record_size)
Header of a list of fixed-size records, checked against the bytes left.
bool ReadRecord(uint32_t bytes, F &&read)
Runs read(record) on the next bytes as a reader of their own, then skips what it left unread (a recor...
bool ReadSizedRecord(F &&read)
Reads one [u32 record_size][record] entry of a variable-size list.
bool ReadVariableListCount(uint32_t &count)
Count of a list of variable-size records, checked against the bytes left.
bool ReadFields(F &&read)
Reads TLV fields until the reader is exhausted.
void WritePod(std::ostream &out, const T &value)
uint32_t GetEngineState(const std::minstd_rand &engine)
The state of a minstd_rand: the value its next draw is computed from.
void WriteFixedListHeader(std::ostream &out, uint32_t count, uint16_t record_size)
Header of a list of fixed-size records.
bool SetEngineState(std::minstd_rand &engine, uint32_t state)
Makes engine continue from state, as GetEngineState returned it.
void WriteField(std::ostream &out, uint8_t kind, const std::string &value)
void WriteFrameDataHeader(std::ostream &out, uint8_t id, uint32_t payload_size)
void WriteSizedBytes(std::ostream &out, const std::string &bytes)
One [u32 size][bytes] entry: a variable-size record or, after a kind byte, a TLV value.
void WriteFrameDataPacket(std::ostream &out, uint8_t id, const std::string &payload)
Writes a packet whose payload was built beforehand.
This file contains definitions of common data structures used in traffic manager.
An IMU's history: its two previous locations (world, cm) and the delta between them; its acceleromete...
std::array< double, 3 > last_location
std::array< double, 3 > older_location
One record of frame data packet 27: the runtime state a sensor of the primary starts this frame's tic...
std::optional< ImuHistory > imu_history
std::optional< float > lidar_horizontal_angle
Ray-cast lidars: where this tick's sweep starts (radians).
std::optional< std::array< double, 4 > > imu_local_rotation
IMU: its rotation relative to its parent (x, y, z, w), which turns the parent's angular velocity into...
std::optional< std::array< double, 3 > > radar_previous_location
Radar: its previous location (world, cm), for its own velocity.
std::optional< uint32_t > random_engine
URandomEngine (noise, lidar drop-off, radar ray directions).
Frame data packet 27, for the sensors of packet 26 that carry state.
std::vector< SensorState > states
void Write(std::ostream &out) const
static constexpr uint8_t kPacketId
bool Read(std::istream &in, uint32_t size)
static std::string Bytes(const T &value)
Field
Field kinds; a reader skips the kinds it does not know.
static void WriteVector(std::ostream &out, const std::array< double, 3 > &vector)
static bool ReadVector(BoundedReader &reader, std::array< double, 3 > &vector)
One record of frame data packet 26: a sensor of the primary that ticks in this frame,...
Frame data packet 26: every sensor of the primary that ticks in this frame.
static constexpr uint8_t kPacketId
std::vector< SensorTick > ticks
static constexpr uint16_t kRecordSizeV1
void Write(std::ostream &out) const
bool Read(std::istream &in, uint32_t size)