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:
- 10:02d3ca034103
- Parent:
- 9:d85804e8d3b3
- Child:
- 11:1b56bb8ccd04
diff -r d85804e8d3b3 -r 02d3ca034103 main.cpp
--- a/main.cpp Sun Nov 25 10:44:40 2018 +0000
+++ b/main.cpp Sun Nov 25 11:31:55 2018 +0000
@@ -5,6 +5,9 @@
#define PING 1
#define PONG 2
+#define ROLE PONG
+#define ID 0
+
#define nrf_CE D2
#define nrf_CSN A3
#define spi_SCK D13
@@ -16,9 +19,6 @@
nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq
-//volatile int role = PING;
-volatile int role = PONG;
-
Serial pc(USBTX, USBRX);
Serial lidar(D1, D0);
@@ -34,27 +34,13 @@
void beepStart();
void endBeep();
+void initNRF(int role);
+void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData);
-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/100+'0';
- *(txData+8) = lspeed/10%10+'0';
- *(txData+9) = lspeed%10+'0';
- *(txData+10) = rspeed/100+'0';
- *(txData+11) = rspeed/10%10+'0';
- *(txData+12) = rspeed%10+'0';
- *(txData+13) = '\0';
-}
int main() {
-
+ int role = ROLE;
+ int id=0;
char txData[TRANSFER_SIZE];
int txDataCnt = 0;
char rxData[TRANSFER_SIZE];
@@ -64,6 +50,59 @@
pc.baud(115200);
// Print setting of radio module
+ initNRF(role);
+
+ while(1) {
+ if(role == PING) {
+ int id = 1;
+ int lspeed=-90;
+ int rspeed=23;
+ //printf("transmitting\r\n");
+ txDataCnt++;
+ getPayload(id, txDataCnt, lspeed, rspeed, txData);
+ //char txData_[] = "123456789ABCD";
+ pc.printf("PING:%s\r\n", txData);
+ nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
+ wait(0.2);
+ }
+ else if(role == PONG) {
+ if ( nrf.readable() ) {
+ rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
+ printf("%s\r\n", rxData);
+ }
+ }
+ }
+}
+
+void turnWheel(int rspd, int lspd)
+{
+ if(rspd>0) {
+ motor_RA.write((float)rspd/100);
+ motor_RB = 0;
+ } else {
+ motor_RB.write((float)rspd/100);
+ motor_RA = 0;
+ }
+ if(lspd>0) {
+ motor_LA.write((float)lspd/100);
+ motor_LB = 1;
+ } else {
+ motor_LB.write((float)lspd/100);
+ motor_LA = 1;
+ }
+}
+
+void dumpRFInfo()
+{
+ printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() );
+ printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() );
+ printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() );
+ printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() );
+ printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() );
+}
+
+void initNRF(int role)
+{
if(role == PING) {
nrf.setTxAddress(0xDEADBEEF0F);
nrf.powerUp();
@@ -77,32 +116,22 @@
nrf.setReceiveMode();
nrf.enable();
}
-
- pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() );
- pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() );
- pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() );
- pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() );
- pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() );
-
- while(1) {
- if(role == PING) {
- int id = 1;
- int lspeed=100;
- int rspeed=123;
- //printf("transmitting\r\n");
- txDataCnt++;
- getPayload(id, txDataCnt, lspeed, rspeed, txData);
- pc.printf("PING:%s\r\n", txData);
- //char txData_[] = "123456789ABCD";
- nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
- wait(0.2);
- }
- else if(role == PONG) {
- if ( nrf.readable() ) {
- rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
- printf("%s\r\n", rxData);
- }
- }
- }
}
+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';
+}
\ No newline at end of file