939浏览
查看: 939|回复: 2

[求助] 求助各位大佬帮我解释一下代码,有偿

[复制链接]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo myServo;
int In1 = 2;
int In2 = 3;
int ENA = 5;
int In3 = 4;
int In4 = 7;
int ENB = 6;
RF24 radio(8,9);
const byte address[6] = "00001";
char receivedData[32] = "";
int LXJ, LYJ, RYJ;
int motorSpeedA, motorSpeedB = 0, diff = 0;
void setup() {
  myServo.attach(10);
  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  pinMode(ENB, OUTPUT);
  
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()){
    radio.read(&receivedData, sizeof(receivedData));
    LXJ = atoi(&receivedData[0]);
    delay(10);
    radio.read(&receivedData, sizeof(receivedData));
    LYJ = atoi(&receivedData[0]);
    delay(10);
    radio.read(&receivedData, sizeof(receivedData));
    RYJ = atoi(&receivedData[0]);
    delay(10);   
  }
  int angelV = map(RYJ, 0, 1023, 5, 170);
  myServo.write(angelV);
  if (LXJ < 470) {
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);  
    digitalWrite(In3, LOW);  
    digitalWrite(In4, HIGH);
    motorSpeedA = motorSpeedB = map(LXJ, 470, 0, 0, 255);
  }
  
  
  else if (LXJ > 550) {
    digitalWrite(In1, LOW);
    digitalWrite(In2, HIGH);
    digitalWrite(In3, HIGH);
    digitalWrite(In4, LOW);
    motorSpeedA = motorSpeedB = map(LXJ, 550, 1023, 0, 255);
  }
  
  else {
    motorSpeedA = motorSpeedB = diff = 0;
  }
  
  if (LYJ < 470) {
    diff = map(LYJ, 470, 0, 0, 255);
    motorSpeedA = motorSpeedA + diff;
    motorSpeedB = motorSpeedB - diff;
   
    if (motorSpeedA > 255) {
      motorSpeedA = 180;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
//Make Tank turn Left by differential
  if (LYJ > 550) {
    diff = map(LYJ, 550, 1023, 0, 255);
    motorSpeedB = motorSpeedB + diff;
    motorSpeedA = motorSpeedA - diff;
    if (motorSpeedB > 255) {
      motorSpeedB = 180;
    }
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
   
  }
  
//Make Tank turn Right on its own center  
  if (LYJ < 470 && 470 < LXJ && LXJ < 550) {
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    digitalWrite(In3, HIGH);
    digitalWrite(In4, LOW);
    motorSpeedA = motorSpeedB = map(LYJ, 470, 0, 0, 255);   
  }
//Make Tank turn Left on its own center
  if (LYJ > 550 && 470 < LXJ && LXJ < 550) {
    digitalWrite(In1, LOW);
    digitalWrite(In2, HIGH);
    digitalWrite(In3, LOW);
    digitalWrite(In4, HIGH);
    motorSpeedA = motorSpeedB = map(LYJ, 550, 1023, 0, 255);
  }
  if (motorSpeedA < 140) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 140) {
    motorSpeedB = 0;
  }
  
  analogWrite(ENA, motorSpeedA);
  analogWrite(ENB, motorSpeedB);
  Serial.print("LXJ: ");
  Serial.print(LXJ);
  Serial.print(" | LYJ: ");
  Serial.print(LYJ);
  Serial.print(" | RYJ: ");
  Serial.print(RYJ);
  Serial.print(" || motorSpeedA: ");
  Serial.print(motorSpeedA);
  Serial.print(" | motorSpeedB: ");
  Serial.print(motorSpeedB);
  Serial.print(" | diff: ");
  Serial.println(diff);
}

遥控编程.docx

20.88 KB, 下载次数: 3396

hnyzcj  版主

发表于 2024-3-23 17:50:22

这段代码是一个基于Arduino的遥控坦克控制系统。它使用了RF24库进行无线通信,通过接收器接收遥控器的信号来控制坦克的运动。  代码的主要功能如下:  初始化引脚和无线模块。 设置无线模块的地址和通信参数。 在主循环中不断监听无线信号。 当接收到信号时,解析数据并更新坦克的控制变量。 根据接收到的数据,控制电机的速度和方向,实现坦克的运动控制。 使用舵机控制坦克的转向。 通过串口输出接收到的数据和电机速度等信息。
回复

使用道具 举报

hnyzcj  版主

