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.
XBee_Robot.cpp@4:af08c7749f9d, 2016-01-05 (annotated)
- Committer:
- sleighton
- Date:
- Tue Jan 05 14:27:40 2016 +0000
- Revision:
- 4:af08c7749f9d
- Parent:
- 3:cf539cfd3d59
- Child:
- 6:fb0316cafaa6
network node class added
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sleighton | 2:5040ec01dba1 | 1 | #include "XBee_Robot.h" |
| sleighton | 2:5040ec01dba1 | 2 | #include <vector> |
| sleighton | 3:cf539cfd3d59 | 3 | #include <algorithm> |
| sleighton | 3:cf539cfd3d59 | 4 | #include <list> |
| sleighton | 2:5040ec01dba1 | 5 | |
| sleighton | 4:af08c7749f9d | 6 | //NETWORK CLASS METHODS |
| sleighton | 4:af08c7749f9d | 7 | NetworkNode::NetworkNode(std::vector<uint8_t> & addrIn, int indexIn){ |
| sleighton | 4:af08c7749f9d | 8 | addr = addrIn; |
| sleighton | 4:af08c7749f9d | 9 | nodeNum = indexIn; |
| sleighton | 4:af08c7749f9d | 10 | } |
| sleighton | 4:af08c7749f9d | 11 | |
| sleighton | 4:af08c7749f9d | 12 | std::vector<uint8_t> NetworkNode::getAddr(){ //returns address of node |
| sleighton | 4:af08c7749f9d | 13 | return addr; |
| sleighton | 2:5040ec01dba1 | 14 | } |
| sleighton | 2:5040ec01dba1 | 15 | |
| sleighton | 4:af08c7749f9d | 16 | int NetworkNode::getIndex(){ //returns index of node |
| sleighton | 4:af08c7749f9d | 17 | return nodeNum; |
| sleighton | 4:af08c7749f9d | 18 | } |
| sleighton | 4:af08c7749f9d | 19 | |
| sleighton | 4:af08c7749f9d | 20 | void NetworkNode::setIndex(int indexIn){ //sets index of node |
| sleighton | 4:af08c7749f9d | 21 | nodeNum = indexIn; |
| sleighton | 4:af08c7749f9d | 22 | } |
| sleighton | 4:af08c7749f9d | 23 | |
| sleighton | 4:af08c7749f9d | 24 | void NetworkNode::setAddr(std::vector<uint8_t> & addrIn){ //sets address of node |
| sleighton | 4:af08c7749f9d | 25 | addr = addrIn; |
| sleighton | 4:af08c7749f9d | 26 | } |
| sleighton | 4:af08c7749f9d | 27 | |
| sleighton | 4:af08c7749f9d | 28 | //XBEE ROBOT CLASS METHODS |
| sleighton | 4:af08c7749f9d | 29 | XBee_Robot::XBee_Robot(PinName _txIn, PinName _rxIn): dataLink(_txIn,_rxIn){} |
| sleighton | 4:af08c7749f9d | 30 | |
| sleighton | 2:5040ec01dba1 | 31 | void XBee_Robot::setRxInterrupt() |
| sleighton | 2:5040ec01dba1 | 32 | { |
| sleighton | 2:5040ec01dba1 | 33 | dataLink.attach(this,&XBee_Robot::Rx_interrupt, Serial::RxIrq); |
| sleighton | 2:5040ec01dba1 | 34 | } |
| sleighton | 2:5040ec01dba1 | 35 | |
| sleighton | 2:5040ec01dba1 | 36 | void XBee_Robot::Rx_interrupt() |
| sleighton | 2:5040ec01dba1 | 37 | { |
| sleighton | 3:cf539cfd3d59 | 38 | std::vector<uint8_t> Rx_buffer; |
| sleighton | 2:5040ec01dba1 | 39 | while(dataLink.readable()){ |
| sleighton | 3:cf539cfd3d59 | 40 | Rx_buffer.push_back(dataLink.getc());//add each incoming byte to buffer |
| sleighton | 3:cf539cfd3d59 | 41 | wait(0.0011); //wait for long enough so the next digit is recognised in the same stream |
| sleighton | 2:5040ec01dba1 | 42 | } |
| sleighton | 3:cf539cfd3d59 | 43 | |
| sleighton | 3:cf539cfd3d59 | 44 | //Check valid packet delimeter and checksum |
| sleighton | 3:cf539cfd3d59 | 45 | if((Rx_buffer[0] == 0x7E) && (Rx_buffer[Rx_buffer.size()] == calculateChecksum(Rx_buffer))) |
| sleighton | 3:cf539cfd3d59 | 46 | RxPacketControl(Rx_buffer); //call packet control function |
| sleighton | 2:5040ec01dba1 | 47 | } |
| sleighton | 2:5040ec01dba1 | 48 | |
| sleighton | 2:5040ec01dba1 | 49 | void XBee_Robot::transmitRequest(uint8_t *BitAddress64, uint8_t *BitAddress16, uint8_t broadcastRadius, uint8_t options, uint8_t *data,size_t dataLength) |
| sleighton | 2:5040ec01dba1 | 50 | { |
| sleighton | 2:5040ec01dba1 | 51 | //calculate checksum |
| sleighton | 2:5040ec01dba1 | 52 | uint16_t length = 0x0E + dataLength; //calculate length of packet (14 + data length) |
| sleighton | 2:5040ec01dba1 | 53 | uint8_t lengthu = length >>8; //upper 8 bits |
| sleighton | 2:5040ec01dba1 | 54 | uint8_t lengthl = length & 0xFF; //lower 8 bits |
| sleighton | 2:5040ec01dba1 | 55 | |
| sleighton | 2:5040ec01dba1 | 56 | |
| sleighton | 2:5040ec01dba1 | 57 | std::vector<uint8_t> transmitRequestPacket; //create new vector packet |
| sleighton | 2:5040ec01dba1 | 58 | //populate packet |
| sleighton | 2:5040ec01dba1 | 59 | transmitRequestPacket.push_back(0x7E); //start delimeter |
| sleighton | 2:5040ec01dba1 | 60 | transmitRequestPacket.push_back(lengthu); //upper byte of length |
| sleighton | 2:5040ec01dba1 | 61 | transmitRequestPacket.push_back(lengthl); //lower byte of length |
| sleighton | 2:5040ec01dba1 | 62 | transmitRequestPacket.push_back(0x10); //API ID (transmit request) |
| sleighton | 2:5040ec01dba1 | 63 | transmitRequestPacket.push_back(0x01); //channel ID |
| sleighton | 2:5040ec01dba1 | 64 | transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress64, BitAddress64+8); //64 bit destination address |
| sleighton | 2:5040ec01dba1 | 65 | transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress16, BitAddress16+2); //16 bit network address |
| sleighton | 2:5040ec01dba1 | 66 | transmitRequestPacket.push_back(broadcastRadius); //broadcast radius (0 = max hops) |
| sleighton | 2:5040ec01dba1 | 67 | transmitRequestPacket.push_back(options); //additional options for packet |
| sleighton | 2:5040ec01dba1 | 68 | transmitRequestPacket.insert(transmitRequestPacket.end(), data, data+dataLength); //data |
| sleighton | 2:5040ec01dba1 | 69 | uint8_t checksum = calculateChecksum(transmitRequestPacket); |
| sleighton | 2:5040ec01dba1 | 70 | transmitRequestPacket.push_back(checksum); //calculate and add checksum |
| sleighton | 2:5040ec01dba1 | 71 | |
| sleighton | 2:5040ec01dba1 | 72 | for (int i = 0; i < transmitRequestPacket.size(); i++){ |
| sleighton | 2:5040ec01dba1 | 73 | dataLink.printf("%c",transmitRequestPacket[i]); //send packet |
| sleighton | 2:5040ec01dba1 | 74 | printf("%c",transmitRequestPacket[i]); |
| sleighton | 2:5040ec01dba1 | 75 | } |
| sleighton | 2:5040ec01dba1 | 76 | } |
| sleighton | 2:5040ec01dba1 | 77 | |
| sleighton | 2:5040ec01dba1 | 78 | uint8_t XBee_Robot::calculateChecksum(std::vector<uint8_t> & packet) |
| sleighton | 2:5040ec01dba1 | 79 | { |
| sleighton | 2:5040ec01dba1 | 80 | uint8_t checksum = 0xFF; //start with FF as last byte of sum is subtracted from FF |
| sleighton | 2:5040ec01dba1 | 81 | for (int i = 3; i < packet.size(); i++) |
| sleighton | 2:5040ec01dba1 | 82 | checksum -= packet[i]; |
| sleighton | 2:5040ec01dba1 | 83 | return checksum; |
| sleighton | 2:5040ec01dba1 | 84 | |
| sleighton | 3:cf539cfd3d59 | 85 | } |
| sleighton | 3:cf539cfd3d59 | 86 | |
| sleighton | 3:cf539cfd3d59 | 87 | void XBee_Robot::RxPacketControl(std::vector<uint8_t> & packet) |
| sleighton | 3:cf539cfd3d59 | 88 | { |
| sleighton | 3:cf539cfd3d59 | 89 | uint8_t command = packet[3]; //take API address |
| sleighton | 3:cf539cfd3d59 | 90 | switch (command) { //index for different commands |
| sleighton | 3:cf539cfd3d59 | 91 | case 0x90:{ //Receive packet command |
| sleighton | 3:cf539cfd3d59 | 92 | |
| sleighton | 3:cf539cfd3d59 | 93 | std::vector<uint8_t> source_addr64; //create new vector to store source address |
| sleighton | 3:cf539cfd3d59 | 94 | source_addr64.insert(source_addr64.end(), packet.begin() + 4, packet.begin() + 12); //insert source address part of packet into new vector |
| sleighton | 3:cf539cfd3d59 | 95 | checkSourceAddr(source_addr64); |
| sleighton | 3:cf539cfd3d59 | 96 | |
| sleighton | 3:cf539cfd3d59 | 97 | std::vector<uint8_t> data; //create new vector to store data |
| sleighton | 3:cf539cfd3d59 | 98 | data.insert(data.end(), packet.begin() + 15, packet.end() -1); //insert data part of packet into new vector |
| sleighton | 3:cf539cfd3d59 | 99 | for(int i = 0; i<data.size();i++){ |
| sleighton | 3:cf539cfd3d59 | 100 | printf("Data: %d\n",(int)data[i]); //display data from packet |
| sleighton | 3:cf539cfd3d59 | 101 | } |
| sleighton | 3:cf539cfd3d59 | 102 | |
| sleighton | 3:cf539cfd3d59 | 103 | break; |
| sleighton | 3:cf539cfd3d59 | 104 | } |
| sleighton | 3:cf539cfd3d59 | 105 | default: |
| sleighton | 3:cf539cfd3d59 | 106 | printf("Received API address not recognised: %c",command); |
| sleighton | 3:cf539cfd3d59 | 107 | } |
| sleighton | 3:cf539cfd3d59 | 108 | } |
| sleighton | 3:cf539cfd3d59 | 109 | |
| sleighton | 3:cf539cfd3d59 | 110 | void XBee_Robot::checkSourceAddr(std::vector<uint8_t> & addr) |
| sleighton | 3:cf539cfd3d59 | 111 | { |
| sleighton | 4:af08c7749f9d | 112 | bool exists = false; |
| sleighton | 4:af08c7749f9d | 113 | for (int i = 0; i<node_list.size();i++){ //search each entry in node_list for matching address |
| sleighton | 4:af08c7749f9d | 114 | if(node_list[i].getAddr() == addr){ |
| sleighton | 4:af08c7749f9d | 115 | exists = true; |
| sleighton | 4:af08c7749f9d | 116 | printf("Recognised node %d\n",node_list[i].getIndex()); //print node number |
| sleighton | 4:af08c7749f9d | 117 | } |
| sleighton | 4:af08c7749f9d | 118 | } |
| sleighton | 4:af08c7749f9d | 119 | if (exists == false){ //add new address to list if no match found |
| sleighton | 4:af08c7749f9d | 120 | NetworkNode newNode(addr,node_list.size());//create new node |
| sleighton | 4:af08c7749f9d | 121 | node_list.push_back(newNode); //add new node to list |
| sleighton | 3:cf539cfd3d59 | 122 | printf("New address added\n"); |
| sleighton | 3:cf539cfd3d59 | 123 | } |
| sleighton | 3:cf539cfd3d59 | 124 | |
| sleighton | 4:af08c7749f9d | 125 | } |
| sleighton | 4:af08c7749f9d | 126 |