def gen_halcomp(name, topic, msgtype, dir): ''' define a HAL component whose pins match msg field names and types ''' c = hal.Component(name) c.newpin('test2', hal.HAL_FLOAT, hal.HAL_IN) msg = msgtype() pins = dict() for i in range(len(msg.__slots__)): # check the attributes for types and if they are of an array type # then iterate over them, if not, than ignore them. fname = msg.__slots__[i] ftype = msg._slot_types[i] if ftype in arraytypes: #iterate an array of supported types for j in range(len(fname)): #ptype = fname._slot_types[j]#arraytypes[ftype] #print("pin type = %s") % (ptype) pname = "%s.%s.%s" % (topic, fname, j) print("pin name = %s") % (pname) pins[pname] = c.newpin(pname, hal.HAL_FLOAT, dir) else: if not ftype in pintypes: print("type %s not supported... ignoring this type") % ftype else: pname = "%s.%s" % (topic, fname) print("pin name = %s") % (pname) pins[pname] = c.newpin(pname, pintypes[ftype], dir) print("c.ready()") c.ready() return pins
def test_inst(self): self.cfg = ConfigParser() self.cfg.read(os.getenv("MACHINEKIT_INI")) self.uuid = self.cfg.get("MACHINEKIT", "MKUUID") rt = rtapi.RTAPIcommand(uuid=self.uuid) rt.loadrt("icomp") rt.newinst("icomp", "foo") assert len(hal.instances) == 1 rt.newinst("icomp", "bar") assert len(hal.instances) == 2 rt.delinst("foo") assert len(hal.instances) == 1 with pytest.raises(RuntimeError): # HAL error: duplicate component name 'icomp' c = hal.Component("icomp") c = hal.components["icomp"] for name in hal.instances: inst = hal.instances[name] assert c.id == inst.owner_id assert c.name == inst.owner.name assert "foo" in hal.instances assert "bar" in hal.instances assert hal.instances["foo"].size > 0 assert hal.instances["bar"].size > 0 try: x = hal.instances["nonexistent"] raise "should not happen" except NameError: pass
def setUp(self): c1 = hal.Component("c1") self.s32out = c1.newpin("s32out", hal.HAL_S32, hal.HAL_OUT, init=42) self.s32in = c1.newpin("s32in", hal.HAL_S32, hal.HAL_IN, init=42) self.s32io = c1.newpin("s32io", hal.HAL_S32, hal.HAL_IO, init=42) self.u32out = c1.newpin("u32out", hal.HAL_U32, hal.HAL_OUT, init=123) self.u32in = c1.newpin("u32in", hal.HAL_U32, hal.HAL_IN, init=123) self.u32io = c1.newpin("u32io", hal.HAL_U32, hal.HAL_IO, init=123) self.floatout = c1.newpin("floatout", hal.HAL_FLOAT, hal.HAL_OUT, init=3.14) self.floatin = c1.newpin("floatin", hal.HAL_FLOAT, hal.HAL_IN, init=3.14) self.floatio = c1.newpin("floatio", hal.HAL_FLOAT, hal.HAL_IO, init=3.14) self.bitout = c1.newpin("bitout", hal.HAL_BIT, hal.HAL_OUT, init=True) self.bitin = c1.newpin("bitin", hal.HAL_BIT, hal.HAL_IN, init=True) self.bitio = c1.newpin("bitio", hal.HAL_BIT, hal.HAL_IO, init=True) c1.ready() self.c1 = c1
def gen_halcomp(cname, prefix, msgtype, dir, count): ''' define a HAL component whose pins match msg field names and types ''' global c c = hal.Component(cname) msg = msgtype() pins = dict() for i in range(len(msg.__slots__)): fname = msg.__slots__[i] ftype = msg._slot_types[i] pinlist = list() if ftype in arraytypes: for i in range(count): pname = "%s.%s.%d" % (prefix, fname, i) pinlist.append(c.newpin(pname, typemap[arraytypes[ftype]], dir)) pins[fname] = pinlist continue if not ftype in typemap: print("ignoring type: %s") % (ftype) pins[fname] = None else: pname = "%s.%s" % (prefix, fname) pins[fname] = c.newpin(pname, typemap[ftype], dir) c.ready() return pins
def setUp(self): self.cfg = ConfigParser.ConfigParser() self.cfg.read(os.getenv("MACHINEKIT_INI")) self.uuid = self.cfg.get("MACHINEKIT", "MKUUID") rt = rtapi.RTAPIcommand(uuid=self.uuid) rt.loadrt("icomp") rt.newinst("icomp", "foo") assert len(instances) == 1 rt.newinst("icomp", "bar") assert len(instances) == 2 rt.delinst("foo") assert len(instances) == 1 c = hal.Component("icomp") for i in instances: assert c.id == i.owner_id assert c.name == i.owner().name assert "foo" in instances assert "bar" in instances assert instances["foo"].size > 0 assert instances["bar"].size > 0 try: x = instances["nonexistent"] raise "should not happen" except NameError: pass
def gen_halcomp(cname, prefix, msgtype, dir, count): ''' define a HAL component whose pins match msg field names and types ''' c = hal.Component(cname) msg = msgtype() pins = dict() for i in range(len(msg.__slots__)): fname = msg.__slots__[i] ftype = msg._slot_types[i] if ftype in arraytypes: pinlist = list() for i in range(count): pname = "%s.%s.%d" % (prefix, fname, i) pinlist.append(c.newpin(pname, typemap[arraytypes[ftype]], dir)) pins[fname] = pinlist continue if not ftype in typemap: raise Exception, "type %s not supported" % ftype pname = "%s.%s" % (prefix, fname) pins[fname] = c.newpin(pname, typemap[ftype], dir) c.ready() return pins
def test_component_creation(): global c1, c2 c1 = hal.Component("c1") c1.newpin("s32out", hal.HAL_S32, hal.HAL_OUT, init=42) c1.newpin("s32in", hal.HAL_S32, hal.HAL_IN) c1.newpin("s32io", hal.HAL_S32, hal.HAL_IO) c1.newpin("floatout", hal.HAL_FLOAT, hal.HAL_OUT, init=42) c1.newpin("floatin", hal.HAL_FLOAT, hal.HAL_IN) c1.newpin("floatio", hal.HAL_FLOAT, hal.HAL_IO) c1.ready() c2 = hal.Component("c2") c2.newpin("s32out", hal.HAL_S32, hal.HAL_OUT, init=4711) c2.newpin("s32in", hal.HAL_S32, hal.HAL_IN) c2.newpin("s32io", hal.HAL_S32, hal.HAL_IO) c2.newpin("floatout", hal.HAL_FLOAT, hal.HAL_OUT, init=4711) c2.newpin("floatin", hal.HAL_FLOAT, hal.HAL_IN) c2.newpin("floatio", hal.HAL_FLOAT, hal.HAL_IO) c2.ready()
def test_ccomp_and_epsilon(): # custom deltas (leave epsilon[0] - the default - untouched) hal.epsilon[1] = 100.0 hal.epsilon[2] = 1000.0 c = hal.Component('epstest') # select epsilon[1] for change detection on 'out1' # means: out1 must change by more than 100.0 to report a changed pin p1 = c.newpin("out1", hal.HAL_FLOAT, hal.HAL_OUT, eps=1) # but use epsilon[2] - 1000.0 for out2 p2 = c.newpin("out2", hal.HAL_FLOAT, hal.HAL_OUT, eps=2) c.ready() print p1.eps, p1.epsilon, p2.eps, p2.epsilon # we havent changed pins yet from default, so c.changed() reports 0 # (the number of pin changes detected) assert c.changed() == 0 pinlist = [] # report_all=True forces a report of all pins # regardless of change status, so 2 pins to report: c.changed(userdata=pinlist, report_all=True) assert len(pinlist) == 2 # passing a list as 'userdata=<list>' always clears the list before # appending pins c.changed(userdata=pinlist, report_all=True) assert len(pinlist) == 2 c["out1"] += 101 # larger than out1's epsilon value c["out2"] += 101 # smaller than out2's epsilon value # this must result in out1 reported changed, but not out2 c.changed(userdata=pinlist) assert len(pinlist) == 1 c["out2"] += 900 # exceed out2's epsilon value in second update: c.changed(userdata=pinlist) assert len(pinlist) == 1 # since no changes since last report, must be 0: assert c.changed() == 0
#!/usr/bin/python2 # vim: sts=4 sw=4 et import time from machinekit import hal c = hal.Component('zzz') p = c.pin_s32("p0", hal.hal_pin_dir.Out) o0 = c.pin("p1", hal.hal_type.S32, hal.hal_pin_dir.In) o1 = c.pin("p2", hal.hal_type.S32, hal.hal_pin_dir.In) s = hal.Signal("signal", hal.hal_type.S32) p.link(s) s.link(o0, o1) c.ready() p.set(10) print(o0.get(), o1.get()) p.set(20) print(o0.get(), o1.get()) # give some time to watch in halcmd time.sleep(100) # exiting here will remove the comp, and unlink the pins
output_pins_joined = ','.join(str(x) for x in output_pins) input_pins_joined = ','.join(str(x) for x in input_pins) rt.loadrt('hal_bb_gpio', output_pins=output_pins_joined, input_pins=input_pins_joined) rt.loadrt(c.find('PRUCONF', 'DRIVER'), 'prucode={}/{}'.format(c.Config().EMC2_RTLIB_DIR, c.find('PRUCONF', 'PRUBIN')), pru=0, num_stepgens=0, num_pwmgens=2, num_encoders=1, halname='hpg') hpg = hal.Component('hpg') hpg.pin('pwmgen.00.out.00.pin').set(IN1) hpg.pin('pwmgen.00.out.01.pin').set(IN2) hpg.pin('encoder.00.chan.00.A-pin').set(pru0_input_pins[ENC_A]) hpg.pin('encoder.00.chan.00.B-pin').set(pru0_input_pins[ENC_B]) hpg.pin('encoder.00.chan.00.index-pin').set(17) # ignore hpg.pin('encoder.00.chan.00.counter-mode').set(0) # Quadrature Mode gpio = hal.Component('bb_gpio') setup_storage() # we need a thread to execute the component functions rt.newthread('main-thread', 1000000, fp=True)
def test_component_creation(): global c hal.epsilon[epsindex] = epsval c = hal.Component(cname) c.newpin(pname, hal.HAL_S32, hal.HAL_OUT, init=42, eps=epsindex) c.ready()
# errorSignals.append('e%i-error' % i) #base.setup_estop(errorSignals, thread='servo-thread') base.setup_estop(errorSignals, thread='servo-thread') base.setup_tool_loopback() # Probe #base.setup_probe(thread='servo-thread') # Setup Hardware hardware.setup_hardware(thread='servo-thread') # write out functions hal.addf('motion-controller', 'servo-thread') hardware.hardware_write() # trinamic_dbspi component: ---------------------------------------------------------------------# rt.loadrt('trinamic_dbspi', dbspi_chans='hm2_5i25.0.dbspi.0') trinamicdbspi = hal.Component('trinamic-dbspi.0') # create the signals for connecting the components drvctrl = hal.newsig('drvctrl-reg-unsigned', hal.HAL_U32) chopconf = hal.newsig('chopconf-reg-unsigned', hal.HAL_U32) smarten = hal.newsig('smarten-reg-unsigned', hal.HAL_U32) sgcsconf = hal.newsig('sgcsconf-reg-unsigned', hal.HAL_U32) drvconf = hal.newsig('drvconf-reg-unsigned', hal.HAL_U32) fullval_0 = hal.newsig('full-val-0-unsigned', hal.HAL_U32) fullval_1 = hal.newsig('full-val-1-unsigned', hal.HAL_U32) fullval_2 = hal.newsig('full-val-2-unsigned', hal.HAL_U32) fullval_3 = hal.newsig('full-val-3-unsigned', hal.HAL_U32) fullval_4 = hal.newsig('full-val-4-unsigned', hal.HAL_U32) fullval_5 = hal.newsig('full-val-5-unsigned', hal.HAL_U32) sgreadout = hal.newsig('sg-readout-float', hal.HAL_FLOAT)
pname = "%s.%s" % (topic, fname) print("pin name = %s") % (pname) pins[pname] = c.newpin(pname, pintypes[ftype], dir) print("c.ready()") c.ready() return pins #def talker(pins, cname, topic, msgtype, rate): # ''' publish the pins in comp ''' # rospy.init_node(cname, anonymous=True) # pub = rospy.Publisher(cname + "/" + topic, msgtype, queue_size=10) # r = rospy.Rate(rate) # msg = msgtype() # while not rospy.is_shutdown(): # for i in range(len(msg.__slots__)): # fname = msg.__slots__[i] ## msg.__setattr__(fname, pins[fname].get()) # print("type == %s") % (ftype) # pub.publish(msg) # r.sleep() if __name__ == '__main__': c = hal.Component('test') c.newpin('test', hal.HAL_FLOAT, hal.HAL_IN) pins = gen_halcomp(cname, topic, JointState, hal.HAL_IN) # gen_halcomp(cname, topic, JointState, hal.HAL_IN) # try: # talker(pins, cname, topic, JointState, 10) # except rospy.ROSInterruptException: pass
from machinekit import hal from machinekit import rtapi as rt # we need a thread to execute the component functions rt.newthread('main-thread', 1000000, fp=True) # load GPIO driver rt.loadrt('hal_chip_gpio', output_pins='0,1,2,3', input_pins='4,5,6,7') gpio = hal.Component('chip_gpio') gpio.pin('out-00').link('square') # load siggen rt.loadrt('siggen') siggen = hal.Component('siggen.0') siggen.pin('frequency').set(10.0) siggen.pin('clock').link('square') # setup update functions hal.addf('chip_gpio.read', 'main-thread') hal.addf('siggen.0.update', 'main-thread') hal.addf('chip_gpio.write', 'main-thread') # ready to start the threads hal.start_threads() # start haltalk server after everything is initialized # else binding the remote components on the UI might fail #hal.loadusr('haltalk')