Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
sensor::vl6180::LaeVL6180Mux Class Reference

VL6180 Time of Flight Class. More...

#include <laeVL6180.h>

Public Member Functions

 LaeVL6180Mux (laelaps::LaeI2CMux &mux, int nChan, double fBeamDir, double fDeadzone, const std::string &strNameId="VL6180", const std::string &strDesc="Time-of-Flight range sensor")
 Intialization constructor. More...
 
 LaeVL6180Mux (const LaeVL6180Mux &src)
 Copy constructor. More...
 
virtual ~LaeVL6180Mux ()
 Destructor.
 
int initSensor (bool bForce=false)
 Initialize sensor with recommended settings and defaults. More...
 
int writeDefaults ()
 Write defaults to sensor. More...
 
void readShadowRegs ()
 Read shadows register values and update derived data.
 
void readShadowRegs (byte_t &regRangeOffset, u16_t &regRangeCrossTalk, byte_t &regAlsGain, u16_t &regAlsIntPeriod)
 Read shadows register values, update derived data, and return registers values. More...
 
int tune (uint_t uRangeOffset, uint_t uRangeCrossTalk, double fAlsGain, uint_t uAlsIntPeriod)
 Tune sensor configuration. More...
 
int readId (struct VL6180xIdentification &id)
 Read sensor identification. More...
 
double measureRange (u32_t msecWait=0xffff)
 Measure the sensed target range. More...
 
double measureAmbientLight (u32_t msecWait=0xffff)
 Measure the sensed ambient light illuminance. More...
 
void getMeasurements (double &fRange, double &fAmbientLight)
 Get last sensed measurements. More...
 
std::string getNameId ()
 Get sensor assigned name id. More...
 
std::string getDesc ()
 Get sensor short description. More...
 
double getBeamDir ()
 Get sensor direction of the center of emitter IR beam. More...
 
double getFoV ()
 Get sensor field of view. More...
 
void getMinMax (double &fMin, double &fMax)
 Get sensor's minimum and maximum range. More...
 
std::string getRadiationType ()
 Get radiation type. More...
 
int calibOffset (int &nOffsetPre, double &fAvgPre, int &nOffsetPost, double &fAvgPost)
 Calibrate part-to-part offset. More...
 
int calibCrossTalk (int &nCrossTalk)
 Calibrate cross-talk compensation. More...
 
int readReg8 (u16_t reg, byte_t &val)
 Read an 8-bit value from a register. More...
 
int readReg16 (u16_t reg, u16_t &val)
 Read a 16-bit value from a register. More...
 
int writeReg8 (u16_t reg, byte_t val)
 Write an 8-bit value to a register. More...
 
int writeReg16 (u16_t reg, u16_t val)
 Write a 16-bit value to a register. More...
 

Static Public Member Functions

static double gainEnumToAnalog (vl6180x_als_gain eAlsGain)
 Convert ambient light sensor gain register value enum to analog gain. More...
 
static vl6180x_als_gain gainAnalogToEnum (double fAlsGain)
 Convert ambient light sensor analog gain to associated register value enum. More...
 

Static Public Attributes

static const int NSenseErrorsThreshold = 10
 Consecutive sensed error count threshold. More...
 

Protected Member Functions

void lock ()
 Lock the share resource. More...
 
void unlock ()
 Unlock the shared resource. More...
 
int outOfResetInit ()
 Initialize sensor out-of-reset. More...
 
u32_t waitForSensorReady (u16_t regStatus, u32_t msecWait)
 Wait for sensor to be ready. More...
 
u32_t waitForSensorMeasurement (u32_t msecWait, byte_t bitDone)
 Wait for sensor measurement to complete. More...
 

Protected Attributes

laelaps::LaeI2CMuxm_mux
 I2C multiplexor.
 
int m_nChan
 multiplexed channel number
 
double m_fBeamDir
 center of beam direction(radians)
 
double m_fDeadzone
 sensor deadzone (m)
 
std::string m_strNameId
 name identifier of sensor
 
std::string m_strDesc
 short description
 
int m_nErrorCnt
 consecutive error count
 
bool m_bBlackListed
 sensor is [not] black listed
 
double m_fAlsGain
 ambient light sensor analog gain
 
uint_t m_uAlsIntPeriod
 ALS integration period (msec)
 
byte_t m_regRangeOffset
 range part-to-part offset register
 
u16_t m_regRangeCrossTalk
 range cross-talk register
 
byte_t m_regAlsGain
 ambient light sensor gain register
 
u16_t m_regAlsIntPeriod
 ALS itegration period register.
 
double m_fRange
 range (m)
 
double m_fAmbientLight
 ambient light (lux)
 
pthread_mutex_t m_mutex
 mutex
 

Detailed Description

VL6180 Time of Flight Class.

The interface between the processor and the sensor is through an I2C mulitplexer.

Definition at line 368 of file laeVL6180.h.

Constructor & Destructor Documentation

sensor::vl6180::LaeVL6180Mux::LaeVL6180Mux ( laelaps::LaeI2CMux mux,
int  nChan,
double  fBeamDir,
double  fDeadzone,
const std::string &  strNameId = "VL6180",
const std::string &  strDesc = "Time-of-Flight range sensor" 
)

Intialization constructor.

Parameters
muxI2C multiplexer switch.
nChanMulitplexed channel number.
fBeamDirCenter of IR beam direction (radians).
fDeadzoneSense deadzone range.
strNameIdName identifier.
strDescShort description (e.g. location).
LaeVL6180Mux::LaeVL6180Mux ( const LaeVL6180Mux src)

Copy constructor.

Parameters
srcSource object.

Definition at line 209 of file laeVL6180.cxx.

References m_bBlackListed, m_fAlsGain, m_fAmbientLight, m_fRange, m_mutex, m_nErrorCnt, m_regAlsGain, m_regAlsIntPeriod, m_regRangeCrossTalk, m_regRangeOffset, and m_uAlsIntPeriod.

209  :
210  m_mux(src.m_mux), m_nChan(src.m_nChan),
213 {
214  m_nErrorCnt = src.m_nErrorCnt;
216  m_fAlsGain = src.m_fAlsGain;
222  m_fRange = src.m_fRange;
224 
225  pthread_mutex_destroy(&m_mutex);
226 }
uint_t m_uAlsIntPeriod
ALS integration period (msec)
Definition: laeVL6180.h:695
pthread_mutex_t m_mutex
mutex
Definition: laeVL6180.h:708
u16_t m_regAlsIntPeriod
ALS itegration period register.
Definition: laeVL6180.h:701
laelaps::LaeI2CMux & m_mux
I2C multiplexor.
Definition: laeVL6180.h:684
double m_fAmbientLight
ambient light (lux)
Definition: laeVL6180.h:705
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
byte_t m_regAlsGain
ambient light sensor gain register
Definition: laeVL6180.h:700
int m_nErrorCnt
consecutive error count
Definition: laeVL6180.h:690
double m_fBeamDir
center of beam direction(radians)
Definition: laeVL6180.h:686
double m_fDeadzone
sensor deadzone (m)
Definition: laeVL6180.h:687
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
double m_fAlsGain
ambient light sensor analog gain
Definition: laeVL6180.h:694
bool m_bBlackListed
sensor is [not] black listed
Definition: laeVL6180.h:691
u16_t m_regRangeCrossTalk
range cross-talk register
Definition: laeVL6180.h:699
double m_fRange
range (m)
Definition: laeVL6180.h:704
byte_t m_regRangeOffset
range part-to-part offset register
Definition: laeVL6180.h:698

Member Function Documentation

int LaeVL6180Mux::calibCrossTalk ( int &  nCrossTalk)

Calibrate cross-talk compensation.

The offset is stored in register SYSRANGE_CROSSTALK_COMPENSATION_RATE (0x001e).

See also
Section 2.12.4 in datasheet for setup and procedures.
Parameters
[out]nCrossTalkCross-talk compensation written to compensation register.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 1037 of file laeVL6180.cxx.

References m_regRangeCrossTalk, measureRange(), readReg8(), VL6180X_RANGE_MAX, and writeReg16().

1038 {
1039  u16_t regComp = VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE;
1040  u16_t regRate = VL6180X_RESULT_RANGE_RETURN_RATE;
1041  double fBlackTgt = 0.100; // required black target distance
1042  int minGood = 10; // minimum number of measurement to be valid
1043  int maxIters = 20; // should >= min as per data sheet
1044 
1045  byte_t val; // register value
1046  double fRate; // range return rate measurement
1047  double fSumRate; // return rate sum
1048  double fAvgRate; // return rate average
1049  double fRange; // range measurement
1050  double fSumRange; // range sum
1051  double fAvgRange; // range average
1052  int nMeasured; // number of sucessful range measurements
1053  int i; // iterator
1054  int rc; // return code
1055 
1056  //
1057  // Clear compensation register.
1058  //
1059  if( (rc = writeReg16(regComp, 0)) == LAE_OK )
1060  {
1061  m_regRangeCrossTalk = 0; // update shadow register
1062  }
1063  else
1064  {
1065  LOGDIAG3("Failed to write SYSRANGE_CROSSTALK_COMPENSATION_RATE (0x%04x) "
1066  "register. Cannot proceed with calibration.",
1067  regComp);
1068  return rc;
1069  }
1070 
1071  //
1072  // Make set of measurements.
1073  //
1074  for(i = 0, fSumRange = 0.0, fSumRate = 0.0, nMeasured = 0; i<maxIters; ++i)
1075  {
1076  fRange = measureRange();
1077  rc = readReg8(regRate, val);
1078 
1079  if( (fRange >= 0.0) && (fRange <= VL6180X_RANGE_MAX) && (rc == LAE_OK) )
1080  {
1081  fSumRange += fRange;
1082  fSumRate += (double)val;
1083  ++nMeasured;
1084  }
1085  }
1086 
1087  //
1088  // Not of enough good measurements to determine calibration offset.
1089  //
1090  if( nMeasured < minGood )
1091  {
1092  LOGDIAG3("Made %d/%d rate/range meassurements. "
1093  "A minimum of %d are required."
1094  "Cannot proceed with calibration.",
1095  nMeasured, minGood);
1096  return -LAE_ECODE_IO;
1097  }
1098 
1099  fAvgRange = fSumRange / (double)nMeasured;
1100  fAvgRate = fSumRate / (double)nMeasured;
1101 
1102  val = (byte_t)(fAvgRate * (1.0 - fAvgRange/fBlackTgt));
1103 
1104  //
1105  // Write new compensation value.
1106  //
1107  if( (rc = writeReg16(regComp, val)) == LAE_OK )
1108  {
1109  m_regRangeCrossTalk = val; // update shadow register
1110  }
1111  else
1112  {
1113  LOGDIAG3("Failed to write new SYSRANGE_CROSSTALK_COMPENSATION_RATE (0x%04x)"
1114  " register value. Calibration failed.",
1115  regComp);
1116 
1117  return rc;
1118  }
1119 
1120  nCrossTalk = (int)val & 0x0ff;
1121 
1122  return LAE_OK;
1123 }
#define VL6180X_RANGE_MAX
maximum range (m)
Definition: laeVL6180.h:131
int writeReg16(u16_t reg, u16_t val)
Write a 16-bit value to a register.
Definition: laeVL6180.cxx:1243
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
u16_t m_regRangeCrossTalk
range cross-talk register
Definition: laeVL6180.h:699
double measureRange(u32_t msecWait=0xffff)
Measure the sensed target range.
Definition: laeVL6180.cxx:547
int LaeVL6180Mux::calibOffset ( int &  nOffsetPre,
double &  fAvgPre,
int &  nOffsetPost,
double &  fAvgPost 
)

Calibrate part-to-part offset.

The offset is stored in register SYSRANGE_PART_TO_PART_RANGE_OFFSET (0x0024).

See also
Section 2.12.3 in datasheet for setup and procedures.
Parameters
[out]nOffsetPreCurrent offset register value.
[out]fAvgPreAverage measured range on pre-calibrated sensor.
[out]nOffsetPostPost-procedure offset value written to offset register. If equal to nOffsetPre, no calibration was required.
[out]fAvgPostAverage measured range after new offset written.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 878 of file laeVL6180.cxx.

References m_regRangeOffset, measureRange(), readReg8(), VL6180X_RANGE_MAX, and writeReg8().

882 {
883  u16_t regOffset = VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET;
884  double fWhiteTgt = 0.050; // required white target distance
885  double fRes = 0.003; // range plus/minus resolution is best possible
886  int minGood = 10; // minimum number of measurement to be valid
887  int maxIters = 20; // should >= min as per data sheet
888 
889  s8_t offset; // part-to-part offset in 2's compliment
890  double fRange; // range measurement
891  double fSum; // sum
892  int nMeasured; // number of sucessful range measurements
893  int i; // iterator
894  int rc; // return code
895 
896  //
897  // Read current, pre-calibrated offset register value.
898  //
899  if( (rc = readReg8(regOffset, (byte_t &)offset)) == LAE_OK )
900  {
901  m_regRangeOffset = (byte_t)offset; // update shadow register
902  }
903  else
904  {
905  LOGDIAG3("Failed to read SYSRANGE_PART_TO_PART_RANGE_OFFSET (0x%04x) "
906  "register. Cannot proceed with calibration.",
907  regOffset);
908  return rc;
909  }
910 
911  //
912  // Make a set of measurements to gather performance statistics.
913  //
914  for(i = 0, fSum = 0.0, nMeasured = 0; i<maxIters; ++i)
915  {
916  fRange = measureRange();
917  if( (fRange >= 0.0) && (fRange <= VL6180X_RANGE_MAX) )
918  {
919  fSum += fRange;
920  ++nMeasured;
921  }
922  }
923 
924  //
925  // Not of enough good measurements to determine performance.
926  //
927  if( nMeasured < minGood )
928  {
929  LOGDIAG3("Made %d/%d range meassurements. A minimum of %d are required."
930  "Cannot proceed with calibration.",
931  nMeasured, minGood);
932  return -LAE_ECODE_IO;
933  }
934 
935  nOffsetPre = (int)offset;
936  fAvgPre = fSum / (double)nMeasured;
937 
938  //
939  // No part-to-part offset calibaration required.
940  //
941  if( (fAvgPre >= fWhiteTgt-fRes) && (fAvgPre <= fWhiteTgt+fRes) )
942  {
943  nOffsetPost = nOffsetPre;
944  fAvgPost = fAvgPre;
945  return LAE_OK;
946  }
947 
948  //
949  // Clear offset register.
950  //
951  if( (rc = writeReg8(regOffset, 0)) == LAE_OK )
952  {
953  m_regRangeOffset = 0; // update shadow register
954  }
955  else
956  {
957  LOGDIAG3("Failed to write SYSRANGE_PART_TO_PART_RANGE_OFFSET (0x%04x) "
958  "register. Cannot proceed with calibration.",
959  regOffset);
960  return rc;
961  }
962 
963  //
964  // Make another set of measurements.
965  //
966  for(i = 0, fSum = 0.0, nMeasured = 0; i<maxIters; ++i)
967  {
968  fRange = measureRange();
969  if( (fRange >= 0.0) && (fRange <= VL6180X_RANGE_MAX) )
970  {
971  fSum += fRange;
972  ++nMeasured;
973  }
974  }
975 
976  //
977  // Not of enough good measurements to determine calibration offset.
978  //
979  if( nMeasured < minGood )
980  {
981  LOGDIAG3("Made %d/%d range meassurements. A minimum of %d are required."
982  "Cannot proceed with calibration.",
983  nMeasured, minGood);
984  return -LAE_ECODE_IO;
985  }
986 
987  fAvgPost = fSum / (double)nMeasured;
988  nOffsetPost = (int)((fWhiteTgt - fAvgPost) * 1000.0);
989 
990  offset = (s8_t)(nOffsetPost);
991 
992  //
993  // Write new offset value.
994  //
995  if( (rc = writeReg8(regOffset, (byte_t)offset)) == LAE_OK )
996  {
997  m_regRangeOffset = (byte_t)offset; // update shadow register
998  }
999  else
1000  {
1001  LOGDIAG3("Failed to write new SYSRANGE_PART_TO_PART_RANGE_OFFSET (0x%04x) "
1002  "register. Calibration failed.",
1003  regOffset);
1004  return rc;
1005  }
1006 
1007  //
1008  // One final performance evaluation.
1009  //
1010  for(i = 0, fSum = 0.0, nMeasured = 0; i<maxIters; ++i)
1011  {
1012  fRange = measureRange();
1013  if( (fRange >= 0.0) && (fRange <= VL6180X_RANGE_MAX) )
1014  {
1015  fSum += fRange;
1016  ++nMeasured;
1017  }
1018  }
1019 
1020  //
1021  // Not of enough good measurements to determine performance.
1022  //
1023  if( nMeasured < minGood )
1024  {
1025  LOGWARN("Made %d/%d range meassurements. A minimum of %d are required."
1026  "Post performance evaluation is unknown.",
1027  nMeasured, minGood);
1028  }
1029  else
1030  {
1031  fAvgPost = fSum / (double)nMeasured;
1032  }
1033 
1034  return LAE_OK;
1035 }
#define VL6180X_RANGE_MAX
maximum range (m)
Definition: laeVL6180.h:131
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
double measureRange(u32_t msecWait=0xffff)
Measure the sensed target range.
Definition: laeVL6180.cxx:547
byte_t m_regRangeOffset
range part-to-part offset register
Definition: laeVL6180.h:698
vl6180x_als_gain LaeVL6180Mux::gainAnalogToEnum ( double  fAlsGain)
static

Convert ambient light sensor analog gain to associated register value enum.

The mapped enum with the minimum difference between the target analog gain and the actual is chosen.

Parameters
fAlsGainAmbient light sensor analog gain.
Returns
Gain enum.

Definition at line 1137 of file laeVL6180.cxx.

References AlsGainAnalogTbl, sensor::vl6180::GAIN_1, sensor::vl6180::GAIN_40, and sensor::vl6180::GAIN_NumOf.

Referenced by sensor::vl6180::LaeRangeMuxSubproc::cmdTuneAls(), and tune().

1138 {
1139  int i;
1140  int eGain;
1141  double diff, minDiff;
1142 
1143  fAlsGain = fabs(fAlsGain);
1144 
1145  if( fAlsGain >= AlsGainAnalogTbl[GAIN_40] )
1146  {
1147  return GAIN_40;
1148  }
1149  else if( fAlsGain <= AlsGainAnalogTbl[GAIN_1] )
1150  {
1151  return GAIN_1;
1152  }
1153 
1154  eGain = 0;
1155  minDiff = fabs(AlsGainAnalogTbl[eGain] - fAlsGain);
1156 
1157  //
1158  // Find closest enum.
1159  //
1160  for(i=1; i<GAIN_NumOf; ++i)
1161  {
1162  diff = fabs(AlsGainAnalogTbl[i] - fAlsGain);
1163  if( diff < minDiff )
1164  {
1165  eGain = i;
1166  minDiff = diff;
1167  }
1168  }
1169 
1170  return (vl6180x_als_gain)eGain;
1171 }
vl6180x_als_gain
Ambient Light Sensor gain value enumeration.
Definition: laeVL6180.h:247
number of gain enum values
Definition: laeVL6180.h:257
static double AlsGainAnalogTbl[GAIN_NumOf]
Ambient Light Sensor gain table.
Definition: laeVL6180.cxx:95
double LaeVL6180Mux::gainEnumToAnalog ( vl6180x_als_gain  eAlsGain)
static

Convert ambient light sensor gain register value enum to analog gain.

Parameters
eAlsGainAmbient light sensor gain enumeration.
Returns
Analog gain.

Definition at line 1125 of file laeVL6180.cxx.

References AlsGainAnalogTbl, sensor::vl6180::GAIN_1, and sensor::vl6180::GAIN_NumOf.

Referenced by sensor::vl6180::LaeRangeMuxSubproc::cmdGetTunes(), sensor::vl6180::LaeVL6180MuxArray::readSensorTunes(), readShadowRegs(), and tune().

1126 {
1127  if( (eAlsGain >= 0) && (eAlsGain < GAIN_NumOf) )
1128  {
1129  return AlsGainAnalogTbl[eAlsGain];
1130  }
1131  else
1132  {
1133  return AlsGainAnalogTbl[GAIN_1];
1134  }
1135 }
static double AlsGainAnalogTbl[GAIN_NumOf]
Ambient Light Sensor gain table.
Definition: laeVL6180.cxx:95
actual ALS Gain of 1.01
Definition: laeVL6180.h:255
double sensor::vl6180::LaeVL6180Mux::getBeamDir ( )
inline

Get sensor direction of the center of emitter IR beam.

Returns
Radians.

Definition at line 545 of file laeVL6180.h.

546  {
547  return m_fBeamDir;
548  }
double m_fBeamDir
center of beam direction(radians)
Definition: laeVL6180.h:686
std::string sensor::vl6180::LaeVL6180Mux::getDesc ( )
inline

Get sensor short description.

Returns
String name.

Definition at line 535 of file laeVL6180.h.

536  {
537  return m_strDesc;
538  }
std::string m_strDesc
short description
Definition: laeVL6180.h:689
double sensor::vl6180::LaeVL6180Mux::getFoV ( )
inline

Get sensor field of view.

Returns
Radians.

Definition at line 555 of file laeVL6180.h.

References laelaps::degToRad(), and VL6180X_RANGE_FOV.

556  {
558  }
double degToRad(double d)
Convert degrees to radians.
Definition: laeUtils.h:124
#define VL6180X_RANGE_FOV
field of view (degrees)
Definition: laeVL6180.h:132
void sensor::vl6180::LaeVL6180Mux::getMeasurements ( double &  fRange,
double &  fAmbientLight 
)
inline

Get last sensed measurements.

[out] fRange Sensed object range (meters). [out] fAmbienLight Sensed ambient light (lux).

Definition at line 514 of file laeVL6180.h.

515  {
516  fRange = m_fRange;
517  fAmbientLight = m_fAmbientLight;
518  }
double m_fAmbientLight
ambient light (lux)
Definition: laeVL6180.h:705
double m_fRange
range (m)
Definition: laeVL6180.h:704
void sensor::vl6180::LaeVL6180Mux::getMinMax ( double &  fMin,
double &  fMax 
)
inline

Get sensor's minimum and maximum range.

Returns
Meters, meters.

Definition at line 565 of file laeVL6180.h.

References VL6180X_RANGE_MAX, and VL6180X_RANGE_MIN.

566  {
567  fMin = VL6180X_RANGE_MIN;
568  fMax = VL6180X_RANGE_MAX;
569  }
#define VL6180X_RANGE_MAX
maximum range (m)
Definition: laeVL6180.h:131
#define VL6180X_RANGE_MIN
minimum range (m)
Definition: laeVL6180.h:130
std::string sensor::vl6180::LaeVL6180Mux::getNameId ( )
inline

Get sensor assigned name id.

Returns
String name.

Definition at line 525 of file laeVL6180.h.

526  {
527  return m_strNameId;
528  }
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
std::string sensor::vl6180::LaeVL6180Mux::getRadiationType ( )
inline

Get radiation type.

Returns
String.

Definition at line 576 of file laeVL6180.h.

577  {
578  return "infrared";
579  }
int LaeVL6180Mux::initSensor ( bool  bForce = false)

Initialize sensor with recommended settings and defaults.

Note
Higly recommended to call this method and sensor power-up and after resets.
Parameters
bForceDo [not] force re-initialization.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 232 of file laeVL6180.cxx.

References m_bBlackListed, m_nChan, m_nErrorCnt, m_strNameId, outOfResetInit(), readReg8(), readShadowRegs(), and writeDefaults().

233 {
234  byte_t reset;
235  bool bReinit = true;
236  int rc = LAE_OK;
237 
238  m_nErrorCnt = 0;
239  m_bBlackListed = false;
240 
241  if( !bForce )
242  {
243  rc = readReg8(VL6180X_SYSTEM_FRESH_OUT_OF_RESET, reset);
244 
245  if( (rc == LAE_OK) && (reset == 0) )
246  {
247  LOGDIAG3("VL6180 sensor %s(%d): "
248  "Not fresh out of reset: No initialization required.",
249  m_strNameId.c_str(), m_nChan);
250  bReinit = false;
251  }
252  }
253 
254  if( bReinit )
255  {
256  if( (rc = outOfResetInit()) == LAE_OK )
257  {
258  rc = writeDefaults();
259  }
260  }
261 
262  readShadowRegs();
263 
264  if( rc == LAE_OK )
265  {
266  LOGDIAG3("VL6180 sensor %s(%d): Initialized.",
267  m_strNameId.c_str(), m_nChan);
268  }
269 
270  else
271  {
272  m_bBlackListed = true;
273  LOGERROR("VL6180 sensor %s(%d): Black listed: Failed to initialize.",
274  m_strNameId.c_str(), m_nChan);
275  }
276 
277  return rc;
278 }
int writeDefaults()
Write defaults to sensor.
Definition: laeVL6180.cxx:328
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
int m_nErrorCnt
consecutive error count
Definition: laeVL6180.h:690
void readShadowRegs()
Read shadows register values and update derived data.
Definition: laeVL6180.cxx:496
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
bool m_bBlackListed
sensor is [not] black listed
Definition: laeVL6180.h:691
int outOfResetInit()
Initialize sensor out-of-reset.
Definition: laeVL6180.cxx:280
void sensor::vl6180::LaeVL6180Mux::lock ( )
inlineprotected

Lock the share resource.

The lock()/unlock() primitives provide a thread safe mechanism.

Context:
Any.

Definition at line 718 of file laeVL6180.h.

Referenced by measureAmbientLight(), measureRange(), readId(), and readShadowRegs().

719  {
720  pthread_mutex_lock(&m_mutex);
721  }
pthread_mutex_t m_mutex
mutex
Definition: laeVL6180.h:708
double LaeVL6180Mux::measureAmbientLight ( u32_t  msecWait = 0xffff)

Measure the sensed ambient light illuminance.

Single shot mode.

Parameters
msecWaitMaximum time to wait for the measurement to complete (milliseconds). If the wait time is the special value VL6180X_T_AUTO, then the wait time is auto-calculated base on current sensor configuration.
Returns
On success, returns measured illuminance (lux).
On success, the measured illuminance (lux) is returned.
On error, VL6180X_ERR_MEAS(-1.0) is returned.

Definition at line 696 of file laeVL6180.cxx.

References lock(), m_bBlackListed, m_fAlsGain, m_fAmbientLight, m_nChan, m_nErrorCnt, m_strNameId, m_uAlsIntPeriod, NSenseErrorsThreshold, readReg16(), readReg8(), unlock(), VL6180X_ERR_MEAS, VL6180X_T_AUTO, waitForSensorMeasurement(), waitForSensorReady(), and writeReg8().

697 {
698  static byte_t startMeas = 0x01; // start measurement
699  static byte_t measDone = 0x20; // measurement done bit
700  static byte_t statusErrMask = 0xf0; // status error bit mask
701  static double alsRes = 0.32; // .32 lux/count (default without window)
702 
703  double alsLux; // working lux value
704  u16_t valAlsRaw; // raw als value
705  byte_t valStatus; // als sensor status
706  u32_t msec; // milliseconds
707  int rc = LAE_OK; // return code
708 
709  if( m_bBlackListed )
710  {
711  return VL6180X_ERR_MEAS;
712  }
713 
714  lock();
715 
716  // auto-determine max wait time
717  if( msecWait == VL6180X_T_AUTO )
718  {
719  msecWait = (u32_t)((double)m_uAlsIntPeriod * 1.2);
720  }
721 
722  //
723  // Wait for sensor to be ready.
724  //
725  msec = waitForSensorReady(VL6180X_RESULT_ALS_STATUS, msecWait);
726 
727  if( msec > msecWait )
728  {
729  LOGDIAG3("VL6180 sensor %s(%d): Ambient light sensor busy.",
730  m_strNameId.c_str(), m_nChan);
731  rc = -LAE_ECODE_TIMEDOUT;
732  }
733 
734  //
735  // Start single shot measurement.
736  //
737  if( rc == LAE_OK )
738  {
739  rc = writeReg8(VL6180X_SYSALS_START, startMeas);
740  }
741 
742  //
743  // Wait for ambient sensor measurement to complete.
744  //
745  if( rc == LAE_OK )
746  {
747  msec += waitForSensorMeasurement(msecWait-msec, measDone);
748 
749  if( msec > msecWait )
750  {
751  LOGDIAG3("VL6180 sensor %s(%d): Ambient light measurement timed out.",
752  m_strNameId.c_str(), m_nChan);
753  rc = -LAE_ECODE_TIMEDOUT;
754  }
755  }
756 
757  //
758  // Read ALS raw value and status.
759  //
760  if( rc == LAE_OK )
761  {
762  if( (rc = readReg16(VL6180X_RESULT_ALS_VAL, valAlsRaw)) == LAE_OK )
763  {
764  rc = readReg8(VL6180X_RESULT_ALS_STATUS, valStatus);
765  }
766  }
767 
768  //
769  // Validate measurement and convert to SI units.
770  //
771  if( rc == LAE_OK )
772  {
773  // relevant bits
774  valStatus &= statusErrMask;
775 
776  // bits indicate sensor error
777  if( valStatus )
778  {
779  rc = -LAE_ECODE_BAD_VAL;
780  }
781 
782  // good
783  else
784  {
785  // calculate LUX from formula in AppNotes
786  alsLux = alsRes * ((double)valAlsRaw / m_fAlsGain) *
787  100.0 / (double)(m_uAlsIntPeriod);
788  }
789  }
790 
791  //
792  // Save
793  //
794  if( rc == LAE_OK )
795  {
796  m_fAmbientLight = alsLux;
797  m_nErrorCnt = 0;
798  RtDb.m_range[m_nChan].m_fAmbientLight = m_fAmbientLight;
799  }
800  else
801  {
802  alsLux = VL6180X_ERR_MEAS;
803  ++m_nErrorCnt;
804  }
805 
807  {
808  m_bBlackListed = true;
809  }
810 
811  unlock();
812 
813  return alsLux;
814 }
uint_t m_uAlsIntPeriod
ALS integration period (msec)
Definition: laeVL6180.h:695
void lock()
Lock the share resource.
Definition: laeVL6180.h:718
static const int NSenseErrorsThreshold
Consecutive sensed error count threshold.
Definition: laeVL6180.h:376
double m_fAmbientLight
ambient light (lux)
Definition: laeVL6180.h:705
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
u32_t waitForSensorMeasurement(u32_t msecWait, byte_t bitDone)
Wait for sensor measurement to complete.
Definition: laeVL6180.cxx:844
int m_nErrorCnt
consecutive error count
Definition: laeVL6180.h:690
#define VL6180X_T_AUTO
auto-determine wait time
Definition: laeVL6180.h:169
void unlock()
Unlock the shared resource.
Definition: laeVL6180.h:729
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
#define VL6180X_ERR_MEAS
error meassurement
Definition: laeVL6180.h:164
double m_fAlsGain
ambient light sensor analog gain
Definition: laeVL6180.h:694
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
int readReg16(u16_t reg, u16_t &val)
Read a 16-bit value from a register.
Definition: laeVL6180.cxx:1193
bool m_bBlackListed
sensor is [not] black listed
Definition: laeVL6180.h:691
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
u32_t waitForSensorReady(u16_t regStatus, u32_t msecWait)
Wait for sensor to be ready.
Definition: laeVL6180.cxx:816
double LaeVL6180Mux::measureRange ( u32_t  msecWait = 0xffff)

Measure the sensed target range.

Single shot mode.

Parameters
msecWaitMaximum time to wait for the measurement to complete (milliseconds). If the wait time is the special value VL6180X_T_AUTO, then the wait time is auto-calculated base on current sensor configuration.
Returns
On success, the measured object range (meters) is returned.
If no object is detected within sensor range, VL6180X_RANGE_NO_OBJ(1000000.0) is returned.
On error, VL6180X_ERR_MEAS(-1.0) is returned.

Definition at line 547 of file laeVL6180.cxx.

References lock(), m_bBlackListed, m_fRange, m_nChan, m_nErrorCnt, m_strNameId, NSenseErrorsThreshold, readReg8(), unlock(), VL6180X_ERR_MEAS, VL6180X_RANGE_MAX, VL6180X_RANGE_NO_OBJ, VL6180X_T_AUTO, waitForSensorMeasurement(), waitForSensorReady(), and writeReg8().

Referenced by calibCrossTalk(), and calibOffset().

548 {
549  static byte_t startMeas = 0x01; // start measurement
550  static byte_t measDone = 0x04; // measurement done bit
551  static byte_t statusErrMask = 0xf0; // status error bit mask
552  static byte_t statusErrOverflowRaw = 0xd0; // raw range overflow
553  static byte_t statusErrOverflow = 0xf0; // range overflow
554  static byte_t clearInterrupts = 0x07; // clear all interrupts
555 
556  double fRange; // working measured range
557  byte_t valRange; // raw range value
558  byte_t valStatus; // range sensor status
559  u32_t msec; // milliseconds
560  int rc = LAE_OK; // return code
561 
562  if( m_bBlackListed )
563  {
564  return VL6180X_ERR_MEAS;
565  }
566 
567  lock();
568 
569  // auto-determine max wait time
570  if( msecWait == VL6180X_T_AUTO )
571  {
572  msecWait = 15;
573  }
574 
575  //
576  // Wait for device and sensor to be ready.
577  //
578  msec = waitForSensorReady(VL6180X_RESULT_RANGE_STATUS, msecWait);
579 
580  if( msec > msecWait )
581  {
582  LOGDIAG3("VL6180 sensor %s(%d): Range sensor busy.",
583  m_strNameId.c_str(), m_nChan);
584  rc = -LAE_ECODE_TIMEDOUT;
585  }
586 
587  //
588  // Start single shot measurement.
589  //
590  if( rc == LAE_OK )
591  {
592  rc = writeReg8(VL6180X_SYSRANGE_START, startMeas);
593  }
594 
595  //
596  // Wait for range sensor measurement to complete.
597  //
598  if( rc == LAE_OK )
599  {
600  msec += waitForSensorMeasurement(msecWait-msec, measDone);
601 
602  //
603  // Note:
604  // Typically, this timeout indicates that there a no objects in range.
605  // If it is logged, the too many errors will be printed.
606  //
607  if( msec > msecWait )
608  {
609  LOGDIAG3("VL6180 sensor %s(%d): Range measurement timed out.",
610  m_strNameId.c_str(), m_nChan);
611  rc = -LAE_ECODE_TIMEDOUT;
612  }
613  }
614 
615  writeReg8(VL6180X_SYSTEM_INTERRUPT_CLEAR, clearInterrupts);
616 
617  //
618  // Read range value (mm) and status.
619  //
620  if( rc == LAE_OK )
621  {
622  if( (rc = readReg8(VL6180X_RESULT_RANGE_VAL, valRange)) == LAE_OK )
623  {
624  //rc = readReg8(VL6180X_RESULT_RANGE_STATUS, valStatus);
625  //fprintf(stderr, "DBG: range_status=0x%02x\n", val);
626  }
627  }
628 
629  //
630  // Validate measurement and convert to SI units.
631  //
632  if( rc == LAE_OK )
633  {
634  // relevant bits
635  //valStatus &= statusErrMask;
636 
637  // Sensor overflow error. Usually indicates no detected object.
638  //if( (valStatus == statusErrOverflowRaw) ||
639  // (valStatus == statusErrOverflow) )
640  //{
641  // fRange = VL6180X_RANGE_NO_OBJ;
642  //}
643 
644  // other bits indicate sensor error
645  //else if( valStatus )
646  //{
647  // rc = -LAE_ECODE_BAD_VAL;
648  //}
649 
650  // good
651  //else
652  //{
653  // meters
654  fRange = (double)valRange / 1000.0;
655 
656  // range sensor not trusted after maximum distance
657  if( fRange > VL6180X_RANGE_MAX )
658  {
659  fRange = VL6180X_RANGE_NO_OBJ;
660  }
661  //}
662  }
663 
664  //
665  // Save
666  //
667  if( rc == LAE_OK )
668  {
669  m_fRange = fRange;
670  m_nErrorCnt = 0;
671  RtDb.m_range[m_nChan].m_fRange = m_fRange;
672  }
673  else if( rc == -LAE_ECODE_TIMEDOUT )
674  {
675  fRange = VL6180X_RANGE_NO_OBJ;
676  m_fRange = fRange;
677  ++m_nErrorCnt;
678  RtDb.m_range[m_nChan].m_fRange = m_fRange;
679  }
680  else
681  {
682  fRange = VL6180X_ERR_MEAS;
683  ++m_nErrorCnt;
684  }
685 
687  {
688  m_bBlackListed = true;
689  }
690 
691  unlock();
692 
693  return fRange;
694 }
#define VL6180X_RANGE_MAX
maximum range (m)
Definition: laeVL6180.h:131
void lock()
Lock the share resource.
Definition: laeVL6180.h:718
static const int NSenseErrorsThreshold
Consecutive sensed error count threshold.
Definition: laeVL6180.h:376
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
u32_t waitForSensorMeasurement(u32_t msecWait, byte_t bitDone)
Wait for sensor measurement to complete.
Definition: laeVL6180.cxx:844
int m_nErrorCnt
consecutive error count
Definition: laeVL6180.h:690
#define VL6180X_RANGE_NO_OBJ
no object detected
Definition: laeVL6180.h:133
#define VL6180X_T_AUTO
auto-determine wait time
Definition: laeVL6180.h:169
void unlock()
Unlock the shared resource.
Definition: laeVL6180.h:729
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
#define VL6180X_ERR_MEAS
error meassurement
Definition: laeVL6180.h:164
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
bool m_bBlackListed
sensor is [not] black listed
Definition: laeVL6180.h:691
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
u32_t waitForSensorReady(u16_t regStatus, u32_t msecWait)
Wait for sensor to be ready.
Definition: laeVL6180.cxx:816
double m_fRange
range (m)
Definition: laeVL6180.h:704
int LaeVL6180Mux::outOfResetInit ( )
protected

Initialize sensor out-of-reset.

Note
Recommend.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 280 of file laeVL6180.cxx.

References writeReg8().

Referenced by initSensor().

