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