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