SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
Sensor Class Referenceabstract

#include <Sensor.h>

Inheritance diagram for Sensor:
[legend]

Public Member Functions

virtual float getMechanicalAngle ()
 
virtual float getAngle ()
 
virtual double getPreciseAngle ()
 
virtual float getVelocity ()
 
virtual int32_t getFullRotations ()
 
virtual void update ()
 
virtual int needsSearch ()
 

Public Attributes

float min_elapsed_time = 0.000100
 

Protected Member Functions

virtual float getSensorAngle ()=0
 
virtual void init ()
 

Protected Attributes

float velocity =0.0f
 
float angle_prev =0.0f
 
long angle_prev_ts =0
 
float vel_angle_prev =0.0f
 
long vel_angle_prev_ts =0
 
int32_t full_rotations =0
 
int32_t vel_full_rotations =0
 

Friends

class SmoothingSensor
 

Detailed Description

Sensor abstract class defintion

This class is purposefully kept simple, as a base for all kinds of sensors. Currently we have Encoders, Magnetic Encoders and Hall Sensor implementations. This base class extracts the most basic common features so that a FOC driver can obtain the data it needs for operation.

To implement your own sensors, create a sub-class of this class, and implement the getSensorAngle() method. getSensorAngle() returns a float value, in radians, representing the current shaft angle in the range 0 to 2*PI (one full turn).

To function correctly, the sensor class update() method has to be called sufficiently quickly. Normally, the BLDCMotor's loopFOC() function calls it once per iteration, so you must ensure to call loopFOC() quickly enough, both for correct motor and sensor operation.

The Sensor base class provides an implementation of getVelocity(), and takes care of counting full revolutions in a precise way, but if you wish you can additionally override these methods to provide more optimal implementations for your hardware.

Definition at line 44 of file Sensor.h.

Member Function Documentation

◆ getAngle()

float Sensor::getAngle ( )
virtual

Get current position (in rad) including full rotations and shaft angle. Base implementation uses the values returned by update() so that the same values are returned until update() is called again. Note that this value has limited precision as the number of rotations increases, because the limited precision of float can't capture the large angle of the full rotations and the small angle of the shaft angle at the same time.

Definition at line 79 of file Sensor.cpp.

Here is the caller graph for this function:

◆ getFullRotations()

int32_t Sensor::getFullRotations ( )
virtual

Get the number of full rotations Base implementation uses the values returned by update() so that the same values are returned until update() is called again.

Definition at line 91 of file Sensor.cpp.

◆ getMechanicalAngle()

float Sensor::getMechanicalAngle ( )
virtual

Get mechanical shaft angle in the range 0 to 2PI. This value will be as precise as possible with the hardware. Base implementation uses the values returned by update() so that the same values are returned until update() is called again.

Definition at line 73 of file Sensor.cpp.

Here is the caller graph for this function:

◆ getPreciseAngle()

double Sensor::getPreciseAngle ( )
virtual

On architectures supporting it, this will return a double precision position value, which should have improved precision for large position values. Base implementation uses the values returned by update() so that the same values are returned until update() is called again.

Definition at line 85 of file Sensor.cpp.

◆ getSensorAngle()

virtual float Sensor::getSensorAngle ( )
protectedpure virtual

Get current shaft angle from the sensor hardware, and return it as a float in radians, in the range 0 to 2PI.

This method is pure virtual and must be implemented in subclasses. Calling this method directly does not update the base-class internal fields. Use update() when calling from outside code.

Implemented in Encoder, GenericSensor, HallSensor, MagneticSensorAnalog, MagneticSensorI2C, MagneticSensorPWM, and MagneticSensorSPI.

Here is the caller graph for this function:

◆ getVelocity()

float Sensor::getVelocity ( )
virtual

Get current angular velocity (rad/s) Can be overridden in subclasses. Base implementation uses the values returned by update() so that it only makes sense to call this if update() has been called in the meantime.

get current angular velocity (rad/s)

Reimplemented in Encoder, and HallSensor.

Definition at line 20 of file Sensor.cpp.

Here is the caller graph for this function:

◆ init()

void Sensor::init ( )
protectedvirtual

Call Sensor::init() from your sensor subclass's init method if you want smoother startup The base class init() method calls getSensorAngle() several times to initialize the internal fields to current values, ensuring there is no discontinuity ("jump from zero") during the first calls to sensor.getAngle() and sensor.getVelocity()

Reimplemented in HallSensor, MagneticSensorAnalog, MagneticSensorPWM, Encoder, and GenericSensor.

Definition at line 59 of file Sensor.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ needsSearch()

int Sensor::needsSearch ( )
virtual

returns 0 if it does need search for absolute zero 0 - magnetic sensor (& encoder with index which is found) 1 - ecoder with index (with index not found yet)

Reimplemented in Encoder.

Definition at line 97 of file Sensor.cpp.

Here is the caller graph for this function:

◆ update()

void Sensor::update ( )
virtual

Updates the sensor values by reading the hardware sensor. Some implementations may work with interrupts, and not need this. The base implementation calls getSensorAngle(), and updates internal fields for angle, timestamp and full rotations. This method must be called frequently enough to guarantee that full rotations are not "missed" due to infrequent polling. Override in subclasses if alternative behaviours are required for your sensor hardware.

Reimplemented in Encoder, HallSensor, and MagneticSensorPWM.

Definition at line 7 of file Sensor.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ SmoothingSensor

friend class SmoothingSensor
friend

Definition at line 45 of file Sensor.h.

Member Data Documentation

◆ angle_prev

float Sensor::angle_prev =0.0f
protected

Definition at line 131 of file Sensor.h.

◆ angle_prev_ts

long Sensor::angle_prev_ts =0
protected

Definition at line 132 of file Sensor.h.

◆ full_rotations

int32_t Sensor::full_rotations =0
protected

Definition at line 135 of file Sensor.h.

◆ min_elapsed_time

float Sensor::min_elapsed_time = 0.000100

Minimum time between updates to velocity. If time elapsed is lower than this, the velocity is not updated.

Definition at line 109 of file Sensor.h.

◆ vel_angle_prev

float Sensor::vel_angle_prev =0.0f
protected

Definition at line 133 of file Sensor.h.

◆ vel_angle_prev_ts

long Sensor::vel_angle_prev_ts =0
protected

Definition at line 134 of file Sensor.h.

◆ vel_full_rotations

int32_t Sensor::vel_full_rotations =0
protected

Definition at line 136 of file Sensor.h.

◆ velocity

float Sensor::velocity =0.0f
protected

Definition at line 130 of file Sensor.h.


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