WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LinearSystemProto.hpp
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>
11
17
18template <int States, int Inputs, int Outputs>
19struct wpi::util::Protobuf<wpi::math::LinearSystem<States, Inputs, Outputs>> {
25
26 static std::optional<wpi::math::LinearSystem<States, Inputs, Outputs>> Unpack(
27 InputStream& stream) {
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(
70 OutputStream& stream,
72 wpi::util::PackCallback a{&value.A()};
73 wpi::util::PackCallback b{&value.B()};
74 wpi::util::PackCallback c{&value.C()};
75 wpi::util::PackCallback d{&value.D()};
76
78 .num_states = States,
79 .num_inputs = Inputs,
80 .num_outputs = Outputs,
81 .a = a.Callback(),
82 .b = b.Callback(),
83 .c = c.Callback(),
84 .d = d.Callback(),
85 };
86
87 return stream.Encode(msg);
88 }
89};
A plant defined using state-space notation.
Definition LinearSystem.hpp:35
constexpr const Matrixd< States, States > & A() const
Returns the system matrix A.
Definition LinearSystem.hpp:104
constexpr const Matrixd< States, Inputs > & B() const
Returns the input matrix B.
Definition LinearSystem.hpp:117
constexpr const Matrixd< Outputs, States > & C() const
Returns the output matrix C.
Definition LinearSystem.hpp:130
constexpr const Matrixd< Outputs, Inputs > & D() const
Returns the feedthrough matrix D.
Definition LinearSystem.hpp:143
void SetLimits(DecodeLimits limit) noexcept
Set the limits on what happens if more elements exist in the buffer then expected.
Definition ProtobufCallbacks.hpp:134
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:141
A callback method that will pack elements when called.
Definition ProtobufCallbacks.hpp:487
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:519
Class for wrapping a nanopb istream.
Definition Protobuf.hpp:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.hpp:104
Class for wrapping a nanopb ostream.
Definition Protobuf.hpp:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.hpp:211
A DirectUnpackCallback backed by a SmallVector<T, N>.
Definition ProtobufCallbacks.hpp:335
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.hpp:351
@ Fail
Definition ProtobufCallbacks.hpp:30
Definition CvSource.hpp:15
uint32_t num_inputs
Definition system.npb.h:37
pb_callback_t b
Definition system.npb.h:40
pb_callback_t c
Definition system.npb.h:41
pb_callback_t a
Definition system.npb.h:39
pb_callback_t d
Definition system.npb.h:42
uint32_t num_states
Definition system.npb.h:36
uint32_t num_outputs
Definition system.npb.h:38
static bool Pack(OutputStream &stream, const wpi::math::LinearSystem< States, Inputs, Outputs > &value)
Definition LinearSystemProto.hpp:69
wpi_proto_ProtobufLinearSystem MessageStruct
Definition LinearSystemProto.hpp:20
wpi::util::ProtoInputStream< wpi::math::LinearSystem< States, Inputs, Outputs > > InputStream
Definition LinearSystemProto.hpp:21
wpi::util::ProtoOutputStream< wpi::math::LinearSystem< States, Inputs, Outputs > > OutputStream
Definition LinearSystemProto.hpp:23
static std::optional< wpi::math::LinearSystem< States, Inputs, Outputs > > Unpack(InputStream &stream)
Definition LinearSystemProto.hpp:26
Protobuf serialization template.
Definition Protobuf.hpp:36
struct _wpi_proto_ProtobufLinearSystem wpi_proto_ProtobufLinearSystem