ROV 控制系统 Arduino 代码

本代码展示了使用 Arduino 编写 ROV 控制系统,包括使用 MsTimer2 库实现超时关机功能、使用 LedControl 库控制 LED 点阵显示、使用 Servo 库控制舵机,并实现串口通信接收遥控器数据。

#include <MsTimer2.h>
#include <LedControl.h>
#include <Servo.h>

Servo Left_Front_Thruster; // 左前推进器对象
Servo Left_Upper_Thruster; // 左上推进器对象
Servo Right_Front_Thruster; // 右前推进器对象
Servo Right_Upper_Thruster; // 右上推进器对象
Servo Arm; // 机械臂对象

#define LEFT_FRONT_PIN 3 // 左前推进器引脚
#define LEFT_UPPER_PIN 5 // 左上推进器引脚
#define RIGHT_FRONT_PIN 9 // 右前推进器引脚
#define RIGHT_UPPER_PIN 6 // 右上推进器引脚
#define ARM_PIN 10 // 机械臂引脚

struct
{
  uint8_t Act_Value[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // 动作幅值 前进后退左转右转上浮下潜左翻右翻 0-110 舵机 0-140
  long int Ctl_Value[5] = {1500, 1500, 1500, 1500, 0}; // 控制量 左前右前左上右上 机械臂
  long int Ctl_ValueLast[5]; // 上一次控制量
  long int count; // 超时关机计数器
} ROV_Ctl;

/* LED 点阵变量定义区 */
#define DIN 2
#define CS 4
#define CLK 7
LedControl lc = LedControl(DIN, CLK, CS, 4);
byte Zero[8] = {0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C};


/* 串口通信变量定义区 */
#define Buffer_Lenth 9 // 接收数据数组个数 
long int Receive_Data_Ture = 0; // 数据正确标志位
unsigned char Serial_Receive_Data[Buffer_Lenth]; // 串口接收数据缓存区
char Communicate_state = 0; // 通信状态标志位

///* 手柄结构体定义区 */
//ruct
//{
//  /*** 摇杆值 ***/
//  uint8_t  Joystick_Value[4]; // 0:右摇杆前后运动数值1:右摇杆左右运动数值2:左摇杆前后运动数值3:左摇杆左右运动数值
//  /*** 按键值 ***/
//  uint8_t  Key_Value; // 按键值依次:▲■×↑↓←→L1L2R1R2
//} Ctl_Handle;

void setup()
{
  Serial.begin(9600);

  pinMode(A0, OUTPUT);
  Left_Front_Thruster.attach(LEFT_FRONT_PIN);
  Left_Upper_Thruster.attach(LEFT_UPPER_PIN);
  Right_Front_Thruster.attach(RIGHT_FRONT_PIN);
  Right_Upper_Thruster.attach(RIGHT_UPPER_PIN);
  Arm.attach(ARM_PIN);

  Left_Front_Thruster.writeMicroseconds(1000);
  Left_Upper_Thruster.writeMicroseconds(1000);
  Right_Front_Thruster.writeMicroseconds(1000);
  Right_Upper_Thruster.writeMicroseconds(1000);
  delay(10);
  Left_Front_Thruster.writeMicroseconds(1500);
  Left_Upper_Thruster.writeMicroseconds(1500);
  Right_Front_Thruster.writeMicroseconds(1500);
  Right_Upper_Thruster.writeMicroseconds(1500);

  led();
  MsTimer2::set(500, shut);
  MsTimer2::start();
}

void loop()
{
}

void shut()
{
  Receive_Data_Ture--;
  if (Receive_Data_Ture > 0)
  {
    ROV_Ctl.count = 0;
  }
  if (Receive_Data_Ture <= 0)
  {
    ROV_Ctl.count++;
  }
  Serial.println(ROV_Ctl.count);
  if (ROV_Ctl.count > 100)
  {
    analogWrite(A0, 255);
  }
}

uint8_t Num_len;
#define USART1_REC_LEN      24                // 定义最大接收字节数 200  实际69
unsigned char USART1_RX_BUF[USART1_REC_LEN];  // 接收缓冲,最大USART_REC_LEN个字节.

void serialEvent()
{
  /* 接收定长数据 */
  /* EB 90 00 01 02 03 04 05 06 07 */
  /* 获取缓存区缓存数据个数 */
  uint8_t  Para;
  int Rx_Length = Serial.available();
  if (Rx_Length > 0)
  {
    Para = Serial.read();
    if (Para == 0xEB)
    {
      Num_len = 0;
    }
    USART1_RX_BUF[Num_len++] = Para; // 将接受到的数据存入RX_BUFF
    if (Num_len == 9 && USART1_RX_BUF[0] == 0xEB && USART1_RX_BUF[1] == 0x90 && USART1_RX_BUF[2] == 0xA5)
    {
      memcpy(Serial_Receive_Data, USART1_RX_BUF, Num_len); // 保存数据
      Num_len = 0;
      memset(USART1_RX_BUF, 0, USART1_REC_LEN); // 清空BUF
      Receive_Data_Ture = 2;
      handle();
      mapping();
      execute();
    }
  }
}

void handle()
{
  
}

void mapping()
{
  
}

void execute()
{
  
}

void led()
{
  lc.shutdown(0, false); // 启动时,MAX72XX处于省电模式
  lc.setIntensity(0, 8); // 将亮度设置为最大值
  lc.clearDisplay(0);
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0, i, Zero[i]);
  }
}

// 改变通信协议为
Serial.write(0xEB);
Serial.write(0x90);
Serial.write(R1int);
Serial.write(R2int);
Serial.write(L1int);
Serial.write(L2int);
Serial.write(keyint);
byte SUM = R1int + R2int + L1int + L2int + keyint;
Serial.write(SUM);

通信协议解释

  • 该协议使用 8 位数据进行通信,每个字节代表不同的数据信息。
  • 协议开头为 0xEB0x90,用于标识通信协议。
  • R1intR2intL1intL2int 分别代表遥控器左右摇杆和上下摇杆的值。
  • keyint 代表遥控器的按键值。
  • SUM 为上述所有数据值的校验和,用于验证数据完整性。

优点

  • 减少数据传输的字节数,提高通信效率。
  • 使用 Serial.write() 函数可以直接将数据以二进制形式发送,不需要转换成 ASCII 码。

说明

  • 代码中 handle()mapping()execute() 函数需要根据具体的控制逻辑进行编写。
  • R1intR2intL1intL2intkeyint 需要根据具体的遥控器型号进行定义。
  • SUM 的计算方法可以根据需要进行调整。
  • 代码中使用的 MsTimer2LedControlServo 库需要在 Arduino IDE 中安装。
  • 为了更好的理解和使用代码,建议参考相关库的文档和示例代码。
ROV 控制系统 Arduino 代码 - 使用 MsTimer2、LedControl 和 Servo 库

原文地址: https://www.cveoy.top/t/topic/n7Oi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录