robot-rust 0.2.1

The robot-rust project, bindings for wpilib, ctre, and rev in rust.
/*
 * 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);
	};

}

}
}