I wanted to create a 2d map of my environment in serial monitor using rplidar a1m8 arduino mega, where empty spaces are left as such and obstacles marked by some symbols ex: "*", tried this code but nothing is printed in serial monitor except "mapa". could anyone please guide me on this
#include <RPLidar.h>
RPLidar lidar;
#define RPLIDAR_MOTOR 3
#define mapa_SIZE_X 50
#define mapa_SIZE_Y 50
int mapa[mapa_SIZE_X][mapa_SIZE_Y];
void setup() {
lidar.begin(Serial3);
Serial.begin(115200);
pinMode(RPLIDAR_MOTOR, OUTPUT);
//lidar.setMotorPWM(255);
lidar.startScan();
}
void loop() {
if (IS_OK(lidar.waitPoint())) {
float distance = lidar.getCurrentPoint().distance;
float angle = lidar.getCurrentPoint().angle;
// Convert polar coordinates to cartesian coordinates
int x = int((distance / 10) * cos(angle));
int y = int((distance / 10) * sin(angle));
// mapa the cartesian coordinates to the mapa array
int mapa_x = x + (mapa_SIZE_X / 2);
int mapa_y = y + (mapa_SIZE_Y / 2);
if (mapa_x >= 0 && mapa_x < mapa_SIZE_X && mapa_y >= 0 && mapa_y < mapa_SIZE_Y) {
mapa[mapa_x][mapa_y] = 1;
}
}
// Print out the mapa to the serial monitor
Serial.println("mapa:");
for (int y = mapa_SIZE_Y - 1; y >= 0; y--) {
for (int x = 0; x < mapa_SIZE_X; x++) {
if (mapa[x][y] == 1) {
Serial.print("*");
} else {
Serial.print(" ");
}
}
Serial.println();
}
Serial.println();
delay(1000);
}
tx of rplidar to 15th pin of arduino mega
rx of rp lidar to 14th pin of arduino mega
vcc5v and 5v moto - to 5v supply
gnd, gnd moto to gnd supply
ctrl_moto to 3rd pin of arduino mega
I guess the problem lies here because i tried executing this code, and spits it some random data
#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);
// 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...
Serial.print("distance"); Serial.println(distance/10);
Serial.print("angle"); Serial.println(angle/10);
} 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);
}
}
}
output it gives is this
New Line
115200 baud
11:37:33.182 -> angle30.31
11:37:33.182 -> distance1025.55
11:37:33.182 -> angle3.16
11:37:33.182 -> distance0.00
11:37:33.182 -> angle31.47
11:37:33.182 -> distance4.00
11:37:33.182 -> angle30.60
11:37:33.182 -> distance60.38
11:37:33.182 -> angle32.24
11:37:33.182 -> distance115.20
11:37:33.279 -> angle32.64
11:37:33.279 -> distance1065.07
11:37:33.279 -> angle32.90
11:37:33.279 -> distance198.40
``` what might be the issue, connections are same as i have given in my first post?
i too guess, problem is with data that is given by lidar
It seems the LIDAR is detecting the vehicle (distances 4.00 to 0.00 ??cm??) but getting the angle wrong.
Put the LIDAR in a box, preferably circular, with the LIDAR xcvr at the center of the box (without the vehicle?). Readings should return equal. If equal, put a brick in the box to see ang/dist of the brick.
Is there a way to adjust the reading of the return or the moment of the transmit?
Dronebotworkshop made a video on LIDAR and the RPLIDAR that you have...
Then disconnect the USB cable and power the Arduino externally.
edit: Sorry, see next post. I did not pay attention to using the Mega2560, which has more ports.
19(RX), 18(TX)
17(RX), 16(TX)
15(RX), 14(TX)
0(RX), 1(TX)