This is a Receiver Code for ROBO_ARM.

Dependencies:   mbed nRF24L01P

Committer:
nrrathi
Date:
Mon May 09 16:01:57 2016 +0000
Revision:
0:6e6d30df73c4
This is a Receiver Code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nrrathi 0:6e6d30df73c4 1 #include "mbed.h"
nrrathi 0:6e6d30df73c4 2 #include "nRF24L01P.h"
nrrathi 0:6e6d30df73c4 3
nrrathi 0:6e6d30df73c4 4 #define V_SERVO_CENTER 1800
nrrathi 0:6e6d30df73c4 5 #define V_SERVO_MAX 2200
nrrathi 0:6e6d30df73c4 6 #define V_SERVO_MIN 800
nrrathi 0:6e6d30df73c4 7 #define H_SERVO_CENTER 1600
nrrathi 0:6e6d30df73c4 8 #define H_SERVO_MAX 2200
nrrathi 0:6e6d30df73c4 9 #define H_SERVO_MIN 800
nrrathi 0:6e6d30df73c4 10 #define TRANSFER_SIZE 9
nrrathi 0:6e6d30df73c4 11
nrrathi 0:6e6d30df73c4 12 Serial pc(USBTX, USBRX); // tx, rx
nrrathi 0:6e6d30df73c4 13
nrrathi 0:6e6d30df73c4 14 nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18); // mosi, miso, sck, csn, ce, irq
nrrathi 0:6e6d30df73c4 15
nrrathi 0:6e6d30df73c4 16 DigitalOut myled1(LED1);
nrrathi 0:6e6d30df73c4 17 DigitalOut myled2(LED2);
nrrathi 0:6e6d30df73c4 18 DigitalOut myled3(LED3);
nrrathi 0:6e6d30df73c4 19
nrrathi 0:6e6d30df73c4 20 PwmOut v_servo(PTC2);
nrrathi 0:6e6d30df73c4 21 PwmOut h_servo(PTA2);
nrrathi 0:6e6d30df73c4 22
nrrathi 0:6e6d30df73c4 23 int main() {
nrrathi 0:6e6d30df73c4 24
nrrathi 0:6e6d30df73c4 25 myled1 = 1;
nrrathi 0:6e6d30df73c4 26 myled2 = 1;
nrrathi 0:6e6d30df73c4 27 myled3 = 0;
nrrathi 0:6e6d30df73c4 28
nrrathi 0:6e6d30df73c4 29 pc.baud(115200);
nrrathi 0:6e6d30df73c4 30 char rxData[TRANSFER_SIZE];
nrrathi 0:6e6d30df73c4 31 int rxDataCnt = 0;
nrrathi 0:6e6d30df73c4 32
nrrathi 0:6e6d30df73c4 33 my_nrf24l01p.setRxAddress(0xDEADBEEF0F);
nrrathi 0:6e6d30df73c4 34 my_nrf24l01p.powerUp();
nrrathi 0:6e6d30df73c4 35
nrrathi 0:6e6d30df73c4 36 // Display the (default) setup of the nRF24L01+ chip
nrrathi 0:6e6d30df73c4 37 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
nrrathi 0:6e6d30df73c4 38 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
nrrathi 0:6e6d30df73c4 39 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
nrrathi 0:6e6d30df73c4 40 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
nrrathi 0:6e6d30df73c4 41 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
nrrathi 0:6e6d30df73c4 42
nrrathi 0:6e6d30df73c4 43 pc.printf( "This is the Receiver\r\n");
nrrathi 0:6e6d30df73c4 44
nrrathi 0:6e6d30df73c4 45 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
nrrathi 0:6e6d30df73c4 46
nrrathi 0:6e6d30df73c4 47 my_nrf24l01p.setReceiveMode();
nrrathi 0:6e6d30df73c4 48 my_nrf24l01p.enable();
nrrathi 0:6e6d30df73c4 49
nrrathi 0:6e6d30df73c4 50
nrrathi 0:6e6d30df73c4 51 v_servo.period_us(20000); // servo requires a 20ms period
nrrathi 0:6e6d30df73c4 52 v_servo.pulsewidth_us(V_SERVO_CENTER);
nrrathi 0:6e6d30df73c4 53 h_servo.period_us(20000); // servo requires a 20ms period
nrrathi 0:6e6d30df73c4 54 h_servo.pulsewidth_us(H_SERVO_CENTER);
nrrathi 0:6e6d30df73c4 55
nrrathi 0:6e6d30df73c4 56 int v_pulse = V_SERVO_CENTER;
nrrathi 0:6e6d30df73c4 57 int h_pulse = H_SERVO_CENTER;
nrrathi 0:6e6d30df73c4 58
nrrathi 0:6e6d30df73c4 59 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
nrrathi 0:6e6d30df73c4 60 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
nrrathi 0:6e6d30df73c4 61
nrrathi 0:6e6d30df73c4 62 wait(0.5);
nrrathi 0:6e6d30df73c4 63
nrrathi 0:6e6d30df73c4 64 int32_t acc_x, acc_y;
nrrathi 0:6e6d30df73c4 65
nrrathi 0:6e6d30df73c4 66 while (1) {
nrrathi 0:6e6d30df73c4 67
nrrathi 0:6e6d30df73c4 68 // If we've received anything in the nRF24L01+...
nrrathi 0:6e6d30df73c4 69 if ( my_nrf24l01p.readable() ) {
nrrathi 0:6e6d30df73c4 70 pc.printf("I just received data");
nrrathi 0:6e6d30df73c4 71
nrrathi 0:6e6d30df73c4 72 // ...read the data into the receive buffer
nrrathi 0:6e6d30df73c4 73 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
nrrathi 0:6e6d30df73c4 74
nrrathi 0:6e6d30df73c4 75 // Display the receive buffer contents via the host serial link
nrrathi 0:6e6d30df73c4 76 acc_x = (int32_t)((rxData[0])|(rxData[1]<<8)|(rxData[2]<<16)|(rxData[3]<<24));
nrrathi 0:6e6d30df73c4 77 acc_y = (int32_t)((rxData[4])|(rxData[5]<<8)|(rxData[6]<<16)|(rxData[7]<<24));
nrrathi 0:6e6d30df73c4 78
nrrathi 0:6e6d30df73c4 79 pc.printf("%d\t%d\t- \n\r",acc_x,acc_y);
nrrathi 0:6e6d30df73c4 80 pc.printf("%01x %01x %01x %01x %01x %01x\n\r",rxData[1],rxData[0],rxData[3],rxData[2],rxData[5],rxData[4]);
nrrathi 0:6e6d30df73c4 81
nrrathi 0:6e6d30df73c4 82 v_pulse = V_SERVO_CENTER + acc_x;
nrrathi 0:6e6d30df73c4 83 h_pulse = H_SERVO_CENTER + acc_y;
nrrathi 0:6e6d30df73c4 84
nrrathi 0:6e6d30df73c4 85 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
nrrathi 0:6e6d30df73c4 86 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
nrrathi 0:6e6d30df73c4 87
nrrathi 0:6e6d30df73c4 88 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
nrrathi 0:6e6d30df73c4 89 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
nrrathi 0:6e6d30df73c4 90
nrrathi 0:6e6d30df73c4 91 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
nrrathi 0:6e6d30df73c4 92 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
nrrathi 0:6e6d30df73c4 93
nrrathi 0:6e6d30df73c4 94 pc.printf("%d\t%d\t\n\r",v_pulse,h_pulse);
nrrathi 0:6e6d30df73c4 95 }
nrrathi 0:6e6d30df73c4 96 }
nrrathi 0:6e6d30df73c4 97 }