简体   繁体   English

如何用arduino控制伺服电机

[英]how to control servo motor with arduino

I saw many many videos on pwn and servo motor coding but can't understand how exactly the servo motor code works in arduino. 我在pwn和伺服电机编码上看到了很多视频,但无法理解arduino中伺服电机编码的工作原理。 my main confusion is what is the exact purpose of delay in the below code. 我的主要困惑是,以下代码延迟的确切目的是什么。 when we say servo.write(180); 当我们说Serve.write(180); doesn't it mean go to 180 degrees? 这不是说要达到180度吗? what is the use of the delay here? 延迟在这里有什么用? some milliseconds cause the servo to work properly or jitter or vibrate not move or incorrect rotations etc. 几毫秒会导致伺服器正常工作,或者抖动或振动不动或旋转不正确等。

include 包括

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  // in steps of 1 degree
 myservo.write(pos);  // tell servo to go to position in variable 'pos'
    delay(10); // waits 15ms for the servo to reach the position
  }

The code attempts to get the servo to move relatively slowly from 0 to 180 degrees. 该代码试图使伺服系统从0180度相对缓慢地移动。 10 ms times 181 steps equals 1.81 seconds . 10 ms乘以181步等于1.81 seconds

Most if not all servos are significantly faster than that if you just move straight from 0 to 180 in one go. 如果不是全部,大多数伺服器的速度要比仅从0直线移动到180的速度快得多。

One problem with this code is that you don't know what the servo's initial position is. 该代码的一个问题是您不知道伺服器的初始位置是什么。 For instance, if the initial position is 180, it will quickly jump to 0 and then slowly move back to 180. 例如,如果初始位置为180,则它将快速跳至0,然后慢慢移回180。

Also note that most servos are controlled with a signal every 20 ms or so, so changing the signal more often than that could lead to problems (like jitter/vibrations/noise/...). 还要注意,大多数伺服器每20毫秒左右就有一个信号被控制,因此改变信号的频率比改变信号的频率高可能会导致问题(例如抖动/振动/噪声/ ...)。

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

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