目录
关于舵机的理论知识,网上有比较多的介绍,这里我就不赘述了(我也说不清楚)。这里只是简单说一下我所了解的,以及自己的一些想法:
- 舵机只能有两个旋转方向:要么正转、要么反转
- 舵机的转动有一个能够转动的最大角度和最小角度,当转动达到最值,也就不能再转动了(在舵机允许的最值之内,你可以自己重新定义这些最值)
- 要实现摄像头既能水平方向转动,又能垂直方向转动,那就需要两个舵机
- 我先实现一个控制舵机的类,用来实现一个舵机运动方向的控制;然后定义一个摄像机控制类,来实现摄像机方向的控制
- 我所实现的功能是输入对应指令,可以控制摄像头上、下、左、右方向的运动,以及让摄像头回归初始位置
说明:摄像头实现摄像功能这里就不讲了,网上有很多资料可以参考,我用的是MJPG-Streamer,实现方式还是比较简单的
1. 舵机控制程序
steering.py:
# -*- coding: UTF-8 -*-
import RPi.GPIO as GPIO
import time
import atexit
# 这个类表示单个的SG90模块
class Steering:
max_delay = 0.2
min_delay = 0.04
def __init__(self, channel, init_position, min_angle, max_angle, speed):
self.channel = channel
self.init_position = init_position
self.position = init_position
self.min_angle = min_angle
self.max_angle = max_angle
self.speed = speed
atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self.channel, GPIO.OUT, initial=False)
self.pwm = GPIO.PWM(self.channel, 50) # PWM
self.pwm.start(2.5 + 10 * self.position / 180) # 让舵机转到初始位置
time.sleep(Steering.max_delay)
self.pwm.ChangeDutyCycle(0) # 这一步比较重要,如果不加的话,舵机会不规则抖动(具体原因还不知道)
time.sleep(Steering.min_delay)
def forwardRotation(self):
print("current postion: " + str(self.position))
if (self.position + self.speed) <= self.max_angle:
self.position = self.position + self.speed
self.pwm.ChangeDutyCycle(2.5 + 10 * self.position / 180) # 设置舵机角度
time.sleep(Steering.min_delay)
self.pwm.ChangeDutyCycle(0) # 舵机回到中位
time.sleep(Steering.min_delay)
def reverseRotation(self):
print("current postion: " + str(self.position))
if (self.position - self.speed) >= self.min_angle:
self.position = self.position - self.speed
self.pwm.ChangeDutyCycle(2.5 + 10 * self.position / 180) # 设置舵机角度
time.sleep(Steering.min_delay)
self.pwm.ChangeDutyCycle(0) # 舵机回到中位
time.sleep(Steering.min_delay)
def reset(self):
'''
Reset the steering to the middle
'''
self.position = self.init_position
self.pwm.start(2.5 + 10 * self.init_position / 180) # 让舵机转到初始位置
time.sleep(Steering.max_delay)
self.pwm.ChangeDutyCycle(0) # 这一步比较重要,如果不加的话,舵机会不规则抖动(具体原因还不知道)
time.sleep(Steering.min_delay)
def stop(self):
self.pwm.stop()
time.sleep(Steering.max_delay)
GPIO.cleanup()
if __name__ == "__main__":
steer = Steering(38, 90, 45, 136, 5)
while True:
direction = input("Please input direction: ")
if direction == "F":
steer.forwardRotation()
elif direction == "R":
steer.reverseRotation()
elif direction == "S":
steer.stop()
2. 配置文件
config.ini
[car]
# This is the parmaters that will control the car's wheels
# The number is the interface number of GPIO (GPIO.BOARD)
LEFT_FRONT_1 = 7
LEFT_FRONT_2 = 11
RIGHT_FRONT_1 = 13
RIGHT_FRONT_2 = 15
LEFT_BEHIND_1 = 31
LEFT_BEHIND_2 = 33
RIGHT_BEHIND_1 = 35
RIGHT_BEHIND_2 = 37
[camera]
# This is the parameters that will control camera's horizonal direction move
HIntfNum = 38
HInitPosition = 90
HMinPosition = 40
HMaxPosition = 140
HSpeed = 5
# This is the parameters that will control camera's vertical direction move
VIntfNum = 40
VInitPosition = 120
VMinPosition = 75
VMaxPosition = 180
VSpeed = 5
[server]
3. 摄像机控制程序
camera_controler.py
# -*- coding = UTF-8 -*-
from steering import Steering
import time
import configparser
class Camera:
def __init__(self):
'''
Read config file to init camera's parameter
'''
config = configparser.ConfigParser()
config.read("config.ini")
# Horiazonal direction control parameters
HIntfNum = config.getint("camera", "HIntfNum")
HInitPosition = config.getint("camera", "HInitPosition")
HMinPosition = config.getint("camera", "HMinPosition")
HMaxPosition = config.getint("camera", "HMaxPosition")
HSpeed = config.getint("camera", "HSpeed")
# Vertical direction control parameters
VIntfNum = config.getint("camera", "VIntfNum")
VInitPosition = config.getint("camera", "VInitPosition")
VMinPosition = config.getint("camera", "VMinPosition")
VMaxPosition = config.getint("camera", "VMaxPosition")
VSpeed = config.getint("camera", "VSpeed")
self.HCameraControl = Steering(
HIntfNum, HInitPosition, HMinPosition, HMaxPosition, HSpeed)
self.VCameraControl = Steering(
VIntfNum, VInitPosition, VMinPosition, VMaxPosition, VSpeed)
def cameraRotate(self, direction):
'''
This method is used to contorl the camera's rotating
The value of parameter direction and its meaning as follow:
HR - Turn right
HL - Turn left
VU - Turn upward
VD - Turn downword
'''
if direction == "HL":
self.HCameraControl.forwardRotation()
elif direction == "HR":
self.HCameraControl.reverseRotation()
elif direction == "VU":
self.VCameraControl.forwardRotation()
elif direction == "VD":
self.VCameraControl.reverseRotation()
elif direction == "RESET":
self.HCameraControl.reset()
self.VCameraControl.reset()
else:
print(
"Your input for camera direction is wrong, please input: HR, HL, VU, VD or RESET!")
if __name__ == "__main__":
camera = Camera()
while(True):
direction = input("Please input direction: ")
camera.cameraRotate(direction)