Lidar Distance Microservice
Introduction
This document provides documentation for the Lidar Distance Controller, a Python-based program designed to handle Lidar data streaming, distance sending, and WebSocket communication. The program consists of three main threads: LidarDataThread
, DistanceSenderThread
, and WebSocketClientThread
. Additionally, utility functions and global variables are imported from utils
and globals
modules, respectively.
Components
- Lidar Distance Main (
LidarDistanceMain.py
):- This module initializes and manages the main control logic of the robot.
- It establishes connections, handles motor control, and manages various functionalities such as driving, spinning, and lightbar control.
- LidarDataThread (
LidarDataThread.py
):- Manages the Lidar data streaming from a specified IP and port.
- Utilizes a Lidar data buffer to store and provide data to other threads.
- DistanceSenderThread (
DistanceSenderThread.py
):- Uses the Lidar data buffer for accessing Lidar data
- Checks distances in five directions: left, front-left, front, front-right and right and sets the associated stopping flag if the distance is shorter than the minimum distance
- Sends a stream of distance data for the five directions over a TCP connection to a specified IP and port.
- WebSocketClientThread (
WebSocketClientThread.py
):- Establishes a WebSocket client connection to a specified IP and port.
- Sends a json package with the necessary information for stopping the robot to app control handler if one of the stop-flags, set by the DistanceSenderThread, is changing
- utils (
utils.py
):- Provides utility functions, including
pregenerate_lidar_data
for generating initial Lidar data,connect_to_server
for connecting to a tcp server andsignal_handler
for handling Ctrl+C signals.
- Provides utility functions, including
- globals (
globals.py
):- Contains global variables, including
stop_threads
to control thread termination and stop flags, e.g.stop_front
, to stop the robot if to close to an obsticle.
- Contains global variables, including
Usage
Prerequisites
- Python 3.x
Installation
-
Clone the repository:
git clone [repository_url]
Running the System
-
Run the LidarDistanceMain script:
python3 LidarDistanceController.py [lidarstream_ip] [lidarstream_port] [appHandler_ip] [appHandler_port] [distanceSender_ip] [distanceSender_port]
Example:
python3 LidarDistanceController.py 192.168.8.20 9011 192.168.8.20 6942 192.168.8.20 3031
Replace the arguments with the appropriate values.
-
To stop the program, use Ctrl+C. The program will gracefully terminate, stopping all threads.