RX basestation controller of the NERF demo. Designed for use with the FRDM-K64F and the nRF24L01P module.
Fork of NerfGun_nRF24L01P_RX by
main.cpp
00001 #include "mbed.h" 00002 #include "nRF24L01P.h" 00003 00004 #define V_SERVO_CENTER 1800 00005 #define V_SERVO_MAX 2200 00006 #define V_SERVO_MIN 800 00007 #define H_SERVO_CENTER 1600 00008 #define H_SERVO_MAX 2200 00009 #define H_SERVO_MIN 800 00010 #define TRANSFER_SIZE 9 00011 00012 Serial pc(USBTX, USBRX); // tx, rx 00013 00014 nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18); // mosi, miso, sck, csn, ce, irq 00015 00016 DigitalOut myled1(LED1); 00017 DigitalOut myled2(LED2); 00018 DigitalOut myled3(LED3); 00019 00020 DigitalOut fire(PTB23); 00021 PwmOut v_servo(PTC2); 00022 PwmOut h_servo(PTA2); 00023 00024 int main() { 00025 00026 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's 00027 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) 00028 // only handles 4 byte transfers in the ATMega code. 00029 myled1 = 1; 00030 myled2 = 1; 00031 myled3 = 0; 00032 00033 pc.baud(115200); 00034 char rxData[TRANSFER_SIZE]; 00035 int rxDataCnt = 0; 00036 00037 my_nrf24l01p.setRxAddress(0xDEADBEEF0F); 00038 my_nrf24l01p.powerUp(); 00039 00040 // Display the (default) setup of the nRF24L01+ chip 00041 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); 00042 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); 00043 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); 00044 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); 00045 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); 00046 00047 pc.printf( "This is the Receiver\r\n"); 00048 00049 my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); 00050 00051 my_nrf24l01p.setReceiveMode(); 00052 my_nrf24l01p.enable(); 00053 00054 00055 v_servo.period_us(20000); // servo requires a 20ms period 00056 v_servo.pulsewidth_us(V_SERVO_CENTER); 00057 h_servo.period_us(20000); // servo requires a 20ms period 00058 h_servo.pulsewidth_us(H_SERVO_CENTER); 00059 fire = 0; 00060 00061 int v_pulse = V_SERVO_CENTER; 00062 int h_pulse = H_SERVO_CENTER; 00063 00064 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00065 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00066 00067 wait(0.5); 00068 00069 int32_t acc_x, acc_y; 00070 char fire_button; 00071 00072 while (1) { 00073 00074 // If we've received anything in the nRF24L01+... 00075 if ( my_nrf24l01p.readable() ) { 00076 00077 // ...read the data into the receive buffer 00078 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); 00079 00080 // Display the receive buffer contents via the host serial link 00081 acc_x = (int32_t)((rxData[0])|(rxData[1]<<8)|(rxData[2]<<16)|(rxData[3]<<24)); 00082 acc_y = (int32_t)((rxData[4])|(rxData[5]<<8)|(rxData[6]<<16)|(rxData[7]<<24)); 00083 fire_button = rxData[8]; 00084 00085 //pc.printf("%d\t%d\t- %d\n\r",acc_x,acc_y,fire_button); 00086 //pc.printf("%01x %01x %01x %01x %01x %01x\n\r",rxData[1],rxData[0],rxData[3],rxData[2],rxData[5],rxData[4]); 00087 00088 v_pulse = V_SERVO_CENTER + acc_x; 00089 h_pulse = H_SERVO_CENTER + acc_y; 00090 00091 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; 00092 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; 00093 00094 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; 00095 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; 00096 00097 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00098 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00099 00100 //pc.printf("%d\t%d\t\n\r",v_pulse,h_pulse); 00101 00102 myled1 = fire_button; 00103 myled3 = !fire_button; 00104 fire = !fire_button; 00105 00106 } 00107 } 00108 }
Generated on Wed Jul 13 2022 18:59:12 by 1.7.2