rc new protocol

This commit is contained in:
clf3 2023-09-04 21:38:24 +08:00
parent d949e97b09
commit 805f8de61c
3 changed files with 89 additions and 95 deletions

View File

@ -19,6 +19,9 @@ int Xmax = 160; //屏幕的横向像素数
int Xcent = Xmax / 2; //x中位
int base = 118; //基线高度
int scanline = 105; //雷达扫描线长度
int outcircle=81;
int midcircle=54;
int incircle=27;
Servo baseServo;
Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
@ -122,25 +125,25 @@ void fix()
ucg.setColor(0, 40, 0);
//画基线圆盘
ucg.drawDisc(Xcent, base+1, 3, UCG_DRAW_ALL);
ucg.drawCircle(Xcent, base+1, 115, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, 115, UCG_DRAW_UPPER_RIGHT);
ucg.drawCircle(Xcent, base+1, 78, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, 78, UCG_DRAW_UPPER_RIGHT);
ucg.drawCircle(Xcent, base+1, 40, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, 40, UCG_DRAW_UPPER_RIGHT);
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, 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);
//画刻度表
for(int i= 40;i < 140; i+=2)
{
// for(int i= 40;i < 140; i+=2)
// {
if (i % 10 == 0)
ucg.drawLine(105*cos(radians(i))+Xcent,base - 105*sin(radians(i)) , 113*cos(radians(i))+Xcent,base - 113*sin(radians(i)));
else
// if (i % 10 == 0)
// ucg.drawLine(105*cos(radians(i))+Xcent,base - 105*sin(radians(i)) , 113*cos(radians(i))+Xcent,base - 113*sin(radians(i)));
// else
ucg.drawLine(110*cos(radians(i))+Xcent,base - 110*sin(radians(i)) , 113*cos(radians(i))+Xcent,base - 113*sin(radians(i)));
}
// ucg.drawLine(110*cos(radians(i))+Xcent,base - 110*sin(radians(i)) , 113*cos(radians(i))+Xcent,base - 113*sin(radians(i)));
// }
//画一些装饰性图案
ucg.setColor(0,200,0);
@ -172,6 +175,10 @@ void fix()
ucg.drawTetragon(67,123,63,127,93,127,97,123);
ucg.setColor(0,210,0);
ucg.drawTetragon(72,123,68,127,88,127,92,123);
// ucg.setColor(0,155, 0);
// ucg.setPrintPos(0,126);
// ucg.print("DEG: ");
}
@ -189,16 +196,16 @@ void loop(void)
baseServo.write(x); //调整舵机角度
//绘制雷达扫描线
int f = x - 4;
ucg.setColor(0, 255, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
f+=2;
ucg.setColor(0, 128, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
f+=2;
ucg.setColor(0, 0, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
ucg.setColor(0,200, 0);
// int f = x - 4;
// ucg.setColor(0, 255, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// f+=2;
// ucg.setColor(0, 128, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// f+=2;
// ucg.setColor(0, 0, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// ucg.setColor(0,200, 0);
//测距
distance = calculateDistance();
@ -206,12 +213,12 @@ void loop(void)
if (distance < 100)
{
ucg.setColor(255,0,0);
ucg.drawDisc(distance*cos(radians(x))+Xcent,-distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
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(116*cos(radians(x))+Xcent,-116*sin(radians(x))+base, 1, UCG_DRAW_ALL);
ucg.drawDisc((outcircle-1)*cos(radians(x))+Xcent,-(outcircle-1)*sin(radians(x))+base, 1, UCG_DRAW_ALL);
}
@ -221,23 +228,23 @@ void loop(void)
Serial.println(distance);
if (x > 70 and x < 110) fix_font(); //扫描线和数字重合时,重绘数字
//if (x > 70 and x < 110) fix_font(); //扫描线和数字重合时,重绘数字
ucg.setColor(0,155, 0);
ucg.setPrintPos(0,126);
ucg.print("DEG: ");
ucg.setPrintPos(24,126);
ucg.print(x);
ucg.print(" ");
ucg.setPrintPos(125,126);
ucg.print(" ");
ucg.print(distance);
ucg.print("cm ");
// ucg.setColor(0,155, 0);
// ucg.setPrintPos(0,126);
// ucg.print("DEG: ");
//ucg.setPrintPos(24,126);
//ucg.print(x);
// ucg.print(" ");
//ucg.setPrintPos(125,126);
// ucg.print(" ");
//ucg.print(distance);
// ucg.print("cm ");
}
//ucg.clearScreen(); //清屏 如果arduino供电不足可能会引起白屏显示信号中断可以用 cls();函数代替 ucg.clearScreen();
ucg.clearScreen(); //清屏 如果arduino供电不足可能会引起白屏显示信号中断可以用 cls();函数代替 ucg.clearScreen();
delay(50);
cls(); //如有频繁白屏情况,可以使用该函数 。或者增加外部供电
//cls(); //如有频繁白屏情况,可以使用该函数 。或者增加外部供电
fix();
fix_font(); //重绘屏幕背景元素
@ -246,16 +253,16 @@ void loop(void)
baseServo.write(x); //调整舵机角度
//绘制雷达扫描线
int f = x + 4;
ucg.setColor(0, 255, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
f-=2;
ucg.setColor(0, 128, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
f-=2;
ucg.setColor(0, 0, 0);
ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
ucg.setColor(0, 200, 0);
// int f = x + 4;
// ucg.setColor(0, 255, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// f-=2;
// ucg.setColor(0, 128, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// f-=2;
// ucg.setColor(0, 0, 0);
// ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
// ucg.setColor(0, 200, 0);
//测距
distance = calculateDistance();
@ -263,12 +270,12 @@ void loop(void)
if (distance < 100)
{
ucg.setColor(255,0,0);
ucg.drawDisc(distance*cos(radians(x))+Xcent,-distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
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(116*cos(radians(x))+Xcent,-116*sin(radians(x))+base, 1, UCG_DRAW_ALL);
ucg.drawDisc((outcircle-1)*cos(radians(x))+Xcent,-(outcircle-1)*sin(radians(x))+base, 1, UCG_DRAW_ALL);
}
//调试代码,输出角度和测距值
@ -276,23 +283,23 @@ void loop(void)
Serial.print(" , ");
Serial.println(distance);
if (x > 70 and x < 110) fix_font(); //扫描线和数字重合时,重绘数字
//if (x > 70 and x < 110) fix_font(); //扫描线和数字重合时,重绘数字
ucg.setColor(0,155, 0);
ucg.setPrintPos(0,126);
ucg.print("DEG: ");
ucg.setPrintPos(24,126);
ucg.print(x);
ucg.print(" ");
ucg.setPrintPos(125,126);
ucg.print(" ");
ucg.print(distance);
ucg.print("cm ");
// ucg.setColor(0,155, 0);
// ucg.setPrintPos(0,126);
// ucg.print("DEG: ");
// ucg.setPrintPos(24,126);
// ucg.print(x);
// ucg.print(" ");
//ucg.setPrintPos(125,126);
// ucg.print(" ");
//ucg.print(distance);
// ucg.print("cm ");
}
//ucg.clearScreen(); //
ucg.clearScreen(); //
delay(50);
cls();
//cls();
}

View File

@ -4,55 +4,41 @@
#include<RF24.h>
#include<TimerOne.h>
RF24 radio(7,8);//端口可能要改
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口,间断20ms
long propeller=0,rudder=0;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long s_propeller=0,s_rudder=0;//发送的信号
long check_array[6];
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口
long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long sending_signal=0;//发送的信号
const byte addresser[6]={"00001"};//创建通信通道地址6用来写8用来读
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
//radio.setChannel(114);//设置信道114号通道
radio.openWritingPipe(addresser);//6
radio.openReadingPipe(1,addresser);//8
radio.openWritingPipe(addresser);
radio.openReadingPipe(1,addresser);
radio.setPALevel(RF24_PA_HIGH);
Timer1.initialize(interval);
Timer1.attachInterrupt(Send);
Timer1.attachInterrupt(send);
}
void Send(){//发送信号函数
void send(){
sending_signal=0;
radio.stopListening();
Serial.println("stop listening!");
propeller=analogRead(Rp);
rudder=analogRead(Rr);
if(propeller>=1000)propeller=999;
if(rudder>=1000)rudder=999;
sending_signal+=propeller;
sending_signal+=rudder*1000;
sending_signal+=1000000;
radio.write(&sending_signal,sizeof(sending_signal));
Serial.println(propeller);
Serial.println(rudder);
propeller+=create_checknumber(propeller)*10000;
rudder+=create_checknumber(rudder)*10000;
propeller+=100000;
rudder+=200000;
radio.write(&propeller,sizeof(propeller));
radio.write(&rudder,sizeof(rudder));
Serial.print("propeller sent");
Serial.print("propeller sent ");
Serial.println(propeller);
Serial.print("rudder sent");
Serial.print("rudder sent ");
Serial.println(rudder);
}
long create_checknumber(long x){//创造校验码校验码规则个位×4十位×3百位×2千位×1
long add=0,sum=0;
while(x!=0){
check_array[add]=x%10;
x/=10;
add++;
}
for(int i=0,j=4;i<add;j--,i++){
sum+=check_array[i]*j;
}
return sum%10;
Serial.println(sending_signal);
}
void loop() {
// put your main code here, to run repeatedly:
radio.startListening();
//读取雷达信息,还未写
}

View File

@ -58,6 +58,7 @@ void setup()
radio.openReadingPipe(1,addresses); //接收通道,
radio.openWritingPipe(addresses);//发送通道
radio.setPALevel(RF24_PA_HIGH); //设置功率放大器级别,将其设置为最小值
//radio.setChannel(0);
delay(1000);
}