Notifications
Clear all

Help with RPLIDAR Arduino driver

30 Posts
6 Users
2 Likes
386 Views
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

Hi, I'm new to the forums but have been looking at the content here involved with LIDARs. I've been trying to use the RPLIDAR driver for use with Arduino, provided in this article: https://dronebotworkshop.com/getting-started-with-lidar/

However, I haven't been able to get the simple_connect program to work. I'll post what the code looks like below. The Lidar works if I manually set RPLIDAR_MOTOR to 255, but the issue seems to be that the IS_OK function is coming back false. The code just jumps straight to the else statement where it sets the motor speed to zero.

I've tried looking through the driver files to understand what's going on, but I'm not incredibly experienced with programming so it is a little difficult for me to follow. I've tried changing baud rates, using external power on the Arduino (Uno), and swapping transmit and receive pins so far. Are there any other things I could try that someone may know about? 

Any and all help greatly appreciated!

 

 

Arduino code:

----------------------------------------

#include <RPLidar.h>

// You need to create an driver instance
RPLidar lidar;

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal

void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
lidar.begin(Serial);

// set pin modes
pinMode(RPLIDAR_MOTOR, OUTPUT);
}

void loop() {
if (IS_OK(lidar.waitPoint())) {
float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
float angle = lidar.getCurrentPoint().angle; //anglue value in degree
bool startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
byte quality = lidar.getCurrentPoint().quality; //quality of the current measurement

//perform data processing here...

} else {
analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor

// try to detect RPLIDAR...
rplidar_response_device_info_t info;
if (IS_OK(lidar.getDeviceInfo(info, 100))) {
// detected...
lidar.startScan();

// start motor rotating at max allowed speed
analogWrite(RPLIDAR_MOTOR, 255);
delay(1000);
}
}
}


   
Quote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

moonfassa Please do NOT type code in. Use the HELP on the right side to learn how to include code.

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1756
 

@moonfassa

If you are using the Arduino IDE right click the text and choose Select All, then right click again and select Copy as HTML.

In your forum post hit the Enter key a few times to make room to insert the source code. Then select the Source code icon in the top menu bar.

posts

In the Source code window right click between the p's where you want the code to go and select Paste and the OK button.

sourceCode

You end up with a nice looking source code like below.

To enlarge an image in a post right click the image and choose Open link in new window.

 

 

#include <RPLidar.h>

// You need to create an driver instance
RPLidar lidar;

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal

void setup() {
  // bind the RPLIDAR driver to the arduino hardware serial
  lidar.begin(Serial);

  // set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);
}

void loop() {
  if (IS_OK(lidar.waitPoint())) {
    float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
    float angle = lidar.getCurrentPoint().angle; //anglue value in degree
    bool startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
    byte quality = lidar.getCurrentPoint().quality; //quality of the current measurement

    //perform data processing here...

  } else {
    analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor

    // try to detect RPLIDAR...
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
      // detected...
      lidar.startScan();

      // start motor rotating at max allowed speed
      analogWrite(RPLIDAR_MOTOR, 255);
      delay(1000);
    }
  }
}


 

 

 


   
ReplyQuote
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

Thanks!

I don't see a way to edit the post so I'll be sure to do that next time


   
ReplyQuote
(@davee)
Noble Member
Joined: 2 years ago
Posts: 970
 

Hi @moonfassa,

  Sorry I don't have one of these units, so I can't check your code properly.

However, I had a quick scan of Bill's notes, and at first glance, your code seems different from his.

If you are stuck, could I suggest you follow his notes, including copying the code from the article and pasting directly into an EMPTY (ie after deleting the default template code) new Arduino IDE window, and try to replicate what he does in the video.

If it works, then you have a base to modify a small piece at a time, after each change, check it is still working.

If you get stuck at some point, then come back to the forum with an update and hopefully someone may be able to help. Please double check your wiring is IDENTICAL to Bill's, and confirm the results of that check in your request.

Best wishes, Dave


   
ReplyQuote
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

No worries,

There are a few things going on in that article: he's using a TFMini for the first part of it, and an RPLIDAR for the second part, which is what I'm using. The code I've taken is just one of the examples from the downloadable Arduino library he's provided on github. He has another example, but it used a colour-changing LED which I don't have. 

Though on a second read, I see that he says the folder arrangement is incorrect for installation into the Arduino IDE. Then says you can download the corrected version "here." But I think he's forgotten to post the link there, or at least it's not showing up for me. 

I don't get any errors when I run the code however, so maybe the library posted is still the correct version. But I've checked my wiring thoroughly and tested all of the pins. The issue I think is with the line that says: 

if (IS_OK(lidar.getDeviceInfo(info, 100)))

The motor should start spinning if this check succeeds but it seems not to. This must be an issue with serial communication between the Arduino and Lidar or maybe something to do with the folder arrangement. I understand the Arduino only has one serial port, but I've tried disconnecting from my PC and powering it with a battery pack instead with no luck. 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

@moonfassa I suggest you read the blog again. As far as 'here' ALL Bill's blogs have a big blue box at the end. One of the several items in the one at the end of the Lidar blog is shown in the attached picture. When you have a problem with a publicly posted article and sketch, the chances that you are the first to have discover an error on Bill's part is a bet I will take every time. READ slowly and comprehend.

Screen Shot 2022 08 14 at 16.35.20

 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
DaveE reacted
ReplyQuote
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

Apologies. I downloaded this quite a few months ago and must've used those links since it looks like my version is the correct one. I re-downloaded just in case but still having the same issues. 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

@moonfassa As far as Serial, see attached pic.

Screen Shot 2022 08 14 at 17.24.59

 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

I briefly looked into this before since I think I'll need to use this library anyways in the future of my project. But I will look into it more and experiment, thanks


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

@moonfassa When you reply, click the reply link so your reply will generate an email to the person you are replying to. Perhaps reading the HELP might help?

ALso, I simply can't say this too many times, READ the blog that goes with the video, did you even know about that. I in trying to help you gave it a real quick look and picked up the solution to both your problems.

Sorry, I don't mean to be critical but it's very frustrating to see people asking questions that are answered in Bills YT video/Blog.

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
(@moonfassa)
Active Member
Joined: 8 months ago
Posts: 10
Topic starter  

@zander Understandable but I don't think any of my problems have been solved. I've read through the blog post and as I mentioned, there are two different lidars he is using, with different sets of code for each. I have set up everything the way he's explained for the RPLIDAR but the motor simply won't turn when I use the provided code. The software serial library is used in his example with the TF Mini, which is the other Lidar device he's using but I don't own this device.

Regardless, the code should be able to function without the need for this serial library, as long as the Arduino is externally powered. This is my understanding of it at least. I'm sure there is nothing wrong on Bill's end but whatever the difference is between mine and his setup is what I'm trying to figure out. 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

@moonfassa The purpose of using the Serial library is so you can have 2 serial devices, it has nothing to do with power. 

Yes Bill is showing us 2 devices, if you have only one of them, ignore the other and it's code.

Did you use the zip file that Bill provided in the blog? It does have the fixes. But first delete all the code you may have got from other sources as the compiler may still 'see' it and create a non-functioning program.

I don't have the funds to spend on such an expensive device and I have no current need for one in any case so I can't try Bill's code as I usually do.

I am sure if you performed exactly the same steps as Bill you will get the same results. Somewhere you deviated from Bill's instructions I know you think you didn't, but his works and you are here trying to get a copy of his working code working. 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2302
 

@moonfassa 

I don't know if it's related to your problem, but you're binding to the Serial stream but never actually issue a begin command for it. That would suggest that nothing that can be read from it which may explain why your sketch fails.

Experience is what you get when you don't get what you want.


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4537
 

@will This isn't it?

Screen Shot 2022 08 15 at 04.49.16

 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
Page 1 / 2