简体   繁体   English

如何使用Raspberry从Arduino控制伺服电机

[英]How to control Servo-Motor from Arduino with Raspberry

at the moment I am using a Servo-Motor and two Motors.目前我正在使用一个伺服电机和两个电机。 My goal is that if I execute the script, it should first start the motors, then turning the Servo-Motor at 30 degrees, waiting 3 seconds, turning the servo motor at his initial start and after 10 seconds the motors should stop.我的目标是,如果我执行脚本,它应该首先启动电机,然后将伺服电机旋转 30 度,等待 3 秒,在初始启动时旋转伺服电机,10 秒后电机应该停止。 However, my motors start without any problems but the Servo-Motor tries to make a 180 ° and both motors stop at the same time Here you can see my code:但是,我的电机启动时没有任何问题,但伺服电机尝试旋转 180°,两个电机同时停止,您可以在这里看到我的代码:

#!/usr/bin/env python
#import
from time import sleep
import RPi.GPIO as GPIO
from picamera import PiCamera

#Variables
in1 = 17
in2 = 27
in3 = 23
in4 = 24

enA = 22
enB = 25

spd = 15

servoMotor = 5


#Setup
GPIO.setmode(GPIO.BCM)

GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(in3, GPIO.OUT)
GPIO.setup(in4, GPIO.OUT)
GPIO.setup(enA, GPIO.OUT)
GPIO.setup(enB, GPIO.OUT)
GPIO.setup(servoMotor, GPIO.OUT)

GPIO.setup(in1, GPIO.LOW)
GPIO.setup(in2, GPIO.LOW)
GPIO.setup(in3, GPIO.LOW)
GPIO.setup(in4, GPIO.LOW)



#Variable 
a = GPIO.PWM(enA,1000)
b = GPIO.PWM(enB,1000)
#PWM with 50Hz
servoP = GPIO.PWM(servoMotor, 50) 
#Starting
a.start(spd)
b.start(spd)
servoP.start(0)

#Code
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
GPIO.output(in3, GPIO.HIGH)
GPIO.output(in4, GPIO.LOW)
sleep(10)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
GPIO.output(in4, GPIO.LOW)
servoP.ChangeDutyCycle(15)
sleep(3)
servoP.ChangeDutyCycle(7.5)
servoP.stop()

Sounds like you might have a continuous rotation (CR) servo. 听起来您可能有一个连续旋转 (CR) 伺服系统。 Even if you don't, all servos have different PWN ranges, so what servo you are using is a detail I would need to give you a better answer (Sorry for asking for clarification in an answer, but I can't comment). 即使您不这样做,所有舵机都有不同的 PWN 范围,所以您使用的舵机是一个细节,我需要给您一个更好的答案(对不起,要求在答案中澄清,但我不能发表评论)。

Try setting your duty cycle to a minimum of 3 and a maximum of 11. That appears to be a better range for the servos you are using.尝试将您的占空比设置为最小 3 和最大 11。对于您使用的伺服系统来说,这似乎是一个更好的范围。 As per the motors, could you please tell me what pins they are connected to?根据电机,你能告诉我它们连接到什么引脚吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM