1

I don't understand how multiple PWM outputs are supposed to work. Look at the pic. enter image description here

In the first(1) case we are using short signal width which would be close to motor staying still. In this case, as can be seen, short pulses follow each other, so does the code.

motor1.writeMicroseconds(shortWidth);
motor2.writeMicroseconds(shortWidth);
motor3.writeMicroseconds(shortWidth);
motor4.writeMicroseconds(shortWidth);

when motor4 ended it's output, motor1 starts it's pulse again causing non-problem consequent pulses.

In the second(2) case pulse is wider which corresponds to setting motor speed close to maximum. After motor1 finishes generating width, it's time for motor2 to generate one. But when it does, motor1's period comes to end and it has to start generate width again, but arduino is busy generating pulse of motor2.

How does PWMs work in this case?

4

1 回答 1

1

您可能想直接查看 和 的Servo.h代码Servo.cpp。但是有两件事可以帮助理解:

  1. 伺服不响应 PWM(脉冲宽度调制 - 脉冲持续时间与周期持续时间,但 PDM(脉冲持续时间调制 - 绝对时间值)。这意味着当脉冲持续时间为 0 时,伺服不会变为 0,它会变为中间范围(90°)脉冲为 1.5 ms 时,向最小位置(0°),脉冲为 1 ms 时,向最大位置(180°),当脉冲持续时间达到 2 ms 时,总周期始终在 20 ms 左右。更准确地说,一般不会完全达到0°和180°,最小时间为0.5 ms,最大时间约为2.5 ms,但这超出了大多数舵机的规范。
  2. 几个舵机由一个定时器控制(通常是 Arduino 上的 timer_1)。当您执行 Servo.write(value) 时,定时器被编程,然后硬件在正确的时间产生引脚变化。

您可以尝试以下代码:

#include <Servo.h>

Servo myServo1;
Servo myServo2;
const int servo1Pin = 9;
const int servo2Pin = 10;

void setup(){
    // attach servo to pin 
    myServo1.attach(servo1Pin);
    myServo2.attach(servo2Pin);
}

void loop(){
    myServo1.write(15); //set servo to 15°
    myServo2.write(45); //set servo to 45°
    delay(1000); //wait 1 second
    myServo1.write(90); //set servo to 90°
    myServo2.write(90); //set servo to 90°
    delay(1000); //wait 1 second
    myServo1.write(165); //set servo to 165°
    myServo2.write(135); //set servo to 135°
    delay(1000); //wait 1 second
}
于 2015-01-10T22:27:52.387 回答