CTRE Phoenix 6 C++ 25.4.0
Loading...
Searching...
No Matches
Follower.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
11#include <sstream>
12
13#include <units/frequency.h>
14#include <units/time.h>
15
16namespace ctre {
17namespace phoenix6 {
18namespace controls {
19
20/**
21 * Follow the motor output of another Talon.
22 *
23 * If Talon is in torque control, the torque is copied - which will increase the total torque applied. If
24 * Talon is in percent supply output control, the duty cycle is matched. Motor direction either matches
25 * master's configured direction or opposes it based on OpposeMasterDirection.
26 */
28{
29 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
30 {
31 if (req.get() != this)
32 {
33 auto const reqCast = dynamic_cast<Follower *>(req.get());
34 if (reqCast != nullptr)
35 {
36 *reqCast = *this;
37 }
38 else
39 {
40 req = std::make_shared<Follower>(*this);
41 }
42 }
43
45 }
46
47public:
48 /**
49 * \brief Device ID of the master to follow.
50 */
52 /**
53 * \brief Set to false for motor invert to match the master's configured Invert
54 * - which is typical when master and follower are mechanically linked and spin
55 * in the same direction. Set to true for motor invert to oppose the master's
56 * configured Invert - this is typical where the the master and follower
57 * mechanically spin in opposite directions.
58 */
60
61 /**
62 * \brief The period at which this control will update at.
63 * This is designated in Hertz, with a minimum of 20 Hz
64 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
65 *
66 * If this field is set to 0 Hz, the control request will
67 * be sent immediately as a one-shot frame. This may be useful
68 * for advanced applications that require outputs to be
69 * synchronized with data acquisition. In this case, we
70 * recommend not exceeding 50 ms between control calls.
71 */
72 units::frequency::hertz_t UpdateFreqHz{20_Hz};
73
74 /**
75 * \brief Follow the motor output of another Talon.
76 *
77 * \details If Talon is in torque control, the torque is copied - which will
78 * increase the total torque applied. If Talon is in percent supply
79 * output control, the duty cycle is matched. Motor direction either
80 * matches master's configured direction or opposes it based on
81 * OpposeMasterDirection.
82 *
83 * \param MasterID Device ID of the master to follow.
84 * \param OpposeMasterDirection Set to false for motor invert to match the
85 * master's configured Invert - which is typical
86 * when master and follower are mechanically
87 * linked and spin in the same direction. Set
88 * to true for motor invert to oppose the
89 * master's configured Invert - this is typical
90 * where the the master and follower
91 * mechanically spin in opposite directions.
92 */
97
98 /**
99 * \brief Modifies this Control Request's MasterID parameter and returns itself for
100 * method-chaining and easier to use request API.
101 *
102 * Device ID of the master to follow.
103 *
104 * \param newMasterID Parameter to modify
105 * \returns Itself
106 */
107 Follower &WithMasterID(int newMasterID)
108 {
109 MasterID = std::move(newMasterID);
110 return *this;
111 }
112
113 /**
114 * \brief Modifies this Control Request's OpposeMasterDirection parameter and returns itself for
115 * method-chaining and easier to use request API.
116 *
117 * Set to false for motor invert to match the master's configured Invert - which
118 * is typical when master and follower are mechanically linked and spin in the
119 * same direction. Set to true for motor invert to oppose the master's
120 * configured Invert - this is typical where the the master and follower
121 * mechanically spin in opposite directions.
122 *
123 * \param newOpposeMasterDirection Parameter to modify
124 * \returns Itself
125 */
126 Follower &WithOpposeMasterDirection(bool newOpposeMasterDirection)
127 {
128 OpposeMasterDirection = std::move(newOpposeMasterDirection);
129 return *this;
130 }
131 /**
132 * \brief Sets the period at which this control will update at.
133 * This is designated in Hertz, with a minimum of 20 Hz
134 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
135 *
136 * If this field is set to 0 Hz, the control request will
137 * be sent immediately as a one-shot frame. This may be useful
138 * for advanced applications that require outputs to be
139 * synchronized with data acquisition. In this case, we
140 * recommend not exceeding 50 ms between control calls.
141 *
142 * \param newUpdateFreqHz Parameter to modify
143 * \returns Itself
144 */
145 Follower &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
146 {
147 UpdateFreqHz = newUpdateFreqHz;
148 return *this;
149 }
150 /**
151 * \brief Returns a string representation of the object.
152 *
153 * \returns a string representation of the object.
154 */
155 std::string ToString() const override
156 {
157 std::stringstream ss;
158 ss << "Control: Follower" << std::endl;
159 ss << " MasterID: " << MasterID << std::endl;
160 ss << " OpposeMasterDirection: " << OpposeMasterDirection << std::endl;
161 return ss.str();
162 }
163
164 /**
165 * \brief Gets information about this control request.
166 *
167 * \returns Map of control parameter names and corresponding applied values
168 */
169 std::map<std::string, std::string> GetControlInfo() const override
170 {
171 std::map<std::string, std::string> controlInfo;
172 std::stringstream ss;
173 controlInfo["Name"] = GetName();
174 ss << MasterID; controlInfo["MasterID"] = ss.str(); ss.str(std::string{});
175 ss << OpposeMasterDirection; controlInfo["OpposeMasterDirection"] = ss.str(); ss.str(std::string{});
176 return controlInfo;
177 }
178};
179
180}
181}
182}
183
CTREXPORT int c_ctre_phoenix6_RequestControlFollower(const char *canbus, uint32_t ecuEncoding, double updateFrequency, int MasterID, bool OpposeMasterDirection)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Follow the motor output of another Talon.
Definition Follower.hpp:28
Follower & WithMasterID(int newMasterID)
Modifies this Control Request's MasterID parameter and returns itself for method-chaining and easier ...
Definition Follower.hpp:107
std::string ToString() const override
Returns a string representation of the object.
Definition Follower.hpp:155
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition Follower.hpp:169
Follower & WithOpposeMasterDirection(bool newOpposeMasterDirection)
Modifies this Control Request's OpposeMasterDirection parameter and returns itself for method-chainin...
Definition Follower.hpp:126
bool OpposeMasterDirection
Set to false for motor invert to match the master's configured Invert.
Definition Follower.hpp:59
Follower(int MasterID, bool OpposeMasterDirection)
Follow the motor output of another Talon.
Definition Follower.hpp:93
Follower & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition Follower.hpp:145
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition Follower.hpp:72
int MasterID
Device ID of the master to follow.
Definition Follower.hpp:51
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition Diff_PositionDutyCycle_Position.hpp:15
Definition span.hpp:401