def test_attach_nonexistent_ring(): # no size given means: attach to existing ring # since not loaded, fail try: r1 = hal.Ring("ring1") raise "should not happen" except NameError: pass
def listener2(): rospy.init_node('listener2', anonymous=True) if "joint_interpolator.traj" in rings: # attach to existing ring global r r = hal.Ring("joint_interpolator.traj") # see what we have print_ring(r) rospy.Subscriber("/joint_path_command", JointTrajectory, callback) rospy.spin()
def listener(): # In ROS, nodes are uniquely named. If two nodes with the same # node are launched, the previous one is kicked off. The # anonymous=True flag means that rospy will choose a unique # name for our 'talker' node so that multiple talkers can # run simultaneously. rospy.init_node('listener', anonymous=True) if "jointpos" in rings: # attach to existing ring global w w = hal.Ring("jointpos") # see what we have print_ring(w) rospy.Subscriber("/joint_path_command", JointTrajectory, callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin()
def multiframe_ring(name): try: r = hal.Ring(name) except RuntimeError: r = hal.Ring(name, size=16384, flags=hal.RINGTYPE_MULTIPART) return (r, hal.MultiframeRing(r))
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
def test_loadrt_ringmods(self): global r1 r1 = hal.Ring("test", size=16384) rt.loadrt("ringread", "ring=test") rt.loadrt("ringwrite", "ring=test") rt.newinst("charge_pump", "charge-pump")
def test_create_ring(): global r1 # size given mean - create existing ring r1 = hal.Ring("ring1", size=4096)
import os, time import random from machinekit import hal from jplan_pb2 import * import google.protobuf.text_format name = "tp.cmd" r = hal.Ring(name) jc = JplanCommand() j = jc.joint.add() # enable and parameterize join 0 j.enable = True # this will override any values from setp in jplan-demo.hal j.max_vel = 1000 j.max_acc = 100 buffer = jc.SerializeToString() # setup message r.write(buffer) # inject random moves on joint 1 while True: jc = JplanCommand() j = jc.joint.add() j.pos_cmd = random.random() buffer = jc.SerializeToString() while not r.write(buffer):
def test_create_ring(): global r, sr r = hal.Ring("ring1", size=size, type=hal.RINGTYPE_STREAM) sr = hal.StreamRing(r)
#!/usr/bin/env python # create or attach to a ring # write a sequence of increasing doubles import os,time,sys from machinekit import hal import struct name = "jointpos" try: # to create the ring r = hal.Ring(name, size=4096) except RuntimeError: # oops, existed already, so just attach r = hal.Ring(name) # Message details: msg_fmt = 'd' msg_size = struct.calcsize(msg_fmt) v = 0.0 while True: if r.available >= msg_size: r.write(struct.pack('d', v)) v += 1.0 time.sleep(1)
import google.protobuf.text_format import pb2json import json # pretty printer class Timeout(Exception): pass timeout = 50.0 interval = 0.1 try: c = hal.Ring("first.in") r = hal.Ring("first.out") except Exception, e: print e r.reader = c.writer = os.getpid() for i in range(10): # construct a protobuf-encoded message container = Container() container.type = MT_MOTCMD motcmd = container.motcmd motcmd.command = EMCMOT_SET_LINE motcmd.commandNum = i pos = motcmd.pos
import os from machinekit import hal name = "streamring" ringsize = 16384 polltime = 0.1 try: r = hal.Ring(name) except RuntimeError, e: r = hal.Ring(name, size=ringsize, flags=hal.RINGTYPE_STREAM) r.writer = os.getpid() sr = hal.StreamRing(r) count = 10 for n in range(count): try: r.write("message %d\n" % n) except RuntimeError, e: print e
# inspect rings # attach to an existing ring and write a few messages import os, time 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
pru=0, num_stepgens=5, num_pwmgens=0, prucode=prubin, halname=card) rt.loadrt('siggen') #rt.newinst("siggen","siggen_0") # load the ring for use with Charles' joint_stream component. # for this you need to have built with catkin_make # https://github.com/cdsteinkuehler/ros_hal_tests.git # and also installed the component `comp --install joint_stream.comp` # newring jointpos 16384 r1 = hal.Ring("jointpos", size=16384) rt.loadrt("joint_stream", "ring=jointpos") # create servo_thread servothread = "servo_thread" # create a new thread rt.newthread('%s' % servothread, 1000000, fp=True) # add components to the thread if hardware == 'mesa-5i20': hal.addf('%s.read' % card, servothread) hal.addf('siggen.0.update', servothread) hal.addf('joint_stream', servothread)
# # 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> while True:
import google.protobuf.text_format import pb2json import json # pretty printer class Timeout(Exception): pass timeout = 50.0 interval = 0.1 try: c = hal.Ring("pbring.0.in") r = hal.Ring("pbring.0.out") except Exception, e: print e r.reader = c.writer = os.getpid() for i in range(10): # construct a protobuf-encoded message container = Container() container.type = MT_MOTCMD motcmd = container.motcmd motcmd.command = EMCMOT_SET_LINE motcmd.commandNum = i pos = motcmd.pos