Notifications
Clear all

RPLidar A1 with Arduino works not constantly  

  RSS

jonaskell
(@jonaskell)
New Member
Joined: 2 months ago
Posts: 4
2020-07-15 5:54 pm  

Hi

I am currently working on a project with a RPLidar A1 connected to an Arduino UNO, where I need to display various distances on the serial monitor. The Lidar works and the connection to the Arduino as well, because I tested the Lidar via Frame Grabber and the Lidar spins when connected to the Arduino. My problem is that the serial monitor doesn't show constant values. After a few measurements the serial monitor prints "⸮P⸮%⸮". Does anyone know how I can fix this so the serial monitor will print constantly?

Serial monitor example:

⸮P⸮%⸮ distance: 0.00 // angle: 74.50
distance: 807.50 // angle: 69.48
⸮P⸮%⸮ distance: 802.25 // angle: 70.72
⸮P⸮%⸮ distance: 0.00 // angle: 138.00
distance: 5143.75 // angle: 131.48
⸮P⸮%⸮ distance: 5060.00 // angle: 132.69
⸮P⸮%⸮ distance: 391.50 // angle: 0.41
distance: 393.00 // angle: 1.69
⸮P⸮%⸮ distance: 394.50 // angle: 2.70
⸮P⸮%⸮ distance: 0.00 // angle: 59.47
distance: 0.00 // angle: 60.67
⸮P⸮%⸮ distance: 0.00 // angle: 61.88
⸮P⸮%⸮ distance: 3370.75 // angle: 111.27

My code:

#include <RPLidar.h>

RPLidar lidar;


#define RPLIDAR_MOTOR 3

void setup() {
lidar.begin(Serial);
pinMode(RPLIDAR_MOTOR, OUTPUT);

pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

Serial.begin(115200);
}

void loop() {
if(IS_OK(lidar.waitPoint())) {
float distance = lidar.getCurrentPoint().distance;
float angle = lidar.getCurrentPoint().angle;
bool startBit = lidar.getCurrentPoint().startBit;
byte quality = lidar.getCurrentPoint().quality;

if(angle < 180) {
Serial.print("distance: ");
Serial.print(distance);
Serial.print(" // angle: ");
Serial.println(angle);
}
}

else {
analogWrite(RPLIDAR_MOTOR, 0);

rplidar_response_device_info_t info;
if (IS_OK(lidar.getDeviceInfo(info, 100))) {
lidar.startScan();

analogWrite(RPLIDAR_MOTOR, 255);
delay(10);
}
}
}

Quote
hstaam
(@hstaam)
Trusted Member
Joined: 1 year ago
Posts: 58
2020-07-16 11:15 am  

I do not have much experience with lidar but I suggest you try 

putting  " lidar.begin(Serial);"   after the " Serial.begin(115200);"

Just a thought.

 

hj


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 6 months ago
Posts: 110
2020-07-20 2:50 am  

@jonaskell

You might have better luck if you reduce the serial speed to the monitor to 9600. That is the default speed for the Uno. It looks like the UART is having trouble syncing at 115200.


ReplyQuote
jonaskell
(@jonaskell)
New Member
Joined: 2 months ago
Posts: 4
2020-07-20 5:04 pm  

Thank you, but both ideas won't work. I checked the baud rate and tried othe rates, but none would work...

Any other ideas?


ReplyQuote
VE1DX
(@ve1dx)
Estimable Member
Joined: 1 year ago
Posts: 136
2020-07-20 5:47 pm  

@jonaskell That looks like the old mess we'd get on RS-232C interfaces if we had the number of data bits wrong, the parity bit set incorrectly, etc.  There are enough "good" characters to suggest it's close.  I agree with @ruplicator that the UART is likely the problem.  Typically old RS-232C interfaces ran at higher voltage swings than 5V — I recall -12V level corresponds to a logical '1' and +12V level corresponds to a logical '0'. 

However, that's old DB-25 connector era thinking . . . back in the medieval days of computing!  Whatever it is, it's close.  I assume you tried different cables, etc.  I wish I had something more to suggest, but I don't.  All I can say is it's close, but some little thing is causing it to act erratically.  Like 7 data bits instead of 8, etc.  If you resolve it, could you let us know what was causing it?

Paul VE1DX


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 6 months ago
Posts: 110
2020-07-20 6:54 pm  

@jonaskell

Grabbing at straws here but you don't have much of a delay in your loop and the Serial.print may be walking on each other. take a look at https://www.baldengineer.com/when-do-you-use-the-arduinos-to-use-serial-flush.html


ReplyQuote
jonaskell
(@jonaskell)
New Member
Joined: 2 months ago
Posts: 4
2020-08-04 7:03 pm  

@ve1dx

Yes, I will let you know. Honestly I don't have any clue what could fix my problem


ReplyQuote
jonaskell
(@jonaskell)
New Member
Joined: 2 months ago
Posts: 4
2020-08-04 7:05 pm  

@ruplicator

I tried the serial.flush() in a few ways, but none would work. Think that the delay caused by the serial.flush() is too high, because in some cases the lidar won't start working or gives more inconstant values than the orignial program.

But thanks anyway 🙂


ReplyQuote
triform
(@triform)
Reputable Member
Joined: 1 year ago
Posts: 330
2020-08-04 8:11 pm  

You are trying to use the same serial port for logging and the lidar?  Soft serial the Lidar if you can and on an Uno (or any 328p), nothing past 57k baud.  Period 🙂

 


ReplyQuote