281 {
282  int rc;
283 
284  //
285  // Required by datasheet.
286  // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
287  //
288  writeReg8(0x0207, 0x01);
289  writeReg8(0x0208, 0x01);
290  writeReg8(0x0096, 0x00);
291  writeReg8(0x0097, 0xfd);
292  writeReg8(0x00e3, 0x00);
293  writeReg8(0x00e4, 0x04);
294  writeReg8(0x00e5, 0x02);
295  writeReg8(0x00e6, 0x01);
296  writeReg8(0x00e7, 0x03);
297  writeReg8(0x00f5, 0x02);
298  writeReg8(0x00d9, 0x05);
299  writeReg8(0x00db, 0xce);
300  writeReg8(0x00dc, 0x03);
301  writeReg8(0x00dd, 0xf8);
302  writeReg8(0x009f, 0x00);
303  writeReg8(0x00a3, 0x3c);
304  writeReg8(0x00b7, 0x00);
305  writeReg8(0x00bb, 0x3c);
306  writeReg8(0x00b2, 0x09);
307  writeReg8(0x00ca, 0x09);
308  writeReg8(0x0198, 0x01);
309  writeReg8(0x01b0, 0x17);
310  writeReg8(0x01ad, 0x00);
311  writeReg8(0x00ff, 0x05);
312  writeReg8(0x0100, 0x05);
313  writeReg8(0x0199, 0x05);
314  writeReg8(0x01a6, 0x1b);
315  writeReg8(0x01ac, 0x3e);
316  writeReg8(0x01a7, 0x1f);
317  writeReg8(0x0030, 0x00);
318 
319  //
320  // Clear fresh reset bit. Default is 1 after boot/reset. Setting to 0 can
321  // be used to detect reset condition.
322  //
323  rc = writeReg8(VL6180X_SYSTEM_FRESH_OUT_OF_RESET, 0);
324 
325  return rc;
326 }
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
int LaeVL6180Mux::readId ( struct VL6180xIdentification id)

Read sensor identification.

Parameters
[out]idIdentification structure.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 529 of file laeVL6180.cxx.

References lock(), readReg16(), readReg8(), and unlock().

530 {
531  lock();
532 
533  readReg8(VL6180X_IDENTIFICATION_MODEL_ID, id.idModel);
534  readReg8(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR, id.idModelRevMajor);
535  readReg8(VL6180X_IDENTIFICATION_MODEL_REV_MINOR, id.idModelRevMinor);
536  readReg8(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR, id.idModuleRevMajor);
537  readReg8(VL6180X_IDENTIFICATION_MODULE_REV_MINOR, id.idModuleRevMinor);
538 
539  readReg16(VL6180X_IDENTIFICATION_DATE, id.idDate);
540  readReg16(VL6180X_IDENTIFICATION_TIME, id.idTime);
541 
542  unlock();
543 
544  return LAE_OK;
545 }
void lock()
Lock the share resource.
Definition: laeVL6180.h:718
void unlock()
Unlock the shared resource.
Definition: laeVL6180.h:729
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
int readReg16(u16_t reg, u16_t &val)
Read a 16-bit value from a register.
Definition: laeVL6180.cxx:1193
int LaeVL6180Mux::readReg16 ( u16_t  reg,
u16_t &  val 
)

Read a 16-bit value from a register.

Parameters
reg16-bit register address.
[out]valRead value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 1193 of file laeVL6180.cxx.

References m_mux, m_nChan, m_strNameId, laelaps::LaeI2CMux::transact(), and VL6180X_ADDR.

Referenced by measureAmbientLight(), readId(), and readShadowRegs().

1194 {
1195  byte_t req[2];
1196  byte_t rsp[2];
1197  int n = 0;
1198  int rc;
1199 
1200  req[n++] = (byte_t)((reg >> 8) & 0xff);
1201  req[n++] = (byte_t)(reg & 0xff);
1202 
1203  rc = m_mux.transact(m_nChan, VL6180X_ADDR, req, n, rsp, 2, 0);
1204 
1205  if( rc == LAE_OK )
1206  {
1207  val = ((u16_t)(rsp[0]) << 8) | (u16_t)rsp[1];
1208  }
1209  else
1210  {
1211  LOGDIAG3("VL6180 sensor %s(%d): Failed to read register 0x%04x.",
1212  m_strNameId.c_str(), m_nChan, reg);
1213  }
1214 
1215  return rc;
1216 }
laelaps::LaeI2CMux & m_mux
I2C multiplexor.
Definition: laeVL6180.h:684
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
#define VL6180X_ADDR
I2C 7-bit address.
Definition: laeVL6180.h:124
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
virtual int transact(int chan, uint_t addr, const byte_t wbuf[], size_t wlen, byte_t rbuf[], size_t rlen, long usec=0)
Perform a write/read transaction to/from an I2C slave endpoint device.
Definition: laeI2CMux.cxx:136
int LaeVL6180Mux::readReg8 ( u16_t  reg,
byte_t &  val 
)

Read an 8-bit value from a register.

Parameters
reg16-bit register address.
[out]valRead value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 1173 of file laeVL6180.cxx.

References m_mux, m_nChan, m_strNameId, laelaps::LaeI2CMux::transact(), and VL6180X_ADDR.

Referenced by calibCrossTalk(), calibOffset(), initSensor(), measureAmbientLight(), measureRange(), readId(), readShadowRegs(), waitForSensorMeasurement(), and waitForSensorReady().

1174 {
1175  byte_t req[2];
1176  int n = 0;
1177  int rc;
1178 
1179  req[n++] = (byte_t)((reg >> 8) & 0xff);
1180  req[n++] = (byte_t)(reg & 0xff);
1181 
1182  rc = m_mux.transact(m_nChan, VL6180X_ADDR, req, n, &val, 1, 0);
1183 
1184  if( rc != LAE_OK )
1185  {
1186  LOGDIAG3("VL6180 sensor %s(%d): Failed to read register 0x%04x.",
1187  m_strNameId.c_str(), m_nChan, reg);
1188  }
1189 
1190  return rc;
1191 }
laelaps::LaeI2CMux & m_mux
I2C multiplexor.
Definition: laeVL6180.h:684
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
#define VL6180X_ADDR
I2C 7-bit address.
Definition: laeVL6180.h:124
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
virtual int transact(int chan, uint_t addr, const byte_t wbuf[], size_t wlen, byte_t rbuf[], size_t rlen, long usec=0)
Perform a write/read transaction to/from an I2C slave endpoint device.
Definition: laeI2CMux.cxx:136
void LaeVL6180Mux::readShadowRegs ( byte_t &  regRangeOffset,
u16_t &  regRangeCrossTalk,
byte_t &  regAlsGain,
u16_t &  regAlsIntPeriod 
)

Read shadows register values, update derived data, and return registers values.

Parameters
[out]regRangeOffsetRange part-to-part offset register value
[out]regRangeCrossTalkRange cross-talk register value.
[out]regAlsGainALS gain register value.
[out]regAlsIntPeriodALS itegration period register value.

Definition at line 516 of file laeVL6180.cxx.

References m_regAlsGain, m_regAlsIntPeriod, m_regRangeCrossTalk, m_regRangeOffset, and readShadowRegs().

520 {
521  readShadowRegs();
522 
523  regRangeOffset = m_regRangeOffset;
524  regRangeCrossTalk = m_regRangeCrossTalk;
525  regAlsGain = m_regAlsGain;
526  regAlsIntPeriod = m_regAlsIntPeriod;
527 }
u16_t m_regAlsIntPeriod
ALS itegration period register.
Definition: laeVL6180.h:701
byte_t m_regAlsGain
ambient light sensor gain register
Definition: laeVL6180.h:700
void readShadowRegs()
Read shadows register values and update derived data.
Definition: laeVL6180.cxx:496
u16_t m_regRangeCrossTalk
range cross-talk register
Definition: laeVL6180.h:699
byte_t m_regRangeOffset
range part-to-part offset register
Definition: laeVL6180.h:698
int LaeVL6180Mux::tune ( uint_t  uRangeOffset,
uint_t  uRangeCrossTalk,
double  fAlsGain,
uint_t  uAlsIntPeriod 
)

Tune sensor configuration.

Parameters
uRangeOffsetToF sensor part-to-part offset. If VL6180X_FACTORY_DFT then leave as is.
uRangeCrossTalkToF sensor cross-talk compensation. If VL6180X_FACTORY_DFT then leave as is.
fAlsGainAmbient light sensor analog gain.
uAlsIntPeriodAmbient light sensor integration period (msec).
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 379 of file laeVL6180.cxx.

