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.
Dependents: ISR_Mini-explorer_Rangefinder
Fork of ISR_Mini-explorer by
Radio.cpp
00001 #include "Radio.h" 00002 #include "mbed.h" 00003 #include "Rangefinder.h" 00004 00005 RF24 radio(PTD2, PTD3, PTD1, PTC12 ,PTC13); 00006 const char max_len = 30; 00007 00008 MBED_PACKED(struct) BasicFrame { // MBED_PACKED is required to avoid unpredictable data sizes due to Padding. 00009 char start_of_frame_1; 00010 char start_of_frame_2; 00011 char identifier; 00012 } BasicFrame1; 00013 00014 MBED_PACKED(struct) MatrixFrame { // MBED_PACKED is required to avoid unpredictable data sizes due to Padding. 00015 char start_of_frame_1; 00016 char start_of_frame_2; 00017 char identifier; 00018 char dim1; 00019 char dim2; 00020 } MatrixFrame1; 00021 00022 /** 00023 * Initializes nrf24l01 module, return value 1 if module is working correcty and 0 if it's not. 00024 * 00025 ** @param channel - Which RF channel to comunicate on, 0-125 00026 * 00027 * \warning Channel on Robot and Board has to be the same. 00028 */ 00029 bool init_nRF(int channel) 00030 { 00031 bool result; 00032 00033 result = radio.begin(); 00034 //pc.printf( "Initialation nrf24l01=%d\r\n", result ); // 1-working,0-not working 00035 radio.setDataRate(RF24_250KBPS); 00036 radio.setCRCLength(RF24_CRC_16); 00037 radio.enableDynamicPayloads(); 00038 radio.setChannel(channel); 00039 radio.setAutoAck(true); 00040 00041 radio.openWritingPipe(0x314e6f6465); 00042 radio.openReadingPipe(1,0x324e6f6465); 00043 00044 radio.startListening(); 00045 00046 return result; 00047 } 00048 00049 void radio_send_string(char *str) 00050 { 00051 char *ptr; 00052 00053 ptr = str; 00054 00055 while(strlen(ptr) > max_len) { 00056 radio.stopListening(); 00057 radio.write(ptr, max_len); 00058 radio.startListening(); 00059 ptr += max_len; 00060 } 00061 00062 radio.stopListening(); 00063 radio.write(ptr, strlen(ptr)); 00064 radio.startListening(); 00065 } 00066 00067 void send_float_matrix(float *matrix, unsigned num_lines, unsigned num_columns) 00068 { 00069 int num_bytes_to_send; 00070 uint8_t *ptr; 00071 00072 // Send frame structure. 00073 MatrixFrame1.start_of_frame_1 = 0x06; 00074 MatrixFrame1.start_of_frame_2 = 0x85; 00075 MatrixFrame1.identifier = 'F'; 00076 MatrixFrame1.dim1 = num_lines; 00077 MatrixFrame1.dim2 = num_columns; 00078 00079 radio.stopListening(); 00080 radio.write(&MatrixFrame1, sizeof(MatrixFrame1)); 00081 00082 // Send matrix. 00083 ptr = (uint8_t*)matrix; 00084 num_bytes_to_send = num_lines * num_columns * sizeof(float); 00085 00086 while(num_bytes_to_send > max_len) { 00087 radio.write(ptr, max_len); // It would be helpful a blocking function. write() should block, but it appears been modified. 00088 ptr += max_len; 00089 num_bytes_to_send -= max_len; 00090 } 00091 00092 radio.write(ptr, num_bytes_to_send); 00093 radio.startListening(); 00094 } 00095 00096 void send_rangefinder() 00097 { 00098 BasicFrame1.start_of_frame_1 = 0x06; 00099 BasicFrame1.start_of_frame_2 = 0x85; 00100 BasicFrame1.identifier = 'R'; 00101 00102 radio.stopListening(); 00103 radio.write(&BasicFrame1, sizeof(BasicFrame1)); 00104 radio.write(rgfMeasurements, sizeof(rgfMeasurements)); 00105 radio.startListening(); 00106 } 00107 00108 void test_send_matrix() 00109 { 00110 const int lines_num = 15, col_num = 15; 00111 float testmatrix[lines_num][col_num]; 00112 int i, j; 00113 float previous = 0.1; 00114 00115 for(i=0; i<lines_num; i++) { 00116 for(j=0; j<col_num; j++) { 00117 testmatrix[i][j] = previous; 00118 previous += 0.1; 00119 } 00120 } 00121 00122 send_float_matrix(&testmatrix[0][0], lines_num, col_num); 00123 } 00124 00125 void read_radio(char* buf, uint8_t array_size) 00126 { 00127 uint8_t payload_len; 00128 int available; 00129 char* buf2; 00130 00131 buf2 = buf; 00132 available = array_size; 00133 00134 while (radio.available()) { 00135 // Fetch the payload. 00136 payload_len = radio.getDynamicPayloadSize(); 00137 00138 // If a corrupt dynamic payload is received, flush it. 00139 if (!payload_len) 00140 continue; 00141 00142 if (available >= payload_len) { 00143 radio.read(buf2, payload_len); 00144 buf2 += payload_len; 00145 available -= payload_len; 00146 } else { 00147 break; 00148 } 00149 } 00150 }
Generated on Fri Jul 15 2022 01:53:00 by
1.7.2
