59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
//player1,remote controler
|
||
#include<SPI.h>
|
||
#include<nRF24L01.h>
|
||
#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-1023,512为静止),rudder为舵(0-1023,512为静止)
|
||
long s_propeller=0,s_rudder=0;//发送的信号
|
||
long check_array[6];
|
||
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.setPALevel(RF24_PA_HIGH);
|
||
Timer1.initialize(interval);
|
||
Timer1.attachInterrupt(Send);
|
||
}
|
||
void Send(){//发送信号函数
|
||
radio.stopListening();
|
||
Serial.println("stop listening!");
|
||
propeller=analogRead(Rp);
|
||
rudder=analogRead(Rr);
|
||
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.println(propeller);
|
||
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;
|
||
}
|
||
void loop() {
|
||
// put your main code here, to run repeatedly:
|
||
radio.startListening();
|
||
//读取雷达信息,还未写
|
||
|
||
}
|