中级会员
- 积分
- 294
- 金钱
- 294
- 注册时间
- 2018-5-3
- 在线时间
- 69 小时
|
1金钱
while(1)
{
key=KEY_Scan(0);
if(key==KEY0_PRES)
{
mode=0;
break;
}else if(key==KEY1_PRES)
{
mode=1;
break;
}
t++;
if(t==100)
{
printf("KEY0:RX_Mode KEY1:TX_Mode\n");
delay_ms(1000);
}
if(t==200)
{
t=0;
}
delay_ms(5);
}
if(mode==0)//RX模式
{
printf("NRF24L01 RX_Mode\n");
//printf("Received DATA:\n");
NRF24L01_RX_Mode();
while(1)
{
if(NRF24L01_RxPacket(tmp_buf)==0)//一旦接收到信息,则显示出来.
{
//tmp_buf[32]=0;//加入字符串结束符
if(tmp_buf[0]==0)
{
pitch2=-(tmp_buf[1]*256+tmp_buf[2]);
}else pitch2=tmp_buf[1]*256+tmp_buf[2];
if(tmp_buf[3]==0)
{
roll2=-(tmp_buf[4]*256+tmp_buf[5]);
}else roll2=-tmp_buf[4]*256+tmp_buf[5];
if(tmp_buf[6]==0)
{
yaw2=-(tmp_buf[7]*256+tmp_buf[8]);
}else yaw2=-tmp_buf[7]*256+tmp_buf[8];
printf("Received DATA:\n");
printf("\r\npitch2=%d\r\n",(int)pitch2);
printf("roll2=%d\r\n",(int)roll2);
printf("yaw2=%d\r\n",(int)yaw2);
}else delay_us(100);
t++;
if(t==10000)//大约1s钟改变一次状态
{
t=0;
LED0=!LED0;
}
};
}else//TX模式
{
printf("NRF24L01 TX_Mode\n");
NRF24L01_TX_Mode();
while(1)
{
if(NRF24L01_TxPacket(tmp_buf)==TX_OK)
{
if(mpu_mpl_get_data(&pitch,&roll,&yaw)==0)
{
MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据
MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据
printf("\r\n横滚角Roll=%d\r\n",-(int)(roll*100));
printf("俯仰角Pitch=%d\r\n",(int)(pitch*100));
printf("航向角Yaw=%d\r\n\r\n",-(int)(yaw*10));
r=-(int)(roll*100);
p=(int)(pitch*100);
y=-(int)(yaw*10);
delay_ms(50);
}
printf("Sended DATA:");
if(p>0)//pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00 度
{
tmp_buf[0]=1;
tmp_buf[1]=p/256;
tmp_buf[2]=p%256;
}else {
p=-p;
tmp_buf[0]=0;
tmp_buf[1]=p/256;
tmp_buf[2]=p%256;
}
if(r>0)
{
tmp_buf[3]=1;
tmp_buf[4]=r/256;
tmp_buf[5]=r%256;
}else {
r=-r;
tmp_buf[3]=0;
tmp_buf[4]=r/256;
tmp_buf[5]=r%256;
}
if(y>0)
{
tmp_buf[6]=1;
tmp_buf[7]=y/256;
tmp_buf[8]=y%256;
//printf("yaw2=%d\r\n",(int)yaw2);
}else {
y=-y;
tmp_buf[6]=0;
tmp_buf[7]=y/256;
tmp_buf[8]=y%256;
}
}else printf("Send Failed\n");
LED0=!LED0;
delay_ms(300);
};
}
|
|