Energy harvesting mobile robot. Developed at Institute of Systems and Robotics — University of Coimbra.
Fork of ISR_Mini-explorer by
Radio.cpp@4:560d24c0e5f8, 2018-04-19 (annotated)
- Committer:
- fabiofaria
- Date:
- Thu Apr 19 15:35:29 2018 +0000
- Revision:
- 4:560d24c0e5f8
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabiofaria | 4:560d24c0e5f8 | 1 | #include "Radio.h" |
fabiofaria | 4:560d24c0e5f8 | 2 | #include "mbed.h" |
fabiofaria | 4:560d24c0e5f8 | 3 | #include "Rangefinder.h" |
fabiofaria | 4:560d24c0e5f8 | 4 | |
fabiofaria | 4:560d24c0e5f8 | 5 | RF24 radio(PTD2, PTD3, PTD1, PTC12 ,PTC13); |
fabiofaria | 4:560d24c0e5f8 | 6 | const char max_len = 30; |
fabiofaria | 4:560d24c0e5f8 | 7 | |
fabiofaria | 4:560d24c0e5f8 | 8 | MBED_PACKED(struct) BasicFrame { // MBED_PACKED is required to avoid unpredictable data sizes due to Padding. |
fabiofaria | 4:560d24c0e5f8 | 9 | char start_of_frame_1; |
fabiofaria | 4:560d24c0e5f8 | 10 | char start_of_frame_2; |
fabiofaria | 4:560d24c0e5f8 | 11 | char identifier; |
fabiofaria | 4:560d24c0e5f8 | 12 | } BasicFrame1; |
fabiofaria | 4:560d24c0e5f8 | 13 | |
fabiofaria | 4:560d24c0e5f8 | 14 | MBED_PACKED(struct) MatrixFrame { // MBED_PACKED is required to avoid unpredictable data sizes due to Padding. |
fabiofaria | 4:560d24c0e5f8 | 15 | char start_of_frame_1; |
fabiofaria | 4:560d24c0e5f8 | 16 | char start_of_frame_2; |
fabiofaria | 4:560d24c0e5f8 | 17 | char identifier; |
fabiofaria | 4:560d24c0e5f8 | 18 | char dim1; |
fabiofaria | 4:560d24c0e5f8 | 19 | char dim2; |
fabiofaria | 4:560d24c0e5f8 | 20 | } MatrixFrame1; |
fabiofaria | 4:560d24c0e5f8 | 21 | |
fabiofaria | 4:560d24c0e5f8 | 22 | /** |
fabiofaria | 4:560d24c0e5f8 | 23 | * Initializes nrf24l01 module, return value 1 if module is working correcty and 0 if it's not. |
fabiofaria | 4:560d24c0e5f8 | 24 | * |
fabiofaria | 4:560d24c0e5f8 | 25 | ** @param channel - Which RF channel to comunicate on, 0-125 |
fabiofaria | 4:560d24c0e5f8 | 26 | * |
fabiofaria | 4:560d24c0e5f8 | 27 | * \warning Channel on Robot and Board has to be the same. |
fabiofaria | 4:560d24c0e5f8 | 28 | */ |
fabiofaria | 4:560d24c0e5f8 | 29 | bool init_nRF(int channel) |
fabiofaria | 4:560d24c0e5f8 | 30 | { |
fabiofaria | 4:560d24c0e5f8 | 31 | bool result; |
fabiofaria | 4:560d24c0e5f8 | 32 | |
fabiofaria | 4:560d24c0e5f8 | 33 | result = radio.begin(); |
fabiofaria | 4:560d24c0e5f8 | 34 | //pc.printf( "Initialation nrf24l01=%d\r\n", result ); // 1-working,0-not working |
fabiofaria | 4:560d24c0e5f8 | 35 | radio.setDataRate(RF24_250KBPS); |
fabiofaria | 4:560d24c0e5f8 | 36 | radio.setCRCLength(RF24_CRC_16); |
fabiofaria | 4:560d24c0e5f8 | 37 | radio.enableDynamicPayloads(); |
fabiofaria | 4:560d24c0e5f8 | 38 | radio.setChannel(channel); |
fabiofaria | 4:560d24c0e5f8 | 39 | radio.setAutoAck(true); |
fabiofaria | 4:560d24c0e5f8 | 40 | |
fabiofaria | 4:560d24c0e5f8 | 41 | radio.openWritingPipe(0x314e6f6465); |
fabiofaria | 4:560d24c0e5f8 | 42 | radio.openReadingPipe(1,0x324e6f6465); |
fabiofaria | 4:560d24c0e5f8 | 43 | |
fabiofaria | 4:560d24c0e5f8 | 44 | radio.startListening(); |
fabiofaria | 4:560d24c0e5f8 | 45 | |
fabiofaria | 4:560d24c0e5f8 | 46 | return result; |
fabiofaria | 4:560d24c0e5f8 | 47 | } |
fabiofaria | 4:560d24c0e5f8 | 48 | |
fabiofaria | 4:560d24c0e5f8 | 49 | void radio_send_string(char *str) |
fabiofaria | 4:560d24c0e5f8 | 50 | { |
fabiofaria | 4:560d24c0e5f8 | 51 | char *ptr; |
fabiofaria | 4:560d24c0e5f8 | 52 | |
fabiofaria | 4:560d24c0e5f8 | 53 | ptr = str; |
fabiofaria | 4:560d24c0e5f8 | 54 | |
fabiofaria | 4:560d24c0e5f8 | 55 | while(strlen(ptr) > max_len) { |
fabiofaria | 4:560d24c0e5f8 | 56 | radio.stopListening(); |
fabiofaria | 4:560d24c0e5f8 | 57 | radio.write(ptr, max_len); |
fabiofaria | 4:560d24c0e5f8 | 58 | radio.startListening(); |
fabiofaria | 4:560d24c0e5f8 | 59 | ptr += max_len; |
fabiofaria | 4:560d24c0e5f8 | 60 | } |
fabiofaria | 4:560d24c0e5f8 | 61 | |
fabiofaria | 4:560d24c0e5f8 | 62 | radio.stopListening(); |
fabiofaria | 4:560d24c0e5f8 | 63 | radio.write(ptr, strlen(ptr)); |
fabiofaria | 4:560d24c0e5f8 | 64 | radio.startListening(); |
fabiofaria | 4:560d24c0e5f8 | 65 | } |
fabiofaria | 4:560d24c0e5f8 | 66 | |
fabiofaria | 4:560d24c0e5f8 | 67 | void send_float_matrix(float *matrix, unsigned num_lines, unsigned num_columns) |
fabiofaria | 4:560d24c0e5f8 | 68 | { |
fabiofaria | 4:560d24c0e5f8 | 69 | int num_bytes_to_send; |
fabiofaria | 4:560d24c0e5f8 | 70 | uint8_t *ptr; |
fabiofaria | 4:560d24c0e5f8 | 71 | |
fabiofaria | 4:560d24c0e5f8 | 72 | // Send frame structure. |
fabiofaria | 4:560d24c0e5f8 | 73 | MatrixFrame1.start_of_frame_1 = 0x06; |
fabiofaria | 4:560d24c0e5f8 | 74 | MatrixFrame1.start_of_frame_2 = 0x85; |
fabiofaria | 4:560d24c0e5f8 | 75 | MatrixFrame1.identifier = 'F'; |
fabiofaria | 4:560d24c0e5f8 | 76 | MatrixFrame1.dim1 = num_lines; |
fabiofaria | 4:560d24c0e5f8 | 77 | MatrixFrame1.dim2 = num_columns; |
fabiofaria | 4:560d24c0e5f8 | 78 | |
fabiofaria | 4:560d24c0e5f8 | 79 | radio.stopListening(); |
fabiofaria | 4:560d24c0e5f8 | 80 | radio.write(&MatrixFrame1, sizeof(MatrixFrame1)); |
fabiofaria | 4:560d24c0e5f8 | 81 | |
fabiofaria | 4:560d24c0e5f8 | 82 | // Send matrix. |
fabiofaria | 4:560d24c0e5f8 | 83 | ptr = (uint8_t*)matrix; |
fabiofaria | 4:560d24c0e5f8 | 84 | num_bytes_to_send = num_lines * num_columns * sizeof(float); |
fabiofaria | 4:560d24c0e5f8 | 85 | |
fabiofaria | 4:560d24c0e5f8 | 86 | while(num_bytes_to_send > max_len) { |
fabiofaria | 4:560d24c0e5f8 | 87 | radio.write(ptr, max_len); // It would be helpful a blocking function. write() should block, but it appears been modified. |
fabiofaria | 4:560d24c0e5f8 | 88 | ptr += max_len; |
fabiofaria | 4:560d24c0e5f8 | 89 | num_bytes_to_send -= max_len; |
fabiofaria | 4:560d24c0e5f8 | 90 | } |
fabiofaria | 4:560d24c0e5f8 | 91 | |
fabiofaria | 4:560d24c0e5f8 | 92 | radio.write(ptr, num_bytes_to_send); |
fabiofaria | 4:560d24c0e5f8 | 93 | radio.startListening(); |
fabiofaria | 4:560d24c0e5f8 | 94 | } |
fabiofaria | 4:560d24c0e5f8 | 95 | |
fabiofaria | 4:560d24c0e5f8 | 96 | void send_rangefinder() |
fabiofaria | 4:560d24c0e5f8 | 97 | { |
fabiofaria | 4:560d24c0e5f8 | 98 | BasicFrame1.start_of_frame_1 = 0x06; |
fabiofaria | 4:560d24c0e5f8 | 99 | BasicFrame1.start_of_frame_2 = 0x85; |
fabiofaria | 4:560d24c0e5f8 | 100 | BasicFrame1.identifier = 'R'; |
fabiofaria | 4:560d24c0e5f8 | 101 | |
fabiofaria | 4:560d24c0e5f8 | 102 | radio.stopListening(); |
fabiofaria | 4:560d24c0e5f8 | 103 | radio.write(&BasicFrame1, sizeof(BasicFrame1)); |
fabiofaria | 4:560d24c0e5f8 | 104 | radio.write(rgfMeasurements, sizeof(rgfMeasurements)); |
fabiofaria | 4:560d24c0e5f8 | 105 | radio.startListening(); |
fabiofaria | 4:560d24c0e5f8 | 106 | } |
fabiofaria | 4:560d24c0e5f8 | 107 | |
fabiofaria | 4:560d24c0e5f8 | 108 | void test_send_matrix() |
fabiofaria | 4:560d24c0e5f8 | 109 | { |
fabiofaria | 4:560d24c0e5f8 | 110 | const int lines_num = 15, col_num = 15; |
fabiofaria | 4:560d24c0e5f8 | 111 | float testmatrix[lines_num][col_num]; |
fabiofaria | 4:560d24c0e5f8 | 112 | int i, j; |
fabiofaria | 4:560d24c0e5f8 | 113 | float previous = 0.1; |
fabiofaria | 4:560d24c0e5f8 | 114 | |
fabiofaria | 4:560d24c0e5f8 | 115 | for(i=0; i<lines_num; i++) { |
fabiofaria | 4:560d24c0e5f8 | 116 | for(j=0; j<col_num; j++) { |
fabiofaria | 4:560d24c0e5f8 | 117 | testmatrix[i][j] = previous; |
fabiofaria | 4:560d24c0e5f8 | 118 | previous += 0.1; |
fabiofaria | 4:560d24c0e5f8 | 119 | } |
fabiofaria | 4:560d24c0e5f8 | 120 | } |
fabiofaria | 4:560d24c0e5f8 | 121 | |
fabiofaria | 4:560d24c0e5f8 | 122 | send_float_matrix(&testmatrix[0][0], lines_num, col_num); |
fabiofaria | 4:560d24c0e5f8 | 123 | } |