import sys,os,time from machinekit import hal name = "ring1" ringsize = 16384 polltime = 0.1 if name in hal.rings(): r = hal.Ring(name) else: r = hal.Ring(name, size=ringsize) r.writer = os.getpid() mf = hal.MultiframeRing(r) try: for msg in range(100): for frame in range(3): mf.write("msg%d-frame%d" % (msg,frame), frame + 4711) mf.flush() time.sleep(0.2) except KeyboardInterrupt: r.writer = 0
from machinekit import hal # print ring properties def print_ring(r): print "name=%s size=%d reader=%d writer=%d scratchpad=%d" % (r.name, r.size, r.reader, r.writer, r.scratchpad_size), print "use_rmutex=%d use_wmutex=%d type=%d in_halmem=%d" % (r.rmutex_mode, r.wmutex_mode, r.type, r.in_halmem) halring = hal.Ring("halmemring%d" % os.getpid(), size=4096, in_halmem=True) shmring = hal.Ring("shmsegring%d" % os.getpid(), size=4096) print_ring(halring) print_ring(shmring) # retrieve list of ring names rings = hal.rings() print "rings: ", rings if "ring_0" in rings: # attach to existing ring w = hal.Ring("ring_0") # see what we have print_ring(w) if w.writer == 0: # not in use write-side # mark as 'writer side in use by <pid>' w.writer = os.getpid() print "max msgsize: ", w.available
# print ring properties def print_ring(r): print 'information on the ring:' print ' - name=%s \n\ - size=%d \n\ - reader=%d \n\ - writer=%d \n\ - scratchpad=%d\n' % (r.name, r.size, r.reader, r.writer, r.scratchpad_size), print ' - use_rmutex=%d\n\ - use_wmutex=%d \n\ - type=%d \n\ - in_halmem=%d' % (r.rmutex_mode, r.wmutex_mode, r.type, r.in_halmem) # retrieve list of ring names rings = hal.rings() print '\navailable rings in HAL: \n ', rings, '\n' # Global variable for ring handle r = 0 n = 0 #p = 0 #tp = None def callback(data): print 'In callback' global n global r prev_point_time = 0.0
# python zmqringsub.py tcp://127.0.0.1:5555 # # adapted from: https://github.com/zeromq/pyzmq/tree/master/examples/pubsub import os,sys import time import zmq from machinekit import hal debug = 1 ip = "127.0.0.1" uri = "tcp://%s:5555" % (ip) ringlist = [] for name in hal.rings(): r = hal.Ring(name) # no size parameter: attach to existing ring if debug: print "inspecting ring %s: reader=%d writer=%d " % (name,r.reader,r.writer) if r.reader == 0 and r.writer: ringlist.append((name,r)) if debug: print "reading from ring",name if not len(ringlist): print "no readable rings found" sys.exit(1) context = zmq.Context() s = context.socket(zmq.PUB) s.bind(uri) # poll rings and publish messages under topic <ringname>