TX (remote) side of the Nerf demo. Uses the FRDM-K64F and nRF24L01P to take acceleration data and send it wirelessly to the base unit.

Dependencies:   DebounceIn FXOS8700Q mbed nRF24L01P

Dependents:   NerfGun_nRF24L01P_TX_9dof

Fork of NerfGun_nRF24L01P_TX by Clark Jarvis

Committer:
johnmc
Date:
Wed May 21 13:47:07 2014 +0000
Revision:
1:ebfa9cb235de
Parent:
0:b5e995814400
TX (remote) side of Nerf demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
clarkjarvis 0:b5e995814400 1 #include "mbed.h"
clarkjarvis 0:b5e995814400 2 #include "nRF24L01P.h"
clarkjarvis 0:b5e995814400 3 #include "FXOS8700Q.h"
clarkjarvis 0:b5e995814400 4 #include "DebounceIn.h"
clarkjarvis 0:b5e995814400 5
clarkjarvis 0:b5e995814400 6 #define ACC_SAMPLE_SIZE 200
clarkjarvis 0:b5e995814400 7 #define ACC_X_GAIN 1
clarkjarvis 0:b5e995814400 8 #define ACC_Y_GAIN 2
clarkjarvis 0:b5e995814400 9 #define TRANSFER_SIZE 9
clarkjarvis 0:b5e995814400 10
clarkjarvis 0:b5e995814400 11 Serial pc(USBTX, USBRX); // tx, rx
clarkjarvis 0:b5e995814400 12
clarkjarvis 0:b5e995814400 13 nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18); // mosi, miso, sck, csn, ce, irq
clarkjarvis 0:b5e995814400 14 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board
clarkjarvis 0:b5e995814400 15
clarkjarvis 0:b5e995814400 16 DigitalOut myled1(LED1);
clarkjarvis 0:b5e995814400 17 DigitalOut myled2(LED2);
clarkjarvis 0:b5e995814400 18 DigitalOut myled3(LED3);
clarkjarvis 0:b5e995814400 19 MotionSensorDataCounts acc_raw;
clarkjarvis 0:b5e995814400 20 int16_t acc_x, acc_y;
clarkjarvis 0:b5e995814400 21 DebounceIn fire_button(PTA4);
clarkjarvis 0:b5e995814400 22 DebounceIn cal_button(PTC6);
clarkjarvis 0:b5e995814400 23
clarkjarvis 0:b5e995814400 24 int main() {
clarkjarvis 0:b5e995814400 25
clarkjarvis 0:b5e995814400 26 char txData[TRANSFER_SIZE];
clarkjarvis 0:b5e995814400 27 int txDataCnt = 0;
clarkjarvis 0:b5e995814400 28
clarkjarvis 0:b5e995814400 29 int acc_x_array[ACC_SAMPLE_SIZE];
clarkjarvis 0:b5e995814400 30 int acc_y_array[ACC_SAMPLE_SIZE];
clarkjarvis 0:b5e995814400 31 int acc_sample_cnt = 0;
clarkjarvis 0:b5e995814400 32
clarkjarvis 0:b5e995814400 33 int acc_x_avg = 0;
clarkjarvis 0:b5e995814400 34 int acc_y_avg = 0;
clarkjarvis 0:b5e995814400 35
clarkjarvis 0:b5e995814400 36 int acc_x_cal = 0;
clarkjarvis 0:b5e995814400 37 int acc_y_cal = 0;
clarkjarvis 0:b5e995814400 38
clarkjarvis 0:b5e995814400 39 pc.baud(115200);
clarkjarvis 0:b5e995814400 40
johnmc 1:ebfa9cb235de 41 my_nrf24l01p.setTxAddress(0xDEADBEEF0F);
clarkjarvis 0:b5e995814400 42 my_nrf24l01p.powerUp();
clarkjarvis 0:b5e995814400 43 myled1 = 1;
clarkjarvis 0:b5e995814400 44 myled2 = 1;
clarkjarvis 0:b5e995814400 45
clarkjarvis 0:b5e995814400 46 // Display the setup of the nRF24L01+ chip
clarkjarvis 0:b5e995814400 47 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
clarkjarvis 0:b5e995814400 48 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
clarkjarvis 0:b5e995814400 49 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
clarkjarvis 0:b5e995814400 50 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
clarkjarvis 0:b5e995814400 51 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
clarkjarvis 0:b5e995814400 52
clarkjarvis 0:b5e995814400 53 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
clarkjarvis 0:b5e995814400 54 my_nrf24l01p.enable();
clarkjarvis 0:b5e995814400 55
clarkjarvis 0:b5e995814400 56 acc.enable();
clarkjarvis 0:b5e995814400 57
clarkjarvis 0:b5e995814400 58 myled3 = 0;
clarkjarvis 0:b5e995814400 59
clarkjarvis 0:b5e995814400 60 for (int x=0; x<ACC_SAMPLE_SIZE; x++) {
clarkjarvis 0:b5e995814400 61 acc_x_array[x]=0;
clarkjarvis 0:b5e995814400 62 acc_y_array[x]=0;
clarkjarvis 0:b5e995814400 63 }
clarkjarvis 0:b5e995814400 64
clarkjarvis 0:b5e995814400 65 while (1) {
clarkjarvis 0:b5e995814400 66 acc.getAxis(acc_raw);
clarkjarvis 0:b5e995814400 67
clarkjarvis 0:b5e995814400 68 acc_x = acc_raw.x - acc_x_cal;
clarkjarvis 0:b5e995814400 69 acc_y = acc_raw.y - acc_y_cal;
clarkjarvis 0:b5e995814400 70
clarkjarvis 0:b5e995814400 71 acc_x_array[acc_sample_cnt]=(int)(acc_x>>2);
clarkjarvis 0:b5e995814400 72 acc_y_array[acc_sample_cnt]=(int)(acc_y>>2);
clarkjarvis 0:b5e995814400 73
clarkjarvis 0:b5e995814400 74 acc_sample_cnt++;
clarkjarvis 0:b5e995814400 75 if (acc_sample_cnt>=ACC_SAMPLE_SIZE) acc_sample_cnt = 0;
clarkjarvis 0:b5e995814400 76
clarkjarvis 0:b5e995814400 77 acc_x_avg=0;
clarkjarvis 0:b5e995814400 78 acc_y_avg=0;
clarkjarvis 0:b5e995814400 79 for (int x=0; x<ACC_SAMPLE_SIZE; x++) {
clarkjarvis 0:b5e995814400 80 acc_x_avg=acc_x_avg+acc_x_array[x];
clarkjarvis 0:b5e995814400 81 acc_y_avg=acc_y_avg+acc_y_array[x];
clarkjarvis 0:b5e995814400 82 }
clarkjarvis 0:b5e995814400 83 acc_x_avg = (int)(acc_x_avg/ACC_SAMPLE_SIZE);
clarkjarvis 0:b5e995814400 84 acc_y_avg = (int)(acc_y_avg/ACC_SAMPLE_SIZE);
clarkjarvis 0:b5e995814400 85
clarkjarvis 0:b5e995814400 86 //pc.printf("%d (%d)\t%d (%d)\n\r",acc_x_avg,acc_raw.x,acc_y_avg,acc_raw.y);
clarkjarvis 0:b5e995814400 87
clarkjarvis 0:b5e995814400 88 txData[0] = (acc_x_avg) & 0xff;
clarkjarvis 0:b5e995814400 89 txData[1] = (acc_x_avg>>8) & 0xff;
clarkjarvis 0:b5e995814400 90 txData[2] = (acc_x_avg>>16) & 0xff;
clarkjarvis 0:b5e995814400 91 txData[3] = (acc_x_avg>>24) & 0xff;
clarkjarvis 0:b5e995814400 92
clarkjarvis 0:b5e995814400 93 txData[4] = (acc_y_avg) & 0xff;
clarkjarvis 0:b5e995814400 94 txData[5] = (acc_y_avg>>8) & 0xff;
clarkjarvis 0:b5e995814400 95 txData[6] = (acc_y_avg>>16) & 0xff;
clarkjarvis 0:b5e995814400 96 txData[7] = (acc_y_avg>>24) & 0xff;
clarkjarvis 0:b5e995814400 97
clarkjarvis 0:b5e995814400 98 txData[8] = (char)(fire_button.read());
clarkjarvis 0:b5e995814400 99 myled1 = fire_button.read();
clarkjarvis 0:b5e995814400 100 myled3 = !myled1;
clarkjarvis 0:b5e995814400 101
clarkjarvis 0:b5e995814400 102 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, sizeof( txData ) );
clarkjarvis 0:b5e995814400 103
clarkjarvis 0:b5e995814400 104 wait(0.001);
clarkjarvis 0:b5e995814400 105
clarkjarvis 0:b5e995814400 106 if (cal_button.read()==0) {
clarkjarvis 0:b5e995814400 107 myled2 = 0;
clarkjarvis 0:b5e995814400 108 myled3 = 1;
clarkjarvis 0:b5e995814400 109
clarkjarvis 0:b5e995814400 110 acc_x_avg=0;
clarkjarvis 0:b5e995814400 111 acc_y_avg=0;
clarkjarvis 0:b5e995814400 112 for (int x=0; x<ACC_SAMPLE_SIZE; x++) {
clarkjarvis 0:b5e995814400 113 acc.getAxis(acc_raw);
clarkjarvis 0:b5e995814400 114 acc_x_avg=acc_x_avg+acc_raw.x;
clarkjarvis 0:b5e995814400 115 acc_y_avg=acc_y_avg+acc_raw.y;
clarkjarvis 0:b5e995814400 116 wait(0.01);
clarkjarvis 0:b5e995814400 117 }
clarkjarvis 0:b5e995814400 118 acc_x_cal = (int)(acc_x_avg/ACC_SAMPLE_SIZE);
clarkjarvis 0:b5e995814400 119 acc_y_cal = (int)(acc_y_avg/ACC_SAMPLE_SIZE);
clarkjarvis 0:b5e995814400 120 myled2 = 1;
clarkjarvis 0:b5e995814400 121 myled3 = 0;
clarkjarvis 0:b5e995814400 122 }
clarkjarvis 0:b5e995814400 123
clarkjarvis 0:b5e995814400 124 }
clarkjarvis 0:b5e995814400 125 }