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