发表于 2024-3-23 17:51:23

  1. #include <SPI.h>
  2. #include <nRF24L01.h>
  3. #include <RF24.h>
  4. #include <Servo.h>
  5. // 定义舵机对象和引脚
  6. Servo myServo;
  7. int In1 = 2;
  8. int In2 = 3;
  9. int ENA = 5;
  10. int In3 = 4;
  11. int In4 = 7;
  12. int ENB = 6;
  13. // 定义无线模块对象和地址
  14. RF24 radio(8,9);
  15. const byte address[6] = "00001";
  16. // 定义接收数据的缓冲区和变量
  17. char receivedData[32] = "";
  18. int LXJ, LYJ, RYJ;
  19. int motorSpeedA, motorSpeedB = 0, diff = 0;
  20. void setup() {
  21.   // 初始化舵机、引脚和无线模块
  22.   myServo.attach(10);
  23.   pinMode(In1, OUTPUT);
  24.   pinMode(In2, OUTPUT);
  25.   pinMode(ENA, OUTPUT);
  26.   pinMode(In3, OUTPUT);
  27.   pinMode(In4, OUTPUT);
  28.   pinMode(ENB, OUTPUT);
  29.   
  30.   Serial.begin(9600);
  31.   radio.begin();
  32.   radio.openReadingPipe(0, address);
  33.   radio.setPALevel(RF24_PA_MIN);
  34.   radio.startListening();
  35. }
  36. void loop() {
  37.   // 检查是否有无线信号接收到
  38.   if (radio.available()){
  39.     // 读取接收到的数据并解析
  40.     radio.read(&receivedData, sizeof(receivedData));
  41.     LXJ = atoi(&receivedData[0]);
  42.     delay(10);
  43.     radio.read(&receivedData, sizeof(receivedData));
  44.     LYJ = atoi(&receivedData[0]);
  45.     delay(10);
  46.     radio.read(&receivedData, sizeof(receivedData));
  47.     RYJ = atoi(&receivedData[0]);
  48.     delay(10);   
  49.   }
  50.   
  51.   // 根据接收到的数据控制舵机的角度
  52.   int angelV = map(RYJ, 0, 1023, 5, 170);
  53.   myServo.write(angelV);
  54.   
  55.   // 根据接收到的数据控制电机的速度和方向
  56.   if (LXJ < 470) {
  57.     digitalWrite(In1, HIGH);
  58.     digitalWrite(In2, LOW);  
  59.     digitalWrite(In3, LOW);  
  60.     digitalWrite(In4, HIGH);
  61.     motorSpeedA = motorSpeedB = map(LXJ, 470, 0, 0, 255);
  62.   } else if (LXJ > 550) {
  63.     digitalWrite(In1, LOW);
  64.     digitalWrite(In2, HIGH);
  65.     digitalWrite(In3, HIGH);
  66.     digitalWrite(In4, LOW);
  67.     motorSpeedA = motorSpeedB = map(LXJ, 550, 1023, 0, 255);
  68.   } else {
  69.     motorSpeedA = motorSpeedB = diff = 0;
  70.   }
  71.   
  72.   // 根据接收到的数据调整电机速度的差异
  73.   if (LYJ < 470) {
  74.     diff = map(LYJ, 470, 0, 0, 255);
  75.     motorSpeedA = motorSpeedA + diff;
  76.     motorSpeedB = motorSpeedB - diff;
  77.     if (motorSpeedA > 255) {
  78.       motorSpeedA = 180;
  79.     }
  80.     if (motorSpeedB < 0) {
  81.       motorSpeedB = 0;
  82.     }
  83.   } else if (LYJ > 550) {
  84.     diff = map(LYJ, 550, 1023, 0, 255);
  85.     motorSpeedB = motorSpeedB + diff;
  86.     motorSpeedA = motorSpeedA - diff;
  87.     if (motorSpeedB > 255) {
  88.       motorSpeedB = 180;
  89.     }
  90.     if (motorSpeedA < 0) {
  91.       motorSpeedA = 0;
  92.     }
  93.   }
  94.   
  95.   // 根据接收到的数据控制坦克的转向
  96.   if (LYJ < 470 && 470 < LXJ && LXJ < 550) {
  97.     digitalWrite(In1, HIGH);
  98.     digitalWrite(In2, LOW);
  99.     digitalWrite(In3, HIGH);
  100.     digitalWrite(In4, LOW);
  101.     motorSpeedA = motorSpeedB = map(LYJ, 470, 0, 0, 255);   
  102.   } else if (LYJ > 550 && 470 < LXJ && LXJ < 550) {
  103.     digitalWrite(In1, LOW);
  104.     digitalWrite(In2, HIGH);
  105.     digitalWrite(In3, LOW);
  106.     digitalWrite(In4, HIGH);
  107.     motorSpeedA = motorSpeedB = map(LYJ, 550, 1023, 0, 255);
  108.   }
  109.   if (motorSpeedA < 140) {
  110.     motorSpeedA = 0;
  111.   }
  112.   if (motorSpeedB < 140) {
  113.     motorSpeedB = 0;
  114.   }
  115.   
  116.   // 控制电机的速度和方向
  117.   analogWrite(ENA, motorSpeedA);
  118.   analogWrite(ENB, motorSpeedB);
  119.   
  120.   // 通过串口输出接收到的数据和电机速度等信息
  121.   Serial.print("LXJ: ");
  122.   Serial.print(LXJ);
  123.   Serial.print(" | LYJ: ");
  124.   Serial.print(LYJ);
  125.   Serial.print(" | RYJ: ");
  126.   Serial.print(RYJ);
  127.   Serial.print(" || motorSpeedA: ");
  128.   Serial.print(motorSpeedA);
  129.   Serial.print(" | motorSpeedB: ");
  130.   Serial.print(motorSpeedB);
  131.   Serial.print(" | diff: ");
  132.   Serial.println(diff);
  133. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail