ReduxLib C++ 2025.0.0-beta2
Loading...
Searching...
No Matches
CanandmagSettings.h
1// Copyright (c) Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the 3-clause BSD license.
3
4#pragma once
5#include <cinttypes>
6#include <unordered_map>
7#include <optional>
8#include <units/time.h>
9#include <units/angle.h>
10#include "redux/canand/CanandSettings.h"
11
13
14/**
15 * The settings class for the Canandmag.
16 *
17 * <p>
18 * This class holds settings values that can be used to reconfigure Canandmag via Canandmag::SetSettings
19 * Additionally, objects of this class can be filled using Canandmag.GetSettings which can be used to
20 * read the encoder's settings.
21 * </p>
22 *
23 * ```cpp
24 * Canandmag enc = Canandmag{0};
25 *
26 * // Only settings that are explicitly set here will be edited, so other settings
27 * // such as the status frame period will remain untouched.
28 * CanandmagSettings stg;
29 * stg.SetPositionFramePeriod(0_ms); // disables position readings
30 * stg.SetVelocityFramePeriod(20_ms); // sets the rate of velocity measurements to every 20 ms
31 * stg.SetInvertDirection(true); // inverts the encoder direction
32 *
33 * enc.SetSettings(stg);
34 *
35 * ```
36 * Objects returned by the blocking Canandmag::GetSettings() method will always have all setting values populated and the getters will never return std::nullopt.
37 *
38 * However, they may return std::nullopt if the object is either manually constructed and the corresponding getter hasn't been called yet
39 * (e.g. calling GetStatusFramePeriod before SetStatusFramePeriod on an object you made)
40 * or if the object comes from Canandmag::GetSettingsAsync() and not all settings have been received (use AllSettingsReceived() to check if all are present)
41 *
42 * <a href="https://en.cppreference.com/w/cpp/utility/optional">cppreference on std::optional</a> may be helpful here.
43 *
44 * Example blocking fetch:
45 * ```cpp
46 * Canandmag canandmag{0};
47 * CanandmagSettings stg = canandmag.GetSettings();
48 * if (stg.AllSettingsReceived()) { // check for timeout
49 * fmt::print("status frame period: {}\n", *stg.GetStatusFramePeriod()); // print the status frame period (usually 1000 ms)
50 * }
51 * ```
52 *
53 */
55 public:
56 CanandmagSettings() = default;
57 const std::vector<uint8_t>& SettingAddresses() const override;
58
59 /**
60 * Sets the velocity filter width in milliseconds to sample over.
61 * Velocity is computed by averaging all the points in the past widthMs milliseconds.
62 * By factory default, the velocity filter averages over the past 25 milliseconds.
63 *
64 * @param widthMs the new number of samples to average over. Minimum accepted is 0.25 milliseconds, maximum is 63.75 ms.
65 */
66 void SetVelocityFilterWidth(units::millisecond_t widthMs);
67
68 /**
69 * Sets the position frame period in seconds.
70 * By factory default, position frames are sent every 20 milliseconds (period=0.020_s)
71 * If 0 is passed in, position frames will be disabled and the methods Canandmag::GetPosition()
72 * and Canandmag::GetAbsPosition() will not return new values.
73 *
74 * @param period the new period for position frames in seconds in range [0_s, 65.535_s].
75 */
76 void SetPositionFramePeriod(units::second_t period);
77
78 /**
79 * Sets the velocity frame period in seconds.
80 * By factory default, velocity frames are sent every 20 milliseconds (period=0.020_s)
81 * If 0 is passed in, velocity frames will be disabled and Canandmag::GetVelocity() will not return
82 * new values.
83 *
84 * @param period the new period for velocity frames in seconds in range [0_s, 65.535_s].
85 */
86 void SetVelocityFramePeriod(units::second_t period);
87
88 /**
89 * Sets the status frame period in seconds.
90 * By factory default, the encoder will broadcast 1 status message per second (period=1.000_s).
91 *
92 * @param period the new period for status frames in seconds in range [0.001_s, 16.383_s].
93 */
94 void SetStatusFramePeriod(units::second_t period);
95
96 /**
97 * Inverts the direction read from the sensor. By factory default, the sensor will read counterclockwise from its reading face
98 * as positive (invert=false).
99 * @param invert whether to invert (negate) readings from the encoder
100 */
101 void SetInvertDirection(bool invert);
102
103 /**
104 * Sets whether or not the sensor should disallow zeroing and factory resets from the onboard button.
105 * By factory default, the sensor will allow the zero button to function when pressed (disable=false)
106 * @param disable whether to disable the onboard zeroing button's functionality
107 */
108 void SetDisableZeroButton(bool disable);
109
110 /**
111 * Sets the zero offset of the encoder directly, rather than adjusting the zero offset
112 * relative to the currently read position.
113 *
114 * <p>The zero offset is subtracted from the raw reading of the encoder's magnetic sensor to
115 * get the adjusted absolute position as returned by Canandmag.GetAbsPosition().</p>
116 *
117 * Users are encouraged to use Canandmag.SetAbsPosition instead.
118 *
119 * @param offset the new offset in rotations [0..1)
120 */
121 void SetZeroOffset(units::turn_t offset);
122
123 /**
124 * Gets the velocity filter width in milliseconds [0.25..63.75], or std::nullopt if the value has not been set on this object.
125 * @return the velocity filter width in milliseconds [0.25..63.75], or std::nullopt if the value has not been set on this object.
126 */
127 std::optional<units::millisecond_t> GetVelocityFilterWidth();
128
129 /**
130 * Gets the position frame period in seconds [0..65.535], or std::nullopt if the value has not been set on this object.
131 * A value of 0 means position messages are disabled.
132 * @return the position frame period in seconds [0..65.535], or std::nullopt if the value has not been set on this object.
133 */
134 std::optional<units::second_t> GetPositionFramePeriod();
135
136 /**
137 * Gets the velocity frame period in seconds [0..65.535], or std::nullopt if the value has not been set on this object.
138 * A value of 0 means velocity messages are disabled.
139 * @return the velocity frame period in seconds [0..65.535], or std::nullopt if the value has not been set on this object.
140 */
141 std::optional<units::second_t> GetVelocityFramePeriod();
142
143 /**
144 * Gets the status frame period in seconds [0.001..16.383], or std::nullopt if the value has not been set on this object.
145 * A value of 0 means status messages are disabled.
146 * @return the status frame period in seconds [0.001..16.383], or std::nullopt if the value has not been set on this object.
147 */
148 std::optional<units::second_t> GetStatusFramePeriod();
149
150 /**
151 * Gets whether or not the encoder has an inverted direction (0 for no, 1 for yes, std::nullopt for unset).
152 * @return whether or not the encoder has an inverted direction (0 for no, 1 for yes, std::nullopt for unset).
153 */
154 std::optional<bool> GetInvertDirection();
155
156 /**
157 * Gets whether or not the sensor should disallow zeroing and factory resets from the onboard button (0 for allow, 1 for disallow, std::nullopt for unset).
158 * @return whether or not the encoder has its onboard zero button's functionality disabled (0 for allow, 1 for disallow, std::nullopt for unset).
159 */
160 std::optional<bool> GetDisableZeroButton();
161
162 /**
163 * Gets the zero offset of the encoder.
164 *
165 * The zero offset is subtracted from the raw reading of the encoder's magnetic sensor to
166 * get the adjusted absolute position as returned by Canandmag.GetAbsPosition().
167 *
168 * @return the zero offset [0..1), or std::nullopt if the value has not been set on this object.
169 */
170 std::optional<units::turn_t> GetZeroOffset();
171};
172}
Definition: CanandSettings.h:20
Definition: CanandmagSettings.h:54
void SetZeroOffset(units::turn_t offset)
void SetVelocityFilterWidth(units::millisecond_t widthMs)
std::optional< units::second_t > GetPositionFramePeriod()
void SetStatusFramePeriod(units::second_t period)
std::optional< units::second_t > GetStatusFramePeriod()
std::optional< units::turn_t > GetZeroOffset()
std::optional< units::millisecond_t > GetVelocityFilterWidth()
void SetVelocityFramePeriod(units::second_t period)
std::optional< units::second_t > GetVelocityFramePeriod()
std::optional< bool > GetDisableZeroButton()
const std::vector< uint8_t > & SettingAddresses() const override
void SetPositionFramePeriod(units::second_t period)
Definition: CanandmagSettings.h:12