Qian Yuyang
/
blocky_controller_test
111
Revision 3:ffdc85db2044, committed 2019-04-21
- Comitter:
- AlexQian
- Date:
- Sun Apr 21 06:50:40 2019 +0000
- Parent:
- 2:4704fdd9ef91
- Commit message:
- 111
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 4704fdd9ef91 -r ffdc85db2044 main.cpp --- a/main.cpp Sun Dec 24 03:12:05 2017 +0000 +++ b/main.cpp Sun Apr 21 06:50:40 2019 +0000 @@ -1,87 +1,142 @@ +#define HIGH 1 +#define LOW 0 #include "mbed.h" +#include <string> +typedef bool boolean; +typedef std::string String; #include "nRF24L01P.h" -#define TRANSFER_SIZE 12 -#define Send_Repeat_Times 10//命令重发次数 -#define command_roll 0x51 -#define command_start 0x50 -#define command_end 0x52 -#define command_slow 0x53 - -int flag=0;//发送状态标志 0:发送姿态命令 1:发送启动命令 2:发送急停命令 3:发送缓降命令 -char txdata[TRANSFER_SIZE]; -nRF24L01P my_nrf24l01p(PB_15, PB_14, PB_13, PB_6, PB_5, PB_7); // mosi, miso, sck, csn, ce, irq - -DigitalOut myled1(PA_6); -DigitalOut myled2(PA_7); +char txdata[32] ; +float LX; +float LY; +float RX; +float RY; +int Yaw; +int Roll; +int Pitch; +int Power; +long CycleCount; +int Yaw_config; +int Pitch_config; +int Roll_config; +char i; +long SendCount; -//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); -AnalogIn L_X(PA_0),L_Y(PA_1),R_X(PA_2),R_Y(PA_3); -InterruptIn START(PB_4),END(PB_3),SLOW(PB_0); -Serial pc(PA_9,PA_10,9600); +#define NRF24_TRANSFER_SIZE 32 +nRF24L01P nrf24_PB_15(PB_15,PB_14,PB_13,PB_6,PB_5,PB_7); +Serial Serial_1(PA_9,PA_10); +InterruptIn intrPB_0(PB_0); +InterruptIn intrPB_1(PB_1); +InterruptIn intrPB_3(PB_3); +InterruptIn intrPB_4(PB_4); +InterruptIn intrPA_5(PA_5); +InterruptIn intrPB_10(PB_10); +DigitalOut myDigitalOutPA_7(PA_7); +AnalogIn myIOPA_0(PA_0); +AnalogIn myIOPA_1(PA_1); +AnalogIn myIOPA_2(PA_2); +AnalogIn myIOPA_3(PA_3); +Ticker tick742927; +DigitalOut myDigitalOutPA_6(PA_6); -void start(){flag=1;} -void end(){flag=2;} -void slow(){flag = 3;} +void intrPB_0_interrupt_fun() { +Pitch_config = Pitch_config + 1; +} -int main() { - for(int i=0;i!=TRANSFER_SIZE;i++) - txdata[i] = 0; - my_nrf24l01p.powerUp(); - my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); - my_nrf24l01p.setReceiveMode(); - my_nrf24l01p.enable(); - - myled1 = 0; - myled2 = 0; - - START.mode(PullDown); - END.mode(PullDown); - SLOW.mode(PullDown); - START.rise(&start); - END.rise(&end); - SLOW.rise(&slow); - while (1) { - switch (flag) - { - case 0: - txdata[0]= command_roll; - short int data[4]; - data[0] = L_X.read_u16(); - data[1] = L_Y.read_u16(); - data[2] = R_X.read_u16(); - data[3] = R_Y.read_u16(); - txdata[2] = data[0]; - txdata[1] = data[0] >> 8; - txdata[4] = data[1]; - txdata[3] = data[1] >> 8; - txdata[6] = data[2]; - txdata[5] = data[2] >> 8; - txdata[8] = data[3]; - txdata[7] = data[3] >> 8; - txdata[9] = 0; - txdata[10] = 0; - txdata[11] = 0; - my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); - break; - case 1: - txdata[0] = command_start; - for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);} - flag = 0; - break; - case 2: - txdata[0] = command_end; - for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);} - flag = 0; - break; - case 3: - txdata[0] = command_slow; - for(int i=0;i<Send_Repeat_Times;i++) {my_nrf24l01p.write( NRF24L01P_PIPE_P0, txdata, 12); wait(0.01);} - flag = 0; - break; - } - wait(0.05);//防止过快发送造成命令无法被接收 - } +void intrPB_1_interrupt_fun() { +Pitch_config = Pitch_config - 1; +} + +void intrPB_3_interrupt_fun() { +Roll_config = Roll_config + 1; +} + +void intrPB_4_interrupt_fun() { +Roll_config = Roll_config - 1; +} + +void intrPA_5_interrupt_fun() { +Yaw_config = Yaw_config + 1; +} + +void intrPB_10_interrupt_fun() { +Yaw_config = Yaw_config - 1; +} + +void tick742927_handle() { +SendCount = SendCount + 1; +myDigitalOutPA_7.write((CycleCount % 2 == 0)); +LX = myIOPA_0.read(); +LY = myIOPA_1.read(); +RX = myIOPA_2.read(); +RY = myIOPA_3.read(); +Power = (1 - LY) * 1000 + 1000; +Yaw = (1 - LX) * 1000 + 1000; +Pitch = (1 - RY) * 1000 + 1000; +Roll = (1 - RX) * 1000 + 1000; +txdata[0] = 36; +txdata[1] = 77; +txdata[2] = 60; +txdata[3] = 16; +txdata[4] = 105; +txdata[5] = Roll % 256; +txdata[6] = Roll / 256; +txdata[7] = Pitch % 256; +txdata[8] = Pitch / 256; +txdata[9] = Yaw % 256; +txdata[10] = Yaw / 256; +txdata[11] = Power % 256; +txdata[12] = Power / 256; +txdata[13] = 0; +txdata[14] = 0; +txdata[15] = 0; +txdata[16] = 0; +txdata[17] = 0; +txdata[18] = 0; +nrf24_PB_15.write( NRF24L01P_PIPE_P0, txdata, NRF24_TRANSFER_SIZE); } +int main() { +nrf24_PB_15.powerUp(); +nrf24_PB_15.setTransferSize( NRF24_TRANSFER_SIZE ); +nrf24_PB_15.setReceiveMode(); +nrf24_PB_15.setRxAddress(1ull); +nrf24_PB_15.setTxAddress(1ull); +nrf24_PB_15.enable(); + +Serial_1.baud(9600); + +intrPB_0.fall(&intrPB_0_interrupt_fun); +intrPB_0.mode(PullNone); +intrPB_1.fall(&intrPB_1_interrupt_fun); +intrPB_1.mode(PullNone); +intrPB_3.fall(&intrPB_3_interrupt_fun); +intrPB_3.mode(PullNone); +intrPB_4.fall(&intrPB_4_interrupt_fun); +intrPB_4.mode(PullNone); +intrPA_5.fall(&intrPA_5_interrupt_fun); +intrPA_5.mode(PullNone); +intrPB_10.fall(&intrPB_10_interrupt_fun); +intrPB_10.mode(PullNone); + +tick742927.attach(&tick742927_handle,0.01); + +Roll_config = 0; +Pitch_config = 0; +Yaw_config = 0; +CycleCount = 0; +SendCount = 0; +for (i = 1; i <= 32; i++) { +txdata[(i - 1)] = 0; +} +while (true) { +CycleCount = CycleCount + 1; +myDigitalOutPA_6.write((CycleCount % 2 == 0)); +//for (i = 1; i <= 32; i++) { +//Serial_1.printf("%d ",txdata[(i - 1)]); +//} +//Serial_1.printf("Next\n"); +} + +} \ No newline at end of file