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

Why is my Arduino code for running a Bluetooth controlled robot with obstacle sensor not working as it should be?

本文关键字:传感器 障碍物 常工作 工作 机器人 用于 代码 Arduino 我的 运行 控制      更新时间:2023-10-16

我使用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);

  }

我希望我的机器人在我的方向上工作,当他感觉到前端的某些东西时,他应该停在那儿,并通过远程告诉我text_string,然后检查左右侧面是否有障碍物,并告诉我哪个侧面可以使用。之后,它应该再次通过手机接我的订单。

首先,使用蓝牙将机器人连接到智能手机变得不可能,因为机器人非常忙于做其他事情(只有他知道他在做什么(。然后,我在代码上应用了一个"远程"按钮(这不在我在此处发布的代码中(,上面是" if"块;因此,如果通过智能手机按下此开关,则该机器人才会开始移动。因此,做这件事帮助我通过蓝牙将机器人连接到智能手机,但是当我连接时,伺服电机经常旋转(再次,我不知道为什么(,当我打开该开关时,然后,我的机器人只采用了我的第一订单(通常是向前迈进(,并且在我的第一订单上执行了几乎3到5秒钟,然后机器人将其系统挂起来,并且我的智能手机与机器人自动断开。

我无法弄清我的代码中的污垢在哪里,或者我的代码本身是完全肮脏的。这就是问题,我需要你们的帮助

您可能需要获得更好的支持/帮助的其他信息,我正在使用:1(。用于机器人通信的Android的Remotexy应用程序2(。Arduino uno作为微控制器3(。用于编程Arduino的Arduino IDE4(。一个SG 90伺服电机。5(。四个齿轮DC-电动机。6(。L293D驾驶DC电动机和伺服器7(。HC SR-04超声传感器用于障碍物感应8(。12V电池9(。HC-05用于蓝牙通信的模块10(。用于外部指示器/号角的蜂鸣器。在针A411(。Arduino编程的肮脏代码。

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

非常感谢您。❤️

对于需要同时执行很多功能的Arduino机器人,您可能需要研究此https://forum.arduino.cc/index.php?topic = 223286.0p>