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