RX basestation controller of the NERF demo. Designed for use with the FRDM-K64F and the nRF24L01P module.

Dependencies:   mbed nRF24L01P

Fork of NerfGun_nRF24L01P_RX by Clark Jarvis

Revision:
2:9caef35c5062
Parent:
1:5be2682710c6
Child:
3:fb5f79ff292b
--- a/main.cpp	Wed Jan 19 23:53:19 2011 +0000
+++ b/main.cpp	Mon May 12 18:47:06 2014 +0000
@@ -1,24 +1,40 @@
 #include "mbed.h"
 #include "nRF24L01P.h"
 
+#define V_SERVO_CENTER 1800
+#define V_SERVO_MAX 2200
+#define V_SERVO_MIN 800
+#define H_SERVO_CENTER 1600
+#define H_SERVO_MAX 2200
+#define H_SERVO_MIN 800
+#define TRANSFER_SIZE   9
+
 Serial pc(USBTX, USBRX); // tx, rx
 
-nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10);    // mosi, miso, sck, csn, ce, irq
+nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18);    // mosi, miso, sck, csn, ce, irq
 
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+
+DigitalOut fire(PTB23);
+PwmOut v_servo(PTC2);
+PwmOut h_servo(PTA2);
 
 int main() {
 
 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
 //  only handles 4 byte transfers in the ATMega code.
-#define TRANSFER_SIZE   4
+    myled1 = 1;
+    myled2 = 1;
+    myled3 = 0;
 
-    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
-    int txDataCnt = 0;
+    pc.baud(115200);
+    char rxData[TRANSFER_SIZE];
     int rxDataCnt = 0;
 
+    my_nrf24l01p.setRxAddress(0xDEADBEEF01);
     my_nrf24l01p.powerUp();
 
     // Display the (default) setup of the nRF24L01+ chip
@@ -28,48 +44,65 @@
     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
 
-    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
+    pc.printf( "This is the Receiver\r\n");
 
     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
 
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();
 
-    while (1) {
-
-        // If we've received anything over the host serial link...
-        if ( pc.readable() ) {
-
-            // ...add it to the transmit buffer
-            txData[txDataCnt++] = pc.getc();
+    
+    v_servo.period_us(20000);          // servo requires a 20ms period
+    v_servo.pulsewidth_us(V_SERVO_CENTER);
+    h_servo.period_us(20000);          // servo requires a 20ms period
+    h_servo.pulsewidth_us(H_SERVO_CENTER);
+    fire = 0;
+    
+    int v_pulse = V_SERVO_CENTER;
+    int h_pulse = H_SERVO_CENTER;
 
-            // If the transmit buffer is full
-            if ( txDataCnt >= sizeof( txData ) ) {
-
-                // Send the transmitbuffer via the nRF24L01+
-                my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
+    v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
+    h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
 
-                txDataCnt = 0;
-            }
-
-            // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
-            myled1 = !myled1;
-        }
+    wait(0.5);
+    
+    int32_t acc_x, acc_y;
+    char fire_button;
+    
+    while (1) {
 
         // If we've received anything in the nRF24L01+...
         if ( my_nrf24l01p.readable() ) {
-
+            
             // ...read the data into the receive buffer
             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
 
             // Display the receive buffer contents via the host serial link
-            for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
-
-                pc.putc( rxData[i] );
-            }
-
-            // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
-            myled2 = !myled2;
+            acc_x = (int32_t)((rxData[0])|(rxData[1]<<8)|(rxData[2]<<16)|(rxData[3]<<24));
+            acc_y = (int32_t)((rxData[4])|(rxData[5]<<8)|(rxData[6]<<16)|(rxData[7]<<24));
+            fire_button = rxData[8];
+            
+            //pc.printf("%d\t%d\t- %d\n\r",acc_x,acc_y,fire_button);
+            //pc.printf("%01x %01x %01x %01x %01x %01x\n\r",rxData[1],rxData[0],rxData[3],rxData[2],rxData[5],rxData[4]);
+            
+            v_pulse = V_SERVO_CENTER + acc_x;
+            h_pulse = H_SERVO_CENTER + acc_y;
+    
+            if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
+            if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
+    
+            if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
+            if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
+    
+            v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
+            h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
+            
+            //pc.printf("%d\t%d\t\n\r",v_pulse,h_pulse); 
+            
+            myled1 = fire_button;
+            myled3 = !fire_button;
+            fire = !fire_button;
+            
         }
     }
 }