合宙ESP32C3使用servo库后舵机无反应,怎么改角度都没用-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: 1162000863

合宙ESP32C3使用servo库后舵机无反应,怎么改角度都没用

[复制链接]
发表于 2022-8-23 16:33 | 显示全部楼层

  1. #if !defined( ARDUINO_ESP32C3_DEV )
  2.   #error This code is intended to run on the ESP32_C3 platform! Please check your Tools->Board setting.
  3. #endif

  4. #define TIMER_INTERRUPT_DEBUG       1
  5. #define ISR_SERVO_DEBUG             1

  6. // Select different ESP32 timer number (0-1) to avoid conflict
  7. #define USE_ESP32_TIMER_NO          1

  8. #include "ESP32_C3_ISR_Servo.h"

  9. // Published values for SG90 servos; adjust if needed
  10. #define MIN_MICROS      544  //544
  11. #define MAX_MICROS      2450

  12. int servoIndex1  = -1;
  13. int servoIndex2  = -1;

  14. void setup()
  15. {
  16.   Serial.begin(9600);
  17.   while (!Serial);

  18.   delay(200);

  19.   Serial.print(F("\nStarting ISR_MultiServos on ")); Serial.println(ARDUINO_BOARD);
  20.   Serial.println(ESP32_C3_ISR_SERVO_VERSION);
  21.   
  22.   //Select ESP32 timer USE_ESP32_TIMER_NO
  23.   ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO);

  24.   servoIndex1 = ESP32_ISR_Servos.setupServo(10, MIN_MICROS, MAX_MICROS);
  25.   servoIndex2 = ESP32_ISR_Servos.setupServo(2, MIN_MICROS, MAX_MICROS);

  26.   if (servoIndex1 != -1)
  27.     Serial.println(F("Setup Servo1 OK"));
  28.   else
  29.     Serial.println(F("Setup Servo1 failed"));

  30.   if (servoIndex2 != -1)
  31.     Serial.println(F("Setup Servo2 OK"));
  32.   else
  33.     Serial.println(F("Setup Servo2 failed"));
  34. }

  35. void loop()
  36. {
  37.   int position;

  38.   if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) )
  39.   {
  40.     for (position = 0; position <= 180; position++)
  41.     {
  42.       // goes from 0 degrees to 180 degrees
  43.       // in steps of 1 degree

  44.       if (position % 30 == 0)
  45.       {
  46.         Serial.print(F("Servo1 pos = ")); Serial.print(position);
  47.         Serial.print(F(", Servo2 pos = ")); Serial.println(180 - position);
  48.       }

  49.       ESP32_ISR_Servos.setPosition(servoIndex1, position);
  50.       ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position);
  51.       // waits 30ms for the servo to reach the position
  52.       delay(30);
  53.     }
  54.    
  55.     delay(5000);

  56.     for (position = 180; position >= 0; position--)
  57.     {
  58.       // goes from 180 degrees to 0 degrees
  59.       if (position % 30 == 0)
  60.       {
  61.         Serial.print(F("Servo1 pos = ")); Serial.print(position);
  62.         Serial.print(F(", Servo2 pos = ")); Serial.println(180 - position);
  63.       }

  64.       ESP32_ISR_Servos.setPosition(servoIndex1, position);
  65.       ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position);
  66.       // waits 30ms for the servo to reach the position
  67.       delay(30);
  68.     }
  69.    
  70.     delay(5000);
  71.   }
  72. }
复制代码
发表于 2022-8-23 16:34 | 显示全部楼层

可以使用这个库来控制
https://github.com/khoih-prog/ESP32_ISR_Servo
发表于 2022-8-27 12:03 | 显示全部楼层
1162000863 发表于 2022-3-30 17:38
成功动起来了,感谢大佬,希望更多人能看到吧。

...成功动起来了,感谢大佬,希望更多人能看到吧。....

啥原因,麻烦解读一下
发表于 2022-10-12 10:06 | 显示全部楼层
anglor 发表于 2022-3-30 12:12
尝试了一下,0~180度来回摆动:

没有用库,  直接 PWM   ,6666
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:46 , Processed in 0.099913 second(s), 13 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表