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.phoenix6.configs; 008 009import com.ctre.phoenix6.StatusCode; 010import com.ctre.phoenix6.configs.jni.ConfigJNI; 011import com.ctre.phoenix6.spns.*; 012import com.ctre.phoenix6.signals.*; 013 014/** 015 * Configs that affect the magnet sensor and how to interpret it. 016 * <p> 017 * Includes sensor range, sensor direction, and the magnet offset. 018 */ 019public class MagnetSensorConfigs implements ParentConfiguration 020{ 021 /** 022 * Direction of the sensor to determine positive rotation, as seen 023 * facing the LED side of the CANcoder. 024 * 025 */ 026 public SensorDirectionValue SensorDirection = SensorDirectionValue.CounterClockwise_Positive; 027 /** 028 * This offset is added to the reported position, allowing the 029 * application to trim the zero position. When set to the default 030 * value of zero, position reports zero when magnet north pole aligns 031 * with the LED. 032 * 033 * <ul> 034 * <li> <b>Minimum Value:</b> -1 035 * <li> <b>Maximum Value:</b> 1 036 * <li> <b>Default Value:</b> 0 037 * <li> <b>Units:</b> rotations 038 * </ul> 039 */ 040 public double MagnetOffset = 0; 041 /** 042 * The range of the absolute sensor in rotations, either [-0.5, 0.5) 043 * or [0, 1). 044 * 045 */ 046 public AbsoluteSensorRangeValue AbsoluteSensorRange = AbsoluteSensorRangeValue.Signed_PlusMinusHalf; 047 048 /** 049 * Modifies this configuration's SensorDirection parameter and returns itself for 050 * method-chaining and easier to use config API. 051 * <p> 052 * Direction of the sensor to determine positive rotation, as seen 053 * facing the LED side of the CANcoder. 054 * 055 * 056 * @param newSensorDirection Parameter to modify 057 * @return Itself 058 */ 059 public MagnetSensorConfigs withSensorDirection(SensorDirectionValue newSensorDirection) 060 { 061 SensorDirection = newSensorDirection; 062 return this; 063 } 064 /** 065 * Modifies this configuration's MagnetOffset parameter and returns itself for 066 * method-chaining and easier to use config API. 067 * <p> 068 * This offset is added to the reported position, allowing the 069 * application to trim the zero position. When set to the default 070 * value of zero, position reports zero when magnet north pole aligns 071 * with the LED. 072 * 073 * <ul> 074 * <li> <b>Minimum Value:</b> -1 075 * <li> <b>Maximum Value:</b> 1 076 * <li> <b>Default Value:</b> 0 077 * <li> <b>Units:</b> rotations 078 * </ul> 079 * 080 * @param newMagnetOffset Parameter to modify 081 * @return Itself 082 */ 083 public MagnetSensorConfigs withMagnetOffset(double newMagnetOffset) 084 { 085 MagnetOffset = newMagnetOffset; 086 return this; 087 } 088 /** 089 * Modifies this configuration's AbsoluteSensorRange parameter and returns itself for 090 * method-chaining and easier to use config API. 091 * <p> 092 * The range of the absolute sensor in rotations, either [-0.5, 0.5) 093 * or [0, 1). 094 * 095 * 096 * @param newAbsoluteSensorRange Parameter to modify 097 * @return Itself 098 */ 099 public MagnetSensorConfigs withAbsoluteSensorRange(AbsoluteSensorRangeValue newAbsoluteSensorRange) 100 { 101 AbsoluteSensorRange = newAbsoluteSensorRange; 102 return this; 103 } 104 105 106 107 @Override 108 public String toString() 109 { 110 String ss = "Config Group: MagnetSensor\n"; 111 ss += "Name: \"SensorDirection\" Value: \"" + SensorDirection + "\"" + "\n"; 112 ss += "Name: \"MagnetOffset\" Value: \"" + MagnetOffset + "rotations\"" + "\n"; 113 ss += "Name: \"AbsoluteSensorRange\" Value: \"" + AbsoluteSensorRange + "\"" + "\n"; 114 return ss; 115 } 116 117 /** 118 * 119 */ 120 public StatusCode deserialize(String to_deserialize) 121 { 122 SensorDirection = SensorDirectionValue.valueOf(ConfigJNI.Deserializeint(SpnValue.CANcoder_SensorDirection.value, to_deserialize)); 123 MagnetOffset = ConfigJNI.Deserializedouble(SpnValue.CANCoder_MagnetOffset.value, to_deserialize); 124 AbsoluteSensorRange = AbsoluteSensorRangeValue.valueOf(ConfigJNI.Deserializeint(SpnValue.CANcoder_AbsoluteSensorRange.value, to_deserialize)); 125 return StatusCode.OK; 126 } 127 128 /** 129 * 130 */ 131 public String serialize() 132 { 133 String ss = ""; 134 ss += ConfigJNI.Serializeint(SpnValue.CANcoder_SensorDirection.value, SensorDirection.value); 135 ss += ConfigJNI.Serializedouble(SpnValue.CANCoder_MagnetOffset.value, MagnetOffset); 136 ss += ConfigJNI.Serializeint(SpnValue.CANcoder_AbsoluteSensorRange.value, AbsoluteSensorRange.value); 137 return ss; 138 } 139} 140