Index>Robot Kit>Alter All in One Kit for RPi>Lesson 27 Multithreading WS2812 LED Control
No related product information!

Lesson 27 Multithreading WS2812 LED Control

1402

This lesson introduces the use of multithreading to achieve some effects related to WS2812 LED.

27.1 Introduction to Multithreading

This course introduces the use of multithreading to achieve some effects related to WS2812 LED. Multithreading is a very common operation we use in robot projects, because robots have high requirements for real-time response. When performing a certain task,we try not to block the main thread communication.

Multithreading is similar to executing multiple different programs or tasks at the same time. Multithreading has the following advantages:

1. Use threads to put time-consuming tasks in the background for processing.

2. Improve the operating efficiency of the program. The subsequent real-time video and OpenCV processing video frames use multithreading to greatly increase the frame rate.

3. The encapsulated multithreading task is more convenient to call, similar to the non-blocking control method, that is, the control method of the servo is encapsulated by multithreading.

We use Python's threading library to provide thread-related work. A thread is the smallest unit of work in an application. The current version of Python does not yet provide multithreading priority, thread groups, and threads cannot be stopped, suspended, resumed, or interrupted.


27.2 Preparation 

You need to prepare a robot in Alter that has been assembled, and make sure that the robot has been connected to the WS2812 LED module.

image.png


27.3 Realize WS2812 LED effect with multithreading

27.3.1 Code program for warning lights/breathing LED

We use the following code to implement multithreading control of LEDS, and when the LED does not change, we keep the thread blocked to avoid wasting CPU resources.

Here we use Subline IDE to view and edit the code program of this course. Please refer to the content of Lesson 2 for the specific method. Here, the wait() method is used to block the thread. From the realization of the need to control the thread, the specific code and comments are as follows:

Use the Threading module to create a thread, inherit directly from threading.Thread, and then rewrite the __init__ method and run method.

image.png 


Some settings related to LEDS are initialized here.

image.png 


Set the brightness of the three color channels of RGB.There is no need to change the value, for these values will be automatically set after the subsequent call of the breathing LED function.

image.png 

Mode variable,'none' will cause the thread to be blocked and suspended, and the light will not change;'police' is the warning light mode, red and blue flashing alternately;'breath' breathing LED, you can set the specified color.

image.png 


Create a NeoPixel object with the appropriate configuration.

image.png 


Initialize the library (must be called once before other functions).

image.png 

Set the color of all lights.

image.png 

Set the color of some lights, the ID is an array of the serial numbers of the lights.

image.png 

Call this function, set __flag to False to block the thread.

image.png 

Call this function, set __flag to True to start the thread.

image.png 

Call this function to turn on the warning light mode.

image.png 

The concrete realization of the warning light mode.

image.png 

Blue flashes 3 times.

image.png 

Red flashes 3 times.

image.png 

To call this function to turn on the breathing LED mode, you need to enter three parameters, namely the brightness of the three color channels of RGB, which will be the color when the brightness of the breathing LED is at its maximum.

image.png 

The concrete realization method of breathing LED.

image.png 

All the lights gradually brighten.

image.png 

All lights gradually dim.

image.png 

This function is used to select tasks to execute.

image.png 

Functions for multithreading tasks.

image.png 

In the main function, instantiate the object that controls the LED and start the thread.

image.png 

Start breathing LED mode and stop after 15 seconds.

image.png 

Pause for 2 seconds.

image.png 

Start the warning light mode and stop after 15 seconds.

image.png



27.3.2 Run the code program of warning light/breathing LED


1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:

cd adeept_alter/server

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5.  When using the WS2812 module, we need to install the Python dependency library needed to control the WS2812 module. Enter the following command in the console of the command window:

sudo  pip3 install rpi-ws281x

image.png


6.robotLight.py is the sample code of this lesson, enter the command to run this program:

sudo python3 robotLight.py

image.png 


7.After successfully running the program, you will observe that the lights of WS2812 will change. First, the blue breathing LED effect will appear, and then the warning light effect will appear.