Arduino处理串行输入需要1-2秒,如何加快速度?

huangapple go评论85阅读模式
英文:

Arduino takes 1-2 seconds to process serial input, how can I speed this up?

问题

I am almost entirely new to arduino/C++ so forgive me if this seems stupid. I am using a python program that detects faces in the camera frame and sends the x and y coordinate of the face to the arduino via serial (I have not implemented any code to deal with y yet). This works so long as I limit how often it sends the coordinates, however if they are sent too fast, the arduino seems to simply stop responding. My hypothesis as to why is that it is not able to read the data fast enough and it is lost before it can be processed. I know ultimately 1-2 seconds is not that big of a deal but unfortunately for me I'm a bit of a perfectionist and a 1-2 second delay really gets on my nerves.

As for the python script:

  1. while True:
  2. # Facial recognition stuff here
  3. for (x, y, w, h) in faces:
  4. cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  5. print(x, y)
  6. if i == 30:
  7. coord = f'{x} {y}'
  8. arduinoData.write(coord.encode())
  9. i = 0
  10. i += 1

and the code running on my arduino:

  1. #include <Servo.h>
  2. Servo myservo;
  3. int gLED = 2;
  4. int rLED = 3;
  5. String words[2];
  6. void setup() {
  7. Serial.begin(115200);
  8. pinMode(rLED, OUTPUT);
  9. pinMode(gLED, OUTPUT);
  10. myservo.attach(8);
  11. myservo.writeMicroseconds(1450);
  12. }
  13. void loop() {
  14. while (Serial.available() > 0) {
  15. String input = Serial.readStringUntil('\r');
  16. int spaceIndex = input.indexOf(' ');
  17. if (spaceIndex != -1) {
  18. words[0] = input.substring(0, spaceIndex);
  19. words[1] = input.substring(spaceIndex + 1);
  20. } else {
  21. words[0] = input;
  22. words[1] = "";
  23. }
  24. int pos1 = words[0].toInt();
  25. if (pos1 > 300) {
  26. digitalWrite(gLED, HIGH);
  27. digitalWrite(rLED, LOW);
  28. myservo.writeMicroseconds(1400);
  29. }
  30. if (pos1 < 240) {
  31. digitalWrite(rLED, HIGH);
  32. digitalWrite(gLED, LOW);
  33. myservo.writeMicroseconds(1510);
  34. }
  35. if (pos1 >= 240 && pos1 <= 300) {
  36. digitalWrite(gLED, LOW);
  37. digitalWrite(rLED, LOW);
  38. myservo.writeMicroseconds(1450);
  39. }
  40. }
  41. }

Does anyone have any tips on how I can speed up the response? Is it slow because of something stupid that I'm doing or is there maybe a different protocol that is faster? I briefly looked into i2c but I'm not sure if it's a better option.

英文:

I am almost entirely new to arduino/C++ so forgive me if this seems stupid. I am using a python program that detects faces in the camera frame and sends the x and y coordinate of the face to the arduino via serial (I have not implemented any code to deal with y yet). This works so long as I limit how often it sends the coordinates, however if they are sent too fast, the arduino seems to simply stop responding. My hypothesis as to why is that it is not able to read the data fast enough and it is lost before it can be processed. I know ultimately 1-2 seconds is not that big of a deal but unfortunately for me I'm a bit of a perfectionist and a 1-2 second delay really gets on my nerves.

As for the python script:

  1. while True:
  2. # Facial recognition stuff here
  3. for (x, y, w, h) in faces:
  4. cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  5. print(x, y)
  6. if i == 30:
  7. coord = f&#39;{x} {y}&#39;
  8. arduinoData.write(coord.encode())
  9. i = 0
  10. i += 1

and the code running on my arduino:

  1. #include &lt;Servo.h&gt;
  2. Servo myservo;
  3. int gLED = 2;
  4. int rLED = 3;
  5. String words[2];
  6. void setup() {
  7. Serial.begin(115200);
  8. pinMode(rLED, OUTPUT);
  9. pinMode(gLED, OUTPUT);
  10. myservo.attach(8);
  11. myservo.writeMicroseconds(1450);
  12. }
  13. void loop() {
  14. while (Serial.available() &gt; 0) {
  15. String input = Serial.readStringUntil(&#39;\r&#39;);
  16. int spaceIndex = input.indexOf(&#39; &#39;);
  17. if (spaceIndex != -1) {
  18. words[0] = input.substring(0, spaceIndex);
  19. words[1] = input.substring(spaceIndex + 1);
  20. } else {
  21. words[0] = input;
  22. words[1] = &quot;&quot;;
  23. }
  24. int pos1 = words[0].toInt();
  25. if (pos1 &gt; 300) {
  26. digitalWrite(gLED, HIGH);
  27. digitalWrite(rLED, LOW);
  28. myservo.writeMicroseconds(1400);
  29. }
  30. if (pos1 &lt; 240) {
  31. digitalWrite(rLED, HIGH);
  32. digitalWrite(gLED, LOW);
  33. myservo.writeMicroseconds(1510);
  34. }
  35. if (pos1 &gt;= 240 &amp;&amp; pos1 &lt;= 300) {
  36. digitalWrite(gLED, LOW);
  37. digitalWrite(rLED, LOW);
  38. myservo.writeMicroseconds(1450);
  39. }
  40. }
  41. }

Does anyone have any tips on how I can speed up the response? Is it slow because of something stupid that I'm doing or is there maybe a different protocol that is faster? I briefly looked into i2c but I'm not sure if it's a better option.

答案1

得分: 2

  1. @Roman is true. May be better to monitor the serial at regular interval and get data as they arrive:
  2. unsigned long timer;
  3. void loop() {
  4. if (millis() - timer > 200) { // 每200毫秒执行一次
  5. timer = millis();
  6. String input = serialListen();
  7. // 等等.....
  8. }
  9. }
  10. String serialListen() {
  11. String SerMsg = "";
  12. while (Serial.available()) {
  13. // 获取新字节:
  14. char inChar = (char)Serial.read();
  15. if (inChar == '\n') { //结束字符
  16. return SerMsg;
  17. } else {
  18. // 添加到输入字符串中:
  19. SerMsg += inChar;
  20. }
  21. }
  22. }
英文:

@Roman is true. May be better to monitor the serial at regular interval and get data as they arrive:

  1. unsigned long timer;
  2. void loop() {
  3. if (millis() - timer &gt; 200) { //tick every 200mS
  4. timer=millis();
  5. String input = serialListen();
  6. // etc.....
  7. }
  8. }
  9. String serialListen() {
  10. String SerMsg = &quot;&quot;;
  11. while (Serial.available()) {
  12. // get the new byte:
  13. char inChar = (char)Serial.read();
  14. if (inChar == &#39;\n&#39;) { //end char
  15. return SerMsg;
  16. } else {
  17. // add it to the inputString:
  18. SerMsg += inChar;
  19. }
  20. }
  21. }

huangapple
  • 本文由 发表于 2023年2月18日 03:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定