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@11:1b56bb8ccd04, 2018-11-25 (annotated)
- Committer:
- hyunsungkim
- Date:
- Sun Nov 25 12:39:01 2018 +0000
- Branch:
- RF24_library_test_tx
- Revision:
- 11:1b56bb8ccd04
- Parent:
- 10:02d3ca034103
- Child:
- 12:cf6cbf1d1ebf
13?? ??? ??? ? ? ?? ??? ??? ?? ??
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 | 11:1b56bb8ccd04 | 38 | void dumpRFInfo(); |
| hyunsungkim | 11:1b56bb8ccd04 | 39 | void turnWheel(int rspd, int lspd); |
| hyunsungkim | 10:02d3ca034103 | 40 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData); |
| hyunsungkim | 8:78294b936c70 | 41 | |
| hyunsungkim | 6:42ecdff5956a | 42 | |
| hyunsungkim | 0:2dade6db4d3a | 43 | int main() { |
| hyunsungkim | 10:02d3ca034103 | 44 | int role = ROLE; |
| hyunsungkim | 10:02d3ca034103 | 45 | int id=0; |
| hyunsungkim | 8:78294b936c70 | 46 | char txData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 47 | int txDataCnt = 0; |
| hyunsungkim | 8:78294b936c70 | 48 | char rxData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 49 | int rxDataCnt = 0; |
| hyunsungkim | 6:42ecdff5956a | 50 | |
| hyunsungkim | 8:78294b936c70 | 51 | beepStart(); |
| hyunsungkim | 0:2dade6db4d3a | 52 | pc.baud(115200); |
| hyunsungkim | 11:1b56bb8ccd04 | 53 | initNRF(role); |
| hyunsungkim | 11:1b56bb8ccd04 | 54 | dumpRFInfo(); |
| hyunsungkim | 0:2dade6db4d3a | 55 | |
| hyunsungkim | 10:02d3ca034103 | 56 | while(1) { |
| hyunsungkim | 10:02d3ca034103 | 57 | if(role == PING) { |
| hyunsungkim | 10:02d3ca034103 | 58 | int id = 1; |
| hyunsungkim | 11:1b56bb8ccd04 | 59 | int lspeed=-40; |
| hyunsungkim | 11:1b56bb8ccd04 | 60 | int rspeed=50; |
| hyunsungkim | 10:02d3ca034103 | 61 | //printf("transmitting\r\n"); |
| hyunsungkim | 10:02d3ca034103 | 62 | txDataCnt++; |
| hyunsungkim | 10:02d3ca034103 | 63 | getPayload(id, txDataCnt, lspeed, rspeed, txData); |
| hyunsungkim | 10:02d3ca034103 | 64 | //char txData_[] = "123456789ABCD"; |
| hyunsungkim | 10:02d3ca034103 | 65 | pc.printf("PING:%s\r\n", txData); |
| hyunsungkim | 10:02d3ca034103 | 66 | nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE); |
| hyunsungkim | 10:02d3ca034103 | 67 | wait(0.2); |
| hyunsungkim | 10:02d3ca034103 | 68 | } |
| hyunsungkim | 10:02d3ca034103 | 69 | else if(role == PONG) { |
| hyunsungkim | 10:02d3ca034103 | 70 | if ( nrf.readable() ) { |
| hyunsungkim | 10:02d3ca034103 | 71 | rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE); |
| hyunsungkim | 10:02d3ca034103 | 72 | printf("%s\r\n", rxData); |
| hyunsungkim | 11:1b56bb8ccd04 | 73 | int rspd = (rxData[8]-'0')*10+(rxData[9]-'0'); |
| hyunsungkim | 11:1b56bb8ccd04 | 74 | int lspd = (rxData[11]-'0')*10+(rxData[12]-'0'); |
| hyunsungkim | 11:1b56bb8ccd04 | 75 | if(rxData[7]=='-') |
| hyunsungkim | 11:1b56bb8ccd04 | 76 | rspd = -rspd; |
| hyunsungkim | 11:1b56bb8ccd04 | 77 | if(rxData[10]=='-') |
| hyunsungkim | 11:1b56bb8ccd04 | 78 | lspd = -lspd; |
| hyunsungkim | 11:1b56bb8ccd04 | 79 | turnWheel(rspd, lspd); |
| hyunsungkim | 11:1b56bb8ccd04 | 80 | printf("%d, %d\r\n", rspd,lspd); |
| hyunsungkim | 10:02d3ca034103 | 81 | } |
| hyunsungkim | 10:02d3ca034103 | 82 | } |
| hyunsungkim | 10:02d3ca034103 | 83 | } |
| hyunsungkim | 10:02d3ca034103 | 84 | } |
| hyunsungkim | 10:02d3ca034103 | 85 | |
| hyunsungkim | 10:02d3ca034103 | 86 | void turnWheel(int rspd, int lspd) |
| hyunsungkim | 10:02d3ca034103 | 87 | { |
| hyunsungkim | 10:02d3ca034103 | 88 | if(rspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 89 | motor_RA.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 90 | motor_RB = 0; |
| hyunsungkim | 10:02d3ca034103 | 91 | } else { |
| hyunsungkim | 11:1b56bb8ccd04 | 92 | rspd=-rspd; |
| hyunsungkim | 10:02d3ca034103 | 93 | motor_RB.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 94 | motor_RA = 0; |
| hyunsungkim | 10:02d3ca034103 | 95 | } |
| hyunsungkim | 10:02d3ca034103 | 96 | if(lspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 97 | motor_LA.write((float)lspd/100); |
| hyunsungkim | 11:1b56bb8ccd04 | 98 | motor_LB = 0; |
| hyunsungkim | 10:02d3ca034103 | 99 | } else { |
| hyunsungkim | 11:1b56bb8ccd04 | 100 | lspd = -lspd; |
| hyunsungkim | 10:02d3ca034103 | 101 | motor_LB.write((float)lspd/100); |
| hyunsungkim | 11:1b56bb8ccd04 | 102 | motor_LA = 0; |
| hyunsungkim | 10:02d3ca034103 | 103 | } |
| hyunsungkim | 10:02d3ca034103 | 104 | } |
| hyunsungkim | 10:02d3ca034103 | 105 | |
| hyunsungkim | 10:02d3ca034103 | 106 | void dumpRFInfo() |
| hyunsungkim | 10:02d3ca034103 | 107 | { |
| hyunsungkim | 10:02d3ca034103 | 108 | printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() ); |
| hyunsungkim | 10:02d3ca034103 | 109 | printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() ); |
| hyunsungkim | 10:02d3ca034103 | 110 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() ); |
| hyunsungkim | 10:02d3ca034103 | 111 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 112 | printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 113 | } |
| hyunsungkim | 10:02d3ca034103 | 114 | |
| hyunsungkim | 10:02d3ca034103 | 115 | void initNRF(int role) |
| hyunsungkim | 10:02d3ca034103 | 116 | { |
| hyunsungkim | 8:78294b936c70 | 117 | if(role == PING) { |
| hyunsungkim | 8:78294b936c70 | 118 | nrf.setTxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 119 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 120 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 121 | nrf.setTransmitMode(); |
| hyunsungkim | 8:78294b936c70 | 122 | nrf.enable(); |
| hyunsungkim | 8:78294b936c70 | 123 | } else { |
| hyunsungkim | 8:78294b936c70 | 124 | nrf.setRxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 125 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 126 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 127 | nrf.setReceiveMode(); |
| hyunsungkim | 8:78294b936c70 | 128 | nrf.enable(); |
| hyunsungkim | 0:2dade6db4d3a | 129 | } |
| hyunsungkim | 8:78294b936c70 | 130 | } |
| hyunsungkim | 8:78294b936c70 | 131 | |
| hyunsungkim | 10:02d3ca034103 | 132 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData) |
| hyunsungkim | 10:02d3ca034103 | 133 | { |
| hyunsungkim | 10:02d3ca034103 | 134 | *(txData+0) = id/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 135 | *(txData+1) = id%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 136 | *(txData+2) = count/10000+'0'; |
| hyunsungkim | 10:02d3ca034103 | 137 | *(txData+3) = count/1000%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 138 | *(txData+4) = count/100%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 139 | *(txData+5) = count/10%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 140 | *(txData+6) = count%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 141 | *(txData+7) = lspeed>0?'+':'-'; |
| hyunsungkim | 10:02d3ca034103 | 142 | *(txData+8) = abs(lspeed)/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 143 | *(txData+9) = abs(lspeed)%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 144 | *(txData+10) = rspeed>0?'+':'-'; |
| hyunsungkim | 10:02d3ca034103 | 145 | *(txData+11) = abs(rspeed)/10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 146 | *(txData+12) = abs(rspeed)%10+'0'; |
| hyunsungkim | 10:02d3ca034103 | 147 | *(txData+13) = '\0'; |
| hyunsungkim | 10:02d3ca034103 | 148 | } |