SPARK MAX - C++ Documentation
rev::CANEncoder Class Reference

Inherits rev::CANSensor.

Public Types

enum  EncoderType { kNoSensor = 0, kHallSensor = 1, kQuadrature = 2, kSensorless = 3 }
 
enum  AlternateEncoderType { kQuadrature }
 

Public Member Functions

 CANEncoder (CANSparkMax &device, EncoderType sensorType=CANEncoder::EncoderType::kHallSensor, int counts_per_rev=42)
 
 CANEncoder (CANSparkMax &device, AlternateEncoderType sensorType, int counts_per_rev)
 
 CANEncoder (CANEncoder &&rhs)
 
CANEncoderoperator= (CANEncoder &&rhs)
 
double GetPosition ()
 
double GetVelocity ()
 
CANError SetPosition (double position)
 
CANError SetPositionConversionFactor (double factor)
 
CANError SetVelocityConversionFactor (double factor)
 
double GetPositionConversionFactor ()
 
double GetVelocityConversionFactor ()
 
CANError SetAverageDepth (uint32_t depth)
 
CANError SetMeasurementPeriod (uint32_t period_ms)
 
uint32_t GetAverageDepth ()
 
uint32_t GetMeasurementPeriod ()
 
uint32_t GetCPR ()
 
uint32_t GetCountsPerRevolution ()
 
CANError SetInverted (bool inverted) override
 
bool GetInverted () const override
 
CANError GetLastError ()
 
- Public Member Functions inherited from rev::CANSensor
 CANSensor (CANSparkMax &device)
 

Protected Member Functions

int GetID () const override
 

Additional Inherited Members

- Protected Types inherited from rev::CANSensor
enum  FeedbackSensorType {
  kNoSensor = 0, kHallSensor = 1, kQuadrature = 2, kSensorless = 3,
  kAnalog = 4, kAltQuadrature = 5
}
 
- Protected Attributes inherited from rev::CANSensor
CANSparkMaxm_device
 

Constructor & Destructor Documentation

◆ CANEncoder() [1/2]

CANEncoder::CANEncoder ( CANSparkMax device,
EncoderType  sensorType = CANEncoder::EncoderType::kHallSensor,
int  counts_per_rev = 42 
)
explicit

Constructs a CANEncoder.

Parameters
deviceThe Spark Max to which the encoder is attached.
sensorTypeThe encoder type for the motor: kHallEffect or kQuadrature
counts_per_revThe counts per revolution of the encoder

◆ CANEncoder() [2/2]

CANEncoder::CANEncoder ( CANSparkMax device,
AlternateEncoderType  sensorType,
int  counts_per_rev 
)
explicit

Constructs a CANEncoder from the alternate encoder port

Parameters
deviceThe Spark Max to which the encoder is attached.
sensorTypeThe alternate encoder type for the motor: kQuadrature
counts_per_revThe counts per revolution of the encoder

Member Function Documentation

◆ GetAverageDepth()

uint32_t CANEncoder::GetAverageDepth ( )

Get the averafe sampling depth for a quadrature encoder.

Returns
The average sampling depth

◆ GetCountsPerRevolution()

uint32_t CANEncoder::GetCountsPerRevolution ( )

Get the counts per revolution of the quadrature encoder.

For a description on the difference between CPR, PPR, etc. go to https://www.cuidevices.com/blog/what-is-encoder-ppr-cpr-and-lpr

Returns
Counts per revolution

◆ GetCPR()

uint32_t CANEncoder::GetCPR ( )

Get the counts per revolution of the quadrature encoder. This is the same as GetCountsPerRevolution

For a description on the difference between CPR, PPR, etc. go to https://www.cuidevices.com/blog/what-is-encoder-ppr-cpr-and-lpr

Returns
Counts per revolution

◆ GetID()

int CANEncoder::GetID ( ) const
overrideprotectedvirtual

