c++ - 为什么我的用于运行带障碍物传感器的蓝牙控制机器人的 Arduino 代码无法正常工作?

标签 c++ arduino arduino-uno arduino-ide

我之前使用 Arduino 和避障机器人创建了一个基于蓝牙的智能手机控制机器人(四轮车)。 现在,我想把它们结合起来。因此,我以适合我的方式组合了它们的功能和代码。但是,这对我的机器人来说似乎有点不对劲。 我的蓝牙控制机器人运行平稳。我的避障机器人运行完美。但是,当我试图加入他们时,他们都开始哭了。

我尝试更改函数的顺序,两次或三次添加一些函数,将它们从一些当时感觉不对的地方移除。没有什么是值得的。

我需要上传整个代码,因为我不确定我哪里做错了

#include <SoftwareSerial.h> 

#include <RemoteXY.h> 

// RemoteXY connection settings  
#define REMOTEXY_SERIAL_RX A2 
#define REMOTEXY_SERIAL_TX A3 
#define REMOTEXY_SERIAL_SPEED 9600 


// RemoteXY configurate   
#pragma pack(push, 1) 
uint8_t RemoteXY_CONF[] = 
  { 255,8,0,54,0,176,0,8,228,4,
  5,48,44,26,30,30,0,31,8,1,
  6,0,-84,-142,20,20,0,2,26,129,
  0,6,37,23,8,0,64,78,97,118,
  101,100,0,131,3,51,1,20,5,1,
  2,31,82,111,98,111,116,32,67,97,
  114,0,131,0,62,5,19,5,2,2,
  31,67,111,110,116,114,111,108,108,101,
  114,0,129,0,10,45,13,7,0,16,
  84,72,69,0,129,0,5,52,25,8,
  0,136,83,104,101,105,107,104,0,1,
  0,-34,-111,12,12,1,2,31,88,0,
  1,4,79,44,12,12,0,37,151,240,
  159,147,162,0,65,4,87,9,7,7,
  0,65,1,87,17,7,7,0,65,2,
  87,25,7,7,0,67,5,3,3,25,
  14,0,94,24,51,2,0,86,1,11,
  5,0,135,26,31,31,79,78,0,79,
  70,70,0 }; 

// this structure defines all the variables of your control interface  
struct { 

    // input variable
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position 
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position 
  uint8_t rgb_1_r; // =0..255 Red color value 
  uint8_t rgb_1_g; // =0..255 Green color value 
  uint8_t rgb_1_b; // =0..255 Blue color value 
  uint8_t button_1; // =1 if button pressed, else =0 
  uint8_t button_2; // =1 if button pressed, else =0 
  uint8_t switch_1; // =1 if switch ON and =0 if OFF 

    // output variable
  uint8_t red_led_r; // =0..255 LED Red brightness 
  uint8_t blue_led_b; // =0..255 LED Blue brightness 
  uint8_t green_led_g; // =0..255 LED Green brightness 
  char text_indicator[51];  // string UTF8 end zero 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY; 
#pragma pack(pop) 

///////////////////////////////////////////// 
//           END RemoteXY include          // 
///////////////////////////////////////////// 

#define PIN_BUTTON_2 A4
#define PIN_SWITCH_1 A5



#include<AFMotor.h>
#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define MAX_DISTANCE 250
//#define MAX_SPEED 150 // sets speed of DC  motors
//#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor left_motor_A(1, MOTOR12_64KHZ); 
AF_DCMotor left_motor_B(2, MOTOR12_64KHZ);
AF_DCMotor right_motor_A(3, MOTOR34_64KHZ);
AF_DCMotor right_motor_B(4, MOTOR34_64KHZ);
Servo myservo;   

int distance = 100;

int right_motor_speed = 0;
int left_motor_speed = 0;
//define two arrays with a list of pins for each motor
AF_DCMotor RightMotor[2] = {right_motor_A, right_motor_B};
AF_DCMotor LeftMotor[2] = {left_motor_A, left_motor_B};


//speed control of motors
void Wheel (AF_DCMotor * motor, int v) // v = motor speed, motor = pointer to an array of pins 
{
  if (v > 100) v=100;
  if (v < -100) v=-100;
  if (v > 0){

    motor[0].run(FORWARD);
    motor[1].run(FORWARD);
    motor[0].setSpeed(v * 1.75);
    motor[1].setSpeed(v * 1.75);
  }
  else if ( v<0 ){

    motor[0].run(BACKWARD);
    motor[1].run(BACKWARD);
    motor[0].setSpeed(v * 1.75);
    motor[1].setSpeed(v * 1.75);
/*    //digitalWrite (motor [1], FORWARD);
    analogWrite (motor [2], (v) * 0.75);
    //analogWrite (motor [2], (-v) * 0.75); */
  }
  else{
    motor[0].run(RELEASE);
    motor[1].run(RELEASE);
    motor[0].setSpeed(0);
    motor[1].setSpeed(0);
  }
}

 int lookRight()
 {
    RemoteXY.blue_led_b = 255;
    RemoteXY.red_led_r = 0;
    RemoteXY.green_led_g = 0;
    sprintf(RemoteXY.text_indicator, "CHECKING THE RIGHT SIDE.");
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
 }

 int lookLeft()
 {
    RemoteXY.blue_led_b = 255;
    RemoteXY.red_led_r = 0;
    RemoteXY.green_led_g = 0;
    sprintf(RemoteXY.text_indicator, "CHECKING THE LEFT SIDE.");
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
 }

 int readPing() 
 { 
  delay(100);
  int cm = sonar.ping_cm();
   if(cm==0)
  {
    cm = MAX_DISTANCE ;
  } 
  return cm;
 }

 void moveStop() 
 {
  left_motor_A.run(RELEASE); 
  left_motor_B.run(RELEASE);
  right_motor_A.run(RELEASE);
  right_motor_B.run(RELEASE);

 } 



void setup()  
{ 
  RemoteXY_Init ();  

  pinMode (PIN_BUTTON_2, OUTPUT);
  pinMode (PIN_SWITCH_1, OUTPUT);

  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);


} 

void loop()  
{  
  RemoteXY_Handler (); 

  digitalWrite(PIN_BUTTON_2, (RemoteXY.button_2==0)?LOW:HIGH);
  digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH);

    int distanceR = 0;
    int distanceL =  0;
    delay(40);

    if(distance<=27)
    {
      moveStop();
      RemoteXY.blue_led_b = 0;
      RemoteXY.red_led_r = 255;
      RemoteXY.green_led_g = 0;
      sprintf(RemoteXY.text_indicator, "AN OBSTACLE HAS COME IN FRONT OF YOUR ROBOT!!!");
      digitalWrite(PIN_BUTTON_2, HIGH);
      delay(1500);
      distanceR = lookRight();
      delay(250);
      distanceL = lookLeft();
      delay(250);

      if(distanceR>=distanceL)
      {
        RemoteXY.blue_led_b = 255;
        RemoteXY.red_led_r = 0;
        RemoteXY.green_led_g = 0;

        sprintf(RemoteXY.text_indicator, "IT IS GOOD TO GO ON THE RIGHT SIDE.");
      }

      else
      {
        sprintf(RemoteXY.text_indicator, "IT IS GOOD TO GO ON THE LEFT SIDE.");
      } 
    }
    else
    {
      RemoteXY.blue_led_b = 0;
      RemoteXY.red_led_r = 0;
      RemoteXY.green_led_g = 255;
      //manage the right motor
      Wheel (LeftMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
      Wheel (RightMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);

    }
    distance = readPing();

    //manage the right motor
    //Wheel (LeftMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
    //Wheel (RightMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);


  }

我希望我的机器人按照我的指示工作,当他感觉到前端有东西时,他应该停在那里并通过 RemoteXY.text_string 告诉我,然后检查左右两侧的障碍物并告诉我在哪个方可去。之后,它应该会再次通过我的手机接受我的订单。

首先,无法使用蓝牙将机器人连接到智能手机,因为机器人正忙于做一些其他事情(只有他知道他在做什么)。然后,我在我的代码(不在我在这里发布的代码中)上应用了一个带有“if” block 的“RemoteXY Button”;这样,如果通过智能手机按下此开关,机器人就会开始移动。所以,做这件事帮助我通过蓝牙将我的机器人连接到我的智能手机,但是,当我连接时,伺服电机在安全连接后定期旋转(同样,我不知道为什么),当我打开那个开关时,然后我的机器人只接受了我的第一个订单(通常是向前移动)并且它执行了我的第一个订单几乎前 3 到 5 秒,然后机器人挂断了他的系统并且我的智能手机自动与机器人断开连接。

我无法弄清楚我的代码中哪里有污点,或者我的代码本身就很脏。事情就是这样,我需要你们的帮助

您可能需要更好的支持/帮助的其他信息,我正在使用: 1).用于 Android 到机器人通信的 RemoteXY 应用程序 2). Arduino UNO 作为微 Controller 3).用于编程 Arduino 的 Arduino IDE 4).一台 SG 90 伺服电机。 5).四齿轮直流电机。 6). L293D Motor Driver Shield 用于驱动直流电机和伺服 7). HC SR-04 超声波障碍物传感器 8). 12V电池 9). HC-05 蓝牙通讯模块 10).用于外部指示器/喇叭的蜂鸣器。在引脚 A4 11). Arduino 编程的肮脏代码 😜。

任何有关代码的帮助将不胜感激。

提前致谢。 ❤️

最佳答案

对于需要同时执行许多功能的 arduino 机器人,您可能需要查看此 https://forum.arduino.cc/index.php?topic=223286.0

关于c++ - 为什么我的用于运行带障碍物传感器的蓝牙控制机器人的 Arduino 代码无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333093/

相关文章:

C++ 因式分解模板方法专门化模板类,这可能吗?

c++ - Arduino SIM800L : converting String to char* error

c++ - 如何通过NodeMCU从网络获取数据

c++ - cpp程序在访问类成员的成员变量时挂起

arduino - RabbitMQ 与 Arduino Uno

c++ - 电位器与第二个arduino板的通信状态

c++ - "final"C++ 中的类实现

c++ - 在 Linux 下,C++ 源代码如何成为可执行文件或静态/动态库。以及程序在运行时如何加载到内存中

C#/C++ : Launch an application and handle its I/O calls to the system

c - Arduino闹钟没有rtc项目错误