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.
arq.cpp@0:8e06c9b8b7f6, 2021-05-25 (annotated)
- Committer:
- quagga
- Date:
- Tue May 25 17:45:26 2021 +0000
- Revision:
- 0:8e06c9b8b7f6
arq radio class; todo- crc check on message
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| quagga | 0:8e06c9b8b7f6 | 1 | #include"arq.h" |
| quagga | 0:8e06c9b8b7f6 | 2 | |
| quagga | 0:8e06c9b8b7f6 | 3 | Arq::Arq(MicroBit* bit) |
| quagga | 0:8e06c9b8b7f6 | 4 | { |
| quagga | 0:8e06c9b8b7f6 | 5 | uBit=bit; |
| quagga | 0:8e06c9b8b7f6 | 6 | //uBit->serial.send("Arq::Arq(MicroBit *)\n"); |
| quagga | 0:8e06c9b8b7f6 | 7 | power=0; |
| quagga | 0:8e06c9b8b7f6 | 8 | timeout=5000; |
| quagga | 0:8e06c9b8b7f6 | 9 | maxtries=10; |
| quagga | 0:8e06c9b8b7f6 | 10 | mintries=1; |
| quagga | 0:8e06c9b8b7f6 | 11 | tries=0; |
| quagga | 0:8e06c9b8b7f6 | 12 | rssi=255; |
| quagga | 0:8e06c9b8b7f6 | 13 | min_rssi=70; |
| quagga | 0:8e06c9b8b7f6 | 14 | uBit->radio.setTransmitPower(power); |
| quagga | 0:8e06c9b8b7f6 | 15 | } |
| quagga | 0:8e06c9b8b7f6 | 16 | |
| quagga | 0:8e06c9b8b7f6 | 17 | void Arq::off() |
| quagga | 0:8e06c9b8b7f6 | 18 | { |
| quagga | 0:8e06c9b8b7f6 | 19 | //uBit->serial.send("void Arq::off()\n"); |
| quagga | 0:8e06c9b8b7f6 | 20 | uBit->messageBus.ignore(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, this, &Arq::listen); |
| quagga | 0:8e06c9b8b7f6 | 21 | uBit->radio.disable(); |
| quagga | 0:8e06c9b8b7f6 | 22 | } |
| quagga | 0:8e06c9b8b7f6 | 23 | |
| quagga | 0:8e06c9b8b7f6 | 24 | void Arq::listen(MicroBitEvent e) |
| quagga | 0:8e06c9b8b7f6 | 25 | { |
| quagga | 0:8e06c9b8b7f6 | 26 | //uBit->serial.send("Arq::receive(MicroBitEvent)\n"); |
| quagga | 0:8e06c9b8b7f6 | 27 | //message received |
| quagga | 0:8e06c9b8b7f6 | 28 | rssi=uBit->radio.getRSSI(); |
| quagga | 0:8e06c9b8b7f6 | 29 | PacketBuffer s=uBit->radio.datagram.recv(); |
| quagga | 0:8e06c9b8b7f6 | 30 | if (s[0]==0) //power_test |
| quagga | 0:8e06c9b8b7f6 | 31 | { |
| quagga | 0:8e06c9b8b7f6 | 32 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 33 | int pow=(int)s[1]; |
| quagga | 0:8e06c9b8b7f6 | 34 | uBit->serial.printf("received POWER_TEST at %d\n",pow); |
| quagga | 0:8e06c9b8b7f6 | 35 | #endif |
| quagga | 0:8e06c9b8b7f6 | 36 | received_power=s[1]; |
| quagga | 0:8e06c9b8b7f6 | 37 | MicroBitEvent(ARQ_ID,ARQ_POW_RECVD_EVT); |
| quagga | 0:8e06c9b8b7f6 | 38 | } |
| quagga | 0:8e06c9b8b7f6 | 39 | else //message |
| quagga | 0:8e06c9b8b7f6 | 40 | { |
| quagga | 0:8e06c9b8b7f6 | 41 | //set packet to received packet buffer minus 1st byte |
| quagga | 0:8e06c9b8b7f6 | 42 | packet = PacketBuffer(s.length()-1); |
| quagga | 0:8e06c9b8b7f6 | 43 | for (int i=1;i<s.length();i++){packet[i-1]=s[i];} |
| quagga | 0:8e06c9b8b7f6 | 44 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 45 | //uBit->serial.send(packet); //send packet to serial |
| quagga | 0:8e06c9b8b7f6 | 46 | #endif |
| quagga | 0:8e06c9b8b7f6 | 47 | MicroBitEvent(ARQ_ID,ARQ_MSG_RECVD_EVT); |
| quagga | 0:8e06c9b8b7f6 | 48 | } |
| quagga | 0:8e06c9b8b7f6 | 49 | s=PacketBuffer(1); //send ACK |
| quagga | 0:8e06c9b8b7f6 | 50 | s[0]=(char) rssi; |
| quagga | 0:8e06c9b8b7f6 | 51 | s[0] |= 1UL << 7; // set bit 8 for ACK |
| quagga | 0:8e06c9b8b7f6 | 52 | uBit->radio.datagram.send(s); |
| quagga | 0:8e06c9b8b7f6 | 53 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 54 | uBit->serial.printf("Sending ACK with %d rssi\n",rssi); |
| quagga | 0:8e06c9b8b7f6 | 55 | #endif |
| quagga | 0:8e06c9b8b7f6 | 56 | } |
| quagga | 0:8e06c9b8b7f6 | 57 | |
| quagga | 0:8e06c9b8b7f6 | 58 | void Arq::receive() |
| quagga | 0:8e06c9b8b7f6 | 59 | { |
| quagga | 0:8e06c9b8b7f6 | 60 | uBit->messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, this, &Arq::listen); |
| quagga | 0:8e06c9b8b7f6 | 61 | uBit->radio.setTransmitPower(7); //max power for ACK |
| quagga | 0:8e06c9b8b7f6 | 62 | uBit->radio.enable(); |
| quagga | 0:8e06c9b8b7f6 | 63 | } |
| quagga | 0:8e06c9b8b7f6 | 64 | |
| quagga | 0:8e06c9b8b7f6 | 65 | void Arq::send(PacketBuffer packet) |
| quagga | 0:8e06c9b8b7f6 | 66 | { |
| quagga | 0:8e06c9b8b7f6 | 67 | //uBit->serial.send("void Arq::send()\n"); |
| quagga | 0:8e06c9b8b7f6 | 68 | uBit->radio.enable(); |
| quagga | 0:8e06c9b8b7f6 | 69 | |
| quagga | 0:8e06c9b8b7f6 | 70 | int t,dt; |
| quagga | 0:8e06c9b8b7f6 | 71 | int count=0; |
| quagga | 0:8e06c9b8b7f6 | 72 | bool ack=false; |
| quagga | 0:8e06c9b8b7f6 | 73 | |
| quagga | 0:8e06c9b8b7f6 | 74 | while (count<maxtries) |
| quagga | 0:8e06c9b8b7f6 | 75 | { |
| quagga | 0:8e06c9b8b7f6 | 76 | //clear dataReady queue |
| quagga | 0:8e06c9b8b7f6 | 77 | while (uBit->radio.dataReady()>0){uBit->radio.recv();} |
| quagga | 0:8e06c9b8b7f6 | 78 | uBit->radio.datagram.send(packet); |
| quagga | 0:8e06c9b8b7f6 | 79 | dt=0; |
| quagga | 0:8e06c9b8b7f6 | 80 | t=system_timer_current_time_us(); |
| quagga | 0:8e06c9b8b7f6 | 81 | while (dt<timeout) |
| quagga | 0:8e06c9b8b7f6 | 82 | { |
| quagga | 0:8e06c9b8b7f6 | 83 | dt=system_timer_current_time_us()-t; |
| quagga | 0:8e06c9b8b7f6 | 84 | if (uBit->radio.dataReady()>0){ack=true;break;} |
| quagga | 0:8e06c9b8b7f6 | 85 | } |
| quagga | 0:8e06c9b8b7f6 | 86 | if (ack){break;} |
| quagga | 0:8e06c9b8b7f6 | 87 | count++; |
| quagga | 0:8e06c9b8b7f6 | 88 | } |
| quagga | 0:8e06c9b8b7f6 | 89 | tries=count; |
| quagga | 0:8e06c9b8b7f6 | 90 | if (ack) |
| quagga | 0:8e06c9b8b7f6 | 91 | { |
| quagga | 0:8e06c9b8b7f6 | 92 | //PacketBuffer s=uBit->radio.datagram.recv(); |
| quagga | 0:8e06c9b8b7f6 | 93 | FrameBuffer *s=uBit->radio.recv(); |
| quagga | 0:8e06c9b8b7f6 | 94 | //get rssi reported from receiver |
| quagga | 0:8e06c9b8b7f6 | 95 | rssi=(int)((s->payload[0])^128); |
| quagga | 0:8e06c9b8b7f6 | 96 | delete s; |
| quagga | 0:8e06c9b8b7f6 | 97 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 98 | uBit->serial.printf("%d rssi reported from ACK ",rssi); |
| quagga | 0:8e06c9b8b7f6 | 99 | uBit->serial.printf("after - %d tries\n",tries); |
| quagga | 0:8e06c9b8b7f6 | 100 | uBit->serial.printf("after - %d microseconds\n",dt); |
| quagga | 0:8e06c9b8b7f6 | 101 | #endif |
| quagga | 0:8e06c9b8b7f6 | 102 | if (packet[0]==0){MicroBitEvent(ARQ_ID,ARQ_POW_ACK_RECVD);} |
| quagga | 0:8e06c9b8b7f6 | 103 | else {MicroBitEvent(ARQ_ID,ARQ_MSG_ACK_RECVD);} |
| quagga | 0:8e06c9b8b7f6 | 104 | } |
| quagga | 0:8e06c9b8b7f6 | 105 | |
| quagga | 0:8e06c9b8b7f6 | 106 | else |
| quagga | 0:8e06c9b8b7f6 | 107 | { |
| quagga | 0:8e06c9b8b7f6 | 108 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 109 | uBit->serial.printf("Send Failed!\n"); |
| quagga | 0:8e06c9b8b7f6 | 110 | #endif |
| quagga | 0:8e06c9b8b7f6 | 111 | if (packet[0]==0){MicroBitEvent(ARQ_ID_POW_SEND_FAIL);} |
| quagga | 0:8e06c9b8b7f6 | 112 | else {MicroBitEvent(ARQ_ID,ARQ_MSG_SEND_FAIL);} |
| quagga | 0:8e06c9b8b7f6 | 113 | } |
| quagga | 0:8e06c9b8b7f6 | 114 | |
| quagga | 0:8e06c9b8b7f6 | 115 | //power check? |
| quagga | 0:8e06c9b8b7f6 | 116 | if (packet[0]!=0) //make sure not power_test to avoid infinite loop |
| quagga | 0:8e06c9b8b7f6 | 117 | { |
| quagga | 0:8e06c9b8b7f6 | 118 | if (tries>(mintries-1)){this->incPower();} //if too many attempts power up |
| quagga | 0:8e06c9b8b7f6 | 119 | else if (rssi<min_rssi){this->decPower();} //if rssi high reduce power |
| quagga | 0:8e06c9b8b7f6 | 120 | } |
| quagga | 0:8e06c9b8b7f6 | 121 | |
| quagga | 0:8e06c9b8b7f6 | 122 | uBit->radio.disable(); |
| quagga | 0:8e06c9b8b7f6 | 123 | } |
| quagga | 0:8e06c9b8b7f6 | 124 | |
| quagga | 0:8e06c9b8b7f6 | 125 | |
| quagga | 0:8e06c9b8b7f6 | 126 | void Arq::incPower() |
| quagga | 0:8e06c9b8b7f6 | 127 | { |
| quagga | 0:8e06c9b8b7f6 | 128 | //uBit->serial.send("Arq::incPower()\n"); |
| quagga | 0:8e06c9b8b7f6 | 129 | //send message if ack not received up power |
| quagga | 0:8e06c9b8b7f6 | 130 | uBit->radio.enable(); |
| quagga | 0:8e06c9b8b7f6 | 131 | while (power<7) |
| quagga | 0:8e06c9b8b7f6 | 132 | { |
| quagga | 0:8e06c9b8b7f6 | 133 | //send message |
| quagga | 0:8e06c9b8b7f6 | 134 | PacketBuffer s(2); |
| quagga | 0:8e06c9b8b7f6 | 135 | s[0]=0; |
| quagga | 0:8e06c9b8b7f6 | 136 | s[1]=(char)power; |
| quagga | 0:8e06c9b8b7f6 | 137 | this->send(s); |
| quagga | 0:8e06c9b8b7f6 | 138 | if (tries<mintries){break;} |
| quagga | 0:8e06c9b8b7f6 | 139 | power++; |
| quagga | 0:8e06c9b8b7f6 | 140 | uBit->radio.setTransmitPower(power); |
| quagga | 0:8e06c9b8b7f6 | 141 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 142 | uBit->serial.printf("Power set to %d\n",power); |
| quagga | 0:8e06c9b8b7f6 | 143 | #endif |
| quagga | 0:8e06c9b8b7f6 | 144 | } |
| quagga | 0:8e06c9b8b7f6 | 145 | uBit->radio.disable(); |
| quagga | 0:8e06c9b8b7f6 | 146 | } |
| quagga | 0:8e06c9b8b7f6 | 147 | |
| quagga | 0:8e06c9b8b7f6 | 148 | void Arq::decPower() |
| quagga | 0:8e06c9b8b7f6 | 149 | { |
| quagga | 0:8e06c9b8b7f6 | 150 | //uBit->serial.send("Arq::decPower()\n"); |
| quagga | 0:8e06c9b8b7f6 | 151 | uBit->radio.enable(); |
| quagga | 0:8e06c9b8b7f6 | 152 | while (power>0) |
| quagga | 0:8e06c9b8b7f6 | 153 | { |
| quagga | 0:8e06c9b8b7f6 | 154 | //send message |
| quagga | 0:8e06c9b8b7f6 | 155 | PacketBuffer s(2); |
| quagga | 0:8e06c9b8b7f6 | 156 | s[0]=0; |
| quagga | 0:8e06c9b8b7f6 | 157 | s[1]=(char) power; |
| quagga | 0:8e06c9b8b7f6 | 158 | //try lower power |
| quagga | 0:8e06c9b8b7f6 | 159 | power--; |
| quagga | 0:8e06c9b8b7f6 | 160 | uBit->radio.setTransmitPower(power); |
| quagga | 0:8e06c9b8b7f6 | 161 | this->send(s); |
| quagga | 0:8e06c9b8b7f6 | 162 | if (tries>mintries)//failed at this power,increase power and break |
| quagga | 0:8e06c9b8b7f6 | 163 | { |
| quagga | 0:8e06c9b8b7f6 | 164 | if (power<7) |
| quagga | 0:8e06c9b8b7f6 | 165 | { |
| quagga | 0:8e06c9b8b7f6 | 166 | power++; |
| quagga | 0:8e06c9b8b7f6 | 167 | uBit->radio.setTransmitPower(power); |
| quagga | 0:8e06c9b8b7f6 | 168 | #ifdef ARQ_DEBUG |
| quagga | 0:8e06c9b8b7f6 | 169 | uBit->serial.printf("Power set to %d\n",power); |
| quagga | 0:8e06c9b8b7f6 | 170 | #endif |
| quagga | 0:8e06c9b8b7f6 | 171 | } |
| quagga | 0:8e06c9b8b7f6 | 172 | break; |
| quagga | 0:8e06c9b8b7f6 | 173 | } |
| quagga | 0:8e06c9b8b7f6 | 174 | } |
| quagga | 0:8e06c9b8b7f6 | 175 | uBit->radio.disable(); |
| quagga | 0:8e06c9b8b7f6 | 176 | } |