class MasterMAC(MAC): """ Master MAC implementation. """ @prepend_method_doc(MAC) def __init__(self, radio, timerMux, schedule, frameHandler=None): """ @param schedule: L{Schedule} object giving transmission scheduling. @param frameHandler: Handler function to call at the end of each frame. The packets received in that frame will be passed as a list. """ MAC.__init__(self, radio, timerMux) self.schedule = schedule self.frameHandler = frameHandler self.frameTimer = VirtualTimer(timerMux, self._frameTimerHandler) self.packets = [] self.frameTimer.start(self.schedule.framePeriod, repeat=True) def _frameTimerHandler(self): if self.frameHandler is not None: self.frameHandler(self.packets) self.packets = [] self.radio.transmit(SyncPacket()) def queuePacket(self, packet): raise NotImplementedError def handleIncomingPacket(self, packet): if isinstance(packet, DataPacket): self.packets.append(packet)
class InterSlaveMAC(SlaveMAC): """ Slave MAC implementation with support for slave-slave packets. """ @prepend_method_doc(SlaveMAC) def __init__(self, radio, timerMux, schedule, id, packetHandler=None): """ @param packetHandler: Handler to call on reception of a slave-slave packet. The packet will be passed as an argument. """ SlaveMAC.__init__(self, radio, timerMux, schedule, id) self.auxTxSlots = self.schedule.slaveAuxTxSlots(self.id) self.auxRxSlots = self.schedule.slaveAuxRxSlots(self.id) self.auxSlotTimer = VirtualTimer(timerMux, self._auxSlotTimerHandler) self.auxPackets = {} self.packetHandler = packetHandler self.radio.channel = schedule.dataChannel def _slotTimerHandler(self): SlaveMAC._slotTimerHandler(self) if self.slot == 0: self.auxSlot = 0 else: self.auxSlot += 1 if self.slot < self.schedule.dataSlots: self.auxSlotTimer.start(self.schedule.dataSlotTime/2) if self.auxSlot in self.auxTxSlots + self.auxRxSlots: self.radio.channel = self.schedule.auxChannel def _auxSlotTimerHandler(self): if self.auxSlot in self.auxTxSlots: packet = self.auxPackets.pop(self.auxSlot, None) if packet is not None: self.radio.transmit(packet) if self.slot + 1 in (self.dataTxSlot, len(self.schedule.dataSlots)): self.radio.channel = self.schedule.dataChannel def queueAuxPacket(self, packet): """ Queue a packet for transmission to another slave. @param packet: L{RadioPacket} to transmit. """ if isinstance(packet, AuxPacket): slot = self.schedule.auxSlot(packet.source, packet.dest) self.auxPackets[slot] = packet else: raise ValueError def handleIncomingPacket(self, packet): SlaveMAC.handleIncomingPacket(self, packet) if isinstance(packet, AuxPacket) and packet.dest == self.id: if self.packetHandler is not None: self.packetHandler(packet)
class InterSlaveMAC(SlaveMAC): """ Slave MAC implementation with support for slave-slave packets. """ @prepend_method_doc(SlaveMAC) def __init__(self, radio, timerMux, schedule, id, packetHandler=None): """ @param packetHandler: Handler to call on reception of a slave-slave packet. The packet will be passed as an argument. """ SlaveMAC.__init__(self, radio, timerMux, schedule, id) self.auxTxSlots = self.schedule.slaveAuxTxSlots(self.id) self.auxRxSlots = self.schedule.slaveAuxRxSlots(self.id) self.auxSlotTimer = VirtualTimer(timerMux, self._auxSlotTimerHandler) self.auxPackets = {} self.packetHandler = packetHandler self.radio.channel = schedule.dataChannel def _slotTimerHandler(self): SlaveMAC._slotTimerHandler(self) if self.slot == 0: self.auxSlot = 0 else: self.auxSlot += 1 if self.slot < self.schedule.dataSlots: self.auxSlotTimer.start(self.schedule.dataSlotTime / 2) if self.auxSlot in self.auxTxSlots + self.auxRxSlots: self.radio.channel = self.schedule.auxChannel def _auxSlotTimerHandler(self): if self.auxSlot in self.auxTxSlots: packet = self.auxPackets.pop(self.auxSlot, None) if packet is not None: self.radio.transmit(packet) if self.slot + 1 in (self.dataTxSlot, len(self.schedule.dataSlots)): self.radio.channel = self.schedule.dataChannel def queueAuxPacket(self, packet): """ Queue a packet for transmission to another slave. @param packet: L{RadioPacket} to transmit. """ if isinstance(packet, AuxPacket): slot = self.schedule.auxSlot(packet.source, packet.dest) self.auxPackets[slot] = packet else: raise ValueError def handleIncomingPacket(self, packet): SlaveMAC.handleIncomingPacket(self, packet) if isinstance(packet, AuxPacket) and packet.dest == self.id: if self.packetHandler is not None: self.packetHandler(packet)
def __init__(self, radio, timerMux, schedule, id, packetHandler=None): """ @param packetHandler: Handler to call on reception of a slave-slave packet. The packet will be passed as an argument. """ SlaveMAC.__init__(self, radio, timerMux, schedule, id) self.auxTxSlots = self.schedule.slaveAuxTxSlots(self.id) self.auxRxSlots = self.schedule.slaveAuxRxSlots(self.id) self.auxSlotTimer = VirtualTimer(timerMux, self._auxSlotTimerHandler) self.auxPackets = {} self.packetHandler = packetHandler self.radio.channel = schedule.dataChannel
def __init__(self, radio, timerMux, schedule, id): """ @param schedule: L{Schedule} object giving transmission scheduling. @param id: The device ID of this node. """ MAC.__init__(self, radio, timerMux) self.schedule = schedule self.id = id self.dataTxSlot = self.schedule.dataTxSlot(self.id) self.slotTimer = VirtualTimer(timerMux, self._slotTimerHandler) self.slot = len(self.schedule.dataSlots) self.txPacket = None
def __init__(self, radio, timerMux, schedule, frameHandler=None): """ @param schedule: L{Schedule} object giving transmission scheduling. @param frameHandler: Handler function to call at the end of each frame. The packets received in that frame will be passed as a list. """ MAC.__init__(self, radio, timerMux) self.schedule = schedule self.frameHandler = frameHandler self.frameTimer = VirtualTimer(timerMux, self._frameTimerHandler) self.packets = [] self.frameTimer.start(self.schedule.framePeriod, repeat=True)
class SlaveMAC(MAC): """ Slave MAC implementation. """ @prepend_method_doc(MAC) def __init__(self, radio, timerMux, schedule, id): """ @param schedule: L{Schedule} object giving transmission scheduling. @param id: The device ID of this node. """ MAC.__init__(self, radio, timerMux) self.schedule = schedule self.id = id self.dataTxSlot = self.schedule.dataTxSlot(self.id) self.slotTimer = VirtualTimer(timerMux, self._slotTimerHandler) self.slot = len(self.schedule.dataSlots) self.txPacket = None def _slotTimerHandler(self): if self.slot == len(self.schedule.dataSlots): # End of sync slot, start of first data slot. self.slot = 0 else: self.slot += 1 if self.slot == len(self.schedule.dataSlots): # Start of sync slot. self.slotTimer.start(self.schedule.syncSlotTime) else: self.slotTimer.start(self.schedule.dataSlotTime) if self.slot == self.dataTxSlot: # Our transmit slot. if self.txPacket is not None: self.radio.transmit(self.txPacket) self.txPacket = None def queuePacket(self, packet): if isinstance(packet, DataPacket): self.txPacket = packet else: raise ValueError def handleIncomingPacket(self, packet): if isinstance(packet, SyncPacket): self.slotTimer.start(self.schedule.syncSlotTime/2)
class SlaveMAC(MAC): """ Slave MAC implementation. """ @prepend_method_doc(MAC) def __init__(self, radio, timerMux, schedule, id): """ @param schedule: L{Schedule} object giving transmission scheduling. @param id: The device ID of this node. """ MAC.__init__(self, radio, timerMux) self.schedule = schedule self.id = id self.dataTxSlot = self.schedule.dataTxSlot(self.id) self.slotTimer = VirtualTimer(timerMux, self._slotTimerHandler) self.slot = len(self.schedule.dataSlots) self.txPacket = None def _slotTimerHandler(self): if self.slot == len(self.schedule.dataSlots): # End of sync slot, start of first data slot. self.slot = 0 else: self.slot += 1 if self.slot == len(self.schedule.dataSlots): # Start of sync slot. self.slotTimer.start(self.schedule.syncSlotTime) else: self.slotTimer.start(self.schedule.dataSlotTime) if self.slot == self.dataTxSlot: # Our transmit slot. if self.txPacket is not None: self.radio.transmit(self.txPacket) self.txPacket = None def queuePacket(self, packet): if isinstance(packet, DataPacket): self.txPacket = packet else: raise ValueError def handleIncomingPacket(self, packet): if isinstance(packet, SyncPacket): self.slotTimer.start(self.schedule.syncSlotTime / 2)
def __init__(self, imu, samplingPeriod, calibration=None, filter=None, sampleCallback=None, initialTime=0): """ Initialise IMU behaviour. @param imu: L{IMU} on which the behaviour executes. @param samplingPeriod: Interval at which to sample and process (float). @param calibration: Mapping from IMU L{Sensor}s to L{SensorCalibration}s (dict). @param filter: L{OrientationFilter} to update with each set of samples. @param sampleCallback: Function to call after each sample. The behaviour object will be passed as a single argument. @param initialTime: Initial time for local timekeeping. """ self.imu = imu self.samplingPeriod = samplingPeriod self.calibration = calibration self.filter = filter for sensor in imu.sensors: sensor.rawMeasurements = TimeSeries() if self.calibration is not None: sensor.calibratedMeasurements = TimeSeries() self.sampleCallback = sampleCallback self.timerMux = TimerMultiplexer(imu.timer) PeriodicSampler(imu.adc, imu.sensors, VirtualTimer(self.timerMux), samplingPeriod, self._handleSample) self._time = initialTime