WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
LinearSystemProto.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdexcept>
8#include <utility>
9
10#include <fmt/format.h>
13
17
18template <int States, int Inputs, int Outputs>
19struct wpi::Protobuf<frc::LinearSystem<States, Inputs, Outputs>> {
25
26 static std::optional<frc::LinearSystem<States, Inputs, Outputs>> Unpack(
27 InputStream& stream) {
31 a.SetLimits(wpi::DecodeLimits::Fail);
36
38 msg.a = a.Callback();
39 msg.b = b.Callback();
40 msg.c = c.Callback();
41 msg.d = d.Callback();
42
43 if (!stream.Decode(msg)) {
44 return {};
45 }
46
47 if (msg.num_inputs != Inputs || msg.num_outputs != Outputs ||
48 msg.num_states != States) {
49 return {};
50 }
51
52 auto ai = a.Items();
53 auto bi = b.Items();
54 auto ci = c.Items();
55 auto di = d.Items();
56
57 if (ai.empty() || bi.empty() || ci.empty() || di.empty()) {
58 return {};
59 }
60
62 std::move(ai[0]),
63 std::move(bi[0]),
64 std::move(ci[0]),
65 std::move(di[0]),
66 };
67 }
68
69 static bool Pack(OutputStream& stream,
71 wpi::PackCallback a{&value.A()};
72 wpi::PackCallback b{&value.B()};
73 wpi::PackCallback c{&value.C()};
74 wpi::PackCallback d{&value.D()};
75
77 .num_states = States,
78 .num_inputs = Inputs,
79 .num_outputs = Outputs,
80 .a = a.Callback(),
81 .b = b.Callback(),
82 .c = c.Callback(),
83 .d = d.Callback(),
84 };
85
86 return stream.Encode(msg);
87 }
88};
A plant defined using state-space notation.
Definition LinearSystem.h:35
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.h:141
void SetLimits(DecodeLimits limit) noexcept
Set the limits on what happens if more elements exist in the buffer then expected.
Definition ProtobufCallbacks.h:134
A callback method that will pack elements when called.
Definition ProtobufCallbacks.h:486
Class for wrapping a nanopb istream.
Definition Protobuf.h:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.h:104
Class for wrapping a nanopb ostream.
Definition Protobuf.h:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.h:211
A DirectUnpackCallback backed by a SmallVector<T, N>.
Definition ProtobufCallbacks.h:335
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.h:350
Definition CAN.h:11
Definition system.npb.h:19
uint32_t num_inputs
Definition system.npb.h:25
pb_callback_t b
Definition system.npb.h:28
pb_callback_t c
Definition system.npb.h:29
pb_callback_t a
Definition system.npb.h:27
pb_callback_t d
Definition system.npb.h:30
uint32_t num_states
Definition system.npb.h:24
uint32_t num_outputs
Definition system.npb.h:26
static bool Pack(OutputStream &stream, const frc::LinearSystem< States, Inputs, Outputs > &value)
Definition LinearSystemProto.h:69
static std::optional< frc::LinearSystem< States, Inputs, Outputs > > Unpack(InputStream &stream)
Definition LinearSystemProto.h:26
Protobuf serialization template.
Definition Protobuf.h:36
struct _wpi_proto_ProtobufLinearSystem wpi_proto_ProtobufLinearSystem