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