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.
main.cpp@10:02d3ca034103, 2018-11-25 (annotated)
- Committer:
- hyunsungkim
- Date:
- Sun Nov 25 11:31:55 2018 +0000
- Branch:
- RF24_library_test_tx
- Revision:
- 10:02d3ca034103
- Parent:
- 9:d85804e8d3b3
- Child:
- 11:1b56bb8ccd04
??? ?? ??
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hyunsungkim | 0:2dade6db4d3a | 1 | #include "mbed.h" |
| hyunsungkim | 8:78294b936c70 | 2 | #include "nRF24L01P.h" |
| hyunsungkim | 3:690740ab3394 | 3 | #include "beep.h" |
| hyunsungkim | 3:690740ab3394 | 4 | |
| hyunsungkim | 8:78294b936c70 | 5 | #define PING 1 |
| hyunsungkim | 8:78294b936c70 | 6 | #define PONG 2 |
| hyunsungkim | 3:690740ab3394 | 7 | |
| hyunsungkim | 10:02d3ca034103 | 8 | #define ROLE PONG |
| hyunsungkim | 10:02d3ca034103 | 9 | #define ID 0 |
| hyunsungkim | 10:02d3ca034103 | 10 | |
| hyunsungkim | 3:690740ab3394 | 11 | #define nrf_CE D2 |
| hyunsungkim | 3:690740ab3394 | 12 | #define nrf_CSN A3 |
| hyunsungkim | 3:690740ab3394 | 13 | #define spi_SCK D13 |
| hyunsungkim | 3:690740ab3394 | 14 | #define spi_MOSI D11 |
| hyunsungkim | 3:690740ab3394 | 15 | #define spi_MISO D12 |
| hyunsungkim | 8:78294b936c70 | 16 | #define spi_IRQ D4 |
| hyunsungkim | 8:78294b936c70 | 17 | |
| hyunsungkim | 8:78294b936c70 | 18 | #define TRANSFER_SIZE 14 |
| hyunsungkim | 8:78294b936c70 | 19 | |
| hyunsungkim | 8:78294b936c70 | 20 | nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq |
| hyunsungkim | 8:78294b936c70 | 21 | |
| hyunsungkim | 0:2dade6db4d3a | 22 | Serial pc(USBTX, USBRX); |
| hyunsungkim | 0:2dade6db4d3a | 23 | Serial lidar(D1, D0); |
| hyunsungkim | 8:78294b936c70 | 24 | |
| hyunsungkim | 0:2dade6db4d3a | 25 | PwmOut motor_RA(D9); |
| hyunsungkim | 0:2dade6db4d3a | 26 | PwmOut motor_RB(D10); |
| hyunsungkim | 0:2dade6db4d3a | 27 | PwmOut motor_LA(D3); |
| hyunsungkim | 0:2dade6db4d3a | 28 | PwmOut motor_LB(D6); |
| hyunsungkim | 4:ae81aeeed069 | 29 | PwmOut led_B(A5); |
| hyunsungkim | 0:2dade6db4d3a | 30 | PwmOut led_G(A2); |
| hyunsungkim | 4:ae81aeeed069 | 31 | PwmOut led_R(A1); |
| hyunsungkim | 0:2dade6db4d3a | 32 | PwmOut buzzer(D5); |
| hyunsungkim | 0:2dade6db4d3a | 33 | AnalogIn batteryCheck(A0); |
| hyunsungkim | 0:2dade6db4d3a | 34 | |
| hyunsungkim | 8:78294b936c70 | 35 | void beepStart(); |
| hyunsungkim | 8:78294b936c70 | 36 | void endBeep(); |
| hyunsungkim | 10:02d3ca034103 | 37 | void initNRF(int role); |
| hyunsungkim | 10:02d3ca034103 | 38 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData); |
| hyunsungkim | 8:78294b936c70 | 39 | |
| hyunsungkim | 6:42ecdff5956a | 40 | |
| hyunsungkim | 0:2dade6db4d3a | 41 | int main() { |
| hyunsungkim | 10:02d3ca034103 | 42 | int role = ROLE; |
| hyunsungkim | 10:02d3ca034103 | 43 | int id=0; |
| hyunsungkim | 8:78294b936c70 | 44 | char txData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 45 | int txDataCnt = 0; |
| hyunsungkim | 8:78294b936c70 | 46 | char rxData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 47 | int rxDataCnt = 0; |
| hyunsungkim | 6:42ecdff5956a | 48 | |
| hyunsungkim | 8:78294b936c70 | 49 | beepStart(); |
| hyunsungkim | 0:2dade6db4d3a | 50 | pc.baud(115200); |
| hyunsungkim | 8:78294b936c70 | 51 | // Print setting of radio module |
| hyunsungkim | 0:2dade6db4d3a | 52 | |
| hyunsungkim | 10:02d3ca034103 | 53 | initNRF(role); |
| hyunsungkim | 10:02d3ca034103 | 54 | |
| hyunsungkim | 10:02d3ca034103 | 55 | while(1) { |
| hyunsungkim | 10:02d3ca034103 | 56 | if(role == PING) { |
| hyunsungkim | 10:02d3ca034103 | 57 | int id = 1; |
| hyunsungkim | 10:02d3ca034103 | 58 | int lspeed=-90; |
| hyunsungkim | 10:02d3ca034103 | 59 | int rspeed=23; |
| hyunsungkim | 10:02d3ca034103 | 60 | //printf("transmitting\r\n"); |
| hyunsungkim | 10:02d3ca034103 | 61 | txDataCnt++; |
| hyunsungkim | 10:02d3ca034103 | 62 | getPayload(id, txDataCnt, lspeed, rspeed, txData); |
| hyunsungkim | 10:02d3ca034103 | 63 | //char txData_[] = "123456789ABCD"; |
| hyunsungkim | 10:02d3ca034103 | 64 | pc.printf("PING:%s\r\n", txData); |
| hyunsungkim | 10:02d3ca034103 | 65 | nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE); |
| hyunsungkim | 10:02d3ca034103 | 66 | wait(0.2); |
| hyunsungkim | 10:02d3ca034103 | 67 | } |
| hyunsungkim | 10:02d3ca034103 | 68 | else if(role == PONG) { |
| hyunsungkim | 10:02d3ca034103 | 69 | if ( nrf.readable() ) { |
| hyunsungkim | 10:02d3ca034103 | 70 | rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE); |
| hyunsungkim | 10:02d3ca034103 | 71 | printf("%s\r\n", rxData); |
| hyunsungkim | 10:02d3ca034103 | 72 | } |
| hyunsungkim | 10:02d3ca034103 | 73 | } |
| hyunsungkim | 10:02d3ca034103 | 74 | } |
| hyunsungkim | 10:02d3ca034103 | 75 | } |
| hyunsungkim | 10:02d3ca034103 | 76 | |
| hyunsungkim | 10:02d3ca034103 | 77 | void turnWheel(int rspd, int lspd) |
| hyunsungkim | 10:02d3ca034103 | 78 | { |
| hyunsungkim | 10:02d3ca034103 | 79 | if(rspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 80 | motor_RA.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 81 | motor_RB = 0; |
| hyunsungkim | 10:02d3ca034103 | 82 | } else { |
| hyunsungkim | 10:02d3ca034103 | 83 | motor_RB.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 84 | motor_RA = 0; |
| hyunsungkim | 10:02d3ca034103 | 85 | } |
| hyunsungkim | 10:02d3ca034103 | 86 | if(lspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 87 | motor_LA.write((float)lspd/100); |
| hyunsungkim | 10:02d3ca034103 | 88 | motor_LB = 1; |
| hyunsungkim | 10:02d3ca034103 | 89 | } else { |
| hyunsungkim | 10:02d3ca034103 | 90 | motor_LB.write((float)lspd/100); |
| hyunsungkim | 10:02d3ca034103 | 91 | motor_LA = 1; |
| hyunsungkim | 10:02d3ca034103 | 92 | } |
| hyunsungkim | 10:02d3ca034103 | 93 | } |
| hyunsungkim | 10:02d3ca034103 | 94 | |
| hyunsungkim | 10:02d3ca034103 | 95 | void dumpRFInfo() |
| hyunsungkim | 10:02d3ca034103 | 96 | { |
| hyunsungkim | 10:02d3ca034103 | 97 | printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() ); |
| hyunsungkim | 10:02d3ca034103 | 98 | printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() ); |
| hyunsungkim | 10:02d3ca034103 | 99 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() ); |
| hyunsungkim | 10:02d3ca034103 | 100 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 101 | printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 102 | } |
| hyunsungkim | 10:02d3ca034103 | 103 | |
| hyunsungkim | 10:02d3ca034103 | 104 | void initNRF(int role) |
| hyunsungkim | 10:02d3ca034103 | 105 | { |
| hyunsungkim | 8:78294b936c70 | 106 | if(role == PING) { |
| hyunsungkim | 8:78294b936c70 | 107 | nrf.setTxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 108 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 109 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 110 | nrf.setTransmitMode(); |
| hyunsungkim | 8:78294b936c70 | 111 | nrf.enable(); |
| hyunsungkim | 8:78294b936c70 | 112 | } else { |
| hyunsungkim | 8:78294b936c70 | 113 | nrf.setRxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 114 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 115 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 116 | nrf.setReceiveMode(); |
| hyunsungkim | 8:78294b936c70 | 117 | nrf.enable(); |
| hyunsungkim | 0:2dade6db4d3a | 118 | } |
| hyunsungkim | 8:78294b936c70 | 119 | } |
| hyunsungkim | 8:78294b936c70 | 120 | |
| hyunsungkim | 10:02d3ca034103 | 121 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData) |
| hyunsungkim | 10:02d3ca034103 | 122 | { |
| hyunsungkim | 10:02d3ca034103 | 123 | *(txData+0) = id/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 124 | *(txData+1) = id%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 125 | *(txData+2) = count/10000+'0'; |
| hyunsungkim | 10:02d3ca034103 | 126 | *(txData+3) = count/1000%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 127 | *(txData+4) = count/100%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 128 | *(txData+5) = count/10%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 129 | *(txData+6) = count%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 130 | *(txData+7) = lspeed>0?'+':'-'; |
| hyunsungkim | 10:02d3ca034103 | 131 | *(txData+8) = abs(lspeed)/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 132 | *(txData+9) = abs(lspeed)%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 133 | *(txData+10) = rspeed>0?'+':'-'; |
| hyunsungkim | 10:02d3ca034103 | 134 | *(txData+11) = abs(rspeed)/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 135 | *(txData+12) = abs(rspeed)%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 136 | *(txData+13) = '\0'; |
| hyunsungkim | 10:02d3ca034103 | 137 | } |