WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SimpleMotorFeedforwardProto.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 "pb.h"
9#include "wpi/units/length.hpp"
12
13// Everything is converted into units for
14// wpi::math::SimpleMotorFeedforward<wpi::units::meters> or
15// wpi::math::SimpleMotorFeedforward<wpi::units::radians>
16
17template <class Distance>
18 requires wpi::units::length_unit<Distance> || wpi::units::angle_unit<Distance>
25
26 static std::optional<wpi::math::SimpleMotorFeedforward<Distance>> Unpack(
27 InputStream& stream) {
28 using BaseUnit =
29 wpi::units::unit<std::ratio<1>,
30 wpi::units::traits::base_unit_of<Distance>>;
31 using BaseFeedforward = wpi::math::SimpleMotorFeedforward<BaseUnit>;
33 if (!stream.Decode(msg)) {
34 return {};
35 }
36
38 wpi::units::volt_t{msg.ks},
39 wpi::units::unit_t<typename BaseFeedforward::kv_unit>{msg.kv},
40 wpi::units::unit_t<typename BaseFeedforward::ka_unit>{msg.ka},
41 wpi::units::second_t{msg.dt},
42 };
43 }
44
45 static bool Pack(OutputStream& stream,
47 using BaseUnit =
48 wpi::units::unit<std::ratio<1>,
49 wpi::units::traits::base_unit_of<Distance>>;
50 using BaseFeedforward = wpi::math::SimpleMotorFeedforward<BaseUnit>;
52 .ks = value.GetKs().value(),
53 .kv =
54 wpi::units::unit_t<typename BaseFeedforward::kv_unit>{value.GetKv()}
55 .value(),
56 .ka =
57 wpi::units::unit_t<typename BaseFeedforward::ka_unit>{value.GetKa()}
58 .value(),
59 .dt = wpi::units::second_t{value.GetDt()}.value(),
60 };
61 return stream.Encode(msg);
62 }
63};
A helper class that computes feedforward voltages for a simple permanent-magnet DC motor.
Definition SimpleMotorFeedforward.hpp:24
constexpr wpi::units::second_t GetDt() const
Returns the period.
Definition SimpleMotorFeedforward.hpp:234
constexpr wpi::units::volt_t GetKs() const
Returns the static gain.
Definition SimpleMotorFeedforward.hpp:213
constexpr wpi::units::unit_t< kv_unit > GetKv() const
Returns the velocity gain.
Definition SimpleMotorFeedforward.hpp:220
constexpr wpi::units::unit_t< ka_unit > GetKa() const
Returns the acceleration gain.
Definition SimpleMotorFeedforward.hpp:227
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
struct _wpi_proto_ProtobufSimpleMotorFeedforward wpi_proto_ProtobufSimpleMotorFeedforward
Definition CvSource.hpp:15
double kv
Definition controller.npb.h:59
double ks
Definition controller.npb.h:58
double ka
Definition controller.npb.h:60
double dt
Definition controller.npb.h:61
static std::optional< wpi::math::SimpleMotorFeedforward< Distance > > Unpack(InputStream &stream)
Definition SimpleMotorFeedforwardProto.hpp:26
wpi::util::ProtoOutputStream< wpi::math::SimpleMotorFeedforward< Distance > > OutputStream
Definition SimpleMotorFeedforwardProto.hpp:23
static bool Pack(OutputStream &stream, const wpi::math::SimpleMotorFeedforward< Distance > &value)
Definition SimpleMotorFeedforwardProto.hpp:45
wpi_proto_ProtobufSimpleMotorFeedforward MessageStruct
Definition SimpleMotorFeedforwardProto.hpp:20
wpi::util::ProtoInputStream< wpi::math::SimpleMotorFeedforward< Distance > > InputStream
Definition SimpleMotorFeedforwardProto.hpp:21
Protobuf serialization template.
Definition Protobuf.hpp:36