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.*;
012
013/**
014 * Configs related to the CANdi™ branded device's quadrature interface
015 * using both the S1IN and S2IN inputs
016 * <p>
017 * All the configs related to the quadrature interface for the CANdi™
018 * branded device , including encoder edges per revolution and sensor
019 * direction.
020 */
021public class QuadratureConfigs implements ParentConfiguration
022{
023    /**
024     * The number of quadrature edges in one rotation for the quadrature
025     * sensor connected to the Talon data port.
026     * <p>
027     * This is the total number of transitions from high-to-low or
028     * low-to-high across both channels per rotation of the sensor. This
029     * is also equivalent to the Counts Per Revolution when using 4x
030     * decoding.
031     * <p>
032     * For example, the SRX Mag Encoder has 4096 edges per rotation, and a
033     * US Digital 1024 CPR (Cycles Per Revolution) quadrature encoder has
034     * 4096 edges per rotation.
035     * <p>
036     * On the Talon FXS, this can be at most 2,000,000,000 / Peak RPM.
037     * 
038     * <ul>
039     *   <li> <b>Minimum Value:</b> 1
040     *   <li> <b>Maximum Value:</b> 1000000
041     *   <li> <b>Default Value:</b> 4096
042     *   <li> <b>Units:</b> 
043     * </ul>
044     */
045    public int QuadratureEdgesPerRotation = 4096;
046    /**
047     * Direction of the quadrature sensor to determine positive rotation.
048     * Invert this so that forward motion on the mechanism results in an
049     * increase in quadrature position.
050     * 
051     * <ul>
052     *   <li> <b>Default Value:</b> False
053     * </ul>
054     */
055    public boolean SensorDirection = false;
056    
057    /**
058     * Modifies this configuration's QuadratureEdgesPerRotation parameter and returns itself for
059     * method-chaining and easier to use config API.
060     * <p>
061     * The number of quadrature edges in one rotation for the quadrature
062     * sensor connected to the Talon data port.
063     * <p>
064     * This is the total number of transitions from high-to-low or
065     * low-to-high across both channels per rotation of the sensor. This
066     * is also equivalent to the Counts Per Revolution when using 4x
067     * decoding.
068     * <p>
069     * For example, the SRX Mag Encoder has 4096 edges per rotation, and a
070     * US Digital 1024 CPR (Cycles Per Revolution) quadrature encoder has
071     * 4096 edges per rotation.
072     * <p>
073     * On the Talon FXS, this can be at most 2,000,000,000 / Peak RPM.
074     * 
075     * <ul>
076     *   <li> <b>Minimum Value:</b> 1
077     *   <li> <b>Maximum Value:</b> 1000000
078     *   <li> <b>Default Value:</b> 4096
079     *   <li> <b>Units:</b> 
080     * </ul>
081     *
082     * @param newQuadratureEdgesPerRotation Parameter to modify
083     * @return Itself
084     */
085    public QuadratureConfigs withQuadratureEdgesPerRotation(int newQuadratureEdgesPerRotation)
086    {
087        QuadratureEdgesPerRotation = newQuadratureEdgesPerRotation;
088        return this;
089    }
090    
091    /**
092     * Modifies this configuration's SensorDirection parameter and returns itself for
093     * method-chaining and easier to use config API.
094     * <p>
095     * Direction of the quadrature sensor to determine positive rotation.
096     * Invert this so that forward motion on the mechanism results in an
097     * increase in quadrature position.
098     * 
099     * <ul>
100     *   <li> <b>Default Value:</b> False
101     * </ul>
102     *
103     * @param newSensorDirection Parameter to modify
104     * @return Itself
105     */
106    public QuadratureConfigs withSensorDirection(boolean newSensorDirection)
107    {
108        SensorDirection = newSensorDirection;
109        return this;
110    }
111
112    
113
114    @Override
115    public String toString()
116    {
117        String ss = "Config Group: Quadrature\n";
118        ss += "    QuadratureEdgesPerRotation: " + QuadratureEdgesPerRotation + "\n";
119        ss += "    SensorDirection: " + SensorDirection + "\n";
120        return ss;
121    }
122
123    /**
124     *
125     */
126    public StatusCode deserialize(String to_deserialize)
127    {
128        QuadratureEdgesPerRotation = ConfigJNI.Deserializeint(SpnValue.Config_QuadratureEdgesPerRotation.value, to_deserialize);
129        SensorDirection = ConfigJNI.Deserializeboolean(SpnValue.Config_Quad_SensorDirection.value, to_deserialize);
130        return  StatusCode.OK;
131    }
132
133    /**
134     *
135     */
136    public String serialize()
137    {
138        String ss = "";
139        ss += ConfigJNI.Serializeint(SpnValue.Config_QuadratureEdgesPerRotation.value, QuadratureEdgesPerRotation);
140        ss += ConfigJNI.Serializeboolean(SpnValue.Config_Quad_SensorDirection.value, SensorDirection);
141        return ss;
142    }
143}
144