585,743 active members*
5,118 visitors online*
Register for free
Login
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2008
    Posts
    4

    Read variable with Python

    hi to all,

    the problem:

    in axis there is a signal " axis.0.joint-pos-fb" that it gives back the position of axis 0 (x in my case)

    how I make to read the variable from a program python?

    thanks

    Alessandro

  2. #2
    Join Date
    Feb 2010
    Posts
    0

    axis.N.joint-pos-fb

    Hello

    I have exactly the same question.
    Did you find out how to proceed?
    I will look at the emc2 and axis code to try to find the answer...

    Thanks

    Olivier

  3. #3
    Join Date
    Nov 2005
    Posts
    496
    In your python code include python-HAL glue code.
    your python program will create a HAL pin and then you connect the signal you want to read to it. (This will not be realtime though)
    see:
    Creating Userspace Python Components with the 'hal' module

    By the way that signal name that was quoted is NOT from the AXIS display program.

    see here:
    EMC2's interface to HAL

    Chris M

  4. #4
    Join Date
    Nov 2011
    Posts
    4
    Quote Originally Posted by chester88 View Post
    In your python code include python-HAL glue code.
    Hello!
    Could you give me a small example.
    I want to get into Python code the value motion.program-line.
    (Userspace Python Components know how to create.)
    Sorry for my English.

  5. #5
    Join Date
    Nov 2005
    Posts
    496
    there is an example in the manual that i gave a link to.
    you would need to change the pin type to s32 instead of float, as the pin you wish to connect to is an s32 pin.

    then you would need to connect the pin you wish to read to the pin you made in the python program. You do that in a HAL file eg :

    net signal_name motion.program-line passthrough.in

    (using the example python program in the manual called passthrough)

  6. #6
    Join Date
    Nov 2005
    Posts
    496
    #!/usr/bin/python
    import hal, time
    h = hal.component("passthrough")
    h.newpin("in", hal.HAL_S32, hal.HAL_IN)
    h.newpin("out", hal.HAL_S32, hal.HAL_OUT)
    h.ready()
    try:
    while 1:
    time.sleep(1)
    h['out'] = h['in']
    print h['in']
    except KeyboardInterrupt:
    raise SystemExit



    try this version of passthrough...

  7. #7
    Join Date
    Nov 2011
    Posts
    4
    Thanks for the quick reply!
    But the question is slightly different. Here is my component pult.py :
    [code]
    Code:
    #!/usr/bin/python
    
    import hal, time , serial
    h = hal.component("pult")
    h.newpin("out", hal.HAL_BIT, hal.HAL_OUT)
    h.newpin("a", hal.HAL_BIT, hal.HAL_OUT)
    h.newpin("b", hal.HAL_BIT, hal.HAL_OUT)
    h.newpin("c", hal.HAL_BIT, hal.HAL_OUT)
    
    h.newpin("numberin", hal.HAL_S32, hal.HAL_IN)
    h.newpin("numberout", hal.HAL_S32, hal.HAL_OUT)
    
    
    h.ready()
    h['a'] = False
    h['b'] = False
    h['c'] = False
    h['out'] = False
    
    
    try:
        while 1:
    		time.sleep(1)
    		ser = serial.Serial('/dev/ttyS0', 115200, timeout=1)	
    		x = ser.read()
    		if x == '2' :
    			h['out'] = True
    		else :
    			h['out'] = False
    		if x == '3' :
    			h['a'] = True
    			h['b'] = False
    		if x == '4' :
    			h['b'] = True
    			h['a'] = False
    		if x == '5' :
    			h['c'] = True
    		ser.close()
    		h['numberout'] = h['numberin']
    		n=h['numberout']
    except KeyboardInterrupt:
        raise SystemExit
    In the hal file:
    Code:
    loadusr  -Wn pult python  pult.py
    net numb motion.program-line  pult.numberin
    Now we are writing a new Python script:
    Code:
    #!/usr/bin/python
    bla... bla...bla..
    x=n
    A variable "n" - from pult.py (n = h ['numberout'])
    I have tried so:
    Code:
    #!/usr/bin/python
    import pult
    x=n
    print n
    The result of an error-
    Code:
    >>> import pult
    HAL: ERROR: duplicate component name 'pult'
    Traceback (most recent call last):
      File "", line 1, in 
      File "pult.py", line 4, in 
        h = hal.component("pult")
    hal.error: Invalid argument
    >>> x=n
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'n' is not defined
    >>> print n
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'n' is not defined
    >>>
    The question is, how to use a value of "n" in any script?

  8. #8
    Join Date
    Nov 2011
    Posts
    4
    They here thus far prompted this method:
    Code:
    from subprocess import Popen, PIPE
    a= Popen('halcmd getp minmax.0.max ', shell=True, stdout=PIPE).stdout.read()
    x=int(a)
    print x
    In the hal file:
    Code:
    loadrt conv_s32_float 
    addf conv-s32-float.0 servo-thread
    net numb motion.program-line conv-s32-float.0.in
    loadrt minmax 
    addf minmax.0 servo-thread
    net numb1 conv-s32-float.0.out   minmax.0.in

Similar Threads

  1. G-code programming library in python
    By bermanmk in forum G-Code Programing
    Replies: 1
    Last Post: 11-10-2009, 03:24 AM
  2. How do I read the value of an F-type parameter into a macro variable?
    By Jan d. in forum Mazak, Mitsubishi, Mazatrol
    Replies: 24
    Last Post: 02-18-2009, 05:47 AM
  3. EMC2 variable pitch / variable diameter threading.
    By samco in forum MetalWork Discussion
    Replies: 0
    Last Post: 03-09-2008, 07:40 PM
  4. just another python gcode thing
    By cyclestart in forum LinuxCNC (formerly EMC2)
    Replies: 8
    Last Post: 02-18-2008, 03:54 PM
  5. simple python g-code generators
    By Dan Falck in forum OpenSource Software
    Replies: 0
    Last Post: 11-26-2007, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •