Index>Robot Kit>Alter All in One Kit for RPi>Lesson 6 How to Control 360° Servo
No related product information!

Lesson 6 How to Control 360° Servo

2119

    In this lesson, we will learn how to control 360° Servo.

6.1 Components used in this course


image.png


6.2 Introduction of 360° Servo 

What is a servo?

The servo is a position (angle) servo driver, which is suitable for those control systems that require constant angle changes and can be maintained. It has been widely used in high-end remote control toys, such as airplanes, submarine models, and remote control robots.

We use a 360° servo in this lesson, which can move between 0° and 360°. It is used in Alter Classic Car wheeled robot products of Alter multi-form products. Since the 360° servo can use the PWM signal to control the rotation angle of a certain mechanism, it is a more commonly used module in robot products. The walking robot, robotic arm and pan/tilt are all driven by the it. It is also used as the power unit of Alter Classic Car.

On the Raspberry Pi driver board Robot HAT, there is a PCA9685 chip specially used to control the servo. The Raspberry Pi uses I2C to communicate with the PCA9685. It controls the servo by sending pulse signals from the microcontroller. These pulses tell the servo mechanism of the servo where to move. The picture of the 360° servo is as follows:


image.png

【Note】:

1. The biggest difference between 360° servo and ordinary 180° servo is that the 360° servo can continuously rotate, but it cannot control the angle. It can use the PWM signal to control the speed and rotation direction.

2. Connect the 360° servo to the Robot HAT's PWM ports 8, 9, 10, and 11 in the Alter series product program.

3. If you want to control the 360°servo by using a program written by yourself, you can connect the 360°servo to any PWM port.

Control principle of 360°servo:

The direction and speed of the 360°servo can be controlled by adjusting the corresponding PWM port signal connected to the 360°servo.

When the PWM signal is less than a certain value, the 360°servo starts to rotate in one direction.The smaller the value, the faster the rotation speed; when the PWM signal is greater than a certain value, the 360°servo starts to rotate in the other direction.The larger the value, the faster the rotation speed.

The PWM value of each 360°servo at the beginning of rotation is different, so the two starting values of each 360°servo must be set respectively.


6.3 Wiring diagram (Circuit diagram)

    When the 360°Servo module is in use, it needs to be connected to the servo interface on the Robot HAT drive board. The 360° servo is connected to the Robot HAT's PWM ports 8, 9, 10, 11, and the yellow wire is connected to the yellow pin, the red wire is connected to the red pin, and the brown wire is connected to the black pin, as shown in the figure below:

image.png


6.4 How to control 360°Servo

6.4.1Run the program of this course

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/02CourseCode/01ComponentCode

image.png


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

ls

image.png


5. The 05Servo360 folder stores the sample code of this course. Enter the command to enter this folder:

cd  05Servo360

image.png


6. When using the 360°Servo module, we need to install the Python dependency library needed to control 360°Servo: Adafruit_PCA9685, and enter the following command in the console of the command window:

sudo pip3 install adafruit-pca9685

image.png


7.There are two python programs, ctrlServo.py and startPWM.py, enter the command to view the contents of the current directory:

ls

image.png


8.ctrlServo.py can control the rotation of the servo, directly enter the following commands to run this program on the Raspberry Pi:

sudo  python3  ctrlServo.py

image.png


9. After successfully running the program, you will observe that the 360°servo will rotate clockwise and counterclockwise.

10.startPWM.py controls the direction and speed of the 360 servo by adjusting the corresponding PWM port signal connected to the 360 servo. Then enter the following command to run this program on the Raspberry Pi:

sudo  python3  startPWM.py


After entering the above command, you need to enter the letter ‘w’ or ‘s’ in the command line, and press the Enter key to display a value, which is the analog value of the PWM.

When you enter the letter'w' and press Enter, the PWM value will increase by 1. When the PWM signal is greater than a certain value (you need to write down this data), the 360 servo starts to move in another direction (clockwise or counterclockwise).The larger the value, the faster the rotation speed.

image.png


Enter the letter's' and press Enter, the PWM value will decrease by 1. When the PWM signal is reduced to less than a certain value (you need to write down this data), the 360 servo starts to move in another direction.(clockwise or counterclockwise) The smaller the value, the faster the rotation speed

image.png


11. If you want to terminate the running program, you can press the shortcut key Ctrl+C on the keyboard.



6.4.2 The main code program of this lesson

After the above hands-on practice, you already know how to use and run our course sample code program. Then you must be curious to know how our code program is programmed on the Raspberry Pi to control the 360° servo. Let's learn about the main code program together. Here we use Sublime IDE to view and edit the code program of this course. Please refer to the content of Lesson 2 for the specific method.

(1) Let's first learn the program startPWM.py.

1. Import the library Adafruit_PCA9685 that controls the servo.

image.png


2. Instantiate the servo control object pwm, and set the PWM frequency of the servo to 50 (the frequency is not the duty cycle).

image.png


3. Define an initial PWM value with initPWM = 320. When you press ctrl+c to exit the operation, the PWM value of the corresponding interface will be set back to this initial value; ctrlPort = 8 sets the PWM port connected to the servo, fill in 8 here then the servo will be connected to the 8th PWM port.

image.png


4. Obtain keyboard input instructions with commandInput = input(). If the input is'w', the PWM value will increase by 1; if the input is's', the PWM value will decrease by 1; set the PWM value of the ctrl servo port to the new one with pwm.set_pwm(ctrlPort, 0, setPWM), print out the new PWM value. 

image.png


If you want to run the above program, you need to enter ‘w’ in the command line and press Enter, the PWM value will increase by 1, and after typing ‘s’ and press Enter, the PWM value will decrease by 1. After increasing or decreasing to a certain value, the wheel starts to rotate slowly. Write down this number, you need to record two numbers, one large and one small. They correspond to the initial PWM value when the wheel starts to rotate in two directions.


(2) Then let's learn the code program ctrlServo.py.

1. Import the library Adafruit_PCA9685 that controls the servo.

image.png


2. Instantiate the servo control object pwm, and set the PWM frequency of the servo to 50 (the frequency is not the duty cycle).

image.png


3. Set the PWM port that the servo is connected to. Fill in 8 here to connect the servo to the 8th PWM port.

image.png


4. startMoveA = 339 is the value at which the 360°servo starts to rotate when you enter'w’; startMoveB = 314 is the value at which the 3360°servo starts to rotate when you enter's’.

image.png


5. When the parameter direction in the ctrlServo(direction, speed=0) method is 1 and -1, it means to control the direction of rotation of the servo. When it is 0, the servo will stop rotating; the parameter speed means the import speed. If it is not imported, the value is 0 by default.

image.png


6. Import the parameters according to the position, the first is direction, and the second is speed.

image.png


7. Import according to the parameter name, the position can be arbitrary.

image.png


8. If the speed parameter is not imported, speed is the default value.

image.png