001/* 002 * Copyright (C) Cross The Road Electronics. All rights reserved. 003 * License information can be found in CTRE_LICENSE.txt 004 * For support and suggestions contact support@ctr-electronics.com or file 005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases 006 */ 007package com.ctre.phoenixpro.sim; 008 009import com.ctre.phoenix6.StatusCode; 010import com.ctre.phoenixpro.hardware.core.CoreCANcoder; 011import com.ctre.phoenixpro.hardware.CANcoder; 012import com.ctre.phoenix6.jni.PlatformJNI; 013 014/** 015 * Class to control the state of a simulated {@link CANcoder}. 016 * 017 * @deprecated Classes in the phoenixpro package will be removed in 2024. 018 * Users should instead use classes from the phoenix6 package. 019 */ 020@Deprecated(forRemoval = true) 021public class CANcoderSimState { 022 private final DeviceType kDevType = DeviceType.PRO_CANcoderType; 023 024 private final int _id; 025 026 /** 027 * The orientation of the CANcoder relative to the robot chassis. 028 * <p> 029 * This value should not be changed based on the CANcoder invert. 030 * Rather, this value should be changed when the mechanical linkage 031 * between the CANcoder and the robot changes. 032 */ 033 public ChassisReference Orientation; 034 035 /** 036 * Creates an object to control the state of the given {@link CANcoder}. 037 * <p> 038 * This constructor defaults to a counter-clockwise positive orientation 039 * relative to the robot chassis. 040 * 041 * @param device 042 * Device to which this simulation state is attached 043 * 044 * @deprecated Classes in the phoenixpro package will be removed in 2024. 045 * Users should instead use classes from the phoenix6 package. 046 */ 047 @Deprecated(forRemoval = true) 048 public CANcoderSimState(CoreCANcoder device) { 049 this(device, ChassisReference.CounterClockwise_Positive); 050 } 051 /** 052 * Creates an object to control the state of the given {@link CANcoder}. 053 * 054 * @param device 055 * Device to which this simulation state is attached 056 * @param orientation 057 * Orientation of the device relative to the robot chassis 058 * 059 * @deprecated Classes in the phoenixpro package will be removed in 2024. 060 * Users should instead use classes from the phoenix6 package. 061 */ 062 @Deprecated(forRemoval = true) 063 public CANcoderSimState(CoreCANcoder device, ChassisReference orientation) { 064 _id = device.getDeviceID(); 065 Orientation = orientation; 066 } 067 068 /** 069 * Sets the simulated supply voltage of the CANcoder. 070 * <p> 071 * The minimum allowed supply voltage is 4 V - values below this 072 * will be promoted to 4 V. 073 * 074 * @param volts 075 * The supply voltage in Volts 076 * @return Status code 077 */ 078 public StatusCode setSupplyVoltage(double volts) { 079 return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "SupplyVoltage", volts)); 080 } 081 /** 082 * Sets the simulated raw position of the CANcoder. 083 * <p> 084 * Inputs to this function over time should be continuous, as user calls of {@link CANcoder#setPosition} will be accounted for in the callee. 085 * <p> 086 * The CANcoder integrates this to calculate the true reported position. 087 * <p> 088 * When using the WPI Sim GUI, you will notice a readonly {@code position} and settable {@code rawPositionInput}. 089 * The readonly signal is the emulated position which will match self-test in Tuner and the hardware API. 090 * Changes to {@code rawPositionInput} will be integrated into the emulated position. 091 * This way a simulator can modify the position without overriding hardware API calls for home-ing the sensor. 092 * 093 * @param rotations 094 * The raw position in rotations 095 * @return Status code 096 */ 097 public StatusCode setRawPosition(double rotations) { 098 if (Orientation == ChassisReference.Clockwise_Positive) { 099 rotations = -rotations; 100 } 101 return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "RawPosition", rotations)); 102 } 103 /** 104 * Adds to the simulated position of the CANcoder. 105 * 106 * @param dRotations 107 * The change in position in rotations 108 * @return Status code 109 */ 110 public StatusCode addPosition(double dRotations) { 111 if (Orientation == ChassisReference.Clockwise_Positive) { 112 dRotations = -dRotations; 113 } 114 return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "AddPosition", dRotations)); 115 } 116 /** 117 * Sets the simulated velocity of the CANcoder. 118 * 119 * @param rps 120 * The new velocity in rotations per second 121 * @return Status code 122 */ 123 public StatusCode setVelocity(double rps) { 124 if (Orientation == ChassisReference.Clockwise_Positive) { 125 rps = -rps; 126 } 127 return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "Velocity", rps)); 128 } 129}