NRF+MPU6050

Dependencies:   mbed nRF24L01P

Committer:
wanzq
Date:
Sun Dec 24 03:12:05 2017 +0000
Revision:
2:4704fdd9ef91
Parent:
1:5be2682710c6
Child:
3:46535ec6d8b1
controller pro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Owen 0:a51a6e7da590 1 #include "mbed.h"
Owen 0:a51a6e7da590 2 #include "nRF24L01P.h"
wanzq 2:4704fdd9ef91 3 #define TRANSFER_SIZE 12
wanzq 2:4704fdd9ef91 4 #define Send_Repeat_Times 10//命令重发次数
wanzq 2:4704fdd9ef91 5 #define command_roll 0x51
wanzq 2:4704fdd9ef91 6 #define command_start 0x50
wanzq 2:4704fdd9ef91 7 #define command_end 0x52
wanzq 2:4704fdd9ef91 8 #define command_slow 0x53
Owen 0:a51a6e7da590 9
wanzq 2:4704fdd9ef91 10 int flag=0;//发送状态标志 0:发送姿态命令 1:发送启动命令 2:发送急停命令 3:发送缓降命令
wanzq 2:4704fdd9ef91 11 char txdata[TRANSFER_SIZE];
wanzq 2:4704fdd9ef91 12
wanzq 2:4704fdd9ef91 13 nRF24L01P my_nrf24l01p(PB_15, PB_14, PB_13, PB_6, PB_5, PB_7); // mosi, miso, sck, csn, ce, irq
Owen 0:a51a6e7da590 14
wanzq 2:4704fdd9ef91 15 DigitalOut myled1(PA_6);
wanzq 2:4704fdd9ef91 16 DigitalOut myled2(PA_7);
Owen 0:a51a6e7da590 17
wanzq 2:4704fdd9ef91 18 //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);
wanzq 2:4704fdd9ef91 19 AnalogIn L_X(PA_0),L_Y(PA_1),R_X(PA_2),R_Y(PA_3);
wanzq 2:4704fdd9ef91 20 InterruptIn START(PB_4),END(PB_3),SLOW(PB_0);
wanzq 2:4704fdd9ef91 21 Serial pc(PA_9,PA_10,9600);
wanzq 2:4704fdd9ef91 22
wanzq 2:4704fdd9ef91 23 void start(){flag=1;}
wanzq 2:4704fdd9ef91 24 void end(){flag=2;}
wanzq 2:4704fdd9ef91 25 void slow(){flag = 3;}
Owen 0:a51a6e7da590 26
Owen 0:a51a6e7da590 27 int main() {
wanzq 2:4704fdd9ef91 28 for(int i=0;i!=TRANSFER_SIZE;i++)
wanzq 2:4704fdd9ef91 29 txdata[i] = 0;
Owen 0:a51a6e7da590 30 my_nrf24l01p.powerUp();
Owen 0:a51a6e7da590 31 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
Owen 0:a51a6e7da590 32 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 33 my_nrf24l01p.enable();
wanzq 2:4704fdd9ef91 34
wanzq 2:4704fdd9ef91 35 myled1 = 0;
wanzq 2:4704fdd9ef91 36 myled2 = 0;
wanzq 2:4704fdd9ef91 37
wanzq 2:4704fdd9ef91 38 START.mode(PullDown);
wanzq 2:4704fdd9ef91 39 END.mode(PullDown);
wanzq 2:4704fdd9ef91 40 SLOW.mode(PullDown);
wanzq 2:4704fdd9ef91 41 START.rise(&start);
wanzq 2:4704fdd9ef91 42 END.rise(&end);
wanzq 2:4704fdd9ef91 43 SLOW.rise(&slow);
Owen 0:a51a6e7da590 44 while (1) {
wanzq 2:4704fdd9ef91 45 switch (flag)
wanzq 2:4704fdd9ef91 46 {
wanzq 2:4704fdd9ef91 47 case 0:
wanzq 2:4704fdd9ef91 48 txdata[0]= command_roll;
wanzq 2:4704fdd9ef91 49 short int data[4];
wanzq 2:4704fdd9ef91 50 data[0] = L_X.read_u16();
wanzq 2:4704fdd9ef91 51 data[1] = L_Y.read_u16();
wanzq 2:4704fdd9ef91 52 data[2] = R_X.read_u16();
wanzq 2:4704fdd9ef91 53 data[3] = R_Y.read_u16();
wanzq 2:4704fdd9ef91 54 txdata[2] = data[0];
wanzq 2:4704fdd9ef91 55 txdata[1] = data[0] >> 8;
wanzq 2:4704fdd9ef91 56 txdata[4] = data[1];
wanzq 2:4704fdd9ef91 57 txdata[3] = data[1] >> 8;
wanzq 2:4704fdd9ef91 58 txdata[6] = data[2];
wanzq 2:4704fdd9ef91 59 txdata[5] = data[2] >> 8;
wanzq 2:4704fdd9ef91 60 txdata[8] = data[3];
wanzq 2:4704fdd9ef91 61 txdata[7] = data[3] >> 8;
wanzq 2:4704fdd9ef91 62 txdata[9] = 0;
wanzq 2:4704fdd9ef91 63 txdata[10] = 0;
wanzq 2:4704fdd9ef91 64 txdata[11] = 0;
wanzq 2:4704fdd9ef91 65 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12);
wanzq 2:4704fdd9ef91 66 break;
wanzq 2:4704fdd9ef91 67 case 1:
wanzq 2:4704fdd9ef91 68 txdata[0] = command_start;
wanzq 2:4704fdd9ef91 69 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
wanzq 2:4704fdd9ef91 70 flag = 0;
wanzq 2:4704fdd9ef91 71 break;
wanzq 2:4704fdd9ef91 72 case 2:
wanzq 2:4704fdd9ef91 73 txdata[0] = command_end;
wanzq 2:4704fdd9ef91 74 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
wanzq 2:4704fdd9ef91 75 flag = 0;
wanzq 2:4704fdd9ef91 76 break;
wanzq 2:4704fdd9ef91 77 case 3:
wanzq 2:4704fdd9ef91 78 txdata[0] = command_slow;
wanzq 2:4704fdd9ef91 79 for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);}
wanzq 2:4704fdd9ef91 80 flag = 0;
wanzq 2:4704fdd9ef91 81 break;
Owen 0:a51a6e7da590 82 }
wanzq 2:4704fdd9ef91 83 wait(0.05);//防止过快发送造成命令无法被接收
Owen 0:a51a6e7da590 84 }
Owen 0:a51a6e7da590 85 }
wanzq 2:4704fdd9ef91 86
wanzq 2:4704fdd9ef91 87