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.
Dependencies: DebounceIn FXOS8700Q mbed nRF24L01P
Dependents: NerfGun_nRF24L01P_TX_9dof
Fork of NerfGun_nRF24L01P_TX by
main.cpp
00001 #include "mbed.h" 00002 #include "nRF24L01P.h" 00003 #include "FXOS8700Q.h" 00004 #include "DebounceIn.h" 00005 00006 #define ACC_SAMPLE_SIZE 200 00007 #define ACC_X_GAIN 1 00008 #define ACC_Y_GAIN 2 00009 #define TRANSFER_SIZE 9 00010 00011 Serial pc(USBTX, USBRX); // tx, rx 00012 00013 nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTC12, PTC18); // mosi, miso, sck, csn, ce, irq 00014 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board 00015 00016 DigitalOut myled1(LED1); 00017 DigitalOut myled2(LED2); 00018 DigitalOut myled3(LED3); 00019 MotionSensorDataCounts acc_raw; 00020 int16_t acc_x, acc_y; 00021 DebounceIn fire_button(PTA4); 00022 DebounceIn cal_button(PTC6); 00023 00024 int main() { 00025 00026 char txData[TRANSFER_SIZE]; 00027 int txDataCnt = 0; 00028 00029 int acc_x_array[ACC_SAMPLE_SIZE]; 00030 int acc_y_array[ACC_SAMPLE_SIZE]; 00031 int acc_sample_cnt = 0; 00032 00033 int acc_x_avg = 0; 00034 int acc_y_avg = 0; 00035 00036 int acc_x_cal = 0; 00037 int acc_y_cal = 0; 00038 00039 pc.baud(115200); 00040 00041 my_nrf24l01p.setTxAddress(0xDEADBEEF0F); 00042 my_nrf24l01p.powerUp(); 00043 myled1 = 1; 00044 myled2 = 1; 00045 00046 // Display the setup of the nRF24L01+ chip 00047 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); 00048 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); 00049 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); 00050 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); 00051 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); 00052 00053 my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); 00054 my_nrf24l01p.enable(); 00055 00056 acc.enable(); 00057 00058 myled3 = 0; 00059 00060 for (int x=0; x<ACC_SAMPLE_SIZE; x++) { 00061 acc_x_array[x]=0; 00062 acc_y_array[x]=0; 00063 } 00064 00065 while (1) { 00066 acc.getAxis(acc_raw); 00067 00068 acc_x = acc_raw.x - acc_x_cal; 00069 acc_y = acc_raw.y - acc_y_cal; 00070 00071 acc_x_array[acc_sample_cnt]=(int)(acc_x>>2); 00072 acc_y_array[acc_sample_cnt]=(int)(acc_y>>2); 00073 00074 acc_sample_cnt++; 00075 if (acc_sample_cnt>=ACC_SAMPLE_SIZE) acc_sample_cnt = 0; 00076 00077 acc_x_avg=0; 00078 acc_y_avg=0; 00079 for (int x=0; x<ACC_SAMPLE_SIZE; x++) { 00080 acc_x_avg=acc_x_avg+acc_x_array[x]; 00081 acc_y_avg=acc_y_avg+acc_y_array[x]; 00082 } 00083 acc_x_avg = (int)(acc_x_avg/ACC_SAMPLE_SIZE); 00084 acc_y_avg = (int)(acc_y_avg/ACC_SAMPLE_SIZE); 00085 00086 //pc.printf("%d (%d)\t%d (%d)\n\r",acc_x_avg,acc_raw.x,acc_y_avg,acc_raw.y); 00087 00088 txData[0] = (acc_x_avg) & 0xff; 00089 txData[1] = (acc_x_avg>>8) & 0xff; 00090 txData[2] = (acc_x_avg>>16) & 0xff; 00091 txData[3] = (acc_x_avg>>24) & 0xff; 00092 00093 txData[4] = (acc_y_avg) & 0xff; 00094 txData[5] = (acc_y_avg>>8) & 0xff; 00095 txData[6] = (acc_y_avg>>16) & 0xff; 00096 txData[7] = (acc_y_avg>>24) & 0xff; 00097 00098 txData[8] = (char)(fire_button.read()); 00099 myled1 = fire_button.read(); 00100 myled3 = !myled1; 00101 00102 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, sizeof( txData ) ); 00103 00104 wait(0.001); 00105 00106 if (cal_button.read()==0) { 00107 myled2 = 0; 00108 myled3 = 1; 00109 00110 acc_x_avg=0; 00111 acc_y_avg=0; 00112 for (int x=0; x<ACC_SAMPLE_SIZE; x++) { 00113 acc.getAxis(acc_raw); 00114 acc_x_avg=acc_x_avg+acc_raw.x; 00115 acc_y_avg=acc_y_avg+acc_raw.y; 00116 wait(0.01); 00117 } 00118 acc_x_cal = (int)(acc_x_avg/ACC_SAMPLE_SIZE); 00119 acc_y_cal = (int)(acc_y_avg/ACC_SAMPLE_SIZE); 00120 myled2 = 1; 00121 myled3 = 0; 00122 } 00123 00124 } 00125 }
Generated on Thu Jul 14 2022 01:55:54 by
1.7.2
