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
MusicTone.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#include <units/frequency.h>
14#include <units/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Plays a single tone at the user specified frequency.
24 */
26{
27 bool ApplyConfigsOnRequest{false};
28
29 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
30 {
31 if (req.get() != this)
32 {
33 auto const reqCast = dynamic_cast<MusicTone *>(req.get());
34 if (reqCast != nullptr)
35 {
36 *reqCast = *this;
37 }
38 else
39 {
40 req = std::make_shared<MusicTone>(*this);
41 }
42 }
43
44 std::stringstream ss;
45
46 std::string strs{ss.str()};
47 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
48 ApplyConfigsOnRequest = false;
49 return c_ctre_phoenix6_RequestControlMusicTone(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, AudioFrequency.to<double>());
50 }
51
52public:
53 /**
54 * Sound frequency to play. A value of zero will silence the device. The
55 * effective frequency range is 10-10000Hz. Any nonzero frequency less than 10
56 * Hz will be capped to 10Hz. Any frequency above 10Khz will be capped to
57 * 10KHz.
58 */
59 units::frequency::hertz_t AudioFrequency;
60
61
62
63 /**
64 * \brief The timeout when sending configs associated with this control
65 */
66 units::time::second_t ConfigTimeout{0.1_s};
67
68 /**
69 * \brief The period at which this control will update at.
70 * This is designated in Hertz, with a minimum of 20 Hz
71 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
72 *
73 * If this field is set to 0 Hz, the control request will
74 * be sent immediately as a one-shot frame. This may be useful
75 * for advanced applications that require outputs to be
76 * synchronized with data acquisition. In this case, we
77 * recommend not exceeding 50 ms between control calls.
78 */
79 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
80
81 /**
82 * \brief Plays a single tone at the user specified frequency.
83 *
84 * \param AudioFrequency Sound frequency to play. A value of zero will
85 * silence the device. The effective frequency range is
86 * 10-10000Hz. Any nonzero frequency less than 10 Hz
87 * will be capped to 10Hz. Any frequency above 10Khz
88 * will be capped to 10KHz.
89 */
90 MusicTone(units::frequency::hertz_t AudioFrequency) : ControlRequest{"MusicTone"},
92 {}
93
94 /**
95 * \brief Modifies this Control Request's AudioFrequency parameter and returns itself for
96 * method-chaining and easier to use request API.
97 * \param newAudioFrequency Parameter to modify
98 * \returns Itself
99 */
100 MusicTone& WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
101 {
102 AudioFrequency = std::move(newAudioFrequency);
103 return *this;
104 }
105 /**
106 * \brief Sets the period at which this control will update at.
107 * This is designated in Hertz, with a minimum of 20 Hz
108 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
109 *
110 * If this field is set to 0 Hz, the control request will
111 * be sent immediately as a one-shot frame. This may be useful
112 * for advanced applications that require outputs to be
113 * synchronized with data acquisition. In this case, we
114 * recommend not exceeding 50 ms between control calls.
115 *
116 * \param newUpdateFreqHz Parameter to modify
117 * \returns Itself
118 */
119 MusicTone &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
120 {
121 UpdateFreqHz = newUpdateFreqHz;
122 return *this;
123 }
124 /**
125 * Returns a string representation of the object.
126 *
127 * \returns a string representation of the object.
128 */
129 std::string ToString() const override
130 {
131 std::stringstream ss;
132 ss << "class: MusicTone" << std::endl;
133 ss << "AudioFrequency: " << AudioFrequency.to<double>() << std::endl;
134 return ss.str();
135 }
136
137 /**
138 * \brief Forces configs to be applied the next time this is used in a setControl.
139 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
140 * properly set when they are not already set
141 */
142 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
143
144 /**
145 * \brief Gets information about this control request.
146 *
147 * \returns Map of control parameter names and corresponding applied values
148 */
149 std::map<std::string, std::string> GetControlInfo() const override
150 {
151 std::map<std::string, std::string> controlInfo;
152 std::stringstream ss;
153 controlInfo["Name"] = GetName();
154 ss << AudioFrequency.to<double>(); controlInfo["AudioFrequency"] = ss.str(); ss.str(std::string{});
155 return controlInfo;
156 }
157};
158
159}
160}
161}
162
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_RequestControlMusicTone(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double AudioFrequency)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Plays a single tone at the user specified frequency.
Definition: MusicTone.hpp:26
MusicTone & WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
Modifies this Control Request's AudioFrequency parameter and returns itself for method-chaining and e...
Definition: MusicTone.hpp:100
MusicTone & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MusicTone.hpp:119
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: MusicTone.hpp:66
units::frequency::hertz_t AudioFrequency
Sound frequency to play.
Definition: MusicTone.hpp:59
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: MusicTone.hpp:142
MusicTone(units::frequency::hertz_t AudioFrequency)
Plays a single tone at the user specified frequency.
Definition: MusicTone.hpp:90
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MusicTone.hpp:149
std::string ToString() const override
Returns a string representation of the object.
Definition: MusicTone.hpp:129
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MusicTone.hpp:79
Definition: ManualEvent.hpp:12