Skip to content
Snippets Groups Projects
Commit cd579568 authored by Martin Teichmann's avatar Martin Teichmann
Browse files

support an analog output

parent c10bf540
No related branches found
No related tags found
No related merge requests found
...@@ -13,3 +13,17 @@ class AnalogInput(Device): ...@@ -13,3 +13,17 @@ class AnalogInput(Device):
def update(self): def update(self):
self.value = self.data self.value = self.data
class AnalogOutput(Device):
value = DeviceVar()
data = TerminalVar()
def __init__(self, data):
self.data = data
def program(self):
self.data = self.value
def update(self):
self.data = self.value
from asyncio import ensure_future, gather, sleep from asyncio import ensure_future, gather, sleep
from struct import pack, unpack, calcsize from struct import pack, unpack, calcsize, pack_into, unpack_from
from time import time from time import time
from .arraymap import ArrayMap, ArrayGlobalVarDesc from .arraymap import ArrayMap, ArrayGlobalVarDesc
from .ethercat import ECCmd, EtherCat, Packet, Terminal from .ethercat import ECCmd, EtherCat, Packet, Terminal
...@@ -38,8 +38,14 @@ class TerminalVar(MemoryDesc): ...@@ -38,8 +38,14 @@ class TerminalVar(MemoryDesc):
self.terminal = value.terminal self.terminal = value.terminal
self.position = value.desc.position self.position = value.desc.position
instance.__dict__[self.name] = value instance.__dict__[self.name] = value
else: elif instance.sync_group.current_data is None:
super().__set__(instance.sync_group, value) super().__set__(instance.sync_group, value)
else:
pv = instance.__dict__.get(self.name)
data = instance.sync_group.current_data
start = self.terminal.bases[self.position[0]] + self.position[1]
fmt = "<" + pv.desc.size
pack_into(fmt, data, start, value)
def __get__(self, instance, owner): def __get__(self, instance, owner):
if instance is None: if instance is None:
...@@ -53,7 +59,7 @@ class TerminalVar(MemoryDesc): ...@@ -53,7 +59,7 @@ class TerminalVar(MemoryDesc):
data = instance.sync_group.current_data data = instance.sync_group.current_data
start = self.terminal.bases[self.position[0]] + self.position[1] start = self.terminal.bases[self.position[0]] + self.position[1]
fmt = "<" + pv.desc.size fmt = "<" + pv.desc.size
return unpack(fmt, data[start:start+calcsize(fmt)])[0] return unpack_from(fmt, data, start)[0]
def __set_name__(self, name, owner): def __set_name__(self, name, owner):
self.name = name self.name = name
...@@ -203,9 +209,11 @@ class SyncGroup: ...@@ -203,9 +209,11 @@ class SyncGroup:
async def run(self): async def run(self):
await gather(*[t.to_operational() for t in self.terminals]) await gather(*[t.to_operational() for t in self.terminals])
self.current_data = self.asm_packet
while True: while True:
self.ec.send_packet(self.asm_packet) self.ec.send_packet(self.current_data)
self.current_data = await self.ec.receive_index(self.packet_index) data = await self.ec.receive_index(self.packet_index)
self.current_data = bytearray(data)
for dev in self.devices: for dev in self.devices:
dev.update() dev.update()
......
...@@ -5,6 +5,13 @@ class Generic(EBPFTerminal): ...@@ -5,6 +5,13 @@ class Generic(EBPFTerminal):
pass pass
class EL4104(EBPFTerminal):
ch1_value = PacketDesc((1, 0), 'H')
ch2_value = PacketDesc((1, 2), 'H')
ch3_value = PacketDesc((1, 4), 'H')
ch4_value = PacketDesc((1, 6), 'H')
class EL3164(EBPFTerminal): class EL3164(EBPFTerminal):
ch1_attrs = PacketDesc((0, 0), 'H') ch1_attrs = PacketDesc((0, 0), 'H')
ch2_attrs = PacketDesc((0, 4), 'H') ch2_attrs = PacketDesc((0, 4), 'H')
......
from asyncio import gather, sleep, ensure_future from asyncio import gather, sleep, ensure_future
from .terminals import EL3164, Generic from .terminals import EL3164, EL4104, Generic
from .devices import AnalogInput from .devices import AnalogInput, AnalogOutput
from .ebpfcat import FastEtherCat, FastSyncGroup, SyncGroup from .ebpfcat import FastEtherCat, FastSyncGroup, SyncGroup
tdigi = Generic() tdigi = Generic()
tout = Generic() tout = EL4104()
tin = EL3164() tin = EL3164()
ec = FastEtherCat("eth0", [tdigi, tin, tout]) ec = FastEtherCat("eth0", [tdigi, tin, tout])
...@@ -22,15 +22,19 @@ async def main(): ...@@ -22,15 +22,19 @@ async def main():
#ensure_future(monitor(ec)) #ensure_future(monitor(ec))
ai = AnalogInput(tin.ch1_value) ai = AnalogInput(tin.ch1_value)
fsg = FastSyncGroup(ec, [ai]) ao = AnalogOutput(tout.ch1_value)
#fsg = SyncGroup(ec, [ai]) #fsg = FastSyncGroup(ec, [ai])
fsg = SyncGroup(ec, [ai, ao])
ao.value = 0
fsg.start() fsg.start()
for i in range(10): for i in range(10):
await sleep(0.1) await sleep(0.1)
fsg.properties.read() #fsg.properties.read()
print(i, ai.value, ec.ebpf.count, ec.ebpf.allcount) ao.value = 3000 * i
print(i, ai.value, ao.data, await tout.get_state())
if __name__ == "__main__": if __name__ == "__main__":
from asyncio import get_event_loop from asyncio import get_event_loop
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment