Library for XBee API targeted toward functions with specific use in conjunction with the Pololu 3pi Robot. Work in Progress

Committer:
sleighton
Date:
Tue Jan 05 11:37:13 2016 +0000
Revision:
3:cf539cfd3d59
Parent:
2:5040ec01dba1
Child:
4:af08c7749f9d
Now includes packet handling functions

Who changed what in which revision?

UserRevisionLine numberNew 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 2:5040ec01dba1 6 XBee_Robot::XBee_Robot(PinName _txIn, PinName _rxIn): dataLink(_txIn,_rxIn)
sleighton 2:5040ec01dba1 7 {
sleighton 2:5040ec01dba1 8 _tx = _txIn; //redundant
sleighton 2:5040ec01dba1 9 _rx = _rxIn;
sleighton 2:5040ec01dba1 10 }
sleighton 2:5040ec01dba1 11
sleighton 2:5040ec01dba1 12 void XBee_Robot::setRxInterrupt()
sleighton 2:5040ec01dba1 13 {
sleighton 2:5040ec01dba1 14 dataLink.attach(this,&XBee_Robot::Rx_interrupt, Serial::RxIrq);
sleighton 2:5040ec01dba1 15 }
sleighton 2:5040ec01dba1 16
sleighton 2:5040ec01dba1 17 void XBee_Robot::Rx_interrupt()
sleighton 2:5040ec01dba1 18 {
sleighton 3:cf539cfd3d59 19 std::vector<uint8_t> Rx_buffer;
sleighton 2:5040ec01dba1 20 while(dataLink.readable()){
sleighton 3:cf539cfd3d59 21 Rx_buffer.push_back(dataLink.getc());//add each incoming byte to buffer
sleighton 3:cf539cfd3d59 22 wait(0.0011); //wait for long enough so the next digit is recognised in the same stream
sleighton 2:5040ec01dba1 23 }
sleighton 3:cf539cfd3d59 24
sleighton 3:cf539cfd3d59 25 //Check valid packet delimeter and checksum
sleighton 3:cf539cfd3d59 26 if((Rx_buffer[0] == 0x7E) && (Rx_buffer[Rx_buffer.size()] == calculateChecksum(Rx_buffer)))
sleighton 3:cf539cfd3d59 27 RxPacketControl(Rx_buffer); //call packet control function
sleighton 2:5040ec01dba1 28 }
sleighton 2:5040ec01dba1 29
sleighton 2:5040ec01dba1 30 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 31 {
sleighton 2:5040ec01dba1 32 //calculate checksum
sleighton 2:5040ec01dba1 33 uint16_t length = 0x0E + dataLength; //calculate length of packet (14 + data length)
sleighton 2:5040ec01dba1 34 uint8_t lengthu = length >>8; //upper 8 bits
sleighton 2:5040ec01dba1 35 uint8_t lengthl = length & 0xFF; //lower 8 bits
sleighton 2:5040ec01dba1 36
sleighton 2:5040ec01dba1 37
sleighton 2:5040ec01dba1 38 std::vector<uint8_t> transmitRequestPacket; //create new vector packet
sleighton 2:5040ec01dba1 39 //populate packet
sleighton 2:5040ec01dba1 40 transmitRequestPacket.push_back(0x7E); //start delimeter
sleighton 2:5040ec01dba1 41 transmitRequestPacket.push_back(lengthu); //upper byte of length
sleighton 2:5040ec01dba1 42 transmitRequestPacket.push_back(lengthl); //lower byte of length
sleighton 2:5040ec01dba1 43 transmitRequestPacket.push_back(0x10); //API ID (transmit request)
sleighton 2:5040ec01dba1 44 transmitRequestPacket.push_back(0x01); //channel ID
sleighton 2:5040ec01dba1 45 transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress64, BitAddress64+8); //64 bit destination address
sleighton 2:5040ec01dba1 46 transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress16, BitAddress16+2); //16 bit network address
sleighton 2:5040ec01dba1 47 transmitRequestPacket.push_back(broadcastRadius); //broadcast radius (0 = max hops)
sleighton 2:5040ec01dba1 48 transmitRequestPacket.push_back(options); //additional options for packet
sleighton 2:5040ec01dba1 49 transmitRequestPacket.insert(transmitRequestPacket.end(), data, data+dataLength); //data
sleighton 2:5040ec01dba1 50 uint8_t checksum = calculateChecksum(transmitRequestPacket);
sleighton 2:5040ec01dba1 51 transmitRequestPacket.push_back(checksum); //calculate and add checksum
sleighton 2:5040ec01dba1 52
sleighton 2:5040ec01dba1 53 for (int i = 0; i < transmitRequestPacket.size(); i++){
sleighton 2:5040ec01dba1 54 dataLink.printf("%c",transmitRequestPacket[i]); //send packet
sleighton 2:5040ec01dba1 55 printf("%c",transmitRequestPacket[i]);
sleighton 2:5040ec01dba1 56 }
sleighton 2:5040ec01dba1 57 }
sleighton 2:5040ec01dba1 58
sleighton 2:5040ec01dba1 59 uint8_t XBee_Robot::calculateChecksum(std::vector<uint8_t> & packet)
sleighton 2:5040ec01dba1 60 {
sleighton 2:5040ec01dba1 61 uint8_t checksum = 0xFF; //start with FF as last byte of sum is subtracted from FF
sleighton 2:5040ec01dba1 62 for (int i = 3; i < packet.size(); i++)
sleighton 2:5040ec01dba1 63 checksum -= packet[i];
sleighton 2:5040ec01dba1 64 return checksum;
sleighton 2:5040ec01dba1 65
sleighton 3:cf539cfd3d59 66 }
sleighton 3:cf539cfd3d59 67
sleighton 3:cf539cfd3d59 68 void XBee_Robot::RxPacketControl(std::vector<uint8_t> & packet)
sleighton 3:cf539cfd3d59 69 {
sleighton 3:cf539cfd3d59 70 uint8_t command = packet[3]; //take API address
sleighton 3:cf539cfd3d59 71 switch (command) { //index for different commands
sleighton 3:cf539cfd3d59 72 case 0x90:{ //Receive packet command
sleighton 3:cf539cfd3d59 73
sleighton 3:cf539cfd3d59 74 std::vector<uint8_t> source_addr64; //create new vector to store source address
sleighton 3:cf539cfd3d59 75 source_addr64.insert(source_addr64.end(), packet.begin() + 4, packet.begin() + 12); //insert source address part of packet into new vector
sleighton 3:cf539cfd3d59 76 checkSourceAddr(source_addr64);
sleighton 3:cf539cfd3d59 77
sleighton 3:cf539cfd3d59 78 std::vector<uint8_t> data; //create new vector to store data
sleighton 3:cf539cfd3d59 79 data.insert(data.end(), packet.begin() + 15, packet.end() -1); //insert data part of packet into new vector
sleighton 3:cf539cfd3d59 80 for(int i = 0; i<data.size();i++){
sleighton 3:cf539cfd3d59 81 printf("Data: %d\n",(int)data[i]); //display data from packet
sleighton 3:cf539cfd3d59 82 }
sleighton 3:cf539cfd3d59 83
sleighton 3:cf539cfd3d59 84 break;
sleighton 3:cf539cfd3d59 85 }
sleighton 3:cf539cfd3d59 86 default:
sleighton 3:cf539cfd3d59 87 printf("Received API address not recognised: %c",command);
sleighton 3:cf539cfd3d59 88 }
sleighton 3:cf539cfd3d59 89 }
sleighton 3:cf539cfd3d59 90
sleighton 3:cf539cfd3d59 91 void XBee_Robot::checkSourceAddr(std::vector<uint8_t> & addr)
sleighton 3:cf539cfd3d59 92 {
sleighton 3:cf539cfd3d59 93 if (std::find(addr_list.begin(), addr_list.end(), addr) != addr_list.end())
sleighton 3:cf539cfd3d59 94 printf("Recognised address\n");
sleighton 3:cf539cfd3d59 95 else{
sleighton 3:cf539cfd3d59 96 addr_list.push_back(addr);
sleighton 3:cf539cfd3d59 97 printf("New address added\n");
sleighton 3:cf539cfd3d59 98 }
sleighton 3:cf539cfd3d59 99
sleighton 2:5040ec01dba1 100 }