# YDLidar Setup with ROS
###### tags `ydlidar` `ydlidar-sdk` `Lidar driver installation`
There are two methods to setup YDLidar. The first method will cover the official ydlidar_ros_driver and the second one will cover an exclusive ydlidar repo.
## Method - 1 (Preferred)
### Installing YDLidar-SDK
`mkdir catkin_ws catkin_ws/src`
`cd ~/catkin_ws/src`
clone the YDLidar-SDK repo
`git clone https://github.com/YDLIDAR/YDLidar-SDK.git`
Now refer [this](https://github.com/YDLIDAR/YDLidar-SDK/blob/master/doc/howto/how_to_build_and_install.md) or you can follow these steps -
`sudo apt install cmake pkg-config`
`sudo apt-get install swig`
`sudo apt-get install python-pip`
`cd YDLidar-SDK/build`
> **If this step gives an error that build directory is not present then do the following**
>`cd ~/catkin_ws/src/YDLidar-SDK`
>`mkdir build`
>`cd build`
`cmake ..`
`sudo make`
`sudo make install`
`cd YDLidar-SDK`
`pip install .`
Now you need to compile the Cmake project in vscode. You can either refer [this](https://code.visualstudio.com/docs/cpp/cmake-linux#:~:text=variant%20you%20selected.-,Build%20hello%20world,Target%20from%20the%20Command%20Palette.) or follow the below mentioned steps -
* First, Ensure [Vscode](https://code.visualstudio.com/download) is installed.
* Ensure Cmake is installed or else `sudo apt install cmake`
* Ensure CMake Tools extension is installed in Vscode
* Ensure GCC is installed with the command `gcc --version`. If not search the internet to install it.
`cd ~/catkin_ws/src/YDLidar-SDK`
`code .`
Press CTRL + SHIFT + P and then type "select a kit" and choose the cmake:select a kit option. Choose your GCC compiler.
Then again CTRL + SHIFT + P and select a variant and choose the option of Release.
Again CTRL + SHIFT + P and build
You will also see at the very bottom bar in vscode an option to build, click on it to build.
`cd ~/catkin_ws/src/YDLidar-SDK/build`
`sudo cpack` (If cpack is not installed then `sudo apt install cpack`)
### Install the ydlidar_ros_driver
Refer [this](https://github.com/YDLIDAR/ydlidar_ros_driver/tree/master) or follow these steps -
`cd ~/catkin_ws/src`
>
git clone https://github.com/YDLIDAR/ydlidar_ros_driver.git ydlidar_ws/src/ydlidar_ros_driver
`cd ydlidar_ws`
`catkin_make` -
> If this step gives an error that std_srvs is not present then go to `cd ~/catkin_ws/src/ydlidar_ws/src/ydlidar_ros_driver` and open CMakeLists.txt and where you can see "find_package(catkin REQUIRED COMPONENTS)" and add "std_srvs" in that list and then try catkin_make again
`source ./devel/setup.sh` - Sourcing the workspace
You can also add this to .bashrc
`echo "source ~/ydlidar_ws/devel/setup.bash" >> ~/.bashrc`
* Create serial port Alias [optional]
`chmod 0777 src/ydlidar_ros_driver/startup/*`
`sudo sh src/ydlidar_ros_driver/startup/initenv.sh` - If you are facing any error at this step then leave this step and move on to next one.
Now Plug-in your USB cable which is connected to your lidar
`roslaunch ydlidar_ros_driver X4.launch` (As the lidar we are using is of X4 version)
## Method - 2
Clone this repo
`cd ~/catkin_ws/src/`
`git clone https://github.com/yangfuyuan/ydlidar_ros.git`
`cd ydlidar_ros`
Now you can refer to ydlidar_ros.pdf in this directory for further installation or simply follow these steps -
`cd ydlidar_ros/ydlidar_sdk`
`git submodule init`
`git submodule update`
`cd ~/catkin_ws`
`catkin_make`
> **If catkin_make fails with an error that in lock.c there is an undefined reference to major/minor, then do this**
> `cd ~/catkin_ws/src/ydlidar_ros/ydlidar_sdk/Serial`
> Open lock.c file and along with #include of other header files add this line
> `#include <sys/sysmacros.h>`
`roscd ydlidar/startup`
`sudo chmod 777 ./*`
`sudo sh initenv.sh`
Now Plug-in your USB cable which is connected to your lidar
`roslaunch ydlidar lidar.launch` - **If the lidar doesn't work then open this lidar.launch launch file and make these changes**
* Change the value for param port to "/dev/ttyUSB0"
* Change the baudrate param to 128000 (Check your device Baudrate on the YDLidar website)
Relaunch it and check if the lidar works now
If it still doesn't start spinning, then you have to find which usb port it is connected to with the command `lsusb`. Then add the name of the port in the launch file accordingly.