简体   繁体   English

如何使用Arduino Mega控制伺服电机的速度

[英]How to control speed of a Servo motor using arduino Mega

I am working on a project in which I need to change the speed of servo motors. 我正在做一个需要更改伺服电机速度的项目。 The hardware I am using is an Arduino Mega 2560 board and I am using Servo.h library to control servos. 我使用的硬件是Arduino Mega 2560开发板,并且我正在使用Servo.h库来控制伺服器。 Servo rotates from o to 180 degree. 伺服从o旋转到180度。 I am using 12 servo motors in the project and have to control them simultaneously. 我在项目中使用12台伺服电机,必须同时控制它们。 Is there a way? 有办法吗?

You can use delay() function in a while or for loop 您可以在一段时间或for循环中使用delay()函数

Example: 例:

Servo s;
s.attach(9);
for(int i=0 ; i<180 ; i++)
{
    s.write(i);
    delay(10); //10 milisecond
}

If they all are the same degree, try this code below. 如果它们都是同一学位,请尝试以下代码。

At the very top (Not between any "{}"s) : 在最顶部 (不在任何“ {}”之间)

#include <Servo.h> 
Servo S1;
Servo S2;
Servo S3;
Servo S4;
Servo S5;
Servo S6;
Servo S7;
Servo S8;
Servo S9;
Servo S10;
Servo S11;
Servo S12;

Put this in Setup : 把它放在 Setup

S1.attach(1);
S2.attach(2);
S3.attach(3);
S4.attach(4);
S5.attach(5);
S6.attach(6);
S7.attach(7);
S8.attach(8);
S9.attach(9);
S10.attach(10);
S11.attach(11);
S12.attach(12);

You need to change the pin numbers. 您需要更改引脚号。

Just put this anywhere (Not between any "{}"s) : 只需将其放在任何地方 (不在任何“ {}”之间)

void TurnServos(int toDegree){
  servosAt = S1.read;
  if(servosAt == toDegree){

  }
  if(servosAt > toDegree){
    while(S1.read > toDegree){
      int currentToDegree = S1.read - 1;
      S1.write(currentToDegree);
      S2.write(currentToDegree);
      S3.write(currentToDegree);
      S4.write(currentToDegree);
      S5.write(currentToDegree);
      S6.write(currentToDegree);
      S7.write(currentToDegree);
      S8.write(currentToDegree);
      S9.write(currentToDegree);
      S10.write(currentToDegree);
      S11.write(currentToDegree);
      S12.write(currentToDegree);
      delay(10);         //Adjust this to make it faster or slower.
    }
  }
  if(servosAt < toDegree){
    while(S1.read < toDegree){
      int currentToDegree = S1.read + 1;
      S1.write(currentToDegree);
      S2.write(currentToDegree);
      S3.write(currentToDegree);
      S4.write(currentToDegree);
      S5.write(currentToDegree);
      S6.write(currentToDegree);
      S7.write(currentToDegree);
      S8.write(currentToDegree);
      S9.write(currentToDegree);
      S10.write(currentToDegree);
      S11.write(currentToDegree);
      S12.write(currentToDegree);
      delay(10);         //Adjust this to make it faster or slower.
    }
  }
}
void ClearServos(){
  int startDegree = 90;   //Change this number to anything you want.
  S1.write(startDegree);
  S2.write(startDegree);
  S3.write(startDegree);
  S4.write(startDegree);
  S5.write(startDegree);
  S6.write(startDegree);
  S7.write(startDegree);
  S8.write(startDegree);
  S9.write(startDegree);
  S10.write(startDegree);
  S11.write(startDegree);
  S12.write(startDegree);
}

How to use this: In setup before you do anything with servos but after the part I told you to put in setup , use ClearServos(); 使用方法:setup ,对伺服器进行任何操作之前,但在我告诉您安装程序的部分之后 ,请使用ClearServos(); to prepare the servos to be used. 准备要使用的伺服器。 (This probably isn't necessarily, but I don't know what happens when you use S1.read without changing it and if the servos are at different positions, it will fix problems. It can be avoided if it won't cause problems, but I think you should use it if you can.) All of them will turn to 90 degrees. (这可能不一定,但是我不知道使用S1.read而不更改它会发生什么,并且如果伺服器位于不同的位置,它将解决问题。如果不会引起问题,则可以避免。 ,但我认为如果可以的话,应该使用它。)所有这些都将变成90度。 (90 degrees can be changed with the variable startDegree in void ClearServos .) (可以使用void ClearServos的变量startDegree更改90度。)

To turn them, use TurnServos(90); 要转动它们,请使用TurnServos(90); . 90 is the degree you want it to turn to. 90是您想要的度数。


Haven't tested this because I don't have a Mega or 12 servos. 由于我没有Mega或12个伺服器,因此尚未测试。 Please comment if you notice any errors since this is huge. 如果发现任何错误,请发表评论,因为这是巨大的。 I spent a lot of time on this so I hope I helped. 我花了很多时间在此上,希望能有所帮助。 :) :)

Maybe you can put some resistors in series to your servo's VCC pin, before your servo motor to decrease the voltage across; 也许您可以在伺服电机的VCC引脚上串联一些电阻,以降低跨接的电压。 thus slowing it. 因此减慢了速度。 However, this will cause your servo's to be "constant" speed. 但是,这将使您的伺服器保持“恒定”速度。

An alternative could be to put a transistor in between your servo VCC connection and set PWM on base pin to regulate current (to regulate speed), but that would cost you an extra pin per servo if you're not using a multiplexer in between; 一种替代方法是在您的伺服VCC连接之间放置一个晶体管,并在基本引脚上设置PWM来调节电流(以调节速度),但是如果您之间不使用多路复用器,则每个伺服器会花费一个额外的引脚。 and could make your design a little more complicated. 并使您的设计更加复杂。

对于我的缩时摄影机上的平移和滑移,移动射击,360度伺服,最接近90-null的delayMicroseconds(value)最慢,要及时使用机械快门答题器(微型标准伺服)。

in Servo library WriteMicroseconds(...) function sets servo speed. 伺服库中的WriteMicroseconds(...)函数可设置伺服速度。

for more information please click 有关更多信息,请单击

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

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