ship with detect
This commit is contained in:
		
							parent
							
								
									3510e3cd16
								
							
						
					
					
						commit
						05ce129d19
					
				| 
						 | 
				
			
			@ -38,10 +38,11 @@ void loop() {
 | 
			
		|||
  sending_signal=0;
 | 
			
		||||
  propeller=analogRead(Rp);
 | 
			
		||||
  rudder=analogRead(Rr);
 | 
			
		||||
  propeller=propeller*1.41;
 | 
			
		||||
  rudder=rudder*1.41;
 | 
			
		||||
  propeller=propeller*1.18;
 | 
			
		||||
  rudder=rudder*1.18;
 | 
			
		||||
  if(propeller>=1000)propeller=999;
 | 
			
		||||
  if(rudder>=1000)rudder=999;
 | 
			
		||||
  rudder=999-rudder;
 | 
			
		||||
  sending_signal+=propeller;
 | 
			
		||||
  sending_signal+=rudder*1000;
 | 
			
		||||
  sending_signal+=1000000;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,110 @@
 | 
			
		|||
#include <SPI.h>
 | 
			
		||||
#include<SoftwareSerial.h>
 | 
			
		||||
#include "Ucglib.h"
 | 
			
		||||
#define interruptPin 2
 | 
			
		||||
SoftwareSerial sendingSerial(5,6);
 | 
			
		||||
int Ymax = 160;               //屏幕的竖向像素数
 | 
			
		||||
int Xmax = 128;               //屏幕的横向像素数
 | 
			
		||||
int Xcent = Xmax / 2;         //x中位
 | 
			
		||||
int base = 100;               //基线高度
 | 
			
		||||
int scanline = 105;           //雷达扫描线长度
 | 
			
		||||
int outcircle=80;
 | 
			
		||||
int midcircle=60;
 | 
			
		||||
int midcircle2=40;
 | 
			
		||||
int incircle=20;
 | 
			
		||||
int x=90;
 | 
			
		||||
int distance=50;
 | 
			
		||||
bool rec=false;
 | 
			
		||||
bool clr=false;
 | 
			
		||||
bool ignoreClr=false;
 | 
			
		||||
String received="";//用来接受字符 
 | 
			
		||||
Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
 | 
			
		||||
void fix_font() 
 | 
			
		||||
{
 | 
			
		||||
  ucg.setColor(0, 180, 0);
 | 
			
		||||
  ucg.setPrintPos(Xcent-10,26);
 | 
			
		||||
  ucg.print("1.00");
 | 
			
		||||
  ucg.setPrintPos(Xcent-10,46);
 | 
			
		||||
  ucg.print("0.75");
 | 
			
		||||
  ucg.setPrintPos(Xcent-10,66);
 | 
			
		||||
  ucg.print("0.50");
 | 
			
		||||
  ucg.setPrintPos(Xcent-10,86);
 | 
			
		||||
  ucg.print("0.25");
 | 
			
		||||
}
 | 
			
		||||
void fix()
 | 
			
		||||
{
 | 
			
		||||
  ucg.setColor(0, 40, 0);
 | 
			
		||||
  //画基线圆盘
 | 
			
		||||
  ucg.drawDisc(Xcent, base+1, 3, UCG_DRAW_ALL); 
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, outcircle, UCG_DRAW_UPPER_LEFT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, outcircle, UCG_DRAW_UPPER_RIGHT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, midcircle, UCG_DRAW_UPPER_LEFT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, midcircle, UCG_DRAW_UPPER_RIGHT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, midcircle2, UCG_DRAW_UPPER_LEFT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, midcircle2, UCG_DRAW_UPPER_RIGHT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, incircle, UCG_DRAW_UPPER_LEFT);
 | 
			
		||||
  ucg.drawCircle(Xcent, base+1, incircle, UCG_DRAW_UPPER_RIGHT);
 | 
			
		||||
  ucg.drawLine(0, base+1, Xmax,base+1);     
 | 
			
		||||
  ucg.setColor(0, 120, 0);
 | 
			
		||||
}
 | 
			
		||||
void setup() {
 | 
			
		||||
  ucg.begin(UCG_FONT_MODE_SOLID); //初始化屏幕
 | 
			
		||||
  ucg.setRotate180();              //设置成横屏  如果屏幕显示方向是反的,可以修改函数 setRotate90 或 setRotate270     
 | 
			
		||||
  Serial.begin(9600);             //设置串口传输率
 | 
			
		||||
  sendingSerial.begin(38400);
 | 
			
		||||
  sendingSerial.listen();//必须要加
 | 
			
		||||
  ucg.clearScreen();
 | 
			
		||||
  //cls();
 | 
			
		||||
  ucg.setFontMode(UCG_FONT_MODE_SOLID);
 | 
			
		||||
  ucg.setFont(ucg_font_orgv01_hr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  while(sendingSerial.available()){
 | 
			
		||||
    if(sendingSerial.peek()!='\n'){//peek()查看当前字节是不是换行
 | 
			
		||||
      received+=(char)sendingSerial.read();
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
      rec=true;
 | 
			
		||||
      sendingSerial.read();//把换行符读掉
 | 
			
		||||
      Serial.println(received);
 | 
			
		||||
      if(received[0]=='1'){
 | 
			
		||||
        ignoreClr=false;
 | 
			
		||||
        distance=received.substring(4).toInt();//个十百3位数
 | 
			
		||||
        x=received.substring(1,4).toInt();//第456位  三位数
 | 
			
		||||
      }
 | 
			
		||||
      else if(received[0]=='2'){
 | 
			
		||||
        if(!ignoreClr){
 | 
			
		||||
          clr=true;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      else{
 | 
			
		||||
        rec=false;
 | 
			
		||||
      }
 | 
			
		||||
      received="";
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if(rec){
 | 
			
		||||
    if(clr){
 | 
			
		||||
      ucg.clearScreen();
 | 
			
		||||
      fix();
 | 
			
		||||
      fix_font();  //重绘屏幕背景元素
 | 
			
		||||
      clr=false;
 | 
			
		||||
      ignoreClr=true;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    else if(ignoreClr){
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    if (distance < 100){
 | 
			
		||||
      ucg.setColor(255,0,0);
 | 
			
		||||
      ucg.drawDisc(0.8*distance*cos(radians(x))+Xcent,-0.8*distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
 | 
			
		||||
    }
 | 
			
		||||
    else{ //超过1米以上的,用黄色画在边缘区域示意
 | 
			
		||||
      ucg.setColor(255,255,0);
 | 
			
		||||
      ucg.drawDisc((outcircle-1)*cos(radians(x))+Xcent,-(outcircle-1)*sin(radians(x))+base, 1, UCG_DRAW_ALL);
 | 
			
		||||
    }
 | 
			
		||||
    rec=false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -57,12 +57,9 @@ void setup()
 | 
			
		|||
  pinMode(PWM,OUTPUT);
 | 
			
		||||
  pinMode(A1,INPUT);
 | 
			
		||||
  pinMode(A2,INPUT);
 | 
			
		||||
  MyServo.write(angle);
 | 
			
		||||
  
 | 
			
		||||
  RadarServo.write(0);
 | 
			
		||||
 | 
			
		||||
  delay(1000);
 | 
			
		||||
  MyServo.write(45);
 | 
			
		||||
  RadarServo.write(0);
 | 
			
		||||
  delay(1000);  
 | 
			
		||||
}
 | 
			
		||||
long confirm(long x){
 | 
			
		||||
  if(x>=1000000&&x<=2000000)return x%1000000;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,238 @@
 | 
			
		|||
#include<SPI.h>
 | 
			
		||||
#include "Arduino.h"
 | 
			
		||||
#include "HardwareSerial.h"
 | 
			
		||||
#include<SoftwareSerial.h>
 | 
			
		||||
#include<Servo.h>
 | 
			
		||||
#define  trigPin   2          //超声波模块的Trig口 6#
 | 
			
		||||
#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;
 | 
			
		||||
const int interval=100;
 | 
			
		||||
String sending="",received="";
 | 
			
		||||
Servo MyServo;
 | 
			
		||||
Servo RadarServo;
 | 
			
		||||
SoftwareSerial bluetoothSerial(7,8);
 | 
			
		||||
int count =0; 
 | 
			
		||||
int IN1 =9;
 | 
			
		||||
int IN2 =10;
 | 
			
		||||
int PWM=6;
 | 
			
		||||
int angle=90;
 | 
			
		||||
int period=25;
 | 
			
		||||
int attachwater;
 | 
			
		||||
int j;
 | 
			
		||||
// float dist;
 | 
			
		||||
long dj,z;//dj是舵机,z是螺旋桨转速
 | 
			
		||||
//long propeller=0,rudder=0;//propeller为螺旋桨(0-1023,512为静止),rudder为舵(0-1023,512为静止)
 | 
			
		||||
//long s_propeller=0,s_rudder=0;//发送的信号
 | 
			
		||||
long propeller,rudder;//校验序列、读取内容和解码内容 
 | 
			
		||||
long response=0;
 | 
			
		||||
const byte addresses[6] = "00001";  //创建一个数组,建立接收机地址,或者说两个模块将用于通信的“管道”
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup()
 | 
			
		||||
{
 | 
			
		||||
  Serial.begin(9600);
 | 
			
		||||
  bluetoothSerial.begin(57600);
 | 
			
		||||
  bluetoothSerial.listen();
 | 
			
		||||
  MyServo.attach(4);
 | 
			
		||||
  RadarServo.attach(ServoPin);
 | 
			
		||||
  pinMode(trigPin,OUTPUT);
 | 
			
		||||
  pinMode(echoPin,INPUT);
 | 
			
		||||
  pinMode(IN1,OUTPUT);
 | 
			
		||||
  pinMode(IN2,OUTPUT);
 | 
			
		||||
  pinMode(PWM,OUTPUT);
 | 
			
		||||
  RadarServo.write(0);
 | 
			
		||||
  MyServo.write(45);
 | 
			
		||||
  delay(1000);
 | 
			
		||||
 
 | 
			
		||||
}
 | 
			
		||||
long confirm(long x){
 | 
			
		||||
  if(x>=1000000&&x<=2000000)return x%1000000;
 | 
			
		||||
  else return -1;
 | 
			
		||||
}
 | 
			
		||||
void loop() {
 | 
			
		||||
  int distance=0;
 | 
			
		||||
  attachwater=0;
 | 
			
		||||
  for(j=1;j<=100;j++)
 | 
			
		||||
  {
 | 
			
		||||
  if(analogRead(A1)*5/1023!=4) break;
 | 
			
		||||
  }
 | 
			
		||||
  if(j>=100) attachwater=1;
 | 
			
		||||
  
 | 
			
		||||
  
 | 
			
		||||
   Serial.println(attachwater);
 | 
			
		||||
   
 | 
			
		||||
 // Serial.print("s: ");
 | 
			
		||||
 // Serial.println(status); 
 | 
			
		||||
  if(status==0){
 | 
			
		||||
    if(x<180){
 | 
			
		||||
      x+=2;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
      status=2;
 | 
			
		||||
    }
 | 
			
		||||
    RadarServo.write(x);             //调整舵机角度
 | 
			
		||||
    //测距
 | 
			
		||||
    distance = calculateDistance();
 | 
			
		||||
    //send distance
 | 
			
		||||
    //response=1000000+x*1000+distance;
 | 
			
		||||
    response=0;
 | 
			
		||||
    response+=distance;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
    response+=(long)x*1000;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
    response+=1000000;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
  }
 | 
			
		||||
  else if(status==1){
 | 
			
		||||
    if(x>0){
 | 
			
		||||
      x-=2;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
      status=2;
 | 
			
		||||
    }
 | 
			
		||||
    RadarServo.write(x);             //调整舵机角度
 | 
			
		||||
    //测距
 | 
			
		||||
    distance = calculateDistance();
 | 
			
		||||
    //response=1000000+x*1000+distance;
 | 
			
		||||
    response=0;
 | 
			
		||||
    response+=distance;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
    response+=(long)x*1000;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
    response+=1000000;
 | 
			
		||||
    //Serial.println(response);
 | 
			
		||||
    //send distance
 | 
			
		||||
  }
 | 
			
		||||
  else if(status=2){
 | 
			
		||||
    clrtime--;
 | 
			
		||||
    if(clrtime<=0){
 | 
			
		||||
      clrtime=4;
 | 
			
		||||
      if(x>=180){
 | 
			
		||||
        status=1;
 | 
			
		||||
      }
 | 
			
		||||
      else{
 | 
			
		||||
        status=0;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    //send clear
 | 
			
		||||
    response=2000000;
 | 
			
		||||
  }
 | 
			
		||||
  sending=String(response);
 | 
			
		||||
 | 
			
		||||
  // Serial.print("x: ");
 | 
			
		||||
  // Serial.println(x);
 | 
			
		||||
  // Serial.print("d: ");
 | 
			
		||||
  // Serial.println(distance);
 | 
			
		||||
  // Serial.println(response);  
 | 
			
		||||
  //Serial.println(sending);
 | 
			
		||||
  bluetoothSerial.println(sending);
 | 
			
		||||
  delay(interval);
 | 
			
		||||
  //delay(20-distance/17);
 | 
			
		||||
  //Serial.println(distance);
 | 
			
		||||
  while(bluetoothSerial.available()>0){
 | 
			
		||||
    //Serial.println("available!");
 | 
			
		||||
    if(bluetoothSerial.peek()!='\n'){//peek()查看当前字节是不是换行
 | 
			
		||||
      received+=(char)bluetoothSerial.read();
 | 
			
		||||
      //Serial.println(received);
 | 
			
		||||
      //Serial.println("no \n");
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
      bluetoothSerial.read();//把换行符读掉
 | 
			
		||||
      Serial.println(received);
 | 
			
		||||
      //Serial.println(received.length());
 | 
			
		||||
      //content=toInt(received);
 | 
			
		||||
      if(received[0]=='1'&&received.length()-1==7){
 | 
			
		||||
        propeller=received.substring(4).toInt();
 | 
			
		||||
        rudder=received.substring(1,4).toInt();
 | 
			
		||||
        if(propeller>550||propeller<449){
 | 
			
		||||
          if(propeller>550){ 
 | 
			
		||||
            long zheng=0.568*propeller-312.4;
 | 
			
		||||
            digitalWrite(IN2,HIGH);
 | 
			
		||||
            digitalWrite(IN1,LOW);
 | 
			
		||||
           if(attachwater==1)
 | 
			
		||||
           {
 | 
			
		||||
            analogWrite(PWM,zheng); 
 | 
			
		||||
            z=zheng;
 | 
			
		||||
           }
 | 
			
		||||
           else
 | 
			
		||||
           {
 | 
			
		||||
            analogWrite(PWM,0);
 | 
			
		||||
            z=0;
 | 
			
		||||
           }
 | 
			
		||||
            //Serial.println("forward propelling");
 | 
			
		||||
            //Serial.println(z);
 | 
			
		||||
          
 | 
			
		||||
          }
 | 
			
		||||
          else if(propeller<449){
 | 
			
		||||
        
 | 
			
		||||
            long fan=255-0.568*propeller;
 | 
			
		||||
            digitalWrite(IN1,HIGH);
 | 
			
		||||
            digitalWrite(IN2,LOW);
 | 
			
		||||
            if(attachwater==1)
 | 
			
		||||
            {
 | 
			
		||||
            analogWrite(PWM,fan);
 | 
			
		||||
            z=fan;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
              analogWrite(PWM,0);
 | 
			
		||||
              z=0;
 | 
			
		||||
            }
 | 
			
		||||
            //Serial.println("back propelling");
 | 
			
		||||
            //Serial.println(z);
 | 
			
		||||
           
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        else if(propeller<=550||propeller>=449){
 | 
			
		||||
          z=0;
 | 
			
		||||
          analogWrite(PWM,0);
 | 
			
		||||
          Serial.println("stop propelling");
 | 
			
		||||
          Serial.println(z);
 | 
			
		||||
        } 
 | 
			
		||||
        //Serial.println("inputing ruddering");
 | 
			
		||||
        if(rudder>550||rudder<449)
 | 
			
		||||
        {
 | 
			
		||||
          if(rudder<449){
 | 
			
		||||
            long fan1=0.1*rudder;
 | 
			
		||||
            MyServo.write(fan1);
 | 
			
		||||
            //Serial.println("turn left");
 | 
			
		||||
            dj=2*fan1-90;
 | 
			
		||||
            //Serial.println(dj);
 | 
			
		||||
          }
 | 
			
		||||
          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);
 | 
			
		||||
          }
 | 
			
		||||
        } 
 | 
			
		||||
        else if(rudder<=550||rudder>=449){
 | 
			
		||||
          dj=0;
 | 
			
		||||
          MyServo.write(45);
 | 
			
		||||
          //Serial.println("stop turing");
 | 
			
		||||
          //Serial.println(dj);                     
 | 
			
		||||
        }  
 | 
			
		||||
      }
 | 
			
		||||
      received="";                                                             
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
  
 | 
			
		||||
		Loading…
	
		Reference in New Issue