1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) Cross The Road Electronics. All rights reserved.
* License information can be found in CTRE_LICENSE.txt
* For support and suggestions contact support@ctr-electronics.com or file
* an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
*/
#pragma once
#include "ctre/phoenix/StatusCodes.h"
#include "ctre/phoenix6/sim/ChassisReference.hpp"
#include <units/angle.h>
#include <units/angular_acceleration.h>
#include <units/angular_velocity.h>
#include <units/current.h>
#include <units/voltage.h>
namespace ctre {
namespace phoenix6 {
namespace hardware {
namespace core {
/* forward proto */
class CoreTalonFX;
}
}
namespace sim {
/**
* \brief Class to control the state of a simulated hardware#TalonFX.
*
* For simulated PID control and current limits to behave correctly,
* #SetRawRotorPosition and #SetRotorVelocity must be updated
* periodically. This is typically done by sending the motor output from
* #GetMotorVoltage() or #GetTorqueCurrent() to a physics simulator
* (such as WPILib's `DCMotorSim`), which then calculates the new motor
* position and velocity.
*/
class TalonFXSimState
{
private:
int _id;
public:
/**
* \brief Represents the type of motor connected to the simulated Talon FX.
*/
enum class MotorType {
/** \brief WCP Kraken X60 */
KrakenX60 = 0,
/** \brief WCP Kraken X44 */
KrakenX44 = 1,
};
/**
* \brief The orientation of the TalonFX relative to the robot chassis.
*
* This value should not be changed based on the TalonFX invert.
* Rather, this value should be changed when the mechanical linkage
* between the TalonFX and the robot changes.
*/
ChassisReference Orientation;
/**
* \brief Creates an object to control the state of the given hardware#TalonFX.
*
* \details This constructor defaults to a counter-clockwise positive orientation
* relative to the robot chassis. Note the recommended method of accessing
* simulation features is to use hardware#TalonFX#GetSimState.
*
* \param device Device to which this simulation state is attached
*/
TalonFXSimState(hardware::core::CoreTalonFX const &device) :
TalonFXSimState{device, ChassisReference::CounterClockwise_Positive}
{}
/**
* \brief Creates an object to control the state of the given hardware#TalonFX.
*
* \details Note the recommended method of accessing simulation features is to
* use hardware#TalonFX#GetSimState.
*
* \param device Device to which this simulation state is attached
* \param orientation Orientation of the device relative to the robot chassis
*/
TalonFXSimState(hardware::core::CoreTalonFX const &device, ChassisReference orientation);
/* disallow copy, allow move */
TalonFXSimState(TalonFXSimState const &) = delete;
TalonFXSimState(TalonFXSimState &&) = default;
TalonFXSimState &operator=(TalonFXSimState const &) = delete;
TalonFXSimState &operator=(TalonFXSimState &&) = default;
/**
* \brief Sets the type of motor connected to the simulated Talon FX.
*
* \param motorType The type of motor connected
* \returns Status code
*/
ctre::phoenix::StatusCode SetMotorType(MotorType motorType);
/**
* \brief Gets the last status code generated by a simulation function.
*
* \details Not all functions return a status code but can potentially report errors.
* This function can be used to retrieve those status codes.
*
* \returns Last status code generated by a simulation function
*/
ctre::phoenix::StatusCode GetLastStatusCode() const;
/**
* \brief Gets the simulated output voltage of the motor.
*
* \returns Voltage applied to the motor in Volts
*/
units::voltage::volt_t GetMotorVoltage() const;
/**
* \brief Gets the simulated output torque current of the motor.
*
* \details Phoenix 6 simulation automatically calculates current.
*
* \returns Torque current applied to the motor in Amperes
*/
units::current::ampere_t GetTorqueCurrent() const;
/**
* \brief Gets the simulated supply current of the TalonFX.
*
* \details Phoenix 6 simulation automatically calculates current.
*
* \returns Supply current of the TalonFX in Amperes
*/
units::current::ampere_t GetSupplyCurrent() const;
/**
* \brief Sets the simulated supply voltage of the TalonFX.
*
* \details The minimum allowed supply voltage is 4 V - values below this
* will be promoted to 4 V.
*
* \param volts The supply voltage in Volts
* \returns Status code
*/
ctre::phoenix::StatusCode SetSupplyVoltage(units::voltage::volt_t volts);
/**
* \brief Sets the simulated forward limit switch of the TalonFX.
*
* \param closed Whether the limit switch is closed
* \returns Status code
*/
ctre::phoenix::StatusCode SetForwardLimit(bool closed);
/**
* \brief Sets the simulated reverse limit switch of the TalonFX.
*
* \param closed Whether the limit switch is closed
* \returns Status code
*/
ctre::phoenix::StatusCode SetReverseLimit(bool closed);
/**
* \brief Sets the simulated raw rotor position of the TalonFX. This is the position
* of the rotor (before gear ratio) used for the RotorSensor feedback source.
*
* Inputs to this function over time should be continuous, as user calls of hardware#TalonFX#SetPosition will be accounted for in the callee.
*
* \details The TalonFX integrates this to calculate the true reported rotor position.
*
* When using the WPI Sim GUI, you will notice a readonly `position` and settable `rawPositionInput`.
* The readonly signal is the emulated position which will match self-test in Tuner and the hardware API.
* Changes to `rawPositionInput` will be integrated into the emulated position.
* This way a simulator can modify the position without overriding hardware API calls for home-ing the sensor.
*
* \param rotations The raw position in rotations
* \returns Status code
*/
ctre::phoenix::StatusCode SetRawRotorPosition(units::angle::turn_t rotations);
/**
* \brief Adds to the simulated rotor position of the TalonFX. This adds to the position
* of the rotor (before gear ratio) used for the RotorSensor feedback source.
*
* \param dRotations The change in position in rotations
* \returns Status code
*/
ctre::phoenix::StatusCode AddRotorPosition(units::angle::turn_t dRotations);
/**
* \brief Sets the simulated rotor velocity of the TalonFX. This is the velocity
* of the rotor (before gear ratio) used for the RotorSensor feedback source.
*
* \param rps The new velocity in rotations per second
* \returns Status code
*/
ctre::phoenix::StatusCode SetRotorVelocity(units::angular_velocity::turns_per_second_t rps);
/**
* \brief Sets the simulated rotor acceleration of the TalonFX. This is the acceleration
* of the rotor (before gear ratio) used for the RotorSensor feedback source.
*
* \param rpss The new acceleration in rotations per second²
* \returns Status code
*/
ctre::phoenix::StatusCode SetRotorAcceleration(units::angular_acceleration::turns_per_second_squared_t rpss);
};
}
}
}