Access Modbus pins directly with Python - doable?

More
02 Jan 2016 22:32 #67727 by Simpson36
Thanks to all the help, I have the CEL system connected between the device and LinuxCNC with Python program sending/receiving and managing the command/acknowledge communications. There are actually 18 steps involved in each 'transaction'.

I have one last question that I cannot find in the docs. Hoping it is something simple.

LinuxCNC starts up fine with a custom hal, a PostGui hal, PyVCP and mb2hal and everything works great.

Now I need to add a Python program and another .ini, but I cannot figure out where to add them.

The Python program creates 6 new pins and the .ini nets those pins, so the Python program has to fun after the GUI (I think) and then the INI file has to run after that because the pins don't exist until after the python runs. I can't find that scenario in the documentation.

At the moment I run the Python program as an 'executable' after LinuxCNC starts and then open a terminal and paste in and run this line:

"halcmd -f /home/sfs/LinuxCNC/configs/sim.axis/inturncel.hal"

and that gets everything going, but I'm sure there is a way to get these guys to run automatically, but the timing complicates things as they need to run in a certain order.

Can anyone point me to the correct documentation or provide some guidance (with syntax) as to where and how to add these programs to the startup sequence?

Please Log in or Create an account to join the conversation.

More
02 Jan 2016 22:46 #67728 by andypugh
I think you can have more than one POSTGUI_HALFILE in the INI, if you need to.

Please Log in or Create an account to join the conversation.

More
02 Jan 2016 23:30 #67731 by jepler
No, in a quick survey of the UIs that implement POSTGUI_HALFILE, only one is supported.

Please Log in or Create an account to join the conversation.

More
02 Jan 2016 23:54 - 02 Jan 2016 23:56 #67732 by dgarrett
You can start another application using the ini setting:
[APPLICATIONS]
APP=name_of_app_in_the_PATH

A DELAY can be specified or the program can check to see if required precursors (like
hal pins) exist.