References laelaps::cap(), gainAnalogToEnum(), gainEnumToAnalog(), m_fAlsGain, m_regAlsGain, m_regAlsIntPeriod, m_regRangeCrossTalk, m_regRangeOffset, m_uAlsIntPeriod, VL6180X_AMBIENT_INT_T_MAX, VL6180X_AMBIENT_INT_T_MIN, VL6180X_FACTORY_DFT, VL6180X_RANGE_OFFSET_MAX, VL6180X_RANGE_OFFSET_MIN, VL6180X_RANGE_XTALK_MAX, VL6180X_RANGE_XTALK_MIN, writeReg16(), and writeReg8().

381 {
382  byte_t regRangeOffset;
383  u16_t regRangeCrossTalk;
384  byte_t regAlsGain;
385  u16_t regAlsIntPeriod;
386  int rc;
387 
388  //
389  // Convert to new register values.
390  //
391  if( (int)uRangeOffset == VL6180X_FACTORY_DFT)
392  {
393  regRangeOffset = m_regRangeOffset;
394  }
395  else
396  {
397  regRangeOffset = (byte_t)cap((int)uRangeOffset,
400  }
401 
402  if( (int)uRangeCrossTalk == VL6180X_FACTORY_DFT)
403  {
404  regRangeCrossTalk = m_regRangeCrossTalk;
405  }
406  else
407  {
408  regRangeCrossTalk = (u16_t)cap(uRangeCrossTalk,
409  (uint_t)VL6180X_RANGE_XTALK_MIN,
410  (uint_t)VL6180X_RANGE_XTALK_MAX);
411  }
412 
413  // map analog to register enum, then find exact analog gain value
414  regAlsGain = (byte_t)gainAnalogToEnum(fAlsGain);
415  fAlsGain = gainEnumToAnalog((vl6180x_als_gain)regAlsGain);
416 
417  uAlsIntPeriod = cap(uAlsIntPeriod, (uint_t)VL6180X_AMBIENT_INT_T_MIN,
418  (uint_t)VL6180X_AMBIENT_INT_T_MAX);
419 
420  regAlsIntPeriod = (u16_t)(uAlsIntPeriod - 1);
421 
422  //
423  // New offset value.
424  //
425  if( regRangeOffset != m_regRangeOffset )
426  {
427  rc = writeReg8(VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET, regRangeOffset);
428  if( rc == LAE_OK )
429  {
430  m_regRangeOffset = regRangeOffset;
431  }
432  else
433  {
434  LOGDIAG3("Failed to write new range part-to-part "
435  "offset register value %u.", regRangeOffset);
436  }
437  }
438 
439  //
440  // New cross-talk value.
441  //
442  if( regRangeCrossTalk != m_regRangeCrossTalk )
443  {
444  rc = writeReg16(VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE,
445  regRangeCrossTalk);
446  if( rc == LAE_OK )
447  {
448  m_regRangeOffset = regRangeOffset;
449  }
450  else
451  {
452  LOGDIAG3("Failed to write new range cross-talk compensation "
453  "register value %u.", regRangeCrossTalk);
454  }
455  }
456 
457  //
458  // New gain value.
459  //
460  if( regAlsGain != m_regAlsGain )
461  {
462  rc = writeReg8(VL6180X_SYSALS_ANALOGUE_GAIN, (byte_t)(0x40|regAlsGain));
463  if( rc == LAE_OK )
464  {
465  m_fAlsGain = fAlsGain;
466  m_regAlsGain = regAlsGain;
467  }
468  else
469  {
470  LOGDIAG3("Failed to write new ALS gain register value %u.", regAlsGain);
471  }
472  }
473 
474  //
475  // New integration period value.
476  //
477  if( regAlsIntPeriod != m_regAlsIntPeriod )
478  {
479  rc = writeReg16(VL6180X_SYSALS_INTEGRATION_PERIOD, regAlsIntPeriod);
480  if( rc == LAE_OK )
481  {
482  m_uAlsIntPeriod = uAlsIntPeriod;
483  m_regAlsIntPeriod = regAlsIntPeriod;
484  }
485  else
486  {
487  LOGDIAG3("Failed to write new ALS integration period register value %u.",
488  regAlsIntPeriod);
489  }
490  }
491 
492  // always return success for now
493  return LAE_OK;
494 }
uint_t m_uAlsIntPeriod
ALS integration period (msec)
Definition: laeVL6180.h:695
#define VL6180X_AMBIENT_INT_T_MIN
minimum als int. period (msec)
Definition: laeVL6180.h:156
#define VL6180X_RANGE_XTALK_MIN
minimum tof cross-talk
Definition: laeVL6180.h:140
vl6180x_als_gain
Ambient Light Sensor gain value enumeration.
Definition: laeVL6180.h:247
#define VL6180X_RANGE_OFFSET_MAX
maximum tof offset
Definition: laeVL6180.h:137
u16_t m_regAlsIntPeriod
ALS itegration period register.
Definition: laeVL6180.h:701
#define VL6180X_AMBIENT_INT_T_MAX
maximum als int. period (msec)
Definition: laeVL6180.h:157
static vl6180x_als_gain gainAnalogToEnum(double fAlsGain)
Convert ambient light sensor analog gain to associated register value enum.
Definition: laeVL6180.cxx:1137
byte_t m_regAlsGain
ambient light sensor gain register
Definition: laeVL6180.h:700
int writeReg16(u16_t reg, u16_t val)
Write a 16-bit value to a register.
Definition: laeVL6180.cxx:1243
#define VL6180X_RANGE_XTALK_MAX
maximum tof cross-talk
Definition: laeVL6180.h:141
int cap(int a, int min, int max)
Cap value within limits [min, max].
Definition: laeUtils.h:176
static double gainEnumToAnalog(vl6180x_als_gain eAlsGain)
Convert ambient light sensor gain register value enum to analog gain.
Definition: laeVL6180.cxx:1125
double m_fAlsGain
ambient light sensor analog gain
Definition: laeVL6180.h:694
#define VL6180X_RANGE_OFFSET_MIN
minimum tof offset
Definition: laeVL6180.h:136
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
u16_t m_regRangeCrossTalk
range cross-talk register
Definition: laeVL6180.h:699
#define VL6180X_FACTORY_DFT
use factory default
Definition: laeVL6180.h:170
byte_t m_regRangeOffset
range part-to-part offset register
Definition: laeVL6180.h:698
void sensor::vl6180::LaeVL6180Mux::unlock ( )
inlineprotected

Unlock the shared resource.

Context:
Any.

Definition at line 729 of file laeVL6180.h.

Referenced by measureAmbientLight(), measureRange(), readId(), and readShadowRegs().

730  {
731  pthread_mutex_unlock(&m_mutex);
732  }
pthread_mutex_t m_mutex
mutex
Definition: laeVL6180.h:708
u32_t LaeVL6180Mux::waitForSensorMeasurement ( u32_t  msecWait,
byte_t  bitDone 
)
protected

Wait for sensor measurement to complete.

Parameters
msecWaitMaximum time to wait for the measurement to complete (milliseconds).
bitDoneBit to check for measurement completion.
Returns
Returns the number of milliseconds waited. A value > msecWait indicates a timeout.

Definition at line 844 of file laeVL6180.cxx.

References readReg8().

Referenced by measureAmbientLight(), and measureRange().

845 {
846  static byte_t clearInterrupts = 0x07; // clear all interrupts
847 
848  byte_t val;
849  u32_t msec;
850 
851  //
852  // Wait for sensor device to become available.
853  //
854  for(msec = 0; msec <= msecWait; ++msec)
855  {
856  if( readReg8(VL6180X_RESULT_INTERRUPT_STATUS_GPIO, val) == LAE_OK )
857  {
858  // sensor not busy
859  if( val & bitDone )
860  {
861  // writeReg8(VL6180X_SYSTEM_INTERRUPT_CLEAR, clearInterrupts);
862  return msec;
863  }
864 
865  // sleep 1 msec == 1000 usec
866  else
867  {
868  usleep(1000);
869  }
870  }
871  }
872 
873  //fprintf(stderr, "DBG: status_gpio=0x%02x\n", val);
874 
875  return msec;
876 }
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
u32_t LaeVL6180Mux::waitForSensorReady ( u16_t  regStatus,
u32_t  msecWait 
)
protected

Wait for sensor to be ready.

Parameters
regStatusStatus register to check.
msecWaitMaximum time to wait for the measurement to complete (milliseconds).
Returns
Returns the number of milliseconds waited. A value > msecWait indicates a timeout.

Definition at line 816 of file laeVL6180.cxx.

References readReg8().

Referenced by measureAmbientLight(), and measureRange().

817 {
818  static byte_t statusReady = 0x01; // device and sensor is ready bit
819 
820  byte_t val;
821  u32_t msec;
822 
823  //
824  // Wait for sensor device to become available.
825  //
826  for(msec = 0; msec <= msecWait; ++msec)
827  {
828  // sensor not busy
829  if( (readReg8(regStatus, val) == LAE_OK) && (val & statusReady) )
830  {
831  return msec;
832  }
833 
834  // sleep 1 msec == 1000 usec
835  else
836  {
837  usleep(1000);
838  }
839  }
840 
841  return msec;
842 }
int readReg8(u16_t reg, byte_t &val)
Read an 8-bit value from a register.
Definition: laeVL6180.cxx:1173
int LaeVL6180Mux::writeDefaults ( )

Write defaults to sensor.

The defaults provide reasonable operation within the Laelaps envirionment.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 328 of file laeVL6180.cxx.

References m_regAlsGain, m_regAlsIntPeriod, writeReg16(), and writeReg8().

Referenced by initSensor().

329 {
330  // Recommended settings from datasheet
331  // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
332 
333  // Disable interrupts.
334  //writeReg8(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0);
335  // Set GPIO1 high when sample complete
336  writeReg8(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4));
337 
338  // Set GPIO1 high when sample complete. N/A since interrupts are disabled.
339  writeReg8(VL6180X_SYSTEM_MODE_GPIO1, 0x10);
340 
341  // Set the readout averaging sample period.
342  // RDK: Possible TUNE parameter.
343  writeReg8(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 48);
344 
345  // Set the ALS analog gain.
346  writeReg8(VL6180X_SYSALS_ANALOGUE_GAIN, (byte_t)(0x40 | m_regAlsGain));
347 
348  // Set auto calibration period (Max = 255)/(OFF = 0).
349  writeReg8(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF);
350 
351  // Perform a single temperature calibration
352  writeReg8(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01);
353 
354  // Set ALS integration time in msec.
355  writeReg16(VL6180X_SYSALS_INTEGRATION_PERIOD, m_regAlsIntPeriod);
356 
357  // Optional settings from datasheet
358  // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
359 
360  // Set default ranging inter-measurement period to 100ms.
361  // RDK: Used in interleaved mode which may be the optimal mode.
362  writeReg8(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09);
363 
364  // Set default ALS inter-measurement period to 100ms.
365  // RDK: Used in interleaved mode which may be the optimal mode.
366  writeReg8(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A);
367 
368  // Additional settings defaults from community
369  // RDK Investigate
370  writeReg8(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32);
371  writeReg8(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01);
372  writeReg16(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B );
373 
374  writeReg8(VL6180X_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT, 255);
375 
376  writeReg8(VL6180X_FIRMWARE_RESULT_SCALER, 0x01);
377 }
u16_t m_regAlsIntPeriod
ALS itegration period register.
Definition: laeVL6180.h:701
byte_t m_regAlsGain
ambient light sensor gain register
Definition: laeVL6180.h:700
int writeReg16(u16_t reg, u16_t val)
Write a 16-bit value to a register.
Definition: laeVL6180.cxx:1243
int writeReg8(u16_t reg, byte_t val)
Write an 8-bit value to a register.
Definition: laeVL6180.cxx:1218
int LaeVL6180Mux::writeReg16 ( u16_t  reg,
u16_t  val 
)

