How to control servo
#Servomotor

How to control servo

Servo is mainly used for angle control. It can be used to connect different expansion hardware to realize different functions.

For example, use it to connect a mechanical claw to control the opening or closing of the mechanical claw to grab objects; use the servo to connect a part of the mechanical arm at the front and rear ends, and control the bending and extension of the mechanical arm by turning to connect the distance sensor on the smart car. When the car is not turning, test the distance in all directions and use the same method to rotate the electronic eye.

Its working principle is that the controller sends a PWM (pulse width modulation) signal to the servo to drive the motor to rotate, and at the same time the position detector (potentiometer) returns the position signal to determine whether it has reached the set position. Generally, the servo can rotate to the left 90 degrees, rotate 90 degrees to the right, the overall rotation range is 180 degrees. Therefore, if you want to rotate in the four directions up, down, left, and right, you need two servos to cooperate.

1. wiring

Servo generally has 3 wires ( feedback servo is a 4pin wire ), brown is ground wire connected to GND, red is power connected to VIN, orange is signal wire, in this example it is connected to X1 pin (servo of different brands, wire color May be different).

· If the connection is reversed, the servo may be burnt out. If you find the connection or the servo is heating up, please cut off the power as soon as possible and check the wiring

No alt text provided for this image

2. Principle of Control

The angle of servo rotation is achieved by adjusting the duty cycle of the PWM signal. Duty cycle refers to the ratio of time occupied by high level in one cycle, as shown in Figure 13.4.

For example: the period of the standard PWM signal is fixed at 20ms (50Hz), the theoretical pulse width range is between 1ms and 2ms, but in fact the pulse width is between 0.5ms and 2.5ms, the pulse width and the servo rotation angle are 0°~ Corresponds to 180°. Figure 13.4 Schematic diagram of pulse signal:

No alt text provided for this image

The servo rotates based on the duration of the pulse sent through the control line, and the pulse length will determine the distance the motor rotates. For example: 1.5ms pulse will make the motor rotate to 90 degree position. If it is shorter than 1.5ms, move it counterclockwise to the 0 degree position. Any signal longer than 1.5ms will cause the servo mechanism to rotate clockwise to the position of 180 degrees.

3.1 Operation Servo Method One

There are two methods for TPYBoard to control the servo. The first method is to send a pulse signal to the servo to control the rotation of the servo, which is the realization of the principle of the previous section. The second is to use the packaged control function to control the servo.

The code implementation of the first method is introduced here. Its function is to control the servo steering according to the inclination angle of the microcontroller. The angle of the single-chip microcomputer is obtained by the acceleration sensor on the TPYBoard。

01 import pyb 

02 import time 

03 

04 servo = pyb.Pin('X1', pyb.Pin.OUT_PP) 

05 accel = pyb.Accel() 

06 

07 while True: 

08     x = accel.x() 

09     servo.high() 

10     l = 0.0001*x + 0.0015 

11     time.sleep(l) 

12     servo.low() 

13     w = 0.02 - l 

14      time.sleep(w)

Line 02 introduces the time library to more accurately control the waiting time.

Line 04 opens the interface pointed to by the X1 pin in output mode.

Line 05 gets the acceleration sensor object, which is used to read the angle data of the single-chip microcomputer later.

Line 07 starts the main loop of the program, and the program will continue to run in a loop.

Line 08 reads the value of the acceleration sensor in the x direction, and the value range of x is between -50 and 50.

Line 09 inputs a high level to the servo.

The 10th line calculates the high-level duration, using 1.5ms as the base, and fine-tuning according to the value of x. If x is -20, then l is 0.0001(-20)+0.0015=0.0013, if x is 30, then l is 0.000130+0.0015=0.0018.

The 11th line sets the waiting time as l, that is, the high-level duration.

Line 12 outputs low level to the steering gear.

Line 13 subtracts the high-level duration from the complete period of 20ms to get the remaining time w.

Line 14 sets the waiting time as w, that is, the low-level duration to wait for the end of the pulse period.

3.2 Operation Servo Method Two

Here introduces the use of the packaged servo control function to operate the servo, this method is more simple and intuitive.

 

01 import pyb 

02 

03 accel = pyb.Accel() 

 

04 servo = pyb.Servo(1) 

05 

06 while True: 

07     x = accel.x() 

08     servo.angle(-x*2,300) 

09 pyb.delay(200)

Line 04 gets the encapsulated servo object.

Line 08 directly sets the angle to -x*2, if x is 20, the angle is -40 degrees.

Line 09 waits for 200 milliseconds to avoid reading data too frequently.

3.3 Precautions for Servo Operation

The drive capability of the single-chip microcomputer is limited, so when it is necessary to control more than one servo, an external power supply is required. A manipulator or robot often needs many servos to work at the same time. At this time, a servo control board (URT-1) needs to be added. The servo control board itself is a single-chip microcomputer. It can not only connect 16/24/32 servos, but also simplify The servo operation command has been issued.

In the above example, a 9g FS90 small servo is used. It uses the 5V power supply on the single-chip microcomputer. Some large servos need external power supply to drive. When the external power supply is connected, the voltage must be lowered to the specified voltage of the servo, otherwise the servo will be burnt out.

Generally, the servo can only rotate 90 degrees or 180 degrees in one direction. If you want to rotate back and forth, you need to use multiple servos.

If you use a mechanical claw, you need to pay attention to its rotation range, if the range is within 90 degrees, pay attention to set the positive and negative movement range between -45 degrees to 45 degrees, so as not to damage the hardware. As shown in Figure 13.5, the servo gear is connected to the left gear of the mechanical claw, and the servo rotation drives the right gear through the left gear to achieve the opening and closing effect of the mechanical claw.

Be careful not to pinch your fingers during the experiment.

No alt text provided for this image







要查看或添加评论,请登录

社区洞察

其他会员也浏览了