Get the ID of the sensor that is connected to the SparkMax through the encoder port on the front of the controller (not the top port).

Returns
The ID of the sensor

Implements rev::CANSensor.

◆ GetInverted()

bool CANEncoder::GetInverted ( ) const
overridevirtual

Get the phase of the CANSensor. This will just return false if the user tries to get inverted while the SparkMax is Brushless and using the hall effect sensor.

Returns
The phase of the encoder

Implements rev::CANSensor.

◆ GetLastError()

CANError rev::CANEncoder::GetLastError ( )

Returns the last error generated

◆ GetMeasurementPeriod()

uint32_t CANEncoder::GetMeasurementPeriod ( )

Get the number of samples for reading from a quadrature encoder. This value sets the number of samples in the average for velocity readings.

Returns
Measurement period in microseconds

◆ GetPosition()

double CANEncoder::GetPosition ( )

Get the position of the motor. This returns the native units of 'rotations' by default, and can be changed by a scale factor using setPositionConversionFactor().

Returns
Number of rotations of the motor

◆ GetPositionConversionFactor()

double CANEncoder::GetPositionConversionFactor ( )

Get the conversion factor for position of the encoder. Multiplied by the native output units to give you position

Returns
The conversion factor for position

◆ GetVelocity()

double CANEncoder::GetVelocity ( )

Get the velocity of the motor. This returns the native units of 'RPM' by default, and can be changed by a scale factor using setVelocityConversionFactor().

Returns
Number the RPM of the motor

◆ GetVelocityConversionFactor()

double CANEncoder::GetVelocityConversionFactor ( )

Get the conversion factor for velocity of the encoder. Multiplied by the native output units to give you velocity

Returns
The conversion factor for velocity

◆ SetAverageDepth()

CANError CANEncoder::SetAverageDepth ( uint32_t  depth)

Set the average sampling depth for a quadrature encoder. This value sets the number of samples in the average for velocity readings. This can be any value from 1 to 64.

When the SparkMax controller is in Brushless mode, this will not change any behavior.

Parameters
depthThe average sampling depth between 1 and 64 (default)
Returns
CANError.kOK if successful

◆ SetInverted()

CANError CANEncoder::SetInverted ( bool  inverted)
overridevirtual

Set the phase of the CANSensor so that it is set to be in phase with the motor itself. This only works for quadrature encoders. This will throw an error if the user tries to set inverted while the SparkMax is Brushless and using the hall effect sensor.

Parameters
invertedThe phase of the encoder
Returns
CANError.kOK if successful

Implements rev::CANSensor.

◆ SetMeasurementPeriod()

CANError CANEncoder::SetMeasurementPeriod ( uint32_t  period_ms)

Set the measurement period for velocity measurements of a quadrature encoder. When the SparkMax controller is in Brushless mode, this will not change any behavior.

The basic formula to calculate velocity is change in positon / change in time. This parameter sets the change in time for measurement.

Parameters
period_usMeasurement period in milliseconds. This number may be between 1 and 100 (default).
Returns
CANError.kOK if successful

◆ SetPosition()

CANError CANEncoder::SetPosition ( double  position)

Set the position of the encoder.

Parameters
positionNumber of rotations of the motor
Returns
CANError Set to CANError.kOK if successful

◆ SetPositionConversionFactor()

CANError CANEncoder::SetPositionConversionFactor ( double  factor)

Set the conversion factor for position of the encoder. Multiplied by the native output units to give you position

Parameters
factorThe conversion factor to multiply the native units by
Returns
CANError Set to CANError.kOK if successful

◆ SetVelocityConversionFactor()

CANError CANEncoder::SetVelocityConversionFactor ( double  factor)

Set the conversion factor for velocity of the encoder. Multiplied by the native output units to give you velocity

Parameters
factorThe conversion factor to multiply the native units by
Returns
CANError Set to CANError.kOK if successful

The documentation for this class was generated from the following files: