![](/media/cache/group/V2.png.50x50_q85.png)
FN
Dependencies: mbed RF24Network RF24
main.cpp@6:d46e3c0c8d35, 2019-03-25 (annotated)
- Committer:
- guillaume6544
- Date:
- Mon Mar 25 16:34:59 2019 +0000
- Revision:
- 6:d46e3c0c8d35
- Parent:
- 5:bfef4ea383be
- Child:
- 7:1b47a2b6329a
prend 2 distances;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 0:3982c0e9eda1 | 1 | #include "mbed.h" |
akashvibhute | 0:3982c0e9eda1 | 2 | #include <RF24Network.h> |
akashvibhute | 2:926b93a68399 | 3 | #include <RF24.h> |
guillaume6544 | 6:d46e3c0c8d35 | 4 | |
akashvibhute | 0:3982c0e9eda1 | 5 | Serial pc(USBTX, USBRX); |
guillaume6544 | 6:d46e3c0c8d35 | 6 | InterruptIn button(D8); |
guillaume6544 | 6:d46e3c0c8d35 | 7 | AnalogIn LM35(A2); |
guillaume6544 | 6:d46e3c0c8d35 | 8 | |
akashvibhute | 0:3982c0e9eda1 | 9 | |
wesleytiem | 4:f70f3b565af0 | 10 | RF24 radio(SPI_MOSI, SPI_MISO, SPI_SCK, D9, SPI_CS ); |
guillaume6544 | 5:bfef4ea383be | 11 | Timer timer; |
guillaume6544 | 5:bfef4ea383be | 12 | unsigned int temps; |
guillaume6544 | 5:bfef4ea383be | 13 | int a=0; |
guillaume6544 | 6:d46e3c0c8d35 | 14 | float temperature; |
guillaume6544 | 6:d46e3c0c8d35 | 15 | float vitesse; |
guillaume6544 | 6:d46e3c0c8d35 | 16 | |
guillaume6544 | 6:d46e3c0c8d35 | 17 | const int n = 2; |
guillaume6544 | 6:d46e3c0c8d35 | 18 | int addr[n] = {001,001}; |
guillaume6544 | 6:d46e3c0c8d35 | 19 | float d[n]; |
guillaume6544 | 6:d46e3c0c8d35 | 20 | int indice=0; |
guillaume6544 | 5:bfef4ea383be | 21 | |
guillaume6544 | 5:bfef4ea383be | 22 | void pressed() |
guillaume6544 | 5:bfef4ea383be | 23 | { |
guillaume6544 | 5:bfef4ea383be | 24 | button.disable_irq(); |
guillaume6544 | 5:bfef4ea383be | 25 | timer.stop(); |
guillaume6544 | 5:bfef4ea383be | 26 | temps = timer.read_us(); |
guillaume6544 | 6:d46e3c0c8d35 | 27 | d[indice-1]=(temps-70.0)*(vitesse/1000000.0); |
guillaume6544 | 6:d46e3c0c8d35 | 28 | //printf("distance=%f\r\n",(temps-70.0)*(vitesse/1000000.0); |
guillaume6544 | 6:d46e3c0c8d35 | 29 | //wait_ms(3); |
guillaume6544 | 5:bfef4ea383be | 30 | timer.reset(); |
guillaume6544 | 6:d46e3c0c8d35 | 31 | //a=1; |
guillaume6544 | 5:bfef4ea383be | 32 | } |
guillaume6544 | 6:d46e3c0c8d35 | 33 | |
akashvibhute | 0:3982c0e9eda1 | 34 | // Network uses that radio |
akashvibhute | 0:3982c0e9eda1 | 35 | RF24Network network(radio); |
guillaume6544 | 6:d46e3c0c8d35 | 36 | |
akashvibhute | 0:3982c0e9eda1 | 37 | // Address of our node |
akashvibhute | 3:d605536db315 | 38 | const uint16_t this_node = 01; |
guillaume6544 | 6:d46e3c0c8d35 | 39 | |
akashvibhute | 0:3982c0e9eda1 | 40 | // Address of the other node |
guillaume6544 | 6:d46e3c0c8d35 | 41 | const uint16_t other_node1 = 00; |
guillaume6544 | 6:d46e3c0c8d35 | 42 | const uint16_t other_node2 = 001; |
guillaume6544 | 6:d46e3c0c8d35 | 43 | |
akashvibhute | 2:926b93a68399 | 44 | // How often to send payload packet to the other unit |
guillaume6544 | 6:d46e3c0c8d35 | 45 | const unsigned long interval = 500; //ms |
guillaume6544 | 6:d46e3c0c8d35 | 46 | |
akashvibhute | 0:3982c0e9eda1 | 47 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 48 | unsigned long last_sent; |
akashvibhute | 1:5be48a9550c3 | 49 | Timer t; |
guillaume6544 | 6:d46e3c0c8d35 | 50 | |
akashvibhute | 0:3982c0e9eda1 | 51 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 52 | unsigned long packets_sent; |
akashvibhute | 1:5be48a9550c3 | 53 | Timer t_packet; |
guillaume6544 | 6:d46e3c0c8d35 | 54 | |
akashvibhute | 0:3982c0e9eda1 | 55 | // Structure of our payload |
akashvibhute | 2:926b93a68399 | 56 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 57 | { |
akashvibhute | 2:926b93a68399 | 58 | unsigned long ms; |
akashvibhute | 2:926b93a68399 | 59 | unsigned long counter; |
akashvibhute | 0:3982c0e9eda1 | 60 | }; |
guillaume6544 | 6:d46e3c0c8d35 | 61 | |
guillaume6544 | 6:d46e3c0c8d35 | 62 | |
guillaume6544 | 6:d46e3c0c8d35 | 63 | void emmeteur(){ |
akashvibhute | 0:3982c0e9eda1 | 64 | |
guillaume6544 | 6:d46e3c0c8d35 | 65 | pc.printf("Sending : %d...",indice+1); |
guillaume6544 | 6:d46e3c0c8d35 | 66 | payload_t payload_tx; |
guillaume6544 | 6:d46e3c0c8d35 | 67 | payload_tx.ms = t_packet.read_ms(); |
guillaume6544 | 6:d46e3c0c8d35 | 68 | payload_tx.counter = packets_sent++; |
guillaume6544 | 6:d46e3c0c8d35 | 69 | |
guillaume6544 | 6:d46e3c0c8d35 | 70 | RF24NetworkHeader header_tx(/*to node*/ addr[indice]); |
guillaume6544 | 6:d46e3c0c8d35 | 71 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
guillaume6544 | 6:d46e3c0c8d35 | 72 | if (ok){ |
guillaume6544 | 6:d46e3c0c8d35 | 73 | timer.start(); |
guillaume6544 | 6:d46e3c0c8d35 | 74 | pc.printf("ok : %d\r\n",indice+1); |
guillaume6544 | 6:d46e3c0c8d35 | 75 | // a=1; |
guillaume6544 | 6:d46e3c0c8d35 | 76 | } |
guillaume6544 | 6:d46e3c0c8d35 | 77 | else |
guillaume6544 | 6:d46e3c0c8d35 | 78 | pc.printf("failed : %d.\r\n",indice+1); |
guillaume6544 | 6:d46e3c0c8d35 | 79 | |
guillaume6544 | 6:d46e3c0c8d35 | 80 | |
guillaume6544 | 6:d46e3c0c8d35 | 81 | } |
guillaume6544 | 6:d46e3c0c8d35 | 82 | void emmeteur2(){ |
akashvibhute | 0:3982c0e9eda1 | 83 | |
guillaume6544 | 6:d46e3c0c8d35 | 84 | pc.printf("Sending : 2..."); |
guillaume6544 | 6:d46e3c0c8d35 | 85 | payload_t payload_tx; |
guillaume6544 | 6:d46e3c0c8d35 | 86 | payload_tx.ms = t_packet.read_ms(); |
guillaume6544 | 6:d46e3c0c8d35 | 87 | payload_tx.counter = packets_sent++; |
guillaume6544 | 6:d46e3c0c8d35 | 88 | |
guillaume6544 | 6:d46e3c0c8d35 | 89 | RF24NetworkHeader header_tx(/*to node*/ other_node2); |
guillaume6544 | 6:d46e3c0c8d35 | 90 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
guillaume6544 | 6:d46e3c0c8d35 | 91 | if (ok){ |
guillaume6544 | 6:d46e3c0c8d35 | 92 | timer.start(); |
guillaume6544 | 6:d46e3c0c8d35 | 93 | pc.printf("ok : 2.\r\n"); |
guillaume6544 | 6:d46e3c0c8d35 | 94 | // a=1; |
guillaume6544 | 6:d46e3c0c8d35 | 95 | } |
guillaume6544 | 6:d46e3c0c8d35 | 96 | else |
guillaume6544 | 6:d46e3c0c8d35 | 97 | pc.printf("failed : 2.\r\n"); |
guillaume6544 | 6:d46e3c0c8d35 | 98 | |
guillaume6544 | 6:d46e3c0c8d35 | 99 | |
guillaume6544 | 6:d46e3c0c8d35 | 100 | } |
guillaume6544 | 6:d46e3c0c8d35 | 101 | |
guillaume6544 | 6:d46e3c0c8d35 | 102 | |
guillaume6544 | 6:d46e3c0c8d35 | 103 | |
guillaume6544 | 6:d46e3c0c8d35 | 104 | |
akashvibhute | 2:926b93a68399 | 105 | int main() |
akashvibhute | 0:3982c0e9eda1 | 106 | { |
guillaume6544 | 5:bfef4ea383be | 107 | // double cpt=0; |
guillaume6544 | 6:d46e3c0c8d35 | 108 | float d1,d2; |
wesleytiem | 4:f70f3b565af0 | 109 | pc.baud(115200); |
akashvibhute | 0:3982c0e9eda1 | 110 | wait_ms(1000); |
guillaume6544 | 6:d46e3c0c8d35 | 111 | |
guillaume6544 | 6:d46e3c0c8d35 | 112 | pc.printf("mBed RF24Network node: Tx\r\n"); |
akashvibhute | 0:3982c0e9eda1 | 113 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 114 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 115 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 116 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 117 | t_packet.start(); |
guillaume6544 | 6:d46e3c0c8d35 | 118 | temperature = 0; |
guillaume6544 | 6:d46e3c0c8d35 | 119 | for (int i=0;i<10;i++) { |
guillaume6544 | 6:d46e3c0c8d35 | 120 | temperature += LM35.read(); |
guillaume6544 | 6:d46e3c0c8d35 | 121 | wait_ms(10); |
guillaume6544 | 6:d46e3c0c8d35 | 122 | } |
guillaume6544 | 5:bfef4ea383be | 123 | |
guillaume6544 | 5:bfef4ea383be | 124 | |
guillaume6544 | 6:d46e3c0c8d35 | 125 | |
guillaume6544 | 6:d46e3c0c8d35 | 126 | |
guillaume6544 | 6:d46e3c0c8d35 | 127 | temperature = temperature*34; |
guillaume6544 | 6:d46e3c0c8d35 | 128 | |
guillaume6544 | 6:d46e3c0c8d35 | 129 | vitesse = 331.5 + 0.595*temperature; |
guillaume6544 | 6:d46e3c0c8d35 | 130 | |
guillaume6544 | 5:bfef4ea383be | 131 | button.rise(&pressed); |
akashvibhute | 2:926b93a68399 | 132 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 133 | { |
guillaume6544 | 5:bfef4ea383be | 134 | |
akashvibhute | 0:3982c0e9eda1 | 135 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 136 | network.update(); |
guillaume6544 | 5:bfef4ea383be | 137 | //if(a==0){ |
akashvibhute | 1:5be48a9550c3 | 138 | /* If it's time to send a message, send it! */ |
akashvibhute | 1:5be48a9550c3 | 139 | unsigned long now = t.read_ms(); |
akashvibhute | 2:926b93a68399 | 140 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 141 | { |
guillaume6544 | 6:d46e3c0c8d35 | 142 | if(indice > 1) { |
guillaume6544 | 6:d46e3c0c8d35 | 143 | for(int j=0; j<n; j++) |
guillaume6544 | 6:d46e3c0c8d35 | 144 | printf("distance %d = %f\r\n",j+1,d[j]); |
guillaume6544 | 6:d46e3c0c8d35 | 145 | indice = 0; |
guillaume6544 | 6:d46e3c0c8d35 | 146 | } |
akashvibhute | 1:5be48a9550c3 | 147 | t.reset(); |
guillaume6544 | 5:bfef4ea383be | 148 | |
guillaume6544 | 5:bfef4ea383be | 149 | button.enable_irq(); |
guillaume6544 | 6:d46e3c0c8d35 | 150 | emmeteur(); |
guillaume6544 | 6:d46e3c0c8d35 | 151 | button.enable_irq(); |
akashvibhute | 2:926b93a68399 | 152 | |
guillaume6544 | 6:d46e3c0c8d35 | 153 | indice++; |
akashvibhute | 2:926b93a68399 | 154 | } |
guillaume6544 | 5:bfef4ea383be | 155 | |
guillaume6544 | 5:bfef4ea383be | 156 | |
guillaume6544 | 5:bfef4ea383be | 157 | //} |
guillaume6544 | 5:bfef4ea383be | 158 | |
guillaume6544 | 5:bfef4ea383be | 159 | |
guillaume6544 | 5:bfef4ea383be | 160 | |
guillaume6544 | 5:bfef4ea383be | 161 | |
guillaume6544 | 5:bfef4ea383be | 162 | |
guillaume6544 | 6:d46e3c0c8d35 | 163 | |
guillaume6544 | 6:d46e3c0c8d35 | 164 | |
akashvibhute | 0:3982c0e9eda1 | 165 | } |
guillaume6544 | 6:d46e3c0c8d35 | 166 | |
akashvibhute | 0:3982c0e9eda1 | 167 | } |