通常情况下,伺服电机(舵机)是由一个标准的直流系统和一个内部反馈控制装置(一个减速齿轮和电位计)来组成的。伺服电机(舵机)的主要作用是将齿轮轴旋转到一个预定义的方向上。伺服电机(舵机)有3个输入引脚,GND、VCC和Signal。脉冲宽度调制技术(PWM)被应用于舵机的控制,轴的方向由脉冲的持续时间决定。需要记住的是,舵机转动的方向不是由占空比决定的,而是由脉冲长度 t 决定的。有的舵机使用的PWM频率为 fPWM=50HZ,其对应于的PWM周期 T=20 ms。脉冲长度 t 和转动方向之间的关系是线性的,但也取决于电机和齿轮的配合。下面是一个脉冲长度和转动方向的关系图。
#!/usr/bin/env python # -*- coding: utf-8 -*- # 云台完整代码 from steering import Steering import time import configparser
classCamera: def__init__(self): ''' init camera's parameter ''' config = configparser.ConfigParser() config.read("config.ini") #Horizontal direction control parameters H_servoNum = config.getint("camera","H_servoNum") H_MinPosition = config.getint("camera","H_minPosition") H_MaxPosition = config.getint("camera","H_maxPosition") H_InitPosition = config.getint("camera","H_InitPosition") H_Speed = config.getint("camera","H_speed") #Vertical direction control parameters V_servoNum = config.getint("camera","V_servoNum") V_MinPosition = config.getint("camera","V_minPosition") V_MaxPosition = config.getint("camera","V_maxPosition") V_InitPosition = config.getint("camera","V_InitPosition") V_Speed = config.getint("camera","V_speed") #HC= horizontal control VC=vertiacl control self.HC = Steering(H_servoNum,H_InitPosition, H_MinPosition,H_MaxPosition,H_Speed) self.VC = Steering(V_servoNum,V_InitPosition, V_MinPosition,V_MaxPosition,V_Speed) defcameraRotation(self,direction): ''' This method is used to control the camera's rotating value means: HR = turn right HL = turn left VU = up VD = down GL = left prospective GF = front prospective GR = right prospective GU = up prospective GD = down prospective ''' if direction == "HL": self.HC.forwardRotation() elif direction == "HR": self.HC.reverseRotation() elif direction == "VU": self.VC.reverseRotation() elif direction == "VD": self.VC.forwardRotation() elif direction == "GL": # self.VC.reset() self.HC.turnleft() elif direction == "GR": # self.VC.reset() self.HC.turnrigth() elif direction == "GU": self.VC.turnrigth() elif direction == "GD": self.VC.turnleft() elif direction == "GF": self.VC.reset() self.HC.reset() elif direction == "S": self.VC.stop() self.HC.stop() else: print("input error,please input :HL,HR,VU,VD") if __name__ == "__main__": camera = Camera() while(True): direction = input("Please input direction: ") camera.cameraRotation(direction)