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)
Revision 20:36931ee9af85, committed 2020-04-17
- Comitter:
- mariob
- Date:
- Fri Apr 17 15:19:40 2020 +0000
- Parent:
- 16:b50c3b3b241c
- Child:
- 21:2db8d5db6c57
- Commit message:
- few fixes, moving to new os
Changed in this revision
--- a/ssWi.cpp Thu Mar 07 15:44:05 2013 +0000 +++ b/ssWi.cpp Fri Apr 17 15:19:40 2020 +0000 @@ -68,73 +68,68 @@ int RXRate; -/* -Thread* sender; -Thread* receiver; +void functionSender(); +void functionReceiver(); -void threadSender (void const* arg); -void threadReceiver (void const* arg); +static Thread readingThread; +static Thread writingThread; bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX) { if (channel!=NULL) return false; - TXRate = 1000.0/rateTX; - RXRate = 1000.0/rateRX; - mutexChannel.lock(); + //mutexChannel.lock(); channel = c; - mutexChannel.unlock(); - sender = new Thread(threadSender); - receiver = new Thread(threadReceiver); + //mutexChannel.unlock(); + TXRate = rateTX > 0 ? 1000/rateTX : 0; + RXRate = rateRX > 0 ? 1000/rateRX : 0; + + readingThread.start(functionReceiver); + writingThread.start(functionSender); + return true; } -void threadSender (void const* arg) -{ +void functionSender() { + static DigitalOut led4(LED4); static char buffer[INTERNAL_BUFFER_SIZE]; - while(true) { + + while (1) { + led4 = !led4; int n = 3; int numFrames = 0; - for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) { + for (std::map<int, ssWiPort>::iterator it = ports.begin(); + it != ports.end(); it++) { if ((*it).second.isModified()) { buffer[n++] = (*it).first; PortValue tmp = (*it).second.getTXValue(); memcpy(&buffer[n], &tmp, sizeof(PortValue)); n += sizeof(PortValue); - //printf("Write[%d] = %d\n\r", (*it).first, tmp); numFrames++; } } - if (numFrames>0) { + if (numFrames > 0) { buffer[0] = START_0; buffer[1] = START_1; buffer[2] = START_2; - mutexChannel.lock(); + if (!mutexChannel.trylock()) + return; channel->write(buffer, n); mutexChannel.unlock(); -// printf("W(%d): ", n); -// for(int k=0; k<n; k++) -// printf("%d ", buffer[k]); -// printf("\n\r"); } - Thread::wait(TXRate); + thread_sleep_for(TXRate); } } -void threadReceiver (void const* arg) +void functionReceiver () { static char buffer[INTERNAL_BUFFER_SIZE]; - while(true) { - printf("...read...\n\r"); - mutexChannel.lock(); + + while(1) { + if (!mutexChannel.trylock()) + return; int n = channel->read(buffer); mutexChannel.unlock(); -// if (n>0) { -// printf("R(%d): ", n); -// for(int k=0; k<n; k++) -// printf("%d ", buffer[k]); -// printf("\n\r"); -// } bool alreadyIn = false; for (int i=0; i<(n-2);) { bool found = false; @@ -147,98 +142,14 @@ PortID port = buffer[i++]; PortValue value = 0; memcpy(&value, &buffer[i], sizeof(PortValue)); -// printf("Read[%d] = %d\n\r", port, value); i += sizeof(PortValue); if (ports.find(port)!=ports.end()) ports[port].setRXValue(value); } } - Thread::wait(RXRate); + thread_sleep_for(RXRate); } } -*/ - - -void functionSender (void const* arg); -void functionReceiver (void const* arg); - -RtosTimer *senderTimer; -RtosTimer *receiverTimer; - -bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX) -{ - if (channel!=NULL) - return false; - mutexChannel.lock(); - channel = c; - mutexChannel.unlock(); - senderTimer = new RtosTimer(functionSender, osTimerPeriodic, NULL); - receiverTimer = new RtosTimer(functionReceiver, osTimerPeriodic, NULL); - if (rateTX>0) { - TXRate = 1000/rateTX; - senderTimer->start(TXRate); - } else - TXRate = 0; - if (rateRX>0) { - RXRate = 1000/rateRX; - receiverTimer->start(RXRate); - } else - RXRate = 0; - return true; -} - -void functionSender (void const* arg) -{ - static char buffer[INTERNAL_BUFFER_SIZE]; - int n = 3; - int numFrames = 0; - for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) { - if ((*it).second.isModified()) { - buffer[n++] = (*it).first; - PortValue tmp = (*it).second.getTXValue(); - memcpy(&buffer[n], &tmp, sizeof(PortValue)); - n += sizeof(PortValue); - numFrames++; - } - } - if (numFrames>0) { - buffer[0] = START_0; - buffer[1] = START_1; - buffer[2] = START_2; - if (!mutexChannel.trylock()) - return; - channel->write(buffer, n); - mutexChannel.unlock(); - } -} - -void functionReceiver (void const* arg) -{ - static char buffer[INTERNAL_BUFFER_SIZE]; - if (!mutexChannel.trylock()) - return; - int n = channel->read(buffer); - mutexChannel.unlock(); - bool alreadyIn = false; - for (int i=0; i<(n-2);) { - bool found = false; - if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) { - found = true; - alreadyIn = true; - i += 3; - } - if ((found || alreadyIn) && (n-i)>=(sizeof(PortID)+sizeof(PortValue))) { - PortID port = buffer[i++]; - PortValue value = 0; - memcpy(&value, &buffer[i], sizeof(PortValue)); - i += sizeof(PortValue); - if (ports.find(port)!=ports.end()) - ports[port].setRXValue(value); - } - } -} - - inline bool ssWi_isActive (PortID port)
--- a/ssWi.hpp Thu Mar 07 15:44:05 2013 +0000 +++ b/ssWi.hpp Fri Apr 17 15:19:40 2020 +0000 @@ -29,11 +29,6 @@ */ bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX); -/** \brief disable the communication protocol - * - */ -void ssWi_stop (); - /** \brief check if the communication port is open * * \param port port identified to check
--- a/xbee.lib Thu Mar 07 15:44:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -xbee#1c7f8dfcde17
--- a/xbee/MODSERIAL.lib Thu Mar 07 15:44:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/AjK/code/MODSERIAL/#5c45c21f36b7
--- 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]); } }
--- a/xbee/xbee.hpp Thu Mar 07 15:44:05 2013 +0000 +++ b/xbee/xbee.hpp Fri Apr 17 15:19:40 2020 +0000 @@ -8,8 +8,6 @@ #include "mbed.h" -#include "MODSERIAL.h" - #include <ssWiChannel.hpp> #include "string" @@ -17,23 +15,28 @@ class XBeeAddress { - string low; - string high; + uint32_t low; + uint32_t high; public: - XBeeAddress () : low(""), high("") {} + XBeeAddress () : low(0), high(0) {} - XBeeAddress (string low, string high) { + XBeeAddress (uint32_t low, uint32_t high) { this->low = low; this->high = high; - } + } - string getLowAddr () { + void setAddress(uint32_t low, uint32_t high) { + this->low = low; + this->high = high; + } + + uint32_t getLowAddr () { return low; } - string getHighAddr () { + uint32_t getHighAddr () { return high; } }; @@ -42,14 +45,14 @@ class XBeeBroadcastAddress: public XBeeAddress { public: - XBeeBroadcastAddress () : XBeeAddress("00FFFF", "0") {} + XBeeBroadcastAddress () : XBeeAddress(0x00FFFF, 0) {} }; class XBeeModule: public ssWiChannel { - MODSERIAL xbee; + Serial serial_xbee; XBeeAddress local; bool status; @@ -61,6 +64,8 @@ bool _getLocalAddr (); bool _setChannel (int channel); bool _setPanID (int id); + + bool initSequence(); public: @@ -75,7 +80,7 @@ bool setDstAddress (XBeeAddress addr); - XBeeAddress getDstAddress (); + bool getDstAddress(XBeeAddress &addr); int getChannel ();