Salted fish ZTMR example-servo

Salted fish ZTMR example-servo


Main control board: ZTMR development board

SG90 servo
range 180 ° (-90 ° ~ 90 °)
Insert picture description here

The servo is a kind of position (angle) servo drive, which is suitable for those control systems that need to keep changing the angle and can maintain it. Currently used in high-end remote control toys, such as aircraft models, including aircraft models, submarine models; remote control robots have been more commonly used. The servo is a common name, but it is actually a servo motor.

Pin description

Pin Explanation
Dark gray (brown) GND
red 5V
Orange X1

Servo usage
from pyb import Servo

usage Explanation
s1 = Servo (1) Use X1 control (X1, VIN, GND)
s1.angle(45) Turn to 45 °
s1.angle(-60,1500) Turn to -60 ° within 1500ms
s1.speed(50) Continue to rotate at speed 50
# main.py -- put your code here!
from pyb import Servo
s1=Servo(1)          #使用X1控制
s1.angle(45)         #转到45°
s1.angle(-60,1500)    #1500ms内转到-60°

Principle and code description salted fish hardware-servo Servo

Routine 1: The user presses USER to control the steering gear to turn, and displays the turning angle in the serial port

# main.py -- put your code here!
from pyb import Servo,Switch

sw = Switch()     #定义按键对象名字为sw
s1 = Servo(1)     #构建舵机对象s1,输出引脚为X1

#定义7组角度:0,30,60,90,-30,-60,-90
angle=[0,30,60,90,-30,-60,-90]

key_node = 0  #按键标志位
i = 0         #用于选择角度

def key():
    global key_node
    key_node = 1

sw.callback(key)  #记录按键


#X1指定角度,启动时i=0,默认0°
s1.angle(angle[i])

while True:

    if key_node==1: #按键被按下
        i = i+1
        if i == 7:
            i = 0
        key_node = 0 #清空按键标志位,回归到0°
        s1.angle(angle[i]) #X1指定角度
        print(angle[i])#打印X1角度     

Serial port display effect I
Insert picture description here
have talked about PWM before, try to use duty cycle to achieve

The PWM wave that controls the steering gear is a square wave with a frequency of 50 Hz, and the period is its reciprocal: 20 ms. In each period, the high level accounts for 0.5 ms to 2.5 ms. 0.5ms represents 0 degrees, 2.5ms represents 180 degrees, and other degrees can be scaled.

PWM (Pulse Width Modulation Module)

from pyb import Pin,Timer

p = Pin('X1')
ti = Timer(2,freq=1000)              #X1是定时器2的CH1
ch = ti.channel(1,Timer.PWM,pin=p)   #设置PWM引脚
ch.pluse_width_precent(50)           #设置PWM输出占空比

The control of the steering gear generally requires a time base pulse of about 20ms. The high level part of the pulse is generally in the range of 0.5ms-2.5ms, and the total interval is 2ms. The width of the pulse will determine the distance the motor rotates. For example: 1.5 ms pulse, the motor will turn to the 90 degree position (often called the neutral position, for a 180 ° servo, it is the 90 ° position). If the pulse width is less than 1.5 milliseconds, the motor axis is oriented in the direction of 0 degrees. If the pulse width is greater than 1.5 milliseconds, the axial direction is toward 180 degrees. Take the 180 degree servo as an example, the corresponding control relationship is as follows:
0.5ms ————- 0 degree;
1.0ms ———— 45 degree;
1.5ms ———— 90 degree;
2.0ms ——— 135 degrees;
2.5ms-180 degrees;

Insert picture description here
Routine 2: The user presses USER to control the steering gear to turn, and displays the turning angle in the serial port

from pyb import Pin, Timer, Switch

i = 7.5    # 20ms内的0.5-2.5换成百分比形式就是2.5%-12.5%。7.5即为中间。
sw = Switch()   # 初始化用户按键
p = Pin('X1')
ti=Timer(5,freq=50)
ch1=ti.channel(1,Timer.PWM,pin=p)  #X1为PWM输出

while True:
    swt=sw()
    if swt:
        i += 0.01
        pyb.udelay(1000)    # 延时1000us,减点速
        if i > 12.4:
            i = 12.4
    else:
        i -= 0.01
        pyb.udelay(1000)
        if i < 2.6:
            i = 2.6
    ch1.pulse_width_percent(i)   # 控制占空比,
    print(i)

Hold down the USER button to see the effect
Insert picture description here

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105636898