树莓派使用pigpio 控制舵机

树莓派使用pigpio 控制舵机,第1张

pgpio的github地址在GitHub - joan2937/pigpio: pigpio is a C library for the Raspberry which allows control of the General Purpose Input Outputs (GPIO).https://github.com/joan2937/pigpio

git clone https://github.com/joan2937/pigpio
cd pigpio
make #编译
sudo make install #安装
sudo ./x_pigpio # 检查完整性
sudo pigpiod    # 运行
 make的结果

 sudo make install  sudo ./x_pigpio 检测运行

 

 

 然后执行 sudo pigpiod即可运行服务

PWMServo.py

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import pigpio
import time
from HwServo import PWM_Servo

Servos = ()
pi = None

def setServo(servoId, pos, time):
    if servoId < 1 or servoId > 2:
        return
    if pos > 2500:
        pos = 2500
    elif pos < 500:
        pos = 500
    if time > 30000:
        time = 30000
    elif time < 20:
        time = 20
    else:
        pass
    Servos[servoId - 1].setPosition(pos, time)

def setDeviation(servoId, d):
    if servoId < 1 or servoId > 2:
        return
    if d < -300 or d > 300:
        return
    Servos[servoId - 1].setDeviation(d)

def initLeArm(d):
    global Servos
    global pi
    pi = pigpio.pi()
    servo1 = PWM_Servo(pi, 5, deviation=d[0], control_speed=True)  # 扩展板上的7
    servo2 = PWM_Servo(pi, 13, deviation=d[1], control_speed=True)   # 6
    Servos = (servo1, servo2)

# 9克舵机的转动范围0~180度,对应的PWM值为 500~2500
# 设置偏差值
d = [0, 0]
# 初始化云台舵机
initLeArm(d)

def move_x(x_min,x_max,x_step):
    x_num = 0
    print("-"*10)
    print("x_min = " + str(x_min))
    print("x_max = " + str(x_max))
    print("x_step = " + str(x_step))
    print("-" * 10)
    for i in range(x_min, x_max, x_step):
        setServo(x, i, 100)
        time.sleep(0.1)
        x_num = i
    if x_step == abs(x_step) and x_num == x_max_num - abs(x_step):  # 如果扫描完一行,就往后退。同时y移动一格
        print("从右往左扫描完一行")
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("x_min = "+str(x_min))
        print("x_max = " + str(x_max))
        print("x_step = " + str(-x_step))
        return x_max,x_min,-x_step
    if x_step == -abs(x_step) and x_num == x_min_num + abs(x_step):
        print("从左往右扫描完一行")
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("x_min = " + str(x_min))
        print("x_max = " + str(x_max))
        print("x_step = " + str(x_step))
        return x_max, x_min, -x_step

#500-2500
def move_y(x_min, x_max, x_step,y_min,y_max,y_step):
    y_num = 0
    for i in range(y_min, y_max, y_step):
        setServo(y, i, 100)
        print("*"*10)
        print(i)
        print("*"*10)
        x_min,x_max,x_step = move_x(x_min, x_max, x_step)
        print("*"*5)
        print(x_min,x_max,x_step)
        print("*" * 5)
        time.sleep(0.1)
        y_num = i
    if y_step == abs(y_step) and y_num == y_max_num - abs(y_step):  # 如果扫描完一行,就往后退。同时y移动一格
        print("从上往下扫描完一面")
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("y_min = " + str(y_min))
        print("y_max = " + str(y_max))
        print("y_step = " + str(-y_step))
        return move_y(x_min,x_max,x_step,y_max, y_min, -y_step)
    if y_step == -abs(y_step) and y_num == y_min_num + abs(y_step):
        print("从下往上扫描完一面")
        print("y_min = " + str(y_min))
        print("y_max = " + str(y_max))
        print("y_step = " + str(y_step))
        return move_y(x_min,x_max,x_step,y_max, y_min, -y_step)

if __name__ == '__main__':
    x = 2
    y = 1
    x_step = 1#x的步长
    x_min = 500#
    x_max = 2500
    x_max_num = x_max
    x_min_num = x_min
    x_num = 0
    y_step = 40#y的步长
    y_min = 1300  #700
    y_max = 1500
    y_max_num = y_max
    y_min_num = y_min
    y_num = 0
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    move_y(x_min, x_max, x_step,y_min,y_max,y_step)


HWServo.py

#!/usr/bin/python3
# encoding: utf-8
import pigpio
import time
import threading
import cv2
import math

class PWM_Servo(object):

    def __init__(self, pi, pin, freq = 50, min_width = 500, max_width=2500, deviation = 0, control_speed = False):
        self.pi = pi
        self.SPin = pin
        self.Position = 1500
        self.positionSet = self.Position
        self.Freq = freq
        self.Min = min_width
        self.Max = max_width

        self.Deviation = deviation

        self.stepTime = 20
        self.positionInc = 0.0
        self.Time = 0
        self.Time_t = 0
        self.incTimes = 0
        self.speedControl = control_speed
        self.positionSet_t = 0
        self.posChanged = False
        self.servoRunning = False
        #self.pi.set_PWM_range(self.SPin, int(1000000 / self.Freq))
        #self.pi.set_PWM_frequency(self.SPin, self.Freq)
        if control_speed is True:
            t = threading.Thread(target=PWM_Servo.updatePosition, args=(self,))
            t.setDaemon(True)
            t.start()            

    def setPosition(self, pos, time = 0):
        #print(pos)
        if pos < self.Min or pos > self.Max:
            print(pos)
            return
        if time == 0:
            self.Position = pos
            self.positionSet = self.Position
            self.pi.set_PWM_dutycycle(self.SPin, self.Position + self.Deviation)
        else:
            if time < 20:
                self.Time_t = 20
            elif time > 30000:
                self.Time_t = 30000
            else:
                self.Time_t = time
            self.positionSet_t = pos
            self.posChanged = True
            
    def getPosition(self):
        return self.Position

    def updatePosition(self):
        while True:
            if self.posChanged is True:
                self.Time = self.Time_t
                self.positionSet = self.positionSet_t
                self.posChanged = False
                self.incTimes = int(self.Time / self.stepTime)
                self.positionInc =  self.Position - self.positionSet
                self.positionInc = int(self.positionInc / self.incTimes )
                self.servoRunning = True

            if self.servoRunning is True:
                self.incTimes -= 1
                if  self.incTimes <= 0:
                    self.Position = self.positionSet
                    self.servoRunning = False
                else:
                    self.Position = self.positionSet + int(self.positionInc * self.incTimes)
                try:
                    self.pi.set_servo_pulsewidth(self.SPin, int(self.Position + self.Deviation))
                except:
                    pass
            time.sleep(0.02)
            
        def setDeviation(newD = 0):
            if newD > 300 or newD < -300:
                return
            self.Deviation = newD

将上面两个文件放在一个目录下,运行python PWMServo.py即可

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/799063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存