:py:mod:`phoenix6.swerve.swerve_module` ======================================= .. py:module:: phoenix6.swerve.swerve_module Module Contents --------------- Attributes ~~~~~~~~~~ .. autoapisummary:: phoenix6.swerve.swerve_module.DriveMotorT phoenix6.swerve.swerve_module.SteerMotorT phoenix6.swerve.swerve_module.EncoderT .. py:data:: DriveMotorT .. py:data:: SteerMotorT .. py:data:: EncoderT .. py:class:: SwerveModule(drive_motor_type: Callable[[int, phoenix6.canbus.CANBus], DriveMotorT], steer_motor_type: Callable[[int, phoenix6.canbus.CANBus], SteerMotorT], encoder_type: Callable[[int, phoenix6.canbus.CANBus], EncoderT], constants: phoenix6.swerve.swerve_module_constants.SwerveModuleConstants, canbus: phoenix6.canbus.CANBus, drivetrain_id: int, index: int) Bases: :py:obj:`Generic`\ [\ :py:obj:`DriveMotorT`\ , :py:obj:`SteerMotorT`\ , :py:obj:`EncoderT`\ ] Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices. This class handles the hardware devices and configures them for swerve module operation using the Phoenix 6 API. This class constructs hardware devices internally, so the user only specifies the constants (IDs, PID gains, gear ratios, etc). Getters for these hardware devices are available. :param drive_motor_type: Type of the drive motor :type drive_motor_type: Callable[[int, CANBus], DriveMotorT] :param steer_motor_type: Type of the steer motor :type steer_motor_type: Callable[[int, CANBus], SteerMotorT] :param encoder_type: Type of the azimuth encoder :type encoder_type: Callable[[int, CANBus], EncoderT] :param constants: Constants used to construct the module :type constants: SwerveModuleConstants :param canbus: The CAN bus this module is on :type canbus: CANBus :param drivetrain_id: ID of the swerve drivetrain :type drivetrain_id: int :param index: Index of this swerve module :type index: int .. py:class:: SteerRequestType Bases: :py:obj:`enum.Enum` All possible control requests for the module steer motor. .. py:attribute:: MOTION_MAGIC_EXPO :value: '0' Control the drive motor using a Motion MagicĀ® Expo request. The control output type is determined by SwerveModuleConstants.SteerMotorClosedLoopOutput .. py:attribute:: POSITION :value: '1' Control the drive motor using an unprofiled position request. The control output type is determined by SwerveModuleConstants.SteerMotorClosedLoopOutput .. py:class:: DriveRequestType Bases: :py:obj:`enum.Enum` All possible control requests for the module drive motor. .. py:attribute:: OPEN_LOOP_VOLTAGE :value: '0' Control the drive motor using an open-loop voltage request. .. py:attribute:: VELOCITY :value: '1' Control the drive motor using a velocity closed-loop request. The control output type is determined by SwerveModuleConstants.DriveMotorClosedLoopOutput .. py:class:: ModuleRequest Contains everything the swerve module needs to apply a request. .. py:attribute:: velocity Unoptimized velocity the module should target. .. py:attribute:: wheel_force_feedforward_x :type: phoenix6.units.newton :value: '0.0' Robot-centric wheel force feedforward to apply in the X direction. X is defined as forward according to WPILib convention, so this determines the forward force to apply. This force should include friction applied to the ground. .. py:attribute:: wheel_force_feedforward_y :type: phoenix6.units.newton :value: '0.0' Robot-centric wheel force feedforward to apply in the Y direction. Y is defined as to the left according to WPILib convention, so this determines the force to apply to the left. This force should include friction applied to the ground. .. py:attribute:: drive_request The type of control request to use for the drive motor. .. py:attribute:: steer_request The type of control request to use for the steer motor. .. py:attribute:: update_period :type: second :value: '0.0' The update period of the module request. Setting this to a non-zero value adds a velocity feedforward to the steer motor. .. py:attribute:: enable_foc :value: 'True' When using Voltage-based control, set to true (default) to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%. Set to false to use trapezoidal commutation. This is ignored when using Torque-based control, which always uses FOC. FOC improves motor performance by leveraging torque (current) control. However, this may be inconvenient for applications that require specifying duty cycle or voltage. CTR-Electronics has developed a hybrid method that combines the performances gains of FOC while still allowing applications to provide duty cycle or voltage demand. This not to be confused with simple sinusoidal control or phase voltage control which lacks the performance gains. .. py:method:: with_velocity(new_velocity: SwerveModuleVelocity) -> Self Modifies the velocity parameter and returns itself. Unoptimized velocity the module should target. :param new_velocity: Parameter to modify :type new_velocity: SwerveModuleVelocity :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_wheel_force_feedforward_x(new_wheel_force_feedforward_x: phoenix6.units.newton) -> Self Modifies the wheel_force_feedforward_x parameter and returns itself. Robot-centric wheel force feedforward to apply in the X direction. X is defined as forward according to WPILib convention, so this determines the forward force to apply. This force should include friction applied to the ground. :param wheel_force_feedforward_x: Parameter to modify :type wheel_force_feedforward_x: newton :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_wheel_force_feedforward_y(new_wheel_force_feedforward_y: phoenix6.units.newton) -> Self Modifies the wheel_force_feedforward_y parameter and returns itself. Robot-centric wheel force feedforward to apply in the Y direction. Y is defined as to the left according to WPILib convention, so this determines the force to apply to the left. This force should include friction applied to the ground. :param wheel_force_feedforward_y: Parameter to modify :type wheel_force_feedforward_y: newton :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_drive_request(new_drive_request: SwerveModule) -> Self Modifies the drive_request parameter and returns itself. The type of control request to use for the drive motor. :param new_drive_request: Parameter to modify :type new_drive_request: SwerveModule.DriveRequestType :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_steer_request(new_steer_request: SwerveModule) -> Self Modifies the steer_request parameter and returns itself. The type of control request to use for the steer motor. :param new_steer_request: Parameter to modify :type new_steer_request: SwerveModule.SteerRequestType :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_update_period(new_update_period: second) -> Self Modifies the update_period parameter and returns itself. The update period of the module request. Setting this to a non-zero value adds a velocity feedforward to the steer motor. :param new_update_period: Parameter to modify :type new_update_period: second :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:method:: with_enable_foc(new_enable_foc: bool) -> Self Modifies the enable_foc parameter and returns itself. When using Voltage-based control, set to true (default) to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%. Set to false to use trapezoidal commutation. This is ignored when using Torque-based control, which always uses FOC. FOC improves motor performance by leveraging torque (current) control. However, this may be inconvenient for applications that require specifying duty cycle or voltage. CTR-Electronics has developed a hybrid method that combines the performances gains of FOC while still allowing applications to provide duty cycle or voltage demand. This not to be confused with simple sinusoidal control or phase voltage control which lacks the performance gains. :param new_enable_foc: Parameter to modify :type new_enable_foc: bool :returns: Itself :rtype: SwerveModule.ModuleRequest .. py:property:: drive_closed_loop_output_type :type: phoenix6.swerve.swerve_module_constants.ClosedLoopOutputType Gets the closed-loop output type to use for the drive motor. :returns: Drive motor closed-loop output type :rtype: ClosedLoopOutputType .. py:property:: steer_closed_loop_output_type :type: phoenix6.swerve.swerve_module_constants.ClosedLoopOutputType Gets the closed-loop output type to use for the steer motor. :returns: Steer motor closed-loop output type :rtype: ClosedLoopOutputType .. py:property:: drive_motor :type: DriveMotorT Gets this module's Drive Motor reference. This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself. :returns: This module's Drive Motor reference :rtype: DriveMotorT .. py:property:: steer_motor :type: SteerMotorT Gets this module's Steer Motor reference. This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself. :returns: This module's Steer Motor reference :rtype: SteerMotorT .. py:property:: encoder :type: EncoderT Gets this module's azimuth encoder reference. This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself. :returns: This module's azimuth encoder reference :rtype: EncoderT .. py:method:: apply(module_request: ModuleRequest, /) apply(drive_request: phoenix6.hardware.parent_device.SupportsSendRequest, steer_request: phoenix6.hardware.parent_device.SupportsSendRequest, /) .. py:method:: get_position(refresh: bool) -> SwerveModulePosition Gets the state of this module and passes it back as a SwerveModulePosition object with latency compensated values. This function is blocking when it performs a refresh. :param refresh: True if the signals should be refreshed :type refresh: bool :returns: SwerveModulePosition containing this module's state :rtype: SwerveModulePosition .. py:method:: get_cached_position() -> SwerveModulePosition Gets the last cached swerve module position. This differs from get_position in that it will not perform any latency compensation or refresh the signals. :returns: Last cached SwerveModulePosition :rtype: SwerveModulePosition .. py:method:: get_current_velocity() -> SwerveModuleVelocity Get the current velocity of the module. This is typically used for telemetry, as the SwerveModulePosition is used for odometry. :returns: Current velocity of the module :rtype: SwerveModuleVelocity .. py:method:: get_target_velocity() -> SwerveModuleVelocity Get the target velocity of the module. This is typically used for telemetry. :returns: Target velocity of the module :rtype: SwerveModuleVelocity .. py:method:: reset_position() Resets this module's drive motor position to 0 rotations.