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
- Committer:
- hyunsungkim
- Date:
- 2018-11-25
- Branch:
- RF24_library_test_tx
- Revision:
- 11:1b56bb8ccd04
- Parent:
- 10:02d3ca034103
- Child:
- 12:cf6cbf1d1ebf
File content as of revision 11:1b56bb8ccd04:
#include "mbed.h"
#include "nRF24L01P.h"
#include "beep.h"
#define PING 1
#define PONG 2
#define ROLE PONG
#define ID 0
#define nrf_CE D2
#define nrf_CSN A3
#define spi_SCK D13
#define spi_MOSI D11
#define spi_MISO D12
#define spi_IRQ D4
#define TRANSFER_SIZE 14
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 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;
beepStart();
pc.baud(115200);
initNRF(role);
dumpRFInfo();
while(1) {
if(role == PING) {
int id = 1;
int lspeed=-40;
int rspeed=50;
//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);
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;
turnWheel(rspd, lspd);
printf("%d, %d\r\n", rspd,lspd);
}
}
}
}
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() );
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();
nrf.setTransferSize( TRANSFER_SIZE );
nrf.setTransmitMode();
nrf.enable();
} else {
nrf.setRxAddress(0xDEADBEEF0F);
nrf.powerUp();
nrf.setTransferSize( TRANSFER_SIZE );
nrf.setReceiveMode();
nrf.enable();
}
}
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';
}