CTRE Phoenix 6 C++ 24.2.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 * \param newMasterID Parameter to modify
104 * \returns Itself
105 */
106 Follower& WithMasterID(int newMasterID)
107 {
108 MasterID = std::move(newMasterID);
109 return *this;
110 }
111
112 /**
113 * \brief Modifies this Control Request's OpposeMasterDirection parameter and returns itself for
114 * method-chaining and easier to use request API.
115 * \param newOpposeMasterDirection Parameter to modify
116 * \returns Itself
117 */
118 Follower& WithOpposeMasterDirection(bool newOpposeMasterDirection)
119 {
120 OpposeMasterDirection = std::move(newOpposeMasterDirection);
121 return *this;
122 }
123 /**
124 * \brief Sets the period at which this control will update at.
125 * This is designated in Hertz, with a minimum of 20 Hz
126 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
127 *
128 * If this field is set to 0 Hz, the control request will
129 * be sent immediately as a one-shot frame. This may be useful
130 * for advanced applications that require outputs to be
131 * synchronized with data acquisition. In this case, we
132 * recommend not exceeding 50 ms between control calls.
133 *
134 * \param newUpdateFreqHz Parameter to modify
135 * \returns Itself
136 */
137 Follower &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
138 {
139 UpdateFreqHz = newUpdateFreqHz;
140 return *this;
141 }
142 /**
143 * Returns a string representation of the object.
144 *
145 * \returns a string representation of the object.
146 */
147 std::string ToString() const override
148 {
149 std::stringstream ss;
150 ss << "class: Follower" << std::endl;
151 ss << "MasterID: " << MasterID << std::endl;
152 ss << "OpposeMasterDirection: " << OpposeMasterDirection << std::endl;
153 return ss.str();
154 }
155
156 /**
157 * \brief Gets information about this control request.
158 *
159 * \returns Map of control parameter names and corresponding applied values
160 */
161 std::map<std::string, std::string> GetControlInfo() const override
162 {
163 std::map<std::string, std::string> controlInfo;
164 std::stringstream ss;
165 controlInfo["Name"] = GetName();
166 ss << MasterID; controlInfo["MasterID"] = ss.str(); ss.str(std::string{});
167 ss << OpposeMasterDirection; controlInfo["OpposeMasterDirection"] = ss.str(); ss.str(std::string{});
168 return controlInfo;
169 }
170};
171
172}
173}
174}
175
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:106
std::string ToString() const override
Returns a string representation of the object.
Definition: Follower.hpp:147
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: Follower.hpp:161
Follower & WithOpposeMasterDirection(bool newOpposeMasterDirection)
Modifies this Control Request's OpposeMasterDirection parameter and returns itself for method-chainin...
Definition: Follower.hpp:118
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:137
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: RcManualEvent.hpp:12