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.
Diff: main.cpp
- Branch:
- RF24_library_test_tx
- Revision:
- 14:a4402559cd6e
- Parent:
- 13:f0f9a5586e45
- Child:
- 15:217b3e5a2275
--- a/main.cpp Sat Dec 01 12:56:14 2018 +0000
+++ b/main.cpp Sat Dec 01 15:26:36 2018 +0000
@@ -6,7 +6,7 @@
#define PONG 2
#define PC 3
-#define ROLE PONG
+#define ROLE PC
#define ID 1
#define nrf_CE D2
@@ -16,7 +16,7 @@
#define spi_MISO D12
#define spi_IRQ D4
-#define TRANSFER_SIZE 14
+#define TRANSFER_SIZE 15
nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq
@@ -73,19 +73,22 @@
case PONG:
if ( nrf.readable() ) {
rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
- // printf("%s\r\n", rxData);
- int id = (rxData[0]-'0')*10+(rxData[1]-'0');
- int dataCount = (rxData[2]-'0')*10000+(rxData[3]-'0')*1000+(rxData[4]-'0')*100+(rxData[5]-'0')*10+(rxData[6]-'0');
- int rspd = (rxData[8]-'0')*10+(rxData[9]-'0');
- int lspd = (rxData[11]-'0')*10+(rxData[12]-'0');
- if(rxData[7]=='-')
- rspd = -rspd;
- if(rxData[10]=='-')
- lspd = -lspd;
-
- if(true) {
- turnWheel(rspd, lspd);
- printf("[%d] RxData: %s, rspd: %d, rspd: %d\r\n", id, rxData, rspd, lspd);
+ if(rxData[0] =='$') {
+ int id = (rxData[1]-'0')*10+(rxData[2]-'0');
+ int dataCount = (rxData[3]-'0')*10000+(rxData[4]-'0')*1000+(rxData[5]-'0')*100+(rxData[6]-'0')*10+(rxData[7]-'0');
+ int rspd = (rxData[9]-'0')*10+(rxData[10]-'0');
+ int lspd = (rxData[12]-'0')*10+(rxData[13]-'0');
+ if(rxData[8]=='-')
+ rspd = -rspd;
+ if(rxData[11]=='-')
+ lspd = -lspd;
+
+ if(true) {
+ turnWheel(rspd, lspd);
+ printf("[%d] RxData: %s, rspd: %d, rspd: %d\r\n", id, rxData, rspd, lspd);
+ }
+ } else {
+ printf("Invalid Data\r\n");
}
}
break;
@@ -96,7 +99,7 @@
printf("Data size overflow!\r\n");
inByteCnt = 0;
break;
- } else if(inByte == '\n') {
+ } else if(inByte == '\0') {
buf[inByteCnt] = '\0';
printf("Command: %s\r\n", buf);
nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE);
@@ -158,32 +161,35 @@
void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData)
{
- *(txData+0) = id/10+'0';
- *(txData+1) = id%10+'0';
- *(txData+2) = count/10000+'0';
- *(txData+3) = count/1000%10+'0';
- *(txData+4) = count/100%10+'0';
- *(txData+5) = count/10%10+'0';
- *(txData+6) = count%10+'0';
- *(txData+7) = lspeed>0?'+':'-';
- *(txData+8) = abs(lspeed)/10+'0';
- *(txData+9) = abs(lspeed)%10+'0';
- *(txData+10) = rspeed>0?'+':'-';
- *(txData+11) = abs(rspeed)/10+'0';
- *(txData+12) = abs(rspeed)%10+'0';
- *(txData+13) = '\0';
+ *(txData+0) = '$';
+ *(txData+1) = id/10+'0';
+ *(txData+2) = id%10+'0';
+ *(txData+3) = count/10000+'0';
+ *(txData+4) = count/1000%10+'0';
+ *(txData+5) = count/100%10+'0';
+ *(txData+6) = count/10%10+'0';
+ *(txData+7) = count%10+'0';
+ *(txData+8) = lspeed>0?'+':'-';
+ *(txData+9) = abs(lspeed)/10+'0';
+ *(txData+10) = abs(lspeed)%10+'0';
+ *(txData+11) = rspeed>0?'+':'-';
+ *(txData+12) = abs(rspeed)/10+'0';
+ *(txData+13) = abs(rspeed)%10+'0';
+ *(txData+14) = '\0';
}
/*
xxyyyyyabbcddn
+14 chars
+
xx: robot_id
yyyyy: packet_id
a: sign of lspeed
bb: lspeed
c: sign of rspeed
dd: rspeed
-n: null '\0'
+n: NULL 0
-0100001+30-30
+0100001+30+30
*/
\ No newline at end of file