Write a 16-bit value to a register.

Parameters
reg16-bit register address.
[in]valWrite value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 1243 of file laeVL6180.cxx.

References m_mux, m_nChan, m_strNameId, VL6180X_ADDR, and laelaps::LaeI2CMux::write().

Referenced by calibCrossTalk(), tune(), and writeDefaults().

1244 {
1245  byte_t req[4];
1246  int n = 0;
1247  int k;
1248  int rc;
1249 
1250  req[n++] = (byte_t)((reg >> 8) & 0xff);
1251  req[n++] = (byte_t)(reg & 0xff);
1252  req[n++] = (byte_t)((val >> 8) & 0xff);
1253  req[n++] = (byte_t)(val & 0xff);
1254 
1255  if( (k = m_mux.write(m_nChan, VL6180X_ADDR, req, n)) == n )
1256  {
1257  rc = LAE_OK;
1258  }
1259  else
1260  {
1261  LOGDIAG3("VL6180 sensor %s(%d): Failed to write register 0x%04x.",
1262  m_strNameId.c_str(), m_nChan, reg);
1263  rc = -LAE_ECODE_IO;
1264  }
1265 
1266  return rc;
1267 }
laelaps::LaeI2CMux & m_mux
I2C multiplexor.
Definition: laeVL6180.h:684
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
#define VL6180X_ADDR
I2C 7-bit address.
Definition: laeVL6180.h:124
virtual int write(int chan, uint_t addr, byte_t buf[], size_t len)
Write from an I2C endpoint device.
Definition: laeI2CMux.cxx:106
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685
int LaeVL6180Mux::writeReg8 ( u16_t  reg,
byte_t  val 
)

Write an 8-bit value to a register.

Parameters
reg16-bit register address.
[in]valWrite value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 1218 of file laeVL6180.cxx.

References m_mux, m_nChan, m_strNameId, VL6180X_ADDR, and laelaps::LaeI2CMux::write().

Referenced by calibOffset(), measureAmbientLight(), measureRange(), outOfResetInit(), tune(), and writeDefaults().

1219 {
1220  byte_t req[3];
1221  int n = 0;
1222  int k;
1223  int rc;
1224 
1225  req[n++] = (byte_t)((reg >> 8) & 0xff);
1226  req[n++] = (byte_t)(reg & 0xff);
1227  req[n++] = val;
1228 
1229  if( (k = m_mux.write(m_nChan, VL6180X_ADDR, req, n)) == n )
1230  {
1231  rc = LAE_OK;
1232  }
1233  else
1234  {
1235  LOGDIAG3("VL6180 sensor %s(%d): Failed to write register 0x%04x.",
1236  m_strNameId.c_str(), m_nChan, reg);
1237  rc = -LAE_ECODE_IO;
1238  }
1239 
1240  return rc;
1241 }
laelaps::LaeI2CMux & m_mux
I2C multiplexor.
Definition: laeVL6180.h:684
std::string m_strNameId
name identifier of sensor
Definition: laeVL6180.h:688
#define VL6180X_ADDR
I2C 7-bit address.
Definition: laeVL6180.h:124
virtual int write(int chan, uint_t addr, byte_t buf[], size_t len)
Write from an I2C endpoint device.
Definition: laeI2CMux.cxx:106
int m_nChan
multiplexed channel number
Definition: laeVL6180.h:685

Member Data Documentation

const int sensor::vl6180::LaeVL6180Mux::NSenseErrorsThreshold = 10
static

Consecutive sensed error count threshold.

The sensor will be black listed if errors exceed this threshold.

Definition at line 376 of file laeVL6180.h.

Referenced by measureAmbientLight(), and measureRange().


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