简体   繁体   English

如何为伺服电机创建中断或返回值?

[英]how to create an interrupt or return value for servo motor?

It's been a frustrating day of learning from Arduino tuts.从 Arduino tuts 那里学习是令人沮丧的一天。

I'm working on setting up a servo motor.我正在设置伺服电机。 I troubleshooted why I couldn't get it to receive data and now I got it reading through the serial port and turning to appropriate degrees.我解决了为什么我无法让它接收数据的问题,现在我通过串行端口读取并转向适当的角度。 But now it seems every time it loops through it tries to reset it's value to 0 degrees.但是现在似乎每次循环时它都会尝试将其值重置为 0 度。 I haven't assigned the variable to be 0 and the while loop is supposed to act like an interrupt while it waits for user input.我没有将变量分配为 0,while 循环应该在等待用户输入时充当中断。 So I don't understand why it's doing this.所以我不明白为什么要这样做。

I've also tried to return the value pos to keep from changing/resetting values during each loop but keep getting compiling errors.我还尝试返回值pos以防止在每个循环期间更改/重置值,但不断收到编译错误。 I got one saying that pos returns void .有人说pos返回void And then I got another one when trying to declare the int pos as a method within the loop and nesting the rest of the code inside the int method.然后我在尝试将int pos声明为循环中的方法并将其余代码嵌套在 int 方法中时得到了另一个。

Also interesting side note: when you launch the serial port window in the IDE it'll rotate the motor by a small amount despite no input being given.还有一个有趣的旁注:当您在 IDE 中启动串行端口窗口时,尽管没有输入,它仍会少量旋转电机。 After an given input is entered, it'll go to those degrees then it resets as described before.输入给定的输入后,它会达到这些度数,然后如前所述重置。

Code:代码:

#include <Servo.h> //Including the Servo code library

int servoPin = 6;
int servoDelay = 25;
int pos;

Servo myPointer; // Create a Servo object called myPointer

void setup() {
  Serial.begin(9600);
  //pinMode (servoPin, OUTPUT);

  myPointer.attach(servoPin);

  Serial.println("Hello");

}

void loop() {    


    Serial.println ("Where would you like the servo to point?");
    while (Serial.available()==0){
    }

    pos = Serial.parseInt();

    Serial.println (pos);

    myPointer.write(pos);
}

The servo is running off the 5V power supply on the Arduino and receives instructions OK.伺服正在运行 Arduino 上的 5V 电源并接收指令正常。 It does not reset the position when running through void setup() so this must be the loop causing this.它在通过 void setup() 运行时不会重置位置,因此这一定是导致此问题的循环。 I just don't know why or how to fix it.我只是不知道为什么或如何解决它。

You have ensure the entire message is received before you start to parse it.在开始解析消息之前,您已确保接收到整个消息。 Either set a the inter character gap with Serial.setTimeout() (default 1s) to a larger value before use:在使用前将Serial.setTimeout() (默认为 1s)的字符间隔设置为更大的值:

Serial.setTimeout(2000);    
while (Serial.available() == 0) {}
duration = Serial.parseInt();

Or add a delay between Serial.available() and Serial.parseInt() if the above does not solve all your problems.或者在Serial.available()Serial.parseInt()之间添加延迟,如果上述方法不能解决您的所有问题。

while (Serial.available() == 0) {}
delay(4000);
duration = Serial.parseInt();

Serial.available() would fall thru on the first character received. Serial.available()会落在收到的第一个字符上。 So:所以:

  • Setting the inter character timeout would extend the time before parsing by waiting after each new character to see if any further characters would be received.设置字符间超时将延长解析前的时间,因为在每个新字符之后等待以查看是否会收到更多字符。
  • A delay before parsing would ensure that the entire message is received before you try and parse it.解析前的延迟将确保在您尝试解析之前接收到整个消息。

Additional characters like \\n or \\r could also be present triggering the parse but without there being sensible data.其他字符如\\n\\r也可能存在触发解析,但没有合理的数据。 Serial.parseInt() returns 0 if it fails to convert a number.如果Serial.parseInt()无法转换数字,则返回 0。 So:所以:

  • Do a range check for 1 to 360 degrees to catch these without losing much range of turning.进行 1 到 360 度的范围检查,以在不损失太多转弯范围的情况下捕捉这些。
  • Check if your terminal appends \\n or \\r .检查您的终端是否附加了\\n\\r
  • After parsing empty the receive buffers with Serial.flush() to get rid of any remaining \\n or \\r characters.使用Serial.flush()解析清空接收缓冲区以去除任何剩余的\\n\\r字符后。

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

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