RC_Boat/ship/ship.ino

186 lines
4.5 KiB
Arduino
Raw Normal View History

2023-09-04 06:54:15 +00:00
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
#include "Arduino.h"
#include "HardwareSerial.h"
#include<Servo.h>
2023-09-07 16:52:41 +00:00
#define trigPin 2 //超声波模块的Trig口 6#
2023-09-05 17:38:22 +00:00
#define echoPin 5 //超声波模块的echo口 5#
#define ServoPin 3 //底座舵机端口 3#
unsigned long time=0;
bool increasing=true;
bool clr=false;
//0: increase 1: decrease 2: clr
int status=0;
int clrtime=40;
int x=0;
2023-09-04 06:54:15 +00:00
Servo MyServo;
2023-09-05 17:38:22 +00:00
Servo RadarServo;
2023-09-04 06:54:15 +00:00
RF24 radio(7,8);
int count =0;
2023-09-04 13:42:44 +00:00
int IN1 =9;
int IN2 =10;
2023-09-04 06:54:15 +00:00
int PWM=6;
2023-09-05 17:38:22 +00:00
// int TrigPin=2;
// int EcoPin=3;
// int dAngle=5;
2023-09-04 06:54:15 +00:00
int angle=90;
int period=25;
2023-09-05 17:38:22 +00:00
// float dist;
2023-09-04 06:54:15 +00:00
long dj,z;//dj是舵机z是螺旋桨转速
//long propeller=0,rudder=0;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
//long s_propeller=0,s_rudder=0;//发送的信号
2023-09-04 13:42:44 +00:00
long propeller,rudder,content;//校验序列、读取内容和解码内容
2023-09-06 09:06:17 +00:00
long response=0;
2023-09-04 06:54:15 +00:00
const byte addresses[6] = "00001"; //创建一个数组,建立接收机地址,或者说两个模块将用于通信的“管道”
2023-09-05 17:38:22 +00:00
int calculateDistance()
{
long duration;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration*0.034/2;
}
2023-09-04 13:42:44 +00:00
2023-09-04 06:54:15 +00:00
void setup()
{
2023-09-05 17:38:22 +00:00
Serial.begin(9600);
MyServo.attach(4);
RadarServo.attach(ServoPin);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(PWM,OUTPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
MyServo.write(angle);
RadarServo.write(0);
2023-09-04 06:54:15 +00:00
radio.begin();
2023-09-05 17:38:22 +00:00
//radio.setChannel(114);
2023-09-04 06:54:15 +00:00
radio.openReadingPipe(1,addresses); //接收通道,
radio.openWritingPipe(addresses);//发送通道
radio.setPALevel(RF24_PA_HIGH); //设置功率放大器级别,将其设置为最小值
2023-09-05 17:38:22 +00:00
delay(1000);
MyServo.write(45);
2023-09-04 13:42:44 +00:00
}
long confirm(long x){
if(x>=1000000&&x<=2000000)return x%1000000;
else return -1;
2023-09-04 06:54:15 +00:00
}
2023-09-06 09:06:17 +00:00
void loop() {
2023-09-05 17:38:22 +00:00
radio.startListening();
2023-09-07 16:52:41 +00:00
delay(20);
2023-09-06 09:06:17 +00:00
if(radio.available()){
2023-09-05 17:38:22 +00:00
radio.read(&content,sizeof(content));
2023-09-04 06:54:15 +00:00
Serial.println("The content is");
Serial.println(content);//调试
2023-09-04 13:42:44 +00:00
content=confirm(content);
if(content!=-1){
2023-09-05 17:38:22 +00:00
propeller=content%1000;
rudder=(content-propeller)/1000;
if(propeller>550||propeller<449){
if(propeller>550){
long zheng=0.568*propeller-312.4;
digitalWrite(IN2,HIGH);
digitalWrite(IN1,LOW);
analogWrite(PWM,zheng);
z=zheng;
Serial.println("forward propelling");
Serial.println(z);
}
else if(propeller<449){
long fan=255-0.568*propeller;
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(PWM,fan);
z=fan;
Serial.println("back propelling");
Serial.println(z);
}
2023-09-04 13:42:44 +00:00
}
2023-09-05 17:38:22 +00:00
else if(propeller<=550||propeller>=449){
z=0;
analogWrite(PWM,0);
Serial.println("stop propelling");
Serial.println(z);
2023-09-04 13:42:44 +00:00
}
Serial.println("inputing ruddering");
if(rudder>550||rudder<449)
{
2023-09-05 17:38:22 +00:00
if(rudder<449){
long fan1=0.1*rudder;
MyServo.write(fan1);
Serial.println("turn left");
dj=2*fan1-90;
Serial.println(dj);
2023-09-04 06:54:15 +00:00
}
2023-09-05 17:38:22 +00:00
else if(rudder>550){
long zheng1=0.1*rudder-10.122;
MyServo.write(zheng1);
dj=2*zheng1-90;
Serial.println("turn right");
Serial.println(dj);
2023-09-04 06:54:15 +00:00
}
}
2023-09-05 17:38:22 +00:00
else if(rudder<=550||rudder>=449){
dj=0;
MyServo.write(45);
Serial.println("stop turing");
Serial.println(dj);
}
}
}
radio.stopListening();
int distance=0;
if(status==0){
if(x<180){
x+=2;
}
else{
status=2;
}
RadarServo.write(x); //调整舵机角度
//测距
distance = calculateDistance();
//send distance
2023-09-06 09:06:17 +00:00
response=1000000+x*1000+distance;
2023-09-05 17:38:22 +00:00
}
else if(status==1){
if(x>0){
x-=2;
}
else{
status=2;
}
RadarServo.write(x); //调整舵机角度
//测距
distance = calculateDistance();
2023-09-06 09:06:17 +00:00
response=1000000+x*1000+distance;
2023-09-05 17:38:22 +00:00
//send distance
}
else if(status=2){
clrtime--;
if(clrtime<=0){
clrtime=4;
if(x>=180){
status=1;
}
else{
status=0;
2023-09-04 06:54:15 +00:00
}
}
2023-09-05 17:38:22 +00:00
//send clear
2023-09-06 09:06:17 +00:00
response=2000000;
2023-09-05 17:38:22 +00:00
}
2023-09-06 09:06:17 +00:00
radio.write(response,sizeof(response));
2023-09-07 16:52:41 +00:00
//delay(20-distance/17);
Serial.println(distance);
2023-09-04 06:54:15 +00:00
}