Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of NerfGun_nRF24L01P_RX by
main.cpp@3:fb5f79ff292b, 2014-05-21 (annotated)
- Committer:
- johnmc
- Date:
- Wed May 21 13:44:54 2014 +0000
- Revision:
- 3:fb5f79ff292b
- Parent:
- 2:9caef35c5062
NerfDemo RX (base) side.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
| Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
| Owen | 0:a51a6e7da590 | 3 | |
| clarkjarvis | 2:9caef35c5062 | 4 | #define V_SERVO_CENTER 1800 |
| clarkjarvis | 2:9caef35c5062 | 5 | #define V_SERVO_MAX 2200 |
| clarkjarvis | 2:9caef35c5062 | 6 | #define V_SERVO_MIN 800 |
| clarkjarvis | 2:9caef35c5062 | 7 | #define H_SERVO_CENTER 1600 |
| clarkjarvis | 2:9caef35c5062 | 8 | #define H_SERVO_MAX 2200 |
| clarkjarvis | 2:9caef35c5062 | 9 | #define H_SERVO_MIN 800 |
| clarkjarvis | 2:9caef35c5062 | 10 | #define TRANSFER_SIZE 9 |
| clarkjarvis | 2:9caef35c5062 | 11 | |
| Owen | 0:a51a6e7da590 | 12 | Serial pc(USBTX, USBRX); // tx, rx |
| Owen | 0:a51a6e7da590 | 13 | |
| clarkjarvis | 2:9caef35c5062 | 14 | nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18); // mosi, miso, sck, csn, ce, irq |
| Owen | 0:a51a6e7da590 | 15 | |
| Owen | 0:a51a6e7da590 | 16 | DigitalOut myled1(LED1); |
| Owen | 0:a51a6e7da590 | 17 | DigitalOut myled2(LED2); |
| clarkjarvis | 2:9caef35c5062 | 18 | DigitalOut myled3(LED3); |
| clarkjarvis | 2:9caef35c5062 | 19 | |
| clarkjarvis | 2:9caef35c5062 | 20 | DigitalOut fire(PTB23); |
| clarkjarvis | 2:9caef35c5062 | 21 | PwmOut v_servo(PTC2); |
| clarkjarvis | 2:9caef35c5062 | 22 | PwmOut h_servo(PTA2); |
| Owen | 0:a51a6e7da590 | 23 | |
| Owen | 0:a51a6e7da590 | 24 | int main() { |
| Owen | 0:a51a6e7da590 | 25 | |
| Owen | 0:a51a6e7da590 | 26 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
| Owen | 0:a51a6e7da590 | 27 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
| Owen | 0:a51a6e7da590 | 28 | // only handles 4 byte transfers in the ATMega code. |
| clarkjarvis | 2:9caef35c5062 | 29 | myled1 = 1; |
| clarkjarvis | 2:9caef35c5062 | 30 | myled2 = 1; |
| clarkjarvis | 2:9caef35c5062 | 31 | myled3 = 0; |
| Owen | 0:a51a6e7da590 | 32 | |
| clarkjarvis | 2:9caef35c5062 | 33 | pc.baud(115200); |
| clarkjarvis | 2:9caef35c5062 | 34 | char rxData[TRANSFER_SIZE]; |
| Owen | 0:a51a6e7da590 | 35 | int rxDataCnt = 0; |
| Owen | 0:a51a6e7da590 | 36 | |
| johnmc | 3:fb5f79ff292b | 37 | my_nrf24l01p.setRxAddress(0xDEADBEEF0F); |
| Owen | 0:a51a6e7da590 | 38 | my_nrf24l01p.powerUp(); |
| Owen | 0:a51a6e7da590 | 39 | |
| Owen | 0:a51a6e7da590 | 40 | // Display the (default) setup of the nRF24L01+ chip |
| Owen | 0:a51a6e7da590 | 41 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
| Owen | 0:a51a6e7da590 | 42 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
| Owen | 0:a51a6e7da590 | 43 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
| Owen | 0:a51a6e7da590 | 44 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
| Owen | 0:a51a6e7da590 | 45 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
| Owen | 0:a51a6e7da590 | 46 | |
| clarkjarvis | 2:9caef35c5062 | 47 | pc.printf( "This is the Receiver\r\n"); |
| Owen | 0:a51a6e7da590 | 48 | |
| Owen | 0:a51a6e7da590 | 49 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
| Owen | 0:a51a6e7da590 | 50 | |
| Owen | 0:a51a6e7da590 | 51 | my_nrf24l01p.setReceiveMode(); |
| Owen | 0:a51a6e7da590 | 52 | my_nrf24l01p.enable(); |
| Owen | 0:a51a6e7da590 | 53 | |
| clarkjarvis | 2:9caef35c5062 | 54 | |
| clarkjarvis | 2:9caef35c5062 | 55 | v_servo.period_us(20000); // servo requires a 20ms period |
| clarkjarvis | 2:9caef35c5062 | 56 | v_servo.pulsewidth_us(V_SERVO_CENTER); |
| clarkjarvis | 2:9caef35c5062 | 57 | h_servo.period_us(20000); // servo requires a 20ms period |
| clarkjarvis | 2:9caef35c5062 | 58 | h_servo.pulsewidth_us(H_SERVO_CENTER); |
| clarkjarvis | 2:9caef35c5062 | 59 | fire = 0; |
| clarkjarvis | 2:9caef35c5062 | 60 | |
| clarkjarvis | 2:9caef35c5062 | 61 | int v_pulse = V_SERVO_CENTER; |
| clarkjarvis | 2:9caef35c5062 | 62 | int h_pulse = H_SERVO_CENTER; |
| Owen | 0:a51a6e7da590 | 63 | |
| clarkjarvis | 2:9caef35c5062 | 64 | v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms |
| clarkjarvis | 2:9caef35c5062 | 65 | h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms |
| Owen | 0:a51a6e7da590 | 66 | |
| clarkjarvis | 2:9caef35c5062 | 67 | wait(0.5); |
| clarkjarvis | 2:9caef35c5062 | 68 | |
| clarkjarvis | 2:9caef35c5062 | 69 | int32_t acc_x, acc_y; |
| clarkjarvis | 2:9caef35c5062 | 70 | char fire_button; |
| clarkjarvis | 2:9caef35c5062 | 71 | |
| clarkjarvis | 2:9caef35c5062 | 72 | while (1) { |
| Owen | 0:a51a6e7da590 | 73 | |
| Owen | 0:a51a6e7da590 | 74 | // If we've received anything in the nRF24L01+... |
| Owen | 0:a51a6e7da590 | 75 | if ( my_nrf24l01p.readable() ) { |
| clarkjarvis | 2:9caef35c5062 | 76 | |
| Owen | 0:a51a6e7da590 | 77 | // ...read the data into the receive buffer |
| Owen | 0:a51a6e7da590 | 78 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
| Owen | 0:a51a6e7da590 | 79 | |
| Owen | 0:a51a6e7da590 | 80 | // Display the receive buffer contents via the host serial link |
| clarkjarvis | 2:9caef35c5062 | 81 | acc_x = (int32_t)((rxData[0])|(rxData[1]<<8)|(rxData[2]<<16)|(rxData[3]<<24)); |
| clarkjarvis | 2:9caef35c5062 | 82 | acc_y = (int32_t)((rxData[4])|(rxData[5]<<8)|(rxData[6]<<16)|(rxData[7]<<24)); |
| clarkjarvis | 2:9caef35c5062 | 83 | fire_button = rxData[8]; |
| clarkjarvis | 2:9caef35c5062 | 84 | |
| clarkjarvis | 2:9caef35c5062 | 85 | //pc.printf("%d\t%d\t- %d\n\r",acc_x,acc_y,fire_button); |
| clarkjarvis | 2:9caef35c5062 | 86 | //pc.printf("%01x %01x %01x %01x %01x %01x\n\r",rxData[1],rxData[0],rxData[3],rxData[2],rxData[5],rxData[4]); |
| clarkjarvis | 2:9caef35c5062 | 87 | |
| clarkjarvis | 2:9caef35c5062 | 88 | v_pulse = V_SERVO_CENTER + acc_x; |
| clarkjarvis | 2:9caef35c5062 | 89 | h_pulse = H_SERVO_CENTER + acc_y; |
| clarkjarvis | 2:9caef35c5062 | 90 | |
| clarkjarvis | 2:9caef35c5062 | 91 | if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; |
| clarkjarvis | 2:9caef35c5062 | 92 | if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; |
| clarkjarvis | 2:9caef35c5062 | 93 | |
| clarkjarvis | 2:9caef35c5062 | 94 | if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; |
| clarkjarvis | 2:9caef35c5062 | 95 | if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; |
| clarkjarvis | 2:9caef35c5062 | 96 | |
| clarkjarvis | 2:9caef35c5062 | 97 | v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms |
| clarkjarvis | 2:9caef35c5062 | 98 | h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms |
| clarkjarvis | 2:9caef35c5062 | 99 | |
| clarkjarvis | 2:9caef35c5062 | 100 | //pc.printf("%d\t%d\t\n\r",v_pulse,h_pulse); |
| clarkjarvis | 2:9caef35c5062 | 101 | |
| clarkjarvis | 2:9caef35c5062 | 102 | myled1 = fire_button; |
| clarkjarvis | 2:9caef35c5062 | 103 | myled3 = !fire_button; |
| clarkjarvis | 2:9caef35c5062 | 104 | fire = !fire_button; |
| clarkjarvis | 2:9caef35c5062 | 105 | |
| Owen | 0:a51a6e7da590 | 106 | } |
| Owen | 0:a51a6e7da590 | 107 | } |
| Owen | 0:a51a6e7da590 | 108 | } |
