A library for the RFD77402 ToF distance-sensor. It's a port form the sparkfun-arduino-library.

RFD77402 ToF-sensor

I just ported the sourcecode provided by sparkfun (available on https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library) to the mbed-environment. So if you like it, please support them by e.g. buying a sparkfun-board, because in fact they did all the work :)

If you found any errors, please report them, I'm no professinal coder, so maybe there are some things I dindn't consider while porting the library.

The datasheet of the sensor is available at https://media.digikey.com/pdf/Data%20Sheets/RF%20Digital%20PDFs/RFD77402.pdf

The easiest way to use the sensor is e.g.:

RFD77402 MyDistance;                            //create an object-instance
MyDistance.begin(i2c);                          //initialize the sensor

MyDistance.takeMeasurement();                   //tell sensor to take a measurement

uint16_t distance = MyDistance.getDistance();   //get the last measurement result
Committer:
glx
Date:
Tue Nov 21 21:29:58 2017 +0000
Revision:
0:661414c07859
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
glx 0:661414c07859 1 /*
glx 0:661414c07859 2 This is a library written for the RFD77402 Time of Flight sensor.
glx 0:661414c07859 3 SparkFun sells these at its website: www.sparkfun.com
glx 0:661414c07859 4 Written by Nathan Seidle @ SparkFun Electronics, June 9th, 2017
glx 0:661414c07859 5 The VCSEL (vertical-cavity surface-emitting laser) Time of Flight sensor
glx 0:661414c07859 6 can accurately measure from 10cm to 200cm (2m) with milimeter precision.
glx 0:661414c07859 7 This library handles the initialization of the RFD77402 and bringing in of
glx 0:661414c07859 8 various readings.
glx 0:661414c07859 9 https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library
glx 0:661414c07859 10 Do you like this library? Help support SparkFun. Buy a board!
glx 0:661414c07859 11 Development environment specifics:
glx 0:661414c07859 12 Arduino IDE 1.8.1
glx 0:661414c07859 13 This program is distributed in the hope that it will be useful,
glx 0:661414c07859 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
glx 0:661414c07859 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
glx 0:661414c07859 16 GNU General Public License for more details.
glx 0:661414c07859 17 You should have received a copy of the GNU General Public License
glx 0:661414c07859 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
glx 0:661414c07859 19
glx 0:661414c07859 20 I just ported the sourcecode provided by sparkfun
glx 0:661414c07859 21 (on https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library) to the
glx 0:661414c07859 22 mbed-environment. So if you like it, please support them by e.g. buying a
glx 0:661414c07859 23 sparkfun-board :)
glx 0:661414c07859 24 */
glx 0:661414c07859 25
glx 0:661414c07859 26 #ifndef RFD77402_H_
glx 0:661414c07859 27 #define RFD77402_H_
glx 0:661414c07859 28 #endif
glx 0:661414c07859 29 #ifndef MBED_H_
glx 0:661414c07859 30 #define MBED_H_
glx 0:661414c07859 31 #endif
glx 0:661414c07859 32
glx 0:661414c07859 33 #define RFD77402_ADDR 0x98 //8-bit --> 0x4C in 7-bit I2C Address
glx 0:661414c07859 34
glx 0:661414c07859 35 //Register addresses
glx 0:661414c07859 36 #define RFD77402_ICSR 0x00
glx 0:661414c07859 37 #define RFD77402_INTERRUPTS 0x02
glx 0:661414c07859 38 #define RFD77402_COMMAND 0x04
glx 0:661414c07859 39 #define RFD77402_DEVICE_STATUS 0x06
glx 0:661414c07859 40 #define RFD77402_RESULT 0x08
glx 0:661414c07859 41 #define RFD77402_RESULT_CONFIDENCE 0x0A
glx 0:661414c07859 42 #define RFD77402_CONFIGURE_A 0x0C
glx 0:661414c07859 43 #define RFD77402_CONFIGURE_B 0x0E
glx 0:661414c07859 44 #define RFD77402_HOST_TO_MCPU_MAILBOX 0x10
glx 0:661414c07859 45 #define RFD77402_MCPU_TO_HOST_MAILBOX 0x12
glx 0:661414c07859 46 #define RFD77402_CONFIGURE_PMU 0x14
glx 0:661414c07859 47 #define RFD77402_CONFIGURE_I2C 0x1C
glx 0:661414c07859 48 #define RFD77402_CONFIGURE_HW_0 0x20
glx 0:661414c07859 49 #define RFD77402_CONFIGURE_HW_1 0x22
glx 0:661414c07859 50 #define RFD77402_CONFIGURE_HW_2 0x24
glx 0:661414c07859 51 #define RFD77402_CONFIGURE_HW_3 0x26
glx 0:661414c07859 52 #define RFD77402_MOD_CHIP_ID 0x28
glx 0:661414c07859 53
glx 0:661414c07859 54 #define RFD77402_MODE_MEASUREMENT 0x01
glx 0:661414c07859 55 #define RFD77402_MODE_STANDBY 0x10
glx 0:661414c07859 56 #define RFD77402_MODE_OFF 0x11
glx 0:661414c07859 57 #define RFD77402_MODE_ON 0x12
glx 0:661414c07859 58
glx 0:661414c07859 59 #define CODE_VALID_DATA 0x00
glx 0:661414c07859 60 #define CODE_FAILED_PIXELS 0x01
glx 0:661414c07859 61 #define CODE_FAILED_SIGNAL 0x02
glx 0:661414c07859 62 #define CODE_FAILED_SATURATED 0x03
glx 0:661414c07859 63 #define CODE_FAILED_NOT_NEW 0x04
glx 0:661414c07859 64 #define CODE_FAILED_TIMEOUT 0x05
glx 0:661414c07859 65
glx 0:661414c07859 66 #define I2C_SPEED_STANDARD 100000
glx 0:661414c07859 67 #define I2C_SPEED_FAST 400000
glx 0:661414c07859 68
glx 0:661414c07859 69 class RFD77402 {
glx 0:661414c07859 70 public:
glx 0:661414c07859 71
glx 0:661414c07859 72 //By default use Wire, standard I2C speed, and the defaul AK9750 address
glx 0:661414c07859 73 bool begin(I2C &i2cPort);
glx 0:661414c07859 74
glx 0:661414c07859 75 uint8_t takeMeasurement(); //Takes a single measurement and sets the global variables with new data
glx 0:661414c07859 76 uint16_t getDistance(); //Returns the local variable to the caller
glx 0:661414c07859 77 uint8_t getValidPixels(); //Returns the number of valid pixels found when taking measurement
glx 0:661414c07859 78 uint16_t getConfidenceValue(); //Returns the qualitative value representing how confident the sensor is about its reported distance
glx 0:661414c07859 79 uint8_t getMode(); //Read the command opcode and covert to mode
glx 0:661414c07859 80
glx 0:661414c07859 81 bool goToStandbyMode(); //Tell MCPU to go to standby mode. Return true if successful
glx 0:661414c07859 82 bool goToOffMode(); //Tell MCPU to go to off state. Return true if successful
glx 0:661414c07859 83 bool goToOnMode(); //Tell MCPU to go to on state. Return true if successful
glx 0:661414c07859 84 bool goToMeasurementMode(); //Tell MCPU to go to measurement mode. Takes a measurement. If measurement data is ready, return true
glx 0:661414c07859 85
glx 0:661414c07859 86 uint8_t getPeak(); //Returns the VCSEL peak 4-bit value
glx 0:661414c07859 87 void setPeak(uint8_t peakValue); //Sets the VCSEL peak 4-bit value
glx 0:661414c07859 88 uint8_t getThreshold(); //Returns the VCSEL Threshold 4-bit value
glx 0:661414c07859 89 void setThreshold(uint8_t threshold); //Sets the VCSEL Threshold 4-bit value
glx 0:661414c07859 90 uint8_t getFrequency(); //Returns the VCSEL Frequency 4-bit value
glx 0:661414c07859 91 void setFrequency(uint8_t threshold); //Sets the VCSEL Frequency 4-bit value
glx 0:661414c07859 92
glx 0:661414c07859 93 uint16_t getMailbox(); //Gets whatever is in the 'MCPU to Host' mailbox. Check ICSR bit 5 before reading.
glx 0:661414c07859 94 void reset(); //Software reset the device
glx 0:661414c07859 95 uint16_t getChipID(); //Returns the chip ID. Should be 0xAD01 or higher.
glx 0:661414c07859 96
glx 0:661414c07859 97 //Retreive 2*27 bytes from MCPU for computation of calibration parameters
glx 0:661414c07859 98 //Reads 54 bytes into the calibration[] array
glx 0:661414c07859 99 //Returns true if new cal data is loaded
glx 0:661414c07859 100 bool getCalibrationData();
glx 0:661414c07859 101
glx 0:661414c07859 102 uint16_t readRegister16(char addr); //Reads two bytes from a given location from the RFD77402
glx 0:661414c07859 103 uint8_t readRegister(char addr); //Reads from a given location from the RFD77402
glx 0:661414c07859 104 void writeRegister16(char addr, uint16_t val); //Write a 16 bit value to a spot in the RFD77402
glx 0:661414c07859 105 void writeRegister(char addr, char val); //Write a value to a spot in the RFD77402
glx 0:661414c07859 106
glx 0:661414c07859 107 //Variables
glx 0:661414c07859 108 uint16_t distance;
glx 0:661414c07859 109 uint8_t validPixels;
glx 0:661414c07859 110 uint16_t confidenceValue;
glx 0:661414c07859 111 uint8_t calibrationData[54]; //Loaded by the 0x006 mailbox command
glx 0:661414c07859 112
glx 0:661414c07859 113 private:
glx 0:661414c07859 114
glx 0:661414c07859 115 //Variables
glx 0:661414c07859 116 I2C *_i2cPort; //The generic connection to user's chosen I2C hardware
glx 0:661414c07859 117 };