A simple wireless protocol to let my examples communicate each other. ssWi stands for Shared Slotted Wireless protocol
Dependents: rover_car rover_pc supervisor watering_unit ... more
This library aims at implementing a simple communication protocol among nodes, abstracting from the hardware. The name ssWi stands for Shared Slotted Wireless. Wireless is part of the name, even though the library abstracts from the hardware, as the first version was entirely focused on the XBee modules and then the name has not been changed.
The communication channel is represented by ssWiChannel, an abstract class which models the channel that the transceivers access to. The concrete classes must implement the functions: init, read and write. The protocol automatically sends and receives data through the selected channel, exploiting the operting system timers. Addresses are not considered as the communication lays on broadcast transmissions.
The protocol provides the ssWiPort abstraction which is like memory areas shared among all the connected nodes. Reading from one port lets the node retrieve the last written value from the other nodes. Writing on one port means sending such value to other nodes.
Objects instantiated from ssWiSocket are the interface for allowing nodes to access the protocol ports.
TODO:
- improve the parsing of the received messages
- communication tests with many nodes (so far, only 2 nodes have been tested)
Diff: xbee/xbee.cpp
- Revision:
- 20:36931ee9af85
- Parent:
- 15:fb0f6cbc0ed5
- Child:
- 24:80345e511574
--- a/xbee/xbee.cpp Thu Mar 07 15:44:05 2013 +0000 +++ b/xbee/xbee.cpp Fri Apr 17 15:19:40 2020 +0000 @@ -1,96 +1,97 @@ /** \file xbee.cpp + * * \brief implementation of the ssWi channel using the XBee module * */ - + #include "xbee.hpp" #include <sstream> - void XBeeModule::readResponse (char* msg) { char c = 0; int i = 0; - while (c!='\r') - if (xbee.readable()) { - c = xbee.getc(); + while (c!='\r') { + if (serial_xbee.readable()) { + c = serial_xbee.getc(); msg[i++] = c; } + } } - bool XBeeModule::executeWithOk (const char* cmd) { char msg[5]; - xbee.printf("%s", cmd); + + serial_xbee.printf("%s", cmd); + //printf("command %s\r\n", cmd); readResponse(msg); - //printf("%s -> %s\n\r", cmd, msg); + //printf("response %s\r\n", msg); if (strncmp(msg, "OK\r", 3)!=0) return false; + return true; } void XBeeModule::executeWithRes (const char* cmd, char* res) { - xbee.printf("%s", cmd); + serial_xbee.printf("%s", cmd); readResponse(res); - //printf("\n\r%s -> %s\n\r", cmd, res); } - - +bool XBeeModule::initSequence() +{ + // wait 1 second before starting + ThisThread::sleep_for(1000); + // send start command + if (!executeWithOk("+++")) + return false; + // wait 1 more second after start command + ThisThread::sleep_for(1000); + + return true; +} - - -XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : xbee(tx, rx, 64) +XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : serial_xbee(tx, rx, 9600) { status = false; - wait(1); + // read address if (!_getLocalAddr()) return; - wait(1); + // set channel if (!_setChannel(channel)) return; - wait(1); + // set pan ID if (!_setPanID(panID)) return; - wait(1); status = true; } - - - - - - -XBeeAddress XBeeModule::getDstAddress () +bool XBeeModule::getDstAddress (XBeeAddress &addr) { char tmp[10]; std::stringstream s1, s2; - string high, low; - XBeeAddress addr; - wait(1); - if (!executeWithOk("+++")) - return addr; - - wait(1); - + uint32_t high, low; + + // start communication with transceiver + if (!initSequence()) + return false; + // read high address executeWithRes("ATDH \r", tmp); s1<<std::hex<<tmp; s1>>high; - + // read low address executeWithRes("ATDL \r", tmp); s2<<std::hex<<tmp; s2>>low; - + // terminate if (!executeWithOk("ATCN \r")) - return addr; + return false; + addr.setAddress(low, high); - wait(1); - return XBeeAddress(low, high); + return true; } @@ -98,44 +99,49 @@ { char s[10]; string low, high; - wait(1); - if (!executeWithOk("+++")) + + // start communication with transceiver + if (!initSequence()) return false; - wait(1); - sprintf(s, "ATDH%s \r", addr.getHighAddr().c_str()); - //printf("%s\n\r", addr.getHighAddr().c_str()); + // send high address + sprintf(s, "ATDH%X \r", addr.getHighAddr()); if (!executeWithOk(s)) return false; - - sprintf(s, "ATDL%s \r", addr.getLowAddr().c_str()); - //printf("%s\n\r", addr.getLowAddr().c_str()); + // send low address + sprintf(s, "ATDL%X \r", addr.getLowAddr()); if (!executeWithOk(s)) return false; - + // terminate if (!executeWithOk("ATCN \r")) return false; + ThisThread::sleep_for(1000); return true; } - bool XBeeModule::_getLocalAddr () { char tmp[10]; - string high, low; + uint32_t high, low; std::stringstream s1, s2; - if (!executeWithOk("+++")) + // start communication with transceiver + if (!initSequence()) return false; + // read high address executeWithRes("ATSH \r", tmp); s1<<std::hex<<tmp; s1>>high; + // read low address executeWithRes("ATSL \r", tmp); s2<<std::hex<<tmp; s2>>low; + // terminate if (!executeWithOk("ATCN \r")) return false; + // save address local = XBeeAddress(low, high); + return true; } @@ -143,10 +149,10 @@ bool XBeeModule::_setChannel (int channel) { char s[10]; - - if (!executeWithOk("+++")) + + // start communication with transceiver + if (!initSequence()) return false; - wait(1); sprintf(s, "ATCH%d \r", channel); if (!executeWithOk(s)) return false; @@ -159,15 +165,15 @@ { int channel; char s[10]; - - if (!executeWithOk("+++")) - return -1; - wait(1); + + // start communication with transceiver + if (!initSequence()) + return false; executeWithRes("ATCH \r", s); channel = atoi(s); if (!executeWithOk("ATCN \r")) return -1; - wait(1); + ThisThread::sleep_for(1000); return channel; } @@ -175,13 +181,16 @@ bool XBeeModule::_setPanID (int panID) { char s[10]; - if (!executeWithOk("+++")) + + // start communication with transceiver + if (!initSequence()) return false; sprintf(s, "ATID%d \r", panID); if (!executeWithOk(s)) return false; if (!executeWithOk("ATCN \r")) return false; + return true; } @@ -189,14 +198,15 @@ { int id; char s[10]; - - if (!executeWithOk("+++")) - return -1; + + // start communication with transceiver + if (!initSequence()) + return false; executeWithRes("ATID \r", s); id = atoi(s); if (!executeWithOk("ATCN \r")) return -1; - wait(1); + return id; } @@ -205,8 +215,8 @@ int XBeeModule::read (char* msg) { int i = 0; - while (xbee.readable()) - msg[i++] = xbee.getc(); + while (serial_xbee.readable()) + msg[i++] = serial_xbee.getc(); return i; } @@ -215,7 +225,7 @@ void XBeeModule::write (const char* msg, int n) { for (int i=0; i<n; i++) { - while(!xbee.writeable()); - xbee.putc(msg[i]); + while(!serial_xbee.writeable()); + serial_xbee.putc(msg[i]); } }