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