0

我是 arduino/ESP32 的新手。我想在 ESP32 的帮助下控制几个伺服电机。我想以不同的速度控制所有伺服系统,并想使用正弦波逻辑来控制速度。

我有可以单独控制 2 个伺服系统的代码。我在这里附上了代码,

#include <ESP32Servo.h>
#include "esp32-hal-ledc.h"
// create two servo objects 
Servo servo1SV;
Servo servo2SV;

// Published values of servos; adjust if needed
int minUs = 550;
int maxUs = 2250;

// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33 
int servo1SVPin = 15;   //outlet valve
int servo2SVPin = 16;   //inlet valve


int pos = 0;      // position in degrees
ESP32PWM pwm;
void setup() {
    Serial.begin(115200);
    servo1SV.setPeriodHertz(50);      // Standard 50hz servo
    servo2SV.setPeriodHertz(50);      // Standard 50hz servo
}

void loop() {
    servo1SV.attach(servo1SVPin, 500, maxUs);
    servo2SV.attach(servo2SVPin, minUs, maxUs);  

//// About outlet valve

    for (pos = 0; pos <= 85; pos += 1) { // sweep from 0 degrees to 85 degrees
        // in steps of 1 degree
        servo1SV.write(pos);
        delay(10);             // Opening of outlet valve
    }
 delay(5000);            // Outlet valve stay openned for 5 sec 

    for (pos = 85; pos >= 0; pos -= 1) { // sweep from 85 degrees to 0 degrees
        servo1SV.write(pos);
        delay(10);            // closing of outlet valve
    }

// About inlet valve

    for (pos = 0; pos <= 70; pos += 1) { // sweep from 0 degrees to 70 degrees
        // in steps of 1 degree
        servo2SV.write(pos);
        delay(10);             // Opening of inlet valve 
    }
  delay(5000);            // Inlet valve stay openned for 5 sec

    for (pos = 70; pos >= 0; pos -= 1) { // sweep from 70 degrees to 0 degrees
        servo2SV.write(pos);
        delay(10);            // Opening of inlet valve
    }

    servo1SV.detach();
    servo2SV.detach();
    delay(5000);
}

我想要此代码给出的相同输出,但使用正弦波。但我不知道如何在 ESP32 中实现正弦波逻辑。我怎样才能做到这一点?

4

1 回答 1

0

我不确定你的意思

正弦波逻辑

但是假设您希望随时间推移的伺服位置是一个正弦函数,而您的问题是如何获得该正弦函数:

要么使用 Arduinos sin()函数,要么使用 LUT。

后者更常见,因为您不必每次都计算 sin 值,这非常昂贵。只需定义一个预先计算的值数组,其中包含每个伺服步骤的 sin 值。

于 2019-09-30T14:51:20.550 回答