Esp32-Mcp3008-LineSensor
Library for Espressiff ESP32 based line followers, using the MCP3008 ADC chip.
Classes | Public Member Functions | List of all members
mcp3008::LineSensor Class Reference

The LineSensor device, based on MCP3008 chip. More...

#include <mcp3008_linesensor.h>

Inheritance diagram for mcp3008::LineSensor:
mcp3008::Driver

Classes

struct  CalibrationData
 The LineSensor's calibration data. More...
 

Public Member Functions

struct mcp3008::LineSensor::CalibrationData __attribute__ ((packed))
 
 LineSensor ()
 
virtual ~LineSensor ()
 
LineSensorCalibrator startCalibration ()
 Start the sensor line calibration procedure. More...
 
const CalibrationDatagetCalibration () const
 Get the calibration data used by linesensor, feel free to save this structure somewhere and load it afterwards using setCalibration(). More...
 
bool setCalibration (const CalibrationData &data)
 Set calibration data used by the line sensor. More...
 
float readLine (bool white_line=false, float line_threshold=0.20f) const
 Try to determine a black line's position under the sensors. More...
 
esp_err_t calibratedRead (std::vector< uint16_t > &results) const
 Same as Driver::read(), but returns calibrated result if possible. More...
 
esp_err_t calibratedRead (uint16_t *dest) const
 See the other calibratedRead(std::vector<uint16_t>&) const method. More...
 
uint16_t calibratedReadChannel (uint8_t channel, esp_err_t *result=nullptr) const
 Same as Driver::readChannel(), but returns calibrated data. More...
 
- Public Member Functions inherited from mcp3008::Driver
 Driver ()
 
virtual ~Driver ()
 The uninstall() method is called from the destructor. More...
 
esp_err_t install (const Config &cfg=Config())
 Initialize the SPI bus. Must be called before any other methods, otherwise they will return ESP_FAIL. More...
 
esp_err_t uninstall ()
 Free the SPI bus. Must be called last, or other methods return ESP_FAIL. More...
 
uint8_t getChannelsMask () const
 Get the channel mask, specified in Config::channels_mask. More...
 
esp_err_t read (std::vector< uint16_t > &results, bool differential=false) const
 Read values from the chip. Returns values in range <0; Driver::MAX_VAL>. More...
 
esp_err_t read (uint16_t *dest, bool differential=false) const
 See the other read(std::vector<uint16_t>&, bool) const method. More...
 
uint16_t readChannel (uint8_t channel, bool differential=false, esp_err_t *result=nullptr) const
 Read a single channel from the chip. Returns value is in range <0; Driver::MAX_VAL>. More...
 

Additional Inherited Members

- Static Public Attributes inherited from mcp3008::Driver
static constexpr int CHANNELS = 8
 Amount of channels on the chip. More...
 
static constexpr uint16_t MAX_VAL = 1023
 Maximum value returned by from the chip (10bits). More...
 
- Protected Member Functions inherited from mcp3008::Driver
int requestToChannel (int request) const
 

Detailed Description

The LineSensor device, based on MCP3008 chip.

This class is not thread-safe, you have to make sure the methods are called from one thread at a time only.

Constructor & Destructor Documentation

◆ LineSensor()

mcp3008::LineSensor::LineSensor ( )

◆ ~LineSensor()

mcp3008::LineSensor::~LineSensor ( )
virtual

Member Function Documentation

◆ __attribute__()

struct mcp3008::LineSensor::CalibrationData mcp3008::LineSensor::__attribute__ ( (packed)  )

◆ calibratedRead() [1/2]

esp_err_t mcp3008::LineSensor::calibratedRead ( std::vector< uint16_t > &  results) const

Same as Driver::read(), but returns calibrated result if possible.

Parameters
resultsthe results will be APPENDED to this vector. It will be unchanged unless the ESP_OK result is returned (except possibly its capacity). Between 0 and Driver::CHANNELS values are appended, depending on Config::channels_mask.
Returns
ESP_OK or any error code encountered during reading. Will return ESP_FAIL if called when not installed.

◆ calibratedRead() [2/2]

esp_err_t mcp3008::LineSensor::calibratedRead ( uint16_t *  dest) const

See the other calibratedRead(std::vector<uint16_t>&) const method.

Parameters
destarray MUST be big enough to accomodate all the channels specified by Config::channels_mask!

◆ calibratedReadChannel()

uint16_t mcp3008::LineSensor::calibratedReadChannel ( uint8_t  channel,
esp_err_t *  result = nullptr 
) const

Same as Driver::readChannel(), but returns calibrated data.

Parameters
resultresult code will be written here, may be null.
Returns
measured value or 0xFFFF on error.

◆ getCalibration()

const CalibrationData & mcp3008::LineSensor::getCalibration ( ) const
inline

Get the calibration data used by linesensor, feel free to save this structure somewhere and load it afterwards using setCalibration().

Returns
CalibrationData reference

◆ readLine()

float mcp3008::LineSensor::readLine ( bool  white_line = false,
float  line_threshold = 0.20f 
) const

Try to determine a black line's position under the sensors.

Parameters
white_linethe line is white on black background instead of default black on white background.
line_thresholdvalues above this threshild will be considered "on the line". At least one sensor has to have value above this threshold, otherwise NaN is returned. The value is a fraction, 0.20 == 20% == (1023*0.20) == 200.
Returns
A number in range <-1,1>, where -1 means the line is under the channel with smallest ID, 0 means in the middle and 1 means it is under the one with the greatest ID. Examples, assuming using 8 channels:
-1.0: under channel 0
0.0: under channel 3-4
1.0: under channel 7
Returns NaN when the line is not found (see line_threshold).

◆ setCalibration()

bool mcp3008::LineSensor::setCalibration ( const CalibrationData data)

Set calibration data used by the line sensor.

Parameters
datathe calibration data obtained previously from getCalibration().
Returns
if the result is false, calibration data were invalid (out of range)

◆ startCalibration()

LineSensorCalibrator mcp3008::LineSensor::startCalibration ( )

Start the sensor line calibration procedure.

Returns
LineSensorCalibrator object used for calibration.

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