0

在网上搜索了一番后,我能够以正弦方式运行 2 线直流电机。它是一个解构的打印机托架,由 Arduino Uno 和 L298N 电机驱动器驱动。我正在使用电位器来控制频率。该代码运行良好,但我需要一些帮助。

  1. 有没有一种简单的方法来控制它的运行频率?可能喜欢将其设置为 1.5、1.6、1.7 等赫兹,并让正弦函数运行电机?我目前正在测量赫兹,但我无法让它每隔一段时间准确运行。

  2. 有没有更优雅的方法来控制方向的反转?当罪波达到最小值时,我正在反转。但是在较低的频率下,它会保持最少几个样本,有时我的方法不起作用......我目前正在以一定的速度对样本进行计数,并且硬编码可以反转。


#define enA 9
#define in1 6
#define in2 7
#define INTERVAL 1000   // time between reads

int val;
int dir;
int count;

unsigned long lastRead = 0;
int frequency = 0;
int times = 0;
int sample_count = 0;

int sensorPin = 0;    // The potentiometer is connected to
// analog pin 0

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  Serial.begin(9600);
}

void reverseMotor(int pwnOutput) {
  /* reserse the motor direction
     uses the pwm outpur from the sine function
  */

  if (dir == 0) {
    Serial.println("LEFT <<<<<<<<<<<<<<<<<<<<<<<<<");
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    dir = 1;
  } else if (dir == 1) {
    Serial.println("RIGHT >>>>>>>>>>>>>>>>>>>>>>>>");
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    dir = 0;
  }
  analogWrite(enA, pwnOutput);
  sample_count++;
}

void runMotor(int pwnOutput) {
  analogWrite(enA, pwnOutput);
  sample_count++;
}

void loop() {
  static uint16_t phase;
  int sensorValue;
  int adjusting_speed;
  int reverse_fix;

  sensorValue = analogRead(sensorPin);
  phase = (map(sensorValue, 0, 1023, 1, 256));

  uint16_t sin_wave[phase];

  for (int16_t i = 0; i < (phase); i++) {
    float angle = TWO_PI * i / phase;
    int16_t val = sin(angle) * (1024 - 1);
    int pwmOutput;
    int dir;

    val += 1024;
    sin_wave[i] = val;
    pwmOutput = map(val, 0, 2047, 100, 255); // mapping higher than
    // 0 to ensure motor is
    // still moving,
    Serial.println(pwmOutput);
    //Serial.println(phase);
    //Serial.println(val);

    if (pwmOutput != 100) {
      runMotor(pwmOutput);
      count = 0;
    } else {
      count++;

      // THERE HAS GOT TO BE A BETTER WAY TO DO THIS
      if (phase >= 1 && phase <= 32) {
        reverse_fix = 1;
      }
      if (phase >= 33 && phase <= 54) {
        reverse_fix = 2;
      }
      if (phase >= 55 && phase <= 72) {
        reverse_fix = 3;
      }
      if (phase >= 73 && phase <= 91) {
        reverse_fix = 4;
      }
      if (phase >= 92 && phase <= 109) {
        reverse_fix = 5;
      }
      if (phase >= 110 && phase <= 127) {
        reverse_fix = 6;
      }
      if (phase >= 128 && phase <= 156) {
        reverse_fix = 7;
      }
      if (phase >= 157 && phase <= 174) {
        reverse_fix = 8;
      }
      if (phase >= 175 && phase <= 193) {
        reverse_fix = 9;
      }
      if (phase >= 194 && phase <= 207) {
        reverse_fix = 10;
      }
      if (phase >= 208 && phase <= 233) {
        reverse_fix = 11;
      }
      if (phase >= 234 && phase <= 256) {
        reverse_fix = 12;
      }

      if (count >= reverse_fix) {
        reverseMotor(pwmOutput);
      }
    }
  }
  if (millis() - lastRead >= INTERVAL) { // if INTERVAL has passed
    /*
       Counting and measuring hertz
    */
    val = frequency;
    times++;
    float vals = val / times;
    float hertz = float(sample_count) / float(phase);
    lastRead = millis();
    Serial.println();
    Serial.println("Hz:");
    Serial.println(hertz);
    Serial.println();
    sample_count = 0;
  }
}

我是 Arduino 新手,因此感谢您提供任何帮助或建议。

4

0 回答 0