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:
- 15:217b3e5a2275
- Parent:
- 14:a4402559cd6e
- Child:
- 16:7a78838b3b8a
diff -r a4402559cd6e -r 217b3e5a2275 main.cpp
--- a/main.cpp Sat Dec 01 15:26:36 2018 +0000
+++ b/main.cpp Sat Dec 01 15:37:05 2018 +0000
@@ -2,13 +2,6 @@
#include "nRF24L01P.h"
#include "beep.h"
-#define PING 1
-#define PONG 2
-#define PC 3
-
-#define ROLE PC
-#define ID 1
-
#define nrf_CE D2
#define nrf_CSN A3
#define spi_SCK D13
@@ -21,118 +14,44 @@
nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq
Serial pc(USBTX, USBRX);
-Serial lidar(D1, D0);
-
-PwmOut motor_RA(D9);
-PwmOut motor_RB(D10);
-PwmOut motor_LA(D3);
-PwmOut motor_LB(D6);
-PwmOut led_B(A5);
-PwmOut led_G(A2);
-PwmOut led_R(A1);
PwmOut buzzer(D5);
-AnalogIn batteryCheck(A0);
void beepStart();
void endBeep();
-void initNRF(int role);
+void initNRF();
void dumpRFInfo();
-void turnWheel(int rspd, int lspd);
void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData);
int main() {
- int role = ROLE;
- int id=0;
- char txData[TRANSFER_SIZE];
- int txDataCnt = 0;
- char rxData[TRANSFER_SIZE];
- int rxDataCnt = 0;
-
-
int inByteCnt = 0;
char buf[TRANSFER_SIZE+3];
beepStart();
pc.baud(115200);
- initNRF(role);
+ printf("I'm Master\r\n");
+ initNRF();
dumpRFInfo();
while(1) {
- switch(role) {
- case PING:
- int id = 1;
- int lspeed=-40;
- int rspeed=50;
- txDataCnt++;
- getPayload(id, txDataCnt, lspeed, rspeed, txData);
- pc.printf("PING:%s\r\n", txData);
- nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
- wait(0.2);
- break;
- case PONG:
- if ( nrf.readable() ) {
- rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
- 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");
- }
+ while(pc.readable()) {
+ char inByte = pc.getc();
+ if(inByteCnt > TRANSFER_SIZE+2) {
+ printf("Data size overflow!\r\n");
+ inByteCnt = 0;
+ break;
+ } else if(inByte == '\0') {
+ buf[inByteCnt] = '\0';
+ printf("Command: %s\r\n", buf);
+ nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE);
+ inByteCnt=0;
+ } else {
+ buf[inByteCnt++] = inByte;
}
- break;
- case PC:
- while(pc.readable()) {
- char inByte = pc.getc();
- if(inByteCnt > TRANSFER_SIZE+2) {
- printf("Data size overflow!\r\n");
- inByteCnt = 0;
- break;
- } else if(inByte == '\0') {
- buf[inByteCnt] = '\0';
- printf("Command: %s\r\n", buf);
- nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE);
- inByteCnt=0;
- } else {
- buf[inByteCnt++] = inByte;
- }
- }
- break;
}
}
}
-void turnWheel(int rspd, int lspd)
-{
- if(rspd>0) {
- motor_RA.write((float)rspd/100);
- motor_RB = 0;
- } else {
- rspd=-rspd;
- motor_RB.write((float)rspd/100);
- motor_RA = 0;
- }
- if(lspd>0) {
- motor_LA.write((float)lspd/100);
- motor_LB = 0;
- } else {
- lspd = -lspd;
- motor_LB.write((float)lspd/100);
- motor_LA = 0;
- }
-}
-
void dumpRFInfo()
{
printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() );
@@ -142,21 +61,14 @@
printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() );
}
-void initNRF(int role)
+void initNRF()
{
- if(role == PING || role == PC) {
- nrf.setTxAddress(0xDEADBEEF0F);
- nrf.powerUp();
- nrf.setTransferSize( TRANSFER_SIZE );
- nrf.setTransmitMode();
- nrf.enable();
- } else {
- nrf.setRxAddress(0xDEADBEEF0F);
- nrf.powerUp();
- nrf.setTransferSize( TRANSFER_SIZE );
- nrf.setReceiveMode();
- nrf.enable();
- }
+ nrf.setTxAddress(0xDEADBEEF0F);
+ nrf.powerUp();
+ nrf.setTransferSize( TRANSFER_SIZE );
+ nrf.setTransmitMode();
+ nrf.enable();
+
}
void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData)