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