wan zhouquan / Mbed 2 deprecated controller_v1

Dependencies:   mbed nRF24L01P

Fork of nRF24L01P_Hello_World by Owen Edwards

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "nRF24L01P.h"
00003 #define TRANSFER_SIZE   12
00004 #define Send_Repeat_Times 10//命令重发次数
00005 #define command_roll 0x51
00006 #define command_start 0x50
00007 #define command_end 0x52
00008 #define command_slow 0x53
00009 
00010 int flag=0;//发送状态标志 0:发送姿态命令 1:发送启动命令 2:发送急停命令 3:发送缓降命令
00011 char txdata[TRANSFER_SIZE];
00012 
00013 nRF24L01P my_nrf24l01p(PB_15, PB_14, PB_13, PB_6, PB_5, PB_7);    // mosi, miso, sck, csn, ce, irq
00014 
00015 DigitalOut myled1(PA_6);
00016 DigitalOut myled2(PA_7);
00017 
00018 //DigitalIn sz_n(PB_10),sz_p(PA_5),sx_p(PB_0),sx_n(PB_1),sy_p(PB_3),sy_n(PB_4);
00019 AnalogIn L_X(PA_0),L_Y(PA_1),R_X(PA_2),R_Y(PA_3);
00020 InterruptIn START(PB_4),END(PB_3),SLOW(PB_0);
00021 Serial pc(PA_9,PA_10,9600);
00022 
00023 void start(){flag=1;}
00024 void end(){flag=2;}
00025 void slow(){flag = 3;}
00026 
00027 int main() {
00028     for(int i=0;i!=TRANSFER_SIZE;i++)
00029         txdata[i] = 0;   
00030     my_nrf24l01p.powerUp();
00031     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00032     my_nrf24l01p.setReceiveMode();
00033     my_nrf24l01p.enable();
00034     
00035     myled1 = 0;
00036     myled2 = 0;
00037     
00038     START.mode(PullDown);
00039     END.mode(PullDown);
00040     SLOW.mode(PullDown);
00041     START.rise(&start);
00042     END.rise(&end);
00043     SLOW.rise(&slow);
00044     while (1) {
00045          switch (flag)
00046         {
00047             case 0:
00048                 txdata[0]= command_roll;    
00049                 short int data[4];
00050                 data[0] = L_X.read_u16();
00051                 data[1] = L_Y.read_u16();
00052                 data[2] = R_X.read_u16();
00053                 data[3] = R_Y.read_u16();
00054                 txdata[2] = data[0];
00055                 txdata[1] = data[0] >> 8;
00056                 txdata[4] = data[1];
00057                 txdata[3] = data[1] >> 8;
00058                 txdata[6] = data[2];
00059                 txdata[5] = data[2] >> 8;
00060                 txdata[8] = data[3];
00061                 txdata[7] = data[3] >> 8;
00062                 txdata[9] = 0;
00063                 txdata[10] = 0;
00064                 txdata[11] = 0;    
00065                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12);
00066             break;        
00067             case 1:
00068                 txdata[0] = command_start;
00069                 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
00070                 flag = 0;
00071             break;
00072             case 2:
00073                 txdata[0] = command_end;
00074                 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
00075                 flag = 0;
00076             break;
00077             case 3:
00078                 txdata[0] = command_slow;
00079                 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
00080                 flag = 0;
00081             break;      
00082         }
00083         wait(0.05);//防止过快发送造成命令无法被接收     
00084     }
00085 }
00086 
00087