2016-08-02 55 views
2

微控制器工作:SainSmart兆2560
电机盾:Osepp电机盾V1.0
我想实现我的轮式机器人然而,当电机射频通信运行无线电频率码将不会收到消息。Arduino的射频接收器不与电机屏蔽

电机罩采用引脚4,7,8,12
我已经安装在销22,23发生射频,5

我看到这个参考 Why does VirtualWire conflicts with PWM signal in Arduino/ATmega328 pin D10? ,但我不知道这适用于我的情况。

如何在使用电机屏蔽时使射频接收器/发射器工作?

代码演示情况:

#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]= '\0'; 
      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); 
    } 

回答

0

它看起来像你给可能是问题的参考。要尝试修复只需找到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 
+0

当我做出更改RF组件停止通信。我必须使用定时器2还有其他的东西 – user3140515