For example, sim configs in configs/sim/axis/xhc-hb04/*.ini
start programs named 'xhc-hb04-accels' and 'monitor-xhc-hb04'
that use this facility.

The xhc-hb04-accels script waits to make sure a predefined
hal testpin is available.

Ref:
linuxcnc.org/docs/2.7/html/config/ini-co...applications_section
Last edit: 02 Jan 2016 23:56 by dgarrett.

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 13:23 #67750 by Simpson36
dgarret,

Thanks for the tip. I could not get it to work, unfortunately :(

This seems like it would be a common thing to do, but apparently not.

The documentation says that the python hal ("import hal") can read any pin, but I cannot get that to work either. If I try to set and existing pin group name to hal.component, it says 'duplicate' and it won't take any text as a pin name.

putting the python program in [APPLICATIONS] stops LinuxCNC from starting, whether the program name is used or if it is called by a hal file.

So, with LinuxCNC, there is no way for a python program to read/write existing pins and no way to load a python program at startup?

That's just crazy time :ohmy:

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 14:09 - 03 Jan 2016 14:10 #67751 by dgarrett
Another alternative is haltcl. The commands are almost identical to
halcmd commands and all functionality is available.

linuxcnc.org/docs/2.7/html/hal/haltcl.html

Example (linuxcnc running in a separate shell):
$ haltcl
haltcl: loadrt flipflop names=ff
Component 'flipflop' ready
Program '/data/s/linuxcnc-dev/bin/rtapi_app' started
Realtime module 'flipflop' loaded
haltcl:
haltcl: show pin ff
Component Pins:
Owner   Type  Dir         Value  Name
    80  bit   IN          FALSE  ff.clk
    80  bit   IN          FALSE  ff.data
    80  bit   I/O         FALSE  ff.out
    80  bit   IN          FALSE  ff.reset
    80  bit   IN          FALSE  ff.set
    80  s32   OUT             0  ff.time
haltcl:
haltcl: setp ff.data 1
setting parameter 'ff.data' to '1'
Pin 'ff.data' set to 1
haltcl:
haltcl: show pin ff
Component Pins:
Owner   Type  Dir         Value  Name
    80  bit   IN          FALSE  ff.clk
    80  bit   IN           TRUE  ff.data
    80  bit   I/O         FALSE  ff.out
    80  bit   IN          FALSE  ff.reset
    80  bit   IN          FALSE  ff.set
    80  s32   OUT             0  ff.time

haltcl: 
Last edit: 03 Jan 2016 14:10 by dgarrett.
The following user(s) said Thank You: Simpson36

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 14:54 - 03 Jan 2016 15:00 #67754 by newbynobi
Hallo Simpson36,

if you add a embedded tab in your ini with a command like:
gladevcp -x {XID} -u plasma.py -H plasma.hal plasma.glade

it will load first the glade panel, than excecute the hal file and include the python file as a handler file. so you can make what ever u want.

You can look at sim / gmoccapy / gmoccapy_plasma for an example, as it do handle it your way.

In your handler file, you add two own hal pin with your component name one in and one out,
and connect the in change signal to your own handler.
In the hal file connect whatever pin you want to your component in hal pin.
The handler can now change the out pin to any value.
The out pin is again connected in your hal file to any other pin.

So where is the problem?

Please give a detailed example.

Norbert
Last edit: 03 Jan 2016 15:00 by newbynobi.

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 15:19 - 03 Jan 2016 15:25 #67755 by Simpson36
Norbert,

Thanks for the info. I know and understand what you said, but as I mentioned earlier, the problem with the python handlers is that there is no way to access any pin outside the def for each pin, so the glade stuff, while great for hooking up single signals to single screen controls (apparently what it is designed to do), but it is extremely awkward to get data from one pin to another using the handlers. I was able to get it done using a bunch of variables to transfer the data from one handler to another, but that would be something of a nightmare because it is critical to know if the data is current at all times.

It does not seem to me like what I want to do is at all unusual and the project is to port devices over from MACH3/MACH4, both of which have scripting that has access to the Modbus and is part of the programs.

The be specific, I need to get data off multiple Modbus registers, manipulate that data with calculations, conditional statements, etc and put it back on the Modbus. This is very simple stuff and so far here seems no way to do it in LinuxCNC.

The Hal component for Python is useable and I have that working to do these tasks, but now I cannot get it to load in LinuxCNC at startup. If I make the program executable, it runs fine, creates the pins and stays resident doing the communications (command/ acknowledge loops). The HAL comp seems to have no way to 'net' the pins, so I have to then make a separate .hal file that has to run after the python code to net the pins.

Awkward as heck, but at least it is all working. I could live with this if there is a way to get the stuff to load when LinuxCNC starts up, but so far there is no solution.

Any ideas would be most welcome!


Should I be looking at Bash instead of Python?
Last edit: 03 Jan 2016 15:25 by Simpson36.

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 15:37 #67756 by Simpson36
"The halcmd language excels in specifiying components and connections but offers no computational capabilities"

It looks like the tcl scripting offers better acces as well as some computational capabilities.

Thanks for this tip! I'll take a look at this scripting and see if it works any better than Python.

I don't have a favorite scripting language and the first macros I ever wrote for LinuxCNC was about a month ago using BASH. That was for Path Pilot, but probably the same as LinuxCNC. BASH is an odd duck, but it had access to the pins and also has the math.

Should I be looking at BASH? What do most people use to do serious development on LinuxCNC?

Please Log in or Create an account to join the conversation.

More
03 Jan 2016 15:55 #67757 by newbynobi
have you seen this?
#!/usr/bin/python
import hal, time
h = hal.component("passthrough")
h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT)
h.ready()
try:
    while 1:
        time.sleep(1)
        h['out'] = h['in']
except KeyboardInterrupt:
    raise SystemExit

it is from http://linuxcnc.org/docs/html/hal/halmodule.html

Please Log in or Create an account to join the conversation.

Time to create page: 0.171 seconds
Powered by Kunena Forum