Index>Robot Kit>Alter All in One Kit for RPi>Lesson 29 Uniform Acceleration Multithreading Control of 360° Servo(Classic Car)
No related product information!

Lesson 29 Uniform Acceleration Multithreading Control of 360° Servo(Classic Car)

975

        This lesson introduces the use of mult-threading to achieve uniform acceleration control of 360° servo.

29.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.


29.2 Preparation 

        You need to prepare the assembled Classic Car robot, and make sure that the robot has been connected to the 360°servo.

image.png


29.3 Realizing uniform acceleration control of 360° servo with multithreading

29.3.1 Code program for multithreading control of 360° servo

Here we use Subline IDE to view and edit the code program of this lesson, please see the content of lesson 2 for specific methods. The specific code and comments are as follows:

In the file manager of the MobaXterm terminal, find the directory:

adeept_alter/server  

And open the code of this lesson: 360servo.py.

Import the library Adafruit_PCA9685 that controls the servo and instantiate the object used to control the PWM. Set the frequency of the PWM signal to 50.

image.png 


ID used to pause the thread.

image.png 


Set commandInput ='no', moveSpeed = 100.

image.png 


If command =='forward', then control the robot to move forward.

image.png 


If command =='backward', then control the robot to move backward.

image.png 


If command =='left', then control the robot to turn to left.

image.png 


If command =='right', then control the robot to turn to right.

image.png 


Start the robot; then control the robot forward at a speed of 100, and stop after 5s; again control the robot to turn to right at a speed of 100, and stop after 5s; control the robot to turn to left at a speed of 100 again, and stop after 5s and stop; control the robot backward again at a speed of 100, and stop after 5s.

image.png



29.3.2 Run the code program of multithreading control of 360° servo

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 360°Servo module, we need to install the Python dependency library needed to control 360°Servo: Adafruit_PCA9685, enter the following command in the console of the command window:

sudo pip3 install adafruit-pca9685

image.png


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

sudo  python3  360servo.py

image.png 


7.After successfully running the program, the Classic Car robot will accelerate uniformly.