Rp lidar (A1M8-R6), new version, issue with code

Actually I am using new version of rplidar a1m8-r6 with arduino mega connected to serial1 of it, circuit connection are as given below, SINCE NEW BOARD DOESN'T HAVE USUAL SPACERS BETWEEN PINS I AM USING THE TTL TO USB CONVERTER GIVEN ALONG WITH THE LIDAR, USING ANOTHER USB TO TTL CONVERTER, AND CONNECTING IN THE WAY GIVEN BELOW

ARDUINO MEGA ------------> RPLIDAR
+5V ----------------> +5V
GND ----------------> GND
RX1(18) ----------------> TX
TX 1(19) ----------------> RX

and the code I am using is given below

#include <RPLidar.h>
#define RPLIDAR_MOTOR 3  // The PWM pin for control the speed of RPLIDAR's motor (MOTOCTRL).

RPLidar lidar;

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);  // For RPLidar
  lidar.begin(Serial1);
  pinMode(RPLIDAR_MOTOR, OUTPUT);  // set pin modes
  Serial.println("reached before serial");


  if (!lidar.begin(Serial1)) {
    Serial.println("Failed to initialize lidar!");
    while (1)
      ;
  }
  lidar.startScan();
  Serial.println("start scan run successfully");
}

float minDistance = 100000;
float angleAtMinDist = 0;

void loop() {
  Serial.println("entered loop");
  if (IS_OK(lidar.waitPoint())) {
    //perform data processing here...
    Serial.print("reached inside lidar.waitpoint");
    float distance = lidar.getCurrentPoint().distance;
    float angle = lidar.getCurrentPoint().angle;  // 0-360 deg

    if (lidar.getCurrentPoint().startBit) {
      // a new scan, display the previous data...
      printData(angleAtMinDist, minDistance);
      minDistance = 100000;
      angleAtMinDist = 0;
    } else {
      if (distance > 0 && distance < minDistance) {
        minDistance = distance;
        angleAtMinDist = angle;
      }
    }
  } else {
    analogWrite(RPLIDAR_MOTOR, 0);  //stop the rplidar motor
    // Try to detect RPLIDAR
    Serial.println("entered else condition, without checking lidar waitpoint");
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
      // Detected
      Serial.println("get device info run, succesfully");
      lidar.startScan();
      analogWrite(RPLIDAR_MOTOR, 255);
      delay(1000);
    }
  }
}

void printData(float angle, float distance) {
  Serial.print("dist: ");
  Serial.print(distance);
  Serial.print("    angle: ");
  Serial.println(angle);
}

once I start my serial monitor emits out this data

entered loop
reached before serial
start scan run successfully
entered loop
entered else condition, without checking lidar waitpoint
entered loop
entered else condition, without checking lidar waitpoint
entered loop

I am not able to get exact required data, what might be issue ? I am not sure if I have left out some important data for you to troubleshoot, please let me know if you need more data to answer, will be ready to give

Thank you

Link for the new version of rplidar a1m8-r6 is is here

Click here

what data do you get?

No, simply nothing, I guess communication is not established, tried changing wires and tried but still result is same

Any updates on this issue?, tried lots of ways and means but still not able to arrive at conclusion

it seems that communication started and then you handle something wrong or device is faulty.
do you can translate everything from Serial1 to Serial

Provide both a clear picture and wiring diagram of that setup.

Blockquote Provide both a clear picture and wiring diagram of that setup.
Blockquote

MORE details, this is the rplidar I am using click here, it comes with ttl to usb board with micro usb to usb male connectors, as I cannot directly use the pins as pins are really small, i am using female to female usb connector and then I connect to usb to ttl converter to connect to arduino mega, lidar is rotating once we connect, but no data transfer is happening




Blockquote it seems that communication started and then you handle something wrong or device is faulty.
do you can translate everything from Serial1 to Serial,
Blockquote

i am not able to communicate actually, i can't get any data from Serial 1, is this what you are asking?

Where's the wiring diagram?

If you're doing what I think (you have not yet provided a wiring diagram) that probably won't work. the USB to TTL converter likely must plug into a USB Host.

if you are not able to understand any part of it, do let me know , i am not aware of drawing circuits in softwares thats why I used paper and pen

milestone reached probably I guess, actually when I connect the ttl to microusb converter directly to pc, onboard led on the converter glows like this

and I am able to read using rplidar sdk without any issues, and also I tried using inside ros while using jetson nano it works,

but when I connect it to arduino mega using usb to ttl converter it doesn't work, any way out?

actually, rplidar has given its own ttl to usb converter, it uses cp2102 chip to convert to usb, and then after receiving through usb, I am using ch340g chip to convert from usb to ttl and then we are sending it to arduino, will there be an issue, due to this?

Lidar is USB slave device, it need USB host. use RaspberyPi instead of Arduino

But many people have connected with Arduino mega and seen it working, lots of posts on this forum itself is an evidence of it,

No, it's not. The external USB / UART converter that comes with it is a USB Slave. But the Lidar itself has UART Serial connections.

Unnecessary. @Siddique needs to figure out a way to connect from the Lidar's UART pins to the Arduino's. Perhaps construct or buy a custom cable. Worst case, cut off the connector that connects to the provided converter and connect the bare end of that cable to the Arduino's UART pins.

@Siddique, see this Amazon link:
https://www.amazon.com/gp/product/B07TJW5SXF/ref=ask_ql_qh_dp_hza
One of the pictures on that site shows the cable you need:

BTW, it looks like the LIdar might use JST 2.54mm connectors. If so, those are readily available and you could make a custom cable.

Let God reward you for all your help until now,

connected the lidar as per datasheet to Serial3 of arduino Mega by cutting the wires and rearranging this as shown in previous post circled like give away, lidar which has this datasheet datasheet being used

AND RP LIDAR A1M8 PROVIDES US WITH LIBRARY LIBRARY LINK

While using example program "simple connect" it doesn't emit correct data, comes up with random data, then I realized the code doesn't work for new version, if anyone could help me with interfacing this lidar with arduino mega connected to serial3, it would be of great help, have given correct datasheet for my product as older version is very popular in internet searches and also library runs good for older version and not new version, and this is the output from serial monitor from simple connect program


New Line
115200 baud
19:44:19.222 -> distance: 1494.25
19:44:19.222 -> angle: 34.06
19:44:19.222 -> distance: 6.50
19:44:19.222 -> angle: 487.20
19:44:19.222 -> distance: 128.00
19:44:19.222 -> angle: 0.22
19:44:19.222 -> distance: 128.00
19:44:19.222 -> angle: 0.27
19:44:19.222 -> distance: 0.00
19:44:19.222 -> angle: 77.23
19:44:19.222 -> distance: 0.00
19:44:19.222 -> angle: 84.27
19:44:19.222 -> distance: 0.00
19:44:19.225 -> angle: 90.06
19:44:19.225 -> distance: 128.00
19:44:19.225 -> angle: 91.22
19:44:19.225 -> distance: 128.00
19:44:19.225 -> angle: 104.06
19:44:19.225 -> distance: 0.00
19:44:19.225 -> angle

the code which I have modified from simple connect is this


// This sketch code is based on the RPLIDAR driver library provided by RoboPeak
#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(Serial3);
  Serial.begin(115200);
  //Serial.begin(115200)
  
  // 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
    Serial.print("distance: "); Serial.println(distance);
    Serial.print("angle: ");  Serial.println(angle);
    
    //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);
    }
  }
}

circuit diagram

CONNECTION IS AS FOLLOWS

LIDAR   -------------> ARDUINO MEGA

TX     -------------> 15(RX3 OF ARDUINO MEGA)
RX      -------------> 14
VCC_5V  -------------> 5V SUPPLY
GND     -------------> GND 

GND_MOTO   -------------> GND 
CTRL_MOTO   -------------> 3RD PIN OF ARDUINO MEGA
5V_MOTO         ------------->5V SUPPLY

I am struggling with this for the past 2 weeks to get the data out, this old post is the evidence of it, any help would be of great significance as far as I am concerned

1 Like

data received is not accurate and it shows some random data, what might be the possible issue as, when I connect it to ros(rviz) 2d mapping is perfect, but not here, what could be the possible reason

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.