diff --git a/flopsync_v4/clock.cpp b/flopsync_v4/clock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..038765d01d911ac44d355e6bb1955164b0684432 --- /dev/null +++ b/flopsync_v4/clock.cpp @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "clock.h" + +// +// class Clock +// + +Clock::~Clock() {} diff --git a/flopsync_v4/clock.h b/flopsync_v4/clock.h new file mode 100644 index 0000000000000000000000000000000000000000..1be1fe5695e2237322b6f23a2b3d9ec0faf0b254 --- /dev/null +++ b/flopsync_v4/clock.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef CLOCK_H +#define CLOCK_H + +/** + * Base class from which clocks derive + */ +class Clock +{ +public: + /** + * This member function converts between root frame time, that is, a time + * that starts goes from 0 to nominalPeriod-1 and is referenced to the root + * node, to absolute time referenced to the local node time. It can be used + * to send a packet to the root node at the the frame time it expects it. + * \param root a frame time (0 to nominalPeriod-1) referenced to the root node + * \return the local absolute time time corresponding to the given root time + */ + virtual long long localTime(long long slotGlobalTime)=0; + + virtual long long globalTime(long long slotLocalTime)=0; + + /** + * Destructor + */ + virtual ~Clock(); +}; + +#endif //CLOCK_H \ No newline at end of file diff --git a/flopsync_v4/flooder_root_node.cpp b/flopsync_v4/flooder_root_node.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69d93fc061d06441c3d2ebf78e677d3bd457a30a --- /dev/null +++ b/flopsync_v4/flooder_root_node.cpp @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "flooder_root_node.h" +#include <cstdio> +#include <stdexcept> +#include <miosix.h> + +using namespace std; +using namespace miosix; + +// +// class FlooderRootNode +// + +FlooderRootNode::FlooderRootNode(long long syncPeriod, + unsigned int radioFrequency, + unsigned short panId, short txPower) + : FloodingScheme(syncPeriod), + pm(PowerManager::instance()), + timer(getTransceiverTimer()), + transceiver(Transceiver::instance()), + radioFrequency(radioFrequency), + frameStart(-1), + panId(panId), txPower(txPower), debug(false) +{ + //Minimum ~550us, 200us of slack added + rootNodeWakeupAdvance=timer.ns2tick(750000); +} + +bool FlooderRootNode::synchronize() +{ + if(frameStart>0) + { + frameStart+=syncPeriod; + long long wakeupTime=frameStart-rootNodeWakeupAdvance; + if(timer.getValue()>=wakeupTime) + { + if(debug) puts("FlooderRootNode::synchronize called too late"); + return false; + } + pm.deepSleepUntil(wakeupTime); + } else frameStart=timer.getValue()+rootNodeWakeupAdvance; + + ledOn(); + TransceiverConfiguration cfg(radioFrequency,txPower); + transceiver.configure(cfg); + transceiver.turnOn(); + const unsigned char syncPacket[]= + { + 0x46, //frame type 0b110 (reserved), intra pan + 0x08, //no source addressing, short destination addressing + 0x00, //seq no reused as glossy hop count, 0=root node + static_cast<unsigned char>(panId>>8), + static_cast<unsigned char>(panId & 0xff), //destination pan ID + 0xff, 0xff //destination addr (broadcast) + }; + try { + transceiver.sendAt(syncPacket,sizeof(syncPacket),frameStart); + } catch(exception& e) { + if(debug) puts(e.what()); + } + transceiver.turnOff(); + ledOff(); + + return false; //Root node does not desynchronize +} diff --git a/flopsync_v4/flooder_root_node.h b/flopsync_v4/flooder_root_node.h new file mode 100644 index 0000000000000000000000000000000000000000..76de95a418997a929e8fb9c5b489b8d5773d0976 --- /dev/null +++ b/flopsync_v4/flooder_root_node.h @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef FLOODER_ROOT_NODE_H +#define FLOODER_ROOT_NODE_H + +#include "interfaces-impl/transceiver.h" +#include "interfaces-impl/hardware_timer.h" +#include "interfaces-impl/power_manager.h" +#include "synchronizer.h" +#include "flooding_scheme.h" + +/** + * Glossy flooding scheme, root node + */ +class FlooderRootNode : public FloodingScheme +{ +public: + /** + * Constructor + * \param syncPeriod synchronization period + * \param radioFrequency the radio frequency used for synchronizing + * \param panId pan ID + * \param txPower powr at which the sync packet has to be transmitted + */ + FlooderRootNode(long long syncPeriod, unsigned int radioFrequency, + unsigned short panId, short txPower); + + /** + * Needs to be periodically called to send the synchronization packet. + * This member function sleeps till it's time to send the packet, then + * sends it and returns. If this function isn't called again within + * the synchronization period, the synchronization won't work. + * \return true if the node desynchronized + */ + bool synchronize(); + + /** + * \return The (local) time when the synchronization packet was + * actually received + */ + long long getMeasuredFrameStart() const { return frameStart; } + + /** + * \return The (local) time when the synchronization packet was + * expected to be received + */ + long long getComputedFrameStart() const { return frameStart; } + + /** + * \param enabled if true, this class prints debug data + */ + void debugMode(bool enabled) { debug=enabled; } + +private: + miosix::PowerManager& pm; + miosix::HardwareTimer& timer; + miosix::Transceiver& transceiver; + unsigned int radioFrequency; + long long frameStart; + int rootNodeWakeupAdvance; + unsigned short panId; + short txPower; + bool debug; +}; + +#endif //FLOODER_ROOT_NODE_H diff --git a/flopsync_v4/flooder_sync_node.cpp b/flopsync_v4/flooder_sync_node.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1554037919f0a4c30a515e6d9b1ccfa4650af951 --- /dev/null +++ b/flopsync_v4/flooder_sync_node.cpp @@ -0,0 +1,200 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "flooder_sync_node.h" +#include <cstdio> +#include <cassert> +#include <stdexcept> + +using namespace std; +using namespace miosix; + +// +// class FlooderSyncNode +// + +FlooderSyncNode::FlooderSyncNode(Synchronizer *synchronizer, + long long syncPeriod, + unsigned int radioFrequency, + unsigned short panId, short txPower) + : FloodingScheme(syncPeriod), + pm(PowerManager::instance()), + timer(getTransceiverTimer()), + transceiver(Transceiver::instance()), + synchronizer(synchronizer), + measuredFrameStart(0), computedFrameStart(0), + radioFrequency(radioFrequency), clockCorrection(0), + receiverWindow(synchronizer->getReceiverWindow()), + missPackets(maxMissPackets+1), hop(0), + panId(panId), txPower(txPower), fixedHop(false), debug(false) +{ + //300us and forced receiverWindow=1 fails, keeping this at minimum + syncNodeWakeupAdvance=timer.ns2tick(450000); + //5 byte (4 preamble, 1 SFD) * 32us/byte + packetPreambleTime=timer.ns2tick(160000); + //Time when packet is rebroadcast (time for packet) + //~400us is the minimum to add, added 200us slack + //FIXME: with a so small slack if the packet is received at the end of the + //receiver window there may be no time for rebroadcast + packetRebroadcastTime=timer.ns2tick((syncPacketSize+6)*32000+600000); +} + +bool FlooderSyncNode::synchronize() +{ + if(missPackets>maxMissPackets) return true; + + computedFrameStart+=syncPeriod+clockCorrection; + long long wakeupTime=computedFrameStart-(syncNodeWakeupAdvance+receiverWindow); + long long timeoutTime=computedFrameStart+receiverWindow+packetPreambleTime; + + if(timer.getValue()>=wakeupTime) + { + if(debug) puts("FlooderSyncNode::synchronize called too late"); + ++missPackets; + return false; + } + pm.deepSleepUntil(wakeupTime); + + ledOn(); + //Transceiver configured with non strict timeout + miosix::TransceiverConfiguration cfg(radioFrequency,txPower,true,false); + transceiver.configure(cfg); + transceiver.turnOn(); + unsigned char packet[syncPacketSize]; + short rssi; + + bool timeout=false; + for(;;) + { + try { + auto result=transceiver.recv(packet,syncPacketSize,timeoutTime); + if( result.error==RecvResult::OK && result.timestampValid==true + && result.size==syncPacketSize + && packet[0]==0x46 && packet[1]==0x08 && packet[2]==hop + && packet[3]==static_cast<unsigned char>(panId>>8) + && packet[4]==static_cast<unsigned char>(panId & 0xff) + && packet[5]==0xff && packet[6]==0xff) + { + measuredFrameStart=result.timestamp; + rssi=result.rssi; + break; + } + } catch(exception& e) { + if(debug) puts(e.what()); + break; + } + if(timer.getValue()>=timeoutTime) + { + timeout=true; + break; + } + } + transceiver.idle(); //Save power waiting for rebroadcast time + if(timeout==false) rebroadcast(measuredFrameStart,packet); + transceiver.turnOff(); + ledOff(); + + pair<int,int> r; + if(timeout) + { + if(++missPackets>maxMissPackets) + { + puts("Lost sync"); + return false; + } + r=synchronizer->lostPacket(); + measuredFrameStart=computedFrameStart; + if(debug) printf("miss u=%d w=%d\n",clockCorrection,receiverWindow); + } else { + int e=measuredFrameStart-computedFrameStart; + r=synchronizer->computeCorrection(e); + missPackets=0; + if(debug) printf("e=%d u=%d w=%d rssi=%d\n",e,clockCorrection,receiverWindow,rssi); + } + clockCorrection=r.first; + receiverWindow=r.second; + + //Correct frame start considering hops + measuredFrameStart-=hop*packetRebroadcastTime; + return false; +} + +void FlooderSyncNode::resynchronize() +{ + auto isSyncPacket=[this](RecvResult& result, unsigned char *packet)->bool { + return result.error==RecvResult::OK && result.timestampValid==true + && result.size==syncPacketSize + && packet[0]==0x46 && packet[1]==0x08 + && packet[3]==static_cast<unsigned char>(panId>>8) + && packet[4]==static_cast<unsigned char>(panId & 0xff) + && packet[5]==0xff && packet[6]==0xff; + }; + + if(debug) puts("Resynchronize..."); + synchronizer->reset(); + ledOn(); + miosix::TransceiverConfiguration cfg(radioFrequency,txPower); + transceiver.configure(cfg); + transceiver.turnOn(); + unsigned char packet[syncPacketSize]; + //TODO: attach to strongest signal, not just to the first received packet + for(;;) + { + try { + auto result=transceiver.recv(packet,syncPacketSize,infiniteTimeout); + if(isSyncPacket(result,packet)) + { + computedFrameStart=measuredFrameStart=result.timestamp; + break; + } + } catch(exception& e) { + if(debug) puts(e.what()); + } + } + ledOff(); + clockCorrection=0; + receiverWindow=synchronizer->getReceiverWindow(); + missPackets=0; + hop=packet[2]; + //Correct frame start considering hops + measuredFrameStart-=hop*packetRebroadcastTime; + if(debug) puts("Done."); +} + +void FlooderSyncNode::rebroadcast(long long int receivedTimestamp, + unsigned char* packet) +{ + if(packet[3]>=maxHops-1) return; + packet[3]++; + receivedTimestamp+=packetRebroadcastTime; + try { + transceiver.sendAt(packet,syncPacketSize,receivedTimestamp); + } catch(exception& e) { + if(debug) puts(e.what()); + } +} + diff --git a/flopsync_v4/flooder_sync_node.h b/flopsync_v4/flooder_sync_node.h new file mode 100644 index 0000000000000000000000000000000000000000..e45f80eac30b7d998c6f604a10fa9d13c79bf1e9 --- /dev/null +++ b/flopsync_v4/flooder_sync_node.h @@ -0,0 +1,135 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef FLOODER_SYNC_NODE_H +#define FLOODER_SYNC_NODE_H + +#include "interfaces-impl/transceiver.h" +#include "interfaces-impl/hardware_timer.h" +#include "interfaces-impl/power_manager.h" +#include "synchronizer.h" +#include "flooding_scheme.h" +#include <miosix.h> +#include <cstring> + +/** + * Glossy flooding scheme, generic synchronized node + */ +class FlooderSyncNode : public FloodingScheme +{ +public: + /** + * Constructor + * \param synchronizer pointer to synchronizer + * \param syncPeriod synchronization period + * \param radioFrequency the radio frequency used for synchronizing + * \param panId pan ID + * \param txPower powr at which the sync packet has to be transmitted + */ + FlooderSyncNode(Synchronizer *synchronizer, long long syncPeriod, + unsigned int radioFrequency, unsigned short panId, + short txPower); + + /** + * Needs to be periodically called to send the synchronization packet. + * This member function sleeps till it's time to send the packet, then + * sends it and returns. If this function isn't called again within + * nominalPeriod, the synchronization won't work. + * \return true if the node desynchronized + */ + bool synchronize(); + + /** + * Called if synchronization is lost + */ + void resynchronize(); + + /** + * \return The (local) time when the synchronization packet was + * actually received + */ + long long getMeasuredFrameStart() const { return measuredFrameStart; } + + /** + * \return The (local) time when the synchronization packet was + * expected to be received + */ + long long getComputedFrameStart() const + { + return computedFrameStart-hop*packetRebroadcastTime; + } + + /** + * \return true if in this frame the sync packet was not received + */ + bool isPacketMissed() const { return missPackets>0; } + + /** + * Force the node to belong to a given hop + * \param hop hop to which the node should belong, or 0 to restore the + * default behaviour of automatically joining an hop + */ + void forceHop(unsigned char hop) { this->hop=hop; fixedHop= hop!=0; } + + /** + * \param enabled if true, this class prints debug data + */ + void debugMode(bool enabled) { debug=enabled; } + +private: + /** + * Rebroadcst the synchronization packet using glossy + * \param receivedTimestamp the timestamp when the packet was received + * \param packet the received packet + */ + void rebroadcast(long long receivedTimestamp, unsigned char *packet); + + miosix::PowerManager& pm; + miosix::HardwareTimer& timer; + miosix::Transceiver& transceiver; + Synchronizer *synchronizer; + long long measuredFrameStart; + long long computedFrameStart; + unsigned int radioFrequency; + int clockCorrection; + int receiverWindow; + int syncNodeWakeupAdvance; + int packetPreambleTime; + int packetRebroadcastTime; + unsigned char missPackets; + unsigned char hop; //Hop from which we receive packets (we belong to hop+1) + unsigned short panId; + short txPower; + bool fixedHop; + bool debug; + + static const unsigned char maxHops=20; + static const unsigned char maxMissPackets=3; + static const int syncPacketSize=7; +}; + +#endif //FLOODER_SYNC_NODE_H diff --git a/flopsync_v4/flooding_scheme.cpp b/flopsync_v4/flooding_scheme.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d4e25da45bf82dc4dffe75d12032fd86f040160 --- /dev/null +++ b/flopsync_v4/flooding_scheme.cpp @@ -0,0 +1,36 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "flooding_scheme.h" + +// +// class FloodingScheme +// + +void FloodingScheme::resynchronize() {} + +FloodingScheme::~FloodingScheme() {} diff --git a/flopsync_v4/flooding_scheme.h b/flopsync_v4/flooding_scheme.h new file mode 100644 index 0000000000000000000000000000000000000000..2b1b980a3e5bd445c4b7a0d6fb2226a733a35c16 --- /dev/null +++ b/flopsync_v4/flooding_scheme.h @@ -0,0 +1,83 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef FLOODING_SCHEME_H +#define FLOODING_SCHEME_H + +/** + * Base class from which flooding schemes derive + */ +class FloodingScheme +{ +public: + /** + * Constructor + * \param syncPeriod synchronization period + */ + FloodingScheme(long long syncPeriod) : syncPeriod(syncPeriod) {} + + /** + * Needs to be periodically called to send/receive the synchronization + * packet. This member function sleeps till it's time to send the packet, + * then sends it and returns. If this function isn't called again within + * the synchronization period, the synchronization won't work. + * \return true if the node desynchronized + */ + virtual bool synchronize()=0; + + /** + * Called if synchronization is lost + */ + virtual void resynchronize(); + + /** + * \return The (local) time when the synchronization packet was + * actually received + */ + virtual long long getMeasuredFrameStart() const=0; + + /** + * \return The (local) time when the synchronization packet was + * expected to be received + */ + virtual long long getComputedFrameStart() const=0; + + /** + * \return synchronization period + */ + long long getSyncPeriod() const { return syncPeriod; } + + /** + * Destructor + */ + virtual ~FloodingScheme(); + +protected: + const long long syncPeriod; +}; + +#endif //FLOODING_SCHEME_H diff --git a/flopsync_v4/flopsync1.cpp b/flopsync_v4/flopsync1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..249533ee7693377d48167dc47e41bf88269f9d4c --- /dev/null +++ b/flopsync_v4/flopsync1.cpp @@ -0,0 +1,111 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "flopsync1.h" +#include "interfaces-impl/hardware_timer.h" +#include <algorithm> + +using namespace std; + +#define SWITCHED + +// +// class Flopsync1 +// + +Flopsync1::Flopsync1() +{ + auto& timer=miosix::getTransceiverTimer(); + wMin=timer.ns2tick( 100000)/scaleFactor; //100us + wMax=timer.ns2tick(6000000)/scaleFactor; //6ms + reset(); +} + +pair<int,int> Flopsync1::computeCorrection(int e) +{ + //u(k)=u(k-1)+1.375*e(k)-e(k-1) + int u=uo+11*e-8*eo; + + //The controller output needs to be quantized, but instead of simply + //doing u/8 which rounds towards the lowest number use a slightly more + //advanced algorithm to round towards the closest one, as when the error + //is close to +/-1 timer tick this makes a significant difference. + int sign=u>=0 ? 1 : -1; + int uquant=(u+4*sign)/8; + + #ifdef SWITCHED + //Adaptive state quantization, while the output always needs to be + //quantized, the state is only quantized if the error is zero + uo= e==0 ? 8*uquant : u; + #else //SWITCHED + uo=u; + #endif //SWITCHED + eo=e; + + e/=scaleFactor; + + //Update receiver window size + sum+=e*fp; + squareSum+=e*e*fp; + if(++count>=numSamples) + { + //Variance computed as E[X^2]-E[X]^2 + int average=sum/numSamples; + int var=squareSum/numSamples-average*average/fp; + //Using the Babylonian method to approximate square root + int stddev=var/7; + for(int j=0;j<3;j++) if(stddev>0) stddev=(stddev+var*fp/stddev)/2; + //Set the window size to three sigma + int winSize=stddev*3/fp; + //Clamp between min and max window + dw=max<int>(min<int>(winSize,wMax),wMin); + sum=squareSum=count=0; + } + + return make_pair(uquant,scaleFactor*dw); +} + +pair<int,int> Flopsync1::lostPacket() +{ + //Double receiver window on packet loss, still clamped to max value + dw=min<int>(2*dw,wMax); + return make_pair(getClockCorrection(),scaleFactor*dw); +} + +void Flopsync1::reset() +{ + uo=eo=sum=squareSum=count=0; + dw=wMax; +} + +int Flopsync1::getClockCorrection() const +{ + //Error measure is unavailable if the packet is lost, the best we can + //do is to reuse the past correction value + int sign=uo>=0 ? 1 : -1; + return (uo+4*sign)/8; +} diff --git a/flopsync_v4/flopsync1.h b/flopsync_v4/flopsync1.h new file mode 100644 index 0000000000000000000000000000000000000000..7102a8f7c849952373bb3c62d9b6550c60640b6d --- /dev/null +++ b/flopsync_v4/flopsync1.h @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef CONTROLLER_FLOPSYNC_H +#define CONTROLLER_FLOPSYNC_H + +#include "synchronizer.h" + +/** + * Old flopsync controller. + */ +class Flopsync1 : public Synchronizer +{ +public: + /** + * Constructor + */ + Flopsync1(); + + /** + * Compute clock correction and receiver window given synchronization error + * \param e synchronization error + * \return a pair with the clock correction, and the receiver window + */ + std::pair<int,int> computeCorrection(int e); + + /** + * Compute clock correction and receiver window when a packet is lost + * \return a pair with the clock correction, and the receiver window + */ + std::pair<int,int> lostPacket(); + + /** + * Used after a resynchronization to reset the controller state + */ + void reset(); + + /** + * \return the synchronization error e(k) + */ + int getSyncError() const { return eo; } + + /** + * \return the clock correction u(k) + */ + int getClockCorrection() const; + + /** + * \return the receiver window (w) + */ + int getReceiverWindow() const { return scaleFactor*dw; } + +private: + int uo; + int sum; + int squareSum; + short eo; + unsigned char count; + unsigned char dw; + int wMin; + int wMax; + + static const int numSamples=8; //Number of samples for variance compuation + static const int fp=64; //Fixed point, log2(fp) bits are the decimal part + #ifndef USE_VHT + static const int scaleFactor=1; + #else //USE_VHT + //The maximum value that can enter the window computation algorithm without + //without causing overflows is around 700, resulting in a scaleFactor of + //5 when the vht resolution is 1us, and w is 3ms. That however would cause + //overflow when writing the result to dw, which is just an unsigned char + //(to save RAM). This requires a higher scale factor, of about w/255, or 12. + //However, this requires more iterations to approximate the square root, + //so we're using a scale factor of 30. + static const int scaleFactor=480; + #endif //USE_VHT +}; + +#endif //CONTROLLER_FLOPSYNC_H diff --git a/flopsync_v4/flopsync2.cpp b/flopsync_v4/flopsync2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80b60093356b5e19512ee243fb6d59ea1c56543f --- /dev/null +++ b/flopsync_v4/flopsync2.cpp @@ -0,0 +1,124 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "flopsync2.h" +#include "interfaces-impl/hardware_timer.h" +#include <algorithm> + +using namespace std; + +// +// class OptimizedRampFlopsync2 +// + +Flopsync2::Flopsync2() +{ + auto& timer=miosix::getTransceiverTimer(); + wMin=timer.ns2tick( 100000)/scaleFactor; //100us + wMax=timer.ns2tick(6000000)/scaleFactor; //6ms + reset(); +} + +pair<int,int> Flopsync2::computeCorrection(int e) +{ + //Controller preinit, for fast boot convergence + switch(init) + { + case 0: + init=1; + eo=e; + uo=2*512*e; + uoo=512*e; + return make_pair(2*e,wMax*scaleFactor); //One step of a deadbeat controller + case 1: + init=2; + eo=0; + uo/=2; + } + + //Flopsync controller, with alpha=3/8 + //u(k)=2u(k-1)-u(k-2)+1.875e(k)-2.578125e(k-1)+0.947265625e(k-2) with values kept multiplied by 512 + int u=2*uo-uoo+960*e-1320*eo+485*eoo; + uoo=uo; + uo=u; + eoo=eo; + eo=e; + + int sign=u>=0 ? 1 : -1; + int uquant=(u+256*sign)/512; + + //Scale numbers if VHT is enabled to prevent overflows + e/=scaleFactor; + + //Update receiver window size + sum+=e*fp; + squareSum+=e*e*fp; + if(++count>=numSamples) + { + //Variance computed as E[X^2]-E[X]^2 + int average=sum/numSamples; + int var=squareSum/numSamples-average*average/fp; + //Using the Babylonian method to approximate square root + int stddev=var/7; + for(int j=0;j<3;j++) if(stddev>0) stddev=(stddev+var*fp/stddev)/2; + //Set the window size to three sigma + int winSize=stddev*3/fp; + //Clamp between min and max window + dw=max<int>(min<int>(winSize,wMax),wMin); + sum=squareSum=count=0; + } + + return make_pair(uquant,scaleFactor*dw); +} + +pair<int,int> Flopsync2::lostPacket() +{ + if(init==1) + { + init=2; + eo=0; + uo/=2; + } + //Double receiver window on packet loss, still clamped to max value + dw=min<int>(4*dw,wMax); + return make_pair(getClockCorrection(),scaleFactor*dw); +} + +void Flopsync2::reset() +{ + uo=uoo=eo=eoo=sum=squareSum=count=0; + dw=wMax; + init=0; +} + +int Flopsync2::getClockCorrection() const +{ + //Error measure is unavailable if the packet is lost, the best we can + //do is to reuse the past correction value + int sign=uo>=0 ? 1 : -1; + return (uo+256*sign)/512; +} diff --git a/flopsync_v4/flopsync2.h b/flopsync_v4/flopsync2.h new file mode 100644 index 0000000000000000000000000000000000000000..5ef1ff5befa47aebc2ba0872ee8ff0e278633afb --- /dev/null +++ b/flopsync_v4/flopsync2.h @@ -0,0 +1,109 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef OPTIMIZED_RAMP_FLOPSYNC_2_H +#define OPTIMIZED_RAMP_FLOPSYNC_2_H + +#include "synchronizer.h" + +/** + * A new flopsync controller that can reach zero steady-state error both + * with step-like and ramp-like disturbances. + * It provides better synchronization under temperature changes, that occur + * so slowly with respect to the controller operation to look like ramp + * changes in clock skew. + */ +class Flopsync2 : public Synchronizer +{ +public: + /** + * Constructor + */ + Flopsync2(); + + /** + * Compute clock correction and receiver window given synchronization error + * \param e synchronization error + * \return a pair with the clock correction, and the receiver window + */ + std::pair<int,int> computeCorrection(int e); + + /** + * Compute clock correction and receiver window when a packet is lost + * \return a pair with the clock correction, and the receiver window + */ + std::pair<int,int> lostPacket(); + + /** + * Used after a resynchronization to reset the controller state + */ + void reset(); + + /** + * \return the synchronization error e(k) + */ + int getSyncError() const { return eo; } + + /** + * \return the clock correction u(k) + */ + int getClockCorrection() const; + + /** + * \return the receiver window (w) + */ + int getReceiverWindow() const { return scaleFactor*dw; } + +private: + int uo, uoo; + int sum; + int squareSum; + short eo, eoo; + unsigned char count; + unsigned char dw; + char init; + int wMin; + int wMax; + + static const int numSamples=8; //Number of samples for variance compuation + static const int fp=64; //Fixed point, log2(fp) bits are the decimal part + #ifndef USE_VHT + static const int scaleFactor=1; + #else //USE_VHT + //The maximum value that can enter the window computation algorithm without + //without causing overflows is around 700, resulting in a scaleFactor of + //5 when the vht resolution is 1us, and w is 3ms. That however would cause + //overflow when writing the result to dw, which is just an unsigned char + //(to save RAM). This requires a higher scale factor, of about w/255, or 12. + //However, this requires more iterations to approximate the square root, + //so we're using a scale factor of 30. + static const int scaleFactor=480; + #endif //USE_VHT +}; + +#endif //OPTIMIZED_RAMP_FLOPSYNC_2_H + diff --git a/flopsync_v4/monotonic_clock.cpp b/flopsync_v4/monotonic_clock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..431543ffc778e8b4d3e20542fd65f3baef73810e --- /dev/null +++ b/flopsync_v4/monotonic_clock.cpp @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "monotonic_clock.h" +#include <algorithm> + +using namespace std; + +// +// class MonotonicClock +// + +// ti=tie(k) + (t-t(k)) * (T+ui(k))/T = tie(k) + sgt +sgt*ui(k)/T +// slotGlobalTime =(t-t(k)) +long long MonotonicClock::localTime(long long slotGlobalTime) +{ + long long T=flood.getSyncPeriod(); + int ui=sync.getClockCorrection(); + unsigned long long tie=flood.getComputedFrameStart(); + //Round towards closest + long long correction = slotGlobalTime*ui; + int sign=correction>=0 ? 1 : -1; + long long dividedCorrection=(correction+(sign*T/2))/T; //Round towards closest + return (tie + max(0LL,slotGlobalTime+dividedCorrection)); + + +// long long signSlotGlobalTime = slotGlobalTime; //Conversion unsigned to signed is *required* +// long long T=nominalPeriod; //Conversion unsigned to signed is *required* +// int ui=sync.getClockCorrection(); +// int ei=flood.getMeasuredFrameStart()-flood.getComputedFrameStart(); +// unsigned long long tia=flood.getMeasuredFrameStart(); +// //Round towards closest +// long long correction = slotGlobalTime*(ui-ei); +// int sign=correction>=0 ? 1 : -1; +// long long dividedCorrection=(correction+(sign*T/2))/T; //Round towards closest +// return (tia + max(0ll,signSlotGlobalTime+dividedCorrection)); + +} +//t=t(k)+(ti-tie(k))*T/(T+ui(k)) +//slotLocalTime =(ti-tie(k)) +long long MonotonicClock::globalTime(long long slotLocalTime) +{ +// #ifdef SEND_TIMESTAMPS +// Can't have a monotonic clock with a sync scheme that overwrites the RTC +// assert(sync.overwritesHardwareClock()==false); +// #endif //SEND_TIMESTAMPS +// unsigned long long T=nominalPeriod; +// int ui=sync.getClockCorrection(); +// unsigned long long t=flood.getMeasuredFrameStart(); //FIXME +// return (t + (slotLocalTime * T)/(T+ui)); + return 0; +} \ No newline at end of file diff --git a/flopsync_v4/monotonic_clock.h b/flopsync_v4/monotonic_clock.h new file mode 100644 index 0000000000000000000000000000000000000000..7f9a9490a3cbc4e6ee5a4d61e9914f185a5d3f44 --- /dev/null +++ b/flopsync_v4/monotonic_clock.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef MONOTONIC_CLOCK_H +#define MONOTONIC_CLOCK_H + +#include "clock.h" +#include "synchronizer.h" +#include "flooding_scheme.h" +#include <cassert> + +/** + * A clock that is guaranteed to be monotonic + */ +class MonotonicClock : public Clock +{ +public: + /** + * Constructor + * \param fs the flopsynch controller + * \param flood the flooding scheme + */ + MonotonicClock(const Synchronizer& sync, const FloodingScheme& flood) + : sync(sync), flood(flood) {} + + /** + * This member function converts between root frame time, that is, a time + * that starts goes from 0 to nominalPeriod-1 and is referenced to the root + * node, to absolute time referenced to the local node time. It can be used + * to send a packet to the root node at the the frame time it expects it. + * \param root a frame time (0 to nominalPeriod-1) referenced to the root node + * \return the local absolute time time corresponding to the given root time + */ + long long localTime(long long slotGlobalTime); + + long long globalTime(long long slotLocalTime); + +private: + const Synchronizer& sync; + const FloodingScheme& flood; +}; + +#endif //MONOTONIC_CLOCK_H \ No newline at end of file diff --git a/flopsync_v4/synchronizer.cpp b/flopsync_v4/synchronizer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdc3f2d38bd21697dc2fa751a5b6e178ea54a8fc --- /dev/null +++ b/flopsync_v4/synchronizer.cpp @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "synchronizer.h" + +// +// class Synchronizer +// + +Synchronizer::~Synchronizer() {} diff --git a/flopsync_v4/synchronizer.h b/flopsync_v4/synchronizer.h new file mode 100644 index 0000000000000000000000000000000000000000..080f78b7783c51fd279714de901dcd7ece602235 --- /dev/null +++ b/flopsync_v4/synchronizer.h @@ -0,0 +1,77 @@ +/*************************************************************************** + * Copyright (C) 2013 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef SYNCHRONIZER_H +#define SYNCHRONIZER_H +#include <utility> + +/** + * Base class from which synchronization schemes derive + */ +class Synchronizer +{ +public: + /** + * Compute clock correction and receiver window given synchronization error + * \param e synchronization error + * \return a pair with the clock correction, and the receiver window + */ + virtual std::pair<int,int> computeCorrection(int e)=0; + + /** + * Compute clock correction and receiver window when a packet is lost + * \return a pair with the clock correction, and the receiver window + */ + virtual std::pair<int,int> lostPacket()=0; + + /** + * Used after a resynchronization to reset the controller state + */ + virtual void reset()=0; + + /** + * \return the synchronization error e(k) + */ + virtual int getSyncError() const=0; + + /** + * \return the clock correction u(k) + */ + virtual int getClockCorrection() const=0; + + /** + * \return the receiver window (w) + */ + virtual int getReceiverWindow() const=0; + + /** + * Destructor + */ + virtual ~Synchronizer(); +}; + +#endif //SYNCHRONIZER_H