현성 김 / Mbed 2 deprecated 181202_Castone_design_master

Dependencies:   mbed nRF24L01P

Committer:
hyunsungkim
Date:
Sat Dec 01 12:56:14 2018 +0000
Branch:
RF24_library_test_tx
Revision:
13:f0f9a5586e45
Parent:
12:cf6cbf1d1ebf
Child:
14:a4402559cd6e
?? ?? ??? ?? ???? ?? ??

Who changed what in which revision?

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