Bonjour,
Je souhaite codé le capteur de distance tf-mini de Benewake, cependant je ne possède qu'une arduino méga.
Le constructeur affirme que le capteur peut fonctionner avec des cartes arduino DUE et UNO. Je suppose qu'il est donc possible d'adapter le code pour une arduino MEGA puisqu'il peut fonctionner avec une arduino UNO. Cependant le seul code fourni est un code faisant appel à plusieurs SERIAL. Je suppose donc que ce code est le code pour la arduino DUE.
Est-il possible de l'adapter pour une arduino méga? Et si oui comment?
Merci de vos réponses, voici le code fournis:
/* This program is a parsing routine of TF02 product standard output protocol on Arduino.
The format of data package is 0x59 0x59 Dist_L Dist_H Strength_L Strength_H Sequence_L Sequence_H
CheckSum_L
Refer to the product specification for detailed description.
For Arduino board with one serial port, use software to virtualize serial port’s functions: such as UNO board.
*/
#include<SoftwareSerial.h>// soft serial port header file
SoftwareSerial Serial1(2,3); // define the soft serial port as Serial1, pin2 as RX, and pin3 as TX
/For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use
Serial1 port/
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
void setup()
{
Serial.begin(9600);//set the Baud rate of Arduino and computer serial port
Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop()
{
if (Serial1.available())//check whether the serial port has data input
{
if(Serial1.read()==HEADER)// determine data package frame header 0x59
{
uart[0]=HEADER;
if(Serial1.read()==HEADER)//determine data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)// store data to array
{
uart[i]=Serial1.read();
}
check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
if(uart[8]==(check&0xff))// check the received data as per protocols
{
dist=uart[2]+uart[3]*256;// calculate distance value
strength=uart[4]+uart[5]*256;// calculate signal strength value
Serial.print("dist = ");
Serial.print(dist);// output LiDAR tests distance value
Page 4
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength);// output signal strength value
Serial.print('\n');
}
}
}
}
/* This program is a parsing routine of TF02 product standard output protocol on Arduino.
The format of data package is 0x59 0x59 Dist_L Dist_H Strength_L Strength_H Sequence_L Sequence_H
CheckSum_L
Refer to the product specification for detailed description.
For Arduino board with one serial port, use software to virtualize serial port’s functions: such as UNO board.
*/
#include<SoftwareSerial.h>// soft serial port header file
SoftwareSerial Serial1(2,3); // define the soft serial port as Serial1, pin2 as RX, and pin3 as TX
/For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use
Serial1 port/
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
void setup()
{
Serial.begin(9600);//set the Baud rate of Arduino and computer serial port
Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop()
{
if (Serial1.available())//check whether the serial port has data input
{
if(Serial1.read()==HEADER)// determine data package frame header 0x59
{
uart[0]=HEADER;
if(Serial1.read()==HEADER)//determine data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)// store data to array
{
uart[i]=Serial1.read();
}
check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
if(uart[8]==(check&0xff))// check the received data as per protocols
{
dist=uart[2]+uart[3]*256;// calculate distance value
strength=uart[4]+uart[5]*256;// calculate signal strength value
Serial.print("dist = ");
Serial.print(dist);// output LiDAR tests distance value
Page 4
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength);// output signal strength value
Serial.print('\n');
}
}
}
}