This software cyclically generates a 2D map of environment using RPLIDAR A3M1 and prints it on the integrated display of STM32F746G-DISCO board.
- Gallery
- Desktop version
- Implementation of RPLIDAR on STM32
- Setting up and building the project
- Editing configuration with Cube
- Sources
- Thanks
Garden
Room
Garage
Desktop version of this project developed by my friend can be found here -> https://github.com/knei-knurow/lidar-visualizations. It has many more advanced features which I did not implement in the STM32 version including histogram, adjustable zoom, adjustable "gap filling" between measured points, different display modes and mouse support. I highly recommend you to try RPLIDAR with it, apart from implementing it on STM32 platform.
RPLIDAR A3M1 is connected through UART to STM32 board. The speed of motor is controlled through hardware PWM on STM32. The MCU sets up continuous measurement and then receives packets of data from lidar continuously through DMA, and processes the data in UART interrupt. When a full 360° map is generated, the map (with colors representing the distances) is printed on the screen. The software automatically scales the map, so that it fits perfectly to the screen. The most distant point is marked, and the distance[m] from it is printed on the screen.
The project was created in STM32CubeIDE, using HAL library, code generated by CUBE (in separate files), BSP and CMSIS drivers. The code is written in C++, so every time Cube has to update generated C code, main.cpp is renamed to main.c, and after the update, it is renamed back to main.cpp.
RPLIDAR A3M1 is connected to the MCU through UART6 (Baud rate 256000) controlled by DMA2.
- MCU_UART6_RX(D0) --- LIDAR_TX
- MCU_UART6_TX(D1) --- LIDAR_RX
- MCU_TIM12_CH1(D6) --- LIDAR_PWM
- +5V --- +5V
- GND --- GND
The software uses the fastest data transmission mode of RPLIDAR A3M1 (dense/ultra-capsuled mode). Data parsing functions are similar of these in SDK, but with more low-level approach.
The data is stored in a buffer containing three 132B "data blocks" received from the lidar: "Previous", "Current", "Next". When a new block is completely received through DMA, an interrupt is handled. This block is the "Current" block. The interrupt sets up listening for the "Next" block, and the map data consisting of angle + distance pairs is calculated using the comparison between "Current" and "Previous" block. Two consecutive blocks are always necessary to calculate the data.
The calculated data is then stored in f_angles[]
and f_distances[]
array, where for any point k
, f_angles[k]
corresponds to the angle (float [0°-360°[ ) and f_distances[k
] is the distance from the lidar in millimetres. Use this data as you wish.
When a complete 360° map is complete, it is transformed into a 2D image placed in one of two present layers
Note that the MCU itself does not have enough RAM to store a full 480x272 32bpp display matrix. Both layers are stored in external SDRAM chip on the board, and they are accessed using BSP_LCD functions (BSP - Board Support Package by STM32). The layers are kept in separate memory banks, in order to prevent delays related to memory access issues.
The image layers are repetitively swapped so that the currently edited layer is never visible.
- Clone the repository and make sure you have the latest versions of STM32CubeIDE installed.
- Open the downloaded .project file
- Select "lidar-stm32/stm32-discovery/workspace" as the workspace path
- Check if import is successful
- Close "Information Center" tab :)
- Rename the
main.cpp
file tomain.c
- Open .ioc device configuration file
- Edit the configuration if you want. Then, launch "Device Configuration Tool Code Generation".
- Rename
main.c
back to main.c and comment outMX_LTDC_INIT();
after each time you do it (main.cpp, line 268)
- Build the project (CTRL+B)
- Have fun :)
For any changes in Cube configuration tool, repeat steps 5-10 carefully.
This project was created with use of the following sources
- RPLIDAR A3M1 Datasheet
- RPLIDAR Communication protocol specifications
- RPLIDAR SDK sources
... all of which can be found on RPLIDAR A Series support page and
- UM2298 User manual: STM32Cube BSP drivers development guidelines
- AN4861 Application note: LCD-TFT display controller (LTDC) on STM32 MCUs
- UM1907 User manual: Discovery kit for STM32F7 Series with STM32F746NG MCU
provided by ST Microelectronics.
Last access: 2020-12-30 for all sources.
This project was developed within Electronics and Computer Science Club in Knurów (KNEI for short) where lots of amazing projects and ideas come from. Have a look at our website - https://knei.pl/ - unfortunately, at the moment, only available in Polish. Check out our GitHub too - https://github.com/knei-knurow.
Numerous packages with colourful electronic gadgets like RPLIDAR have been granted to us by our friends from KAMAMI.pl.