TNGv2 Expr.txt usage

Results 1 to 13 of 13

Thread: TNGv2 Expr.txt usage

  1. #1
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default TNGv2 Expr.txt usage

    I notice that a new version 2020-07-31 is available (although you forgot to update the release post). In its release notes it has:

    Add: Multiple Expr.txt files
    Add: Expr.txt #OnInit and #OnShutdown sections

    Is there more information on these additions? I think they may be possibly useful for me, but without examples or updated documentation it is hard to know.

    Cheers
    Simon

    Similar Threads:


  2. #2
    Member PlanetCNC's Avatar
    Join Date
    Mar 2017
    Location
    Slovenia
    Posts
    1304
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    I added some notes here:
    https://www.cnczone.com/forums/plane...-expr-txt.html

    I hope it helps.



  3. #3
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    Thank you. That explains it for me. I will try some things tomorrow if it isn't subzero in the shed.



  4. #4
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    So this is awesome. It gives me the startup event hook that I have been asking for so that I can start an extra python thread!

    I have it working, but in a little bit of a roundabout way. I was unable to figure out how to directly run a python script from expr.txt, but was able to use startcode('M990') which is a script that then just starts the python task.

    What is the correct syntax to call a python script from an expr.txt file?

    Cheers
    Simon



  5. #5
    Member PlanetCNC's Avatar
    Join Date
    Mar 2017
    Location
    Slovenia
    Posts
    1304
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    Be careful - OnInit and OnShutdown are triggered every time you exit settings.

    I'll add 'py' and 'pythr' expression functions.



  6. #6
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    Ok, that is good to know. I assume OnShutdown would be called first followed by OnInit when exiting settings?
    I guess I will need to figure out how to kill the thread that is created in the OnInit in the OnShutdown, so that I don't end up with more than on background thread.



  7. #7
    Member PlanetCNC's Avatar
    Join Date
    Mar 2017
    Location
    Slovenia
    Posts
    1304
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    Or you simply check if thread is already running. If it is then you don't "loop" new thread any you allow it to exit.



  8. #8
    Member PlanetCNC's Avatar
    Join Date
    Mar 2017
    Location
    Slovenia
    Posts
    1304
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    Your idea goes like this (pythr will be in next version):

    Code:
    #OnInit
    print('OnInit')
    pythr('Python\posmon.py')
    
    
    #OnShutdown
    print('OnShutdown')
    _posmonstop=1
    Code:
    import sys
    import time
    import datetime
    import threading
    import planetcnc
        
    
    
    print("|!|bposmon module BEGIN", str(datetime.datetime.now()))
    planetcnc.setParam("_posmonstop", 0)
    cnt = 0
    while True:
        if planetcnc.getParam("_posmonstop"):
            break
        x = planetcnc.getParam("_hw_motor_x")
        y = planetcnc.getParam("_hw_motor_y")
        z = planetcnc.getParam("_hw_motor_z")
        planetcnc.print("Time PC: ", str(datetime.datetime.now()), " cnt: ", cnt, " Position X: ", x, " Y: ", y, " Z: ", z)
        print("Time PY:", str(datetime.datetime.now()), "cnt:", cnt, "Position X:", x, "Y:", y, "Z:", z)
        time.sleep(1)
        cnt = cnt + 1
        #if cnt > 5:
        #    break
            
    print("|!|bposmon module END")




  9. #9
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    That looks promising. The actual script I am starting is this. Basically to incorporate some other home automation stuff that controls other devices in the workshop.
    Code:
    # start with M990 after stating PlanetCNC
    
    import sys
    import time
    import ctypes
    import threading
    import socket
    import paho.mqtt.client as mqtt
    import planetcnc
    
    # The callback for when the client receives a CONNACK response from the server.
    def on_connect(client, userdata, flags, rc):
      client.connected_flag = rc
      if rc==0:
        client.subscribe("stat/+/POWER")
        
    def on_message(client, userdata, message):
        if message.payload.decode("utf-8")=="ON":
          v = 1
        else:
          v = 0
        if message.topic=="stat/garage_compressor/POWER":
          planetcnc.setParam("_compressor", v)
        if message.topic=="stat/garage_vacuum/POWER":
          planetcnc.setParam("_vacuum", v)
        if message.topic=="stat/garage_dustextractor/POWER":
          planetcnc.setParam("_dustextractor", v)
    
    def main():
      try:
        mqtt.Client.connected_flag = -1
        client = mqtt.Client("plantcnc-"+socket.gethostname())
        client.on_connect = on_connect
        client.on_message = on_message
        client.username_pw_set("clientid", "Password")
    
        client.connect_async("someipaddress")
        client.loop_forever()
    
      except Exception as e:
        planetcnc.print("MQTT Exception: ", str(e))
    
    t = threading.Thread(target=main)
    t.daemon = True
    t.start()
    
    




  10. #10
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    I tried using variables to test, but it appears they all get cleared when exiting out of settings. Instead I tried naming my thread and then checking if it already exists. So I have ended up with this:

    Code:
    #OnInitprint('OnInit')
    startcode('M990')
    
    
    #OnShutdown
    print('OnShutdown')
    Code:
    # start with M990 after stating PlanetCNC
    
    
    import sys
    import time
    import ctypes
    import threading
    import socket
    import paho.mqtt.client as mqtt
    import planetcnc
    
    
    # The callback for when the client receives a CONNACK response from the server.
    def on_connect(client, userdata, flags, rc):
      client.connected_flag = rc
      if rc==0:
        client.subscribe("stat/+/POWER")
        
    def on_message(client, userdata, message):
        if message.payload.decode("utf-8")=="ON":
          v = 1
        else:
          v = 0
        if message.topic=="stat/garage_compressor/POWER":
          planetcnc.setParam("_compressor", v)
        if message.topic=="stat/garage_vacuum/POWER":
          planetcnc.setParam("_vacuum", v)
        if message.topic=="stat/garage_dustextractor/POWER":
          planetcnc.setParam("_dustextractor", v)
    
    
    def main():
      try:
        mqtt.Client.connected_flag = -1
        client = mqtt.Client("plantcnc-"+socket.gethostname())
        client.on_connect = on_connect
        client.on_message = on_message
        client.username_pw_set("sun-mqtt", "MDZ6Lnf5^5froH6P")
    
    
        client.connect_async("10.0.1.12")
        client.loop_forever()
    
    
      except Exception as e:
        planetcnc.print("MQTT Exception: ", str(e))
    
    
    planetcnc.print("start mqtt.py")
    mqtt_thread = list(filter(lambda t: t.name == "mqtt", threading.enumerate()))
    if len(mqtt_thread) > 0:
      planetcnc.print("mqtt already running")
    else:
      planetcnc.print("mqtt starting")
      t = threading.Thread(target=main, daemon=True)
      t.name = "mqtt"
      t.start()
      planetcnc.print("TID="+str(t.native_id))
      for s in threading.enumerate():
        planetcnc.print(s.name)
    This works to initially start the thread, but when exiting settings I get the OnShutdown message from expr.txt but then PlanetCNC exits. Any ideas?



  11. #11
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    I should have added that if I execute the M990 command from the MDI everything works as expected.



  12. #12
    Member PlanetCNC's Avatar
    Join Date
    Mar 2017
    Location
    Slovenia
    Posts
    1304
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    In next version variables will not be cleared when exiting settings.
    Crash happens because Python is reopened when exiting settings. Because thread is still running it crashes. This is actually expected and nothing I can do about it.
    In next version I'll check if Python settings are changed and skip reopening it.



  13. #13
    Member theRat's Avatar
    Join Date
    Jun 2017
    Location
    Australia
    Posts
    143
    Downloads
    0
    Uploads
    0

    Default Re: TNGv2 Expr.txt usage

    That explains it all. Thanks so much for all the crazy fast responses and resolutions.



Tags for this Thread

Posting Permissions

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


About CNCzone.com

    We are the largest and most active discussion forum for manufacturing industry. The site is 100% free to join and use, so join today!

Follow us on


Our Brands

TNGv2 Expr.txt usage

TNGv2 Expr.txt usage