Arduino射频接收器不能与电机屏蔽一起工作

Arduino radio frequency receiver does not work with motor shield

本文关键字:电机 屏蔽 一起 工作 不能 接收器 Arduino      更新时间:2023-10-16

微控制器:SainSmart Mega 2560
电机护罩:Osepp电机护罩V1.0
我正试图在我的轮式机器人上实现射频通信,但当电机运行时,射频代码不会收到消息。

电机护罩使用引脚4,7,8,12
我已经将射频设置为发生在引脚22、23、5 上

我看到这个参考为什么VirtualWire与Arduino/ATmega328引脚D10中的PWM信号冲突?但我不确定这是否适用于我的情况。

在使用电机护罩时,如何使射频接收器/发射器工作

演示情况的代码:

  #include <RH_ASK.h>
  #include <SPI.h> // Not actually used but needed to compile
  RH_ASK driver(2000, 22, 23, 5,true); // ESP8266: do not use pin 11
  /// *************************
  //      MOTOR SETUP
  /// *************************
  // Arduino pins for the shift register
  #define MOTORLATCH 12
  #define MOTORCLK 4
  #define MOTORENABLE 7
  #define MOTORDATA 8
  // 8-bit bus after the 74HC595 shift register
  // (not Arduino pins)
  // These are used to set the direction of the bridge driver.
  #define MOTOR1_A 2
  #define MOTOR1_B 3
  #define MOTOR2_A 1
  #define MOTOR2_B 4
  #define MOTOR3_A 5
  #define MOTOR3_B 7
  #define MOTOR4_A 0
  #define MOTOR4_B 6
    // Arduino pins for the PWM signals.
    #define MOTOR1_PWM 11
    #define MOTOR2_PWM 3
    #define MOTOR3_PWM 6
    #define MOTOR4_PWM 5
    #define SERVO1_PWM 10
    #define SERVO2_PWM 9
    // Codes for the motor function.
    #define FORWARD 1
    #define BACKWARD 2
    #define BRAKE 3
    #define RELEASE 4
    void setup()
    {
        Serial.begin(9600); // Debugging only
        if (!driver.init())
             Serial.println("init failed");
      //comment out code below  to allow receiver to read radio frequency  communication
      //BEGIN
        motor(1, FORWARD, 255);
        motor(2, FORWARD, 255);
        motor(4, FORWARD, 255);
        motor(3, FORWARD, 255);
       //END
    }
    void loop()
    {
        uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
        uint8_t buflen = sizeof(buf);
        if (driver.recv(buf, &buflen)) // Non-blocking
        {
            int i=0;
            // Message with a good checksum received, dump it.
            driver.printBuffer("Got:", buf, buflen);
            buf[5]= '';
            Serial.println((char*)buf);
        }
    }

    void motor(int nMotor, int command, int speed)
    {
      int motorA, motorB;
      if (nMotor >= 1 && nMotor <= 4)
      {  
        switch (nMotor)
        {
        case 1:
          motorA   = MOTOR1_A;
          motorB   = MOTOR1_B;
          break;
        case 2:
          motorA   = MOTOR2_A;
          motorB   = MOTOR2_B;
          break;
        case 3:
          motorA   = MOTOR3_A;
          motorB   = MOTOR3_B;
          break;
        case 4:
          motorA   = MOTOR4_A;
          motorB   = MOTOR4_B;
          break;
        default:
          break;
        }
        switch (command)
        {
        case FORWARD:
          motor_output (motorA, HIGH, speed);
          motor_output (motorB, LOW, -1);     // -1: no PWM set
          break;
        case BACKWARD:
          motor_output (motorA, LOW, speed);
          motor_output (motorB, HIGH, -1);    // -1: no PWM set
          break;;
        case RELEASE:
          motor_output (motorA, LOW, 0);  // 0: output floating.
          motor_output (motorB, LOW, -1); // -1: no PWM set
          break;
        default:
          break;
        }
      }
    }
    void motor_output (int output, int high_low, int speed)
    {
      int motorPWM;
      switch (output)
      {
      case MOTOR1_A:
      case MOTOR1_B:
        motorPWM = MOTOR1_PWM;
        break;
      case MOTOR2_A:
      case MOTOR2_B:
        motorPWM = MOTOR2_PWM;
        break;
      case MOTOR3_A:
      case MOTOR3_B:
        motorPWM = MOTOR3_PWM;
        break;
      case MOTOR4_A:
      case MOTOR4_B:
        motorPWM = MOTOR4_PWM;
        break;
      default:
        speed = -3333;
        break;
      }
      if (speed != -3333)
      {
        shiftWrite(output, high_low);
        if (speed >= 0 && speed <= 255)    
        {
          analogWrite(motorPWM, speed);
        }
      }
    }
    void shiftWrite(int output, int high_low)
    {
      static int latch_copy;
      static int shift_register_initialized = false;
      if (!shift_register_initialized)
      {
        // Set pins for shift register to output
        pinMode(MOTORLATCH, OUTPUT);
        pinMode(MOTORENABLE, OUTPUT);
        pinMode(MOTORDATA, OUTPUT);
        pinMode(MOTORCLK, OUTPUT);
        // Set pins for shift register to default value (low);
        digitalWrite(MOTORDATA, LOW);
        digitalWrite(MOTORLATCH, LOW);
        digitalWrite(MOTORCLK, LOW);
        // Enable the shift register, set Enable pin Low.
        digitalWrite(MOTORENABLE, LOW);
        // start with all outputs (of the shift register) low
        latch_copy = 0;
        shift_register_initialized = true;
      }
      bitWrite(latch_copy, output, high_low);
      shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, HIGH);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, LOW);
    }

看起来您提供的引用可能是问题所在。要尝试该修复,只需找到RH_ASK.cpp文件并取消注释行16,如以下所示

// RH_ASK on Arduino uses Timer 1 to generate interrupts 8 times per bit interval
// Define RH_ASK_ARDUINO_USE_TIMER2 if you want to use Timer 2 instead of Timer 1 on Arduino
// You may need this to work around other libraries that insist on using timer 1
#define RH_ASK_ARDUINO_USE_TIMER2

您的电机正在使用引脚5:

#定义MOTOR4_PWM 5

试着为你的收音机使用不同的第三个引脚(确保你的软件和硬件与同一个新引脚匹配)。浏览一下规格表,可以选择引脚24。您的电机代码保留从0到12的每个引脚。所以,试着。。。

RH_ASK驱动程序(2000,22,23,24,true);//ESP8266:不要使用引脚11

或者使用类似的逻辑更改电机引脚。