opensensinglab / tfmini Goto Github PK
View Code? Open in Web Editor NEWAn Arduino driver for the Benewake TFMini time-of-flight distance sensor
An Arduino driver for the Benewake TFMini time-of-flight distance sensor
Issue
When the SIGSTR drops below 20, the distance reads out 65535. Distance displays correctly otherwise and verified with a tape measure.
Output
216 cm sigstr: 29
217 cm sigstr: 25
218 cm sigstr: 21
65535 cm sigstr: 19
65535 cm sigstr: 16
65535 cm sigstr: 15
65535 cm sigstr: 14
65535 cm sigstr: 12
65535 cm sigstr: 12
65535 cm sigstr: 12
65535 cm sigstr: 13
65535 cm sigstr: 13
65535 cm sigstr: 13
65535 cm sigstr: 13
65535 cm sigstr: 14
65535 cm sigstr: 15
65535 cm sigstr: 18
597 cm sigstr: 21
599 cm sigstr: 21
600 cm sigstr: 21
605 cm sigstr: 22
606 cm sigstr: 22
606 cm sigstr: 22
608 cm sigstr: 24
609 cm sigstr: 22
608 cm sigstr: 22
608 cm sigstr: 21
606 cm sigstr: 21
602 cm sigstr: 20
Environment
Tested using a Particle Photon and the tfmini lib. The Photon code was slightly modified to use Serial1 instead of SoftwareSerial
#include "TFMini.h"
TFMini tfmini;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
delay(200);
tfmini.begin(&Serial1);
}
void loop()
{
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);
// Wait some short time before taking the next measurement
delay(25);
}
I am trying to change the sampling rate from the default (10ms).
According to the manual you can use the command "42 57 02 00 EE FF 00 07", but I haven't been able to make it work. Have someone figured it out?
Using TFMini v1.8 device the frame size is 7. The code that calculates the checksum adds all positions that are less than frame size - 2. I found that the checksum was always 9 greater than the calculation. Position 5 is always 9 but not included in the calculation.
This caused the following error:
TF Mini error: too many measurement attempts
Last error:
ERROR_SERIAL_BADCHECKSUM
65535 cm sigstr: 65535
After changing the calculation to include position 5 like so:
if (i < TFMINI_FRAME_SIZE-1) { checksum += frame[i]; }
instead of:
if (i < TFMINI_FRAME_SIZE-2) { checksum += frame[i]; }
Things are now working.
I'm not sure what the frame size is of other TF Devices. Are they different? Maybe older versions have a frame size of 8? Anyway, just wanted to report what I found.
We are using the example BasicReading code included with he library. Our setup is the same as this video:
https://www.youtube.com/watch?v=VhbFbxyOI1k&t=1092s&fbclid=IwAR3khhu7QYcBBaMKj1so-swQpUtuHWnf2FYCyCYwWypNeV1Y2j3mluIvE7A
But when we run the code and look at the serial monitor, the monitor only shows 'Initializing...'
What could be the problem?
Thank you.
here, what code iam run on...
/*
Example code for Benewake TFMini time-of-flight distance sensor.
by Peter Jansen (December 11/2017)
This example code is in the public domain.
This example communicates to the TFMini using a SoftwareSerial port at 115200,
while communicating the distance results through the default Arduino hardware
Serial debug port.
SoftwareSerial for some boards can be unreliable at high speeds (such as 115200).
The driver includes some limited error detection and automatic retries, that
means it can generally work with SoftwareSerial on (for example) an UNO without
the end-user noticing many communications glitches, as long as a constant refresh
rate is not required.
The (UNO) circuit:
* Uno RX is digital pin 10 (connect to TX of TF Mini)
* Uno TX is digital pin 11 (connect to RX of TF Mini)
THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Maple Mini
* RX1 Pin 25, 5V tolerant -> TX TF Mini
* TX1 Pin 26, 5V tolerant -> RX TF Mini
*/
/*
* Bluepill
* RX1 Pin PA10 (31), 5V tolerant -> TX TF Mini
* TX1 Pin PA9 (30), 5V tolerant -> RX TF Mini
*/
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;
SoftwareSerial Serial1(2,3);
void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(115200);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
Serial.println ("Initializing...");
// Step 2: Initialize the data rate for the SoftwareSerial port
Serial1.begin(TFMINI_BAUDRATE);
// Step 3: Initialize the TF Mini sensor
tfmini.begin(&Serial1);
delay(100);
// Step 4: Initialize single measurement mode with external trigger
tfmini.setSingleScanMode();
}
void loop() {
// Take one TF Mini distance measurement
tfmini.externalTrigger();
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
// Display the measurement
Serial.print("triggered - ");
Serial.print(dist);
Serial.print(",");
Serial.println(strength);
// Wait some time before taking the next measurement
// without delay, measurement is super fast
delay(1000);
}
here what iam got is
Arduino: 1.8.13 (Linux), Board: "ATmega168, Yes (UART0), EEPROM retained, 168P / 168PA, BOD 2.7V, LTO Disabled, External 16 MHz"
/home/dev/Documents/PlatformIO/Projects/tfmini/examples/SingleReading/SingleReading.ino: In function 'void setup()':
SingleReading:65:10: error: 'class TFMini' has no member named 'setSingleScanMode'
tfmini.setSingleScanMode();
^~~~~~~~~~~~~~~~~
/home/dev/Documents/PlatformIO/Projects/tfmini/examples/SingleReading/SingleReading.ino: In function 'void loop()':
SingleReading:71:10: error: 'class TFMini' has no member named 'externalTrigger'
tfmini.externalTrigger();
^~~~~~~~~~~~~~~
exit status 1
'class TFMini' has no member named 'setSingleScanMode'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Out of the blue, my TF Mini gets stuck at 65533.
It seems to do that sometimes among correct readings too (only with 65535, instead of 65533, which is weird), but then it resets:
10:21:32.211 -> 183
10:21:32.244 -> 183
10:21:32.346 -> TF Mini error: too many measurement attempts
10:21:32.346 -> Last error:
10:21:32.346 -> ERROR_SERIAL_BADCHECKSUM
10:21:32.346 -> 65535
10:21:32.346 -> 183
10:21:32.381 -> 183
However, when it gets stuck, I don't see an error message:
10:23:11.681 -> 183
10:23:11.715 -> 183
10:23:11.749 -> 65533
10:23:11.783 -> 65533
The only fix is to both reconnect USB and then reupload the sketch.
#include <SoftwareSerial.h>
#include "TFMini.h"
SoftwareSerial mySerial(10, 11);
TFMini tfmini;
void setup() {
Serial.begin(115200);
while(!Serial);
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial);
Serial.println("TF Mini Measurements");
}
void loop() {
uint16_t dist = tfmini.getDistance();
Serial.println(dist);
delay(25);
}
How do I connect tfmini with Particle Photon?
Please provide pin connection details and sample firmware
During my tests I was not able to get the external trigger to work, after some tests using a usb to UART I noticed that its not needed to change to config mode the sensor.
commenting out the setConfigMode() calls worked for me.
At the end I used it as below.
// Set single scan mode (external trigger)
void TFMini::setSingleScanMode() {
// not needed ->setConfigMode();
// setting trigger source to external
streamPtr->write((uint8_t)0x42);
streamPtr->write((uint8_t)0x57);
streamPtr->write((uint8_t)0x02);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x40);
}
// Send external trigger
void TFMini::externalTrigger() {
// not needed -> setConfigMode();
// send trigger
streamPtr->write((uint8_t)0x42);
streamPtr->write((uint8_t)0x57);
streamPtr->write((uint8_t)0x02);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x00);
streamPtr->write((uint8_t)0x41);
}
I'm trying to write code for 2 sensors, it takes a distance average, then, depending on that value, either flashes a light or keeps it solid. I can get it to work flawlessly with one sensor, but when I add the second sensor code, the output stops after going through the loop once. I think I'm setting up the serial ports incorrectly? I noticed another person had a similar issue, but it didn't prove helpful for solving my problem. the issue starts where comment is ** Any help is greatly appreciated; my code is below and an image of Serial Monitor:
`
#include <Arduino.h>
#include <stdint.h>
#include <Wire.h>
#include "stdio.h"
#include <DFRobot_TFmini.h>
#include <SoftwareSerial.h>
#include "TFMini.h"
// Setup software serial port for both sensors
SoftwareSerial mySerial_L(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerial_R(6, 7); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini_L;
TFMini tfmini_R;
int i, j;
int LED_pin_L = 12; //sets LED for LEFT signal as pin 13
int LED_pin_R = 13; //sets LED for RIGHT signal as pin 12
uint16_t dist_L[5], dist_R[5], avg_R, avg_L;
void setup() {
Serial.begin(115200);
pinMode(LED_pin_R, OUTPUT);
pinMode(LED_pin_L, OUTPUT);
}
void loop() {
mySerial_L.begin(115200);
tfmini_L.begin(&mySerial_L);
uint16_t sumR = 0, sumL = 0, veh_apr_L = 0; // sets all variables to 0, resets every loop iteration
uint16_t avg_R = 0, avg_L = 0; //Measure Distance and get signal strength
for (i = 0; i < 5; i++) //gets L sensor data
{
dist_L[i] = tfmini_L.getDistance();
sumL += dist_L[i];
Serial.println(dist_L[i]); //for testing so I can see each data point
delay(50);
}
avg_L = (sumL / 5);
Serial.print("Distance = ");
Serial.print(avg_L);
Serial.println("cm");
delay(50);
mySerial_L.end();
//**This is where it seems to have issues
// **If I remove everything after this relating to second
// **sensor, it works perfectly
delay(50);
mySerial_R.begin(115200);
tfmini_R.begin(&mySerial_R);
for (i = 0; i < 5; i++) //gets R sensor data
{
dist_R[i] = tfmini_R.getDistance();
sumL += dist_R[i];
Serial.println(dist_R[i]);
delay(50);
}
avg_R = (sumR / 5);
Serial.print("Distance = ");
Serial.print(avg_R);
Serial.println("cm");
delay(50);
if (avg_L >= 50 && avg_L < 90)
{
digitalWrite(LED_pin_L, HIGH);
}
else if (avg_L < 50)
{
for (i = 0; i < 8; i++)
{
digitalWrite(LED_pin_L, LOW);
delay(62);
digitalWrite(LED_pin_L, HIGH);
delay(62);
}
}
else
{
digitalWrite(LED_pin_L, LOW);
}
if (avg_R >= 50 && avg_R < 90)
{
digitalWrite(LED_pin_R, HIGH);
}
else if (avg_R < 50)
{
for (i = 0; i < 8; i++)
{
digitalWrite(LED_pin_R, LOW);
delay(62);
digitalWrite(LED_pin_R, HIGH);
delay(62);
}
}
else
{
digitalWrite(LED_pin_R, LOW);
}
delay(50);
}
`
Note: If you are trying to run BasicReading.ino on a ESP8266 / NodeMCU 12E, then you will need to make sure you are using ESPSoftwareSerial library and not standard SoftwareSerial otherwise tfmini.getDistance(); will cause a crash and soft restart in an infinite loop.
On running the BasicReading.ino example, changing pins for mySerial to D7 as RX and D8 as TX, and enabling TFMINI_DEBUGMODE the call to tfmini.getDistance() causes this error:
ERROR: no header
Sometimes it also returns a reading but this is just a bogus number.
From the last few hours trying different approaches, I don't think TFMini is compatible with ESP8266/ NodeMCU 12E due to the high baud rate expected from ESPSoftwareSerial.
It would have been great- a cheap WiFi connected LiDAR so I'm hoping I can be proven wrong.
I am facing error when i connect 2 tf mini lidar to arduino the output gets stuck and it doesn't work. I am attaching the code below.
Thank you
/*
The (UNO) circuit:
Green TX
White RX
Red +5V
Black Gnd
#include <SoftwareSerial.h>
#include "TFMini.h"
#include <NewTone.h>
// Setup software serial port
SoftwareSerial mySerialone(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerialtwo(15,14); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini1;
TFMini tfmini2;
void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(9600);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
Serial.println ("Initializing...");
}
void loop() {
// Take one TF Mini distance measurement
mySerialone.begin(115200);
tfmini1.begin(&mySerialone);
uint16_t dist1 = tfmini1.getDistance();
uint16_t strength1 = tfmini1.getRecentSignalStrength();
// Display the measurement
Serial.print(dist1);
Serial.print(" cm sigstr: ");
Serial.println(strength1);
mySerialone.end();
delay(500);
// Take one TF Mini distance measurement
mySerialtwo.begin(115200);
tfmini2.begin(&mySerialtwo);
uint16_t dist2 = tfmini2.getDistance();
uint16_t strength2 = tfmini2.getRecentSignalStrength();
// Display the measurement
Serial.print(dist2);
Serial.print(" cm sigstr: ");
Serial.println(strength2);
mySerialtwo.end();
// Wait some short time before taking the next measurement
delay(500);
if (dist1<100 || dist2<100)
{
Serial.println("**************************************************");
NewTone(3,50,100);
}
}
my controller is Arduino Pro mini
the library works well with TFmini model
36 cm sigstr: 119
36 cm sigstr: 120
36 cm sigstr: 120
36 cm sigstr: 119
36 cm sigstr: 120
but when I connect to TFmini plus (a model come with IP56 enclosure)
the error occurred repeatly.
Last error:
ERROR_SERIAL_BADCHECKSUM
65535 cm sigstr: 65535
Remark: TFmini plus work well with Benewake TF test software
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.