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@14:a4402559cd6e, 2018-12-01 (annotated)
- Committer:
- hyunsungkim
- Date:
- Sat Dec 01 15:26:36 2018 +0000
- Branch:
- RF24_library_test_tx
- Revision:
- 14:a4402559cd6e
- Parent:
- 13:f0f9a5586e45
- Child:
- 15:217b3e5a2275
?? ?? token '$' ??, ?? ??
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 | 14:a4402559cd6e | 9 | #define ROLE PC |
| hyunsungkim | 13:f0f9a5586e45 | 10 | #define ID 1 |
| 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 | 14:a4402559cd6e | 19 | #define TRANSFER_SIZE 15 |
| 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 | txDataCnt++; |
| hyunsungkim | 10:02d3ca034103 | 68 | getPayload(id, txDataCnt, lspeed, rspeed, txData); |
| hyunsungkim | 10:02d3ca034103 | 69 | pc.printf("PING:%s\r\n", txData); |
| hyunsungkim | 10:02d3ca034103 | 70 | nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE); |
| hyunsungkim | 10:02d3ca034103 | 71 | wait(0.2); |
| hyunsungkim | 12:cf6cbf1d1ebf | 72 | break; |
| hyunsungkim | 12:cf6cbf1d1ebf | 73 | case PONG: |
| hyunsungkim | 10:02d3ca034103 | 74 | if ( nrf.readable() ) { |
| hyunsungkim | 10:02d3ca034103 | 75 | rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE); |
| hyunsungkim | 14:a4402559cd6e | 76 | if(rxData[0] =='$') { |
| hyunsungkim | 14:a4402559cd6e | 77 | int id = (rxData[1]-'0')*10+(rxData[2]-'0'); |
| hyunsungkim | 14:a4402559cd6e | 78 | int dataCount = (rxData[3]-'0')*10000+(rxData[4]-'0')*1000+(rxData[5]-'0')*100+(rxData[6]-'0')*10+(rxData[7]-'0'); |
| hyunsungkim | 14:a4402559cd6e | 79 | int rspd = (rxData[9]-'0')*10+(rxData[10]-'0'); |
| hyunsungkim | 14:a4402559cd6e | 80 | int lspd = (rxData[12]-'0')*10+(rxData[13]-'0'); |
| hyunsungkim | 14:a4402559cd6e | 81 | if(rxData[8]=='-') |
| hyunsungkim | 14:a4402559cd6e | 82 | rspd = -rspd; |
| hyunsungkim | 14:a4402559cd6e | 83 | if(rxData[11]=='-') |
| hyunsungkim | 14:a4402559cd6e | 84 | lspd = -lspd; |
| hyunsungkim | 14:a4402559cd6e | 85 | |
| hyunsungkim | 14:a4402559cd6e | 86 | if(true) { |
| hyunsungkim | 14:a4402559cd6e | 87 | turnWheel(rspd, lspd); |
| hyunsungkim | 14:a4402559cd6e | 88 | printf("[%d] RxData: %s, rspd: %d, rspd: %d\r\n", id, rxData, rspd, lspd); |
| hyunsungkim | 14:a4402559cd6e | 89 | } |
| hyunsungkim | 14:a4402559cd6e | 90 | } else { |
| hyunsungkim | 14:a4402559cd6e | 91 | printf("Invalid Data\r\n"); |
| hyunsungkim | 13:f0f9a5586e45 | 92 | } |
| hyunsungkim | 10:02d3ca034103 | 93 | } |
| hyunsungkim | 12:cf6cbf1d1ebf | 94 | break; |
| hyunsungkim | 12:cf6cbf1d1ebf | 95 | case PC: |
| hyunsungkim | 12:cf6cbf1d1ebf | 96 | while(pc.readable()) { |
| hyunsungkim | 12:cf6cbf1d1ebf | 97 | char inByte = pc.getc(); |
| hyunsungkim | 12:cf6cbf1d1ebf | 98 | if(inByteCnt > TRANSFER_SIZE+2) { |
| hyunsungkim | 12:cf6cbf1d1ebf | 99 | printf("Data size overflow!\r\n"); |
| hyunsungkim | 12:cf6cbf1d1ebf | 100 | inByteCnt = 0; |
| hyunsungkim | 12:cf6cbf1d1ebf | 101 | break; |
| hyunsungkim | 14:a4402559cd6e | 102 | } else if(inByte == '\0') { |
| hyunsungkim | 12:cf6cbf1d1ebf | 103 | buf[inByteCnt] = '\0'; |
| hyunsungkim | 12:cf6cbf1d1ebf | 104 | printf("Command: %s\r\n", buf); |
| hyunsungkim | 12:cf6cbf1d1ebf | 105 | nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE); |
| hyunsungkim | 12:cf6cbf1d1ebf | 106 | inByteCnt=0; |
| hyunsungkim | 12:cf6cbf1d1ebf | 107 | } else { |
| hyunsungkim | 12:cf6cbf1d1ebf | 108 | buf[inByteCnt++] = inByte; |
| hyunsungkim | 12:cf6cbf1d1ebf | 109 | } |
| hyunsungkim | 12:cf6cbf1d1ebf | 110 | } |
| hyunsungkim | 12:cf6cbf1d1ebf | 111 | break; |
| hyunsungkim | 10:02d3ca034103 | 112 | } |
| hyunsungkim | 10:02d3ca034103 | 113 | } |
| hyunsungkim | 10:02d3ca034103 | 114 | } |
| hyunsungkim | 10:02d3ca034103 | 115 | |
| hyunsungkim | 10:02d3ca034103 | 116 | void turnWheel(int rspd, int lspd) |
| hyunsungkim | 10:02d3ca034103 | 117 | { |
| hyunsungkim | 10:02d3ca034103 | 118 | if(rspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 119 | motor_RA.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 120 | motor_RB = 0; |
| hyunsungkim | 10:02d3ca034103 | 121 | } else { |
| hyunsungkim | 11:1b56bb8ccd04 | 122 | rspd=-rspd; |
| hyunsungkim | 10:02d3ca034103 | 123 | motor_RB.write((float)rspd/100); |
| hyunsungkim | 10:02d3ca034103 | 124 | motor_RA = 0; |
| hyunsungkim | 10:02d3ca034103 | 125 | } |
| hyunsungkim | 10:02d3ca034103 | 126 | if(lspd>0) { |
| hyunsungkim | 10:02d3ca034103 | 127 | motor_LA.write((float)lspd/100); |
| hyunsungkim | 11:1b56bb8ccd04 | 128 | motor_LB = 0; |
| hyunsungkim | 10:02d3ca034103 | 129 | } else { |
| hyunsungkim | 11:1b56bb8ccd04 | 130 | lspd = -lspd; |
| hyunsungkim | 10:02d3ca034103 | 131 | motor_LB.write((float)lspd/100); |
| hyunsungkim | 11:1b56bb8ccd04 | 132 | motor_LA = 0; |
| hyunsungkim | 10:02d3ca034103 | 133 | } |
| hyunsungkim | 10:02d3ca034103 | 134 | } |
| hyunsungkim | 10:02d3ca034103 | 135 | |
| hyunsungkim | 10:02d3ca034103 | 136 | void dumpRFInfo() |
| hyunsungkim | 10:02d3ca034103 | 137 | { |
| hyunsungkim | 10:02d3ca034103 | 138 | printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() ); |
| hyunsungkim | 10:02d3ca034103 | 139 | printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() ); |
| hyunsungkim | 10:02d3ca034103 | 140 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() ); |
| hyunsungkim | 10:02d3ca034103 | 141 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 142 | printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 143 | } |
| hyunsungkim | 10:02d3ca034103 | 144 | |
| hyunsungkim | 10:02d3ca034103 | 145 | void initNRF(int role) |
| hyunsungkim | 10:02d3ca034103 | 146 | { |
| hyunsungkim | 12:cf6cbf1d1ebf | 147 | if(role == PING || role == PC) { |
| hyunsungkim | 8:78294b936c70 | 148 | nrf.setTxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 149 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 150 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 151 | nrf.setTransmitMode(); |
| hyunsungkim | 8:78294b936c70 | 152 | nrf.enable(); |
| hyunsungkim | 8:78294b936c70 | 153 | } else { |
| hyunsungkim | 8:78294b936c70 | 154 | nrf.setRxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 155 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 156 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 157 | nrf.setReceiveMode(); |
| hyunsungkim | 8:78294b936c70 | 158 | nrf.enable(); |
| hyunsungkim | 0:2dade6db4d3a | 159 | } |
| hyunsungkim | 8:78294b936c70 | 160 | } |
| hyunsungkim | 8:78294b936c70 | 161 | |
| hyunsungkim | 10:02d3ca034103 | 162 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData) |
| hyunsungkim | 10:02d3ca034103 | 163 | { |
| hyunsungkim | 14:a4402559cd6e | 164 | *(txData+0) = '$'; |
| hyunsungkim | 14:a4402559cd6e | 165 | *(txData+1) = id/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 166 | *(txData+2) = id%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 167 | *(txData+3) = count/10000+'0'; |
| hyunsungkim | 14:a4402559cd6e | 168 | *(txData+4) = count/1000%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 169 | *(txData+5) = count/100%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 170 | *(txData+6) = count/10%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 171 | *(txData+7) = count%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 172 | *(txData+8) = lspeed>0?'+':'-'; |
| hyunsungkim | 14:a4402559cd6e | 173 | *(txData+9) = abs(lspeed)/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 174 | *(txData+10) = abs(lspeed)%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 175 | *(txData+11) = rspeed>0?'+':'-'; |
| hyunsungkim | 14:a4402559cd6e | 176 | *(txData+12) = abs(rspeed)/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 177 | *(txData+13) = abs(rspeed)%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 178 | *(txData+14) = '\0'; |
| hyunsungkim | 13:f0f9a5586e45 | 179 | } |
| hyunsungkim | 13:f0f9a5586e45 | 180 | |
| hyunsungkim | 13:f0f9a5586e45 | 181 | /* |
| hyunsungkim | 13:f0f9a5586e45 | 182 | xxyyyyyabbcddn |
| hyunsungkim | 13:f0f9a5586e45 | 183 | |
| hyunsungkim | 14:a4402559cd6e | 184 | 14 chars |
| hyunsungkim | 14:a4402559cd6e | 185 | |
| hyunsungkim | 13:f0f9a5586e45 | 186 | xx: robot_id |
| hyunsungkim | 13:f0f9a5586e45 | 187 | yyyyy: packet_id |
| hyunsungkim | 13:f0f9a5586e45 | 188 | a: sign of lspeed |
| hyunsungkim | 13:f0f9a5586e45 | 189 | bb: lspeed |
| hyunsungkim | 13:f0f9a5586e45 | 190 | c: sign of rspeed |
| hyunsungkim | 13:f0f9a5586e45 | 191 | dd: rspeed |
| hyunsungkim | 14:a4402559cd6e | 192 | n: NULL 0 |
| hyunsungkim | 13:f0f9a5586e45 | 193 | |
| hyunsungkim | 14:a4402559cd6e | 194 | 0100001+30+30 |
| hyunsungkim | 13:f0f9a5586e45 | 195 | */ |