Welcome to Our Community

Some features disabled for guests. Register Today.

Using Python to Communicate with OpenBuilds Control

Discussion in 'Control Software' started by mbrooks7, Mar 24, 2020.

  1. mbrooks7

    mbrooks7 New
    Builder

    Joined:
    Mar 24, 2020
    Messages:
    1
    Likes Received:
    1
    Hello all, I am currently a part of a University of Arizona Senior Design group and we were tasked with creating an Autonomous Torque Robot and for the foundation of the robot we are using a Workbee 1000x1000 Belt Driven Gantry. We are using a BlackBox Motor Controller to control the gantry and using OpenBuilds Control to talk to the BlackBox.

    So far I am able to open the OpenBuilds Control software from Python using the os library which is a start. Now I need to be able to input a G-Code file and send the file to the BlackBox by using Python code. We are allowed to manually input the zero position for the X,Y, and Z coordinates and as a result we only need to be able to feed the OpenBuilds Control software the G-Code file and then send the G-Code to the BlackBox. I know OpenBuilds Control is open-source code, so I was wondering if anyone would have any advice on how to have Python code talk to OpenBuilds Control software? Specifically to have Python add the G-Code file to the OpenBuilds Control software and then send the G-Code to the BlackBox.

    Thank you in advance for any advice you can provide, I greatly appreciate it.
     
    kevinbaileycrum likes this.
  2. David the swarfer

    David the swarfer OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 6, 2013
    Messages:
    3,431
    Likes Received:
    1,908
  3. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    python supports socket.io python-socketio — python-socketio documentation , it might be easiest to open a socket.io connection to OpenBuildsCONTROL-pc-IP:3000 (already runs a socket.io server that the HTML/JS frontend talks to) and then interact with it over websocket. Read index.js and look at all the socket.emit and socket.on events

    For example, to connect to a serial port: https://github.com/OpenBuilds/OpenBuilds-CONTROL/blob/master/index.js#L626
    To run a single command/gcode line: https://github.com/OpenBuilds/OpenBuilds-CONTROL/blob/master/index.js#L1117
    To run a job: https://github.com/OpenBuilds/OpenBuilds-CONTROL/blob/master/index.js#L1074

    Under the app/js folder there will be plenty of socket.emit and socket.on events too used by the UI
    for example here: https://github.com/OpenBuilds/OpenBuilds-CONTROL/blob/master/app/js/websocket.js#L299 where we get the nice JSON structure with all the feedback data
     
    #3 Peter Van Der Walt, Mar 25, 2020
    Last edited: Mar 25, 2020
  4. Richard Hainzl

    Builder

    Joined:
    Aug 10, 2020
    Messages:
    3
    Likes Received:
    0
    Has someone created and published some example code in Python on how to do this? Would be a nice time saver.
    This would be very useful for use case when you make something where you want to make the control software yourself and opens up for wider use of the Open Builds CONTROL.
     
  5. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    How about directly to Grbl, instead of CONTROL: in the Grbl repository: gnea/grbl
     
  6. Richard Hainzl

    Builder

    Joined:
    Aug 10, 2020
    Messages:
    3
    Likes Received:
    0
    Thank you!
    So I flash gnea/grbl to the BlackBox Controller and talk to it directly from Python. Did I understand that correctly?
     
  7. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    BlackBox already has Grbl installed... The link is to some sample Python scripts for integrating with Grbl
     
  8. Richard Hainzl

    Builder

    Joined:
    Aug 10, 2020
    Messages:
    3
    Likes Received:
    0
    OK, great!
    Thanks again!
     
  9. Upasana

    Upasana New
    Builder

    Joined:
    Sep 19, 2022
    Messages:
    3
    Likes Received:
    0
    Hi, Thanks for the reference. I wish to know what would be the device file for a CNC machine. In this example they are using Arduino.
     
    #9 Upasana, Nov 7, 2022
    Last edited: Nov 7, 2022
  10. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    Not sure what you mean by Device File?
     
  11. Upasana

    Upasana New
    Builder

    Joined:
    Sep 19, 2022
    Messages:
    3
    Likes Received:
    0
    It is present in the github repository that you have shared. The script "stream.py" requires another input along with .gcode file name which is the device file name.
    For sending the GCode to my CNC machine, I would need two arguments as part of serial.Serial() - the device file and the baud rate. How do I access this device file? On arduino, I would search for this in the /dev/ directory!
     
  12. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    Its the serial port. /dev/ttyUSBx or /dev/ttyACMx etc. With Dialout Group permissions
     
  13. Upasana

    Upasana New
    Builder

    Joined:
    Sep 19, 2022
    Messages:
    3
    Likes Received:
    0
    Hi, I tried the above path. However, it throws an error saying "raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port '/dev/ttyUSB5': FileNotFoundError(2, 'The system cannot find the path specified.', None, 3)"
    I tried USB0, USB1, USB2, USB3 and USB5.

    In openbuilds control the CNC machine connects to "FTDI USB to serial COM 5".
     
  14. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,875
    Likes Received:
    4,285
    COM5 is a Windows moniker. /dev/* = linux
    You mentioned
    earlier, leading me to assume you are on Linux?
    But now
    Makes me think you are on Windows. Which is it?

    If it is Linux:
    ls /dev/tty* and ls/dev/ACM* and post the output please

    If its Windows, use COM5
     
  15. Jean-Francois MIchaud

    Builder

    Joined:
    Jul 7, 2023
    Messages:
    1
    Likes Received:
    0
    Well I'm late to this party but…

    for the short version look at this Python gist:

    Interfacing with a cnc using huckle and hcli_hc in python

    For the long version, you can look at "hc" (HCLI CNC) at: GitHub - cometaj2/hcli_hc
    I just finished pumping out a sample HCLI for interfacing with GRBL directly via python (e.g. OpenBuilds BlackBox GRBL v1.1g). I took inspiration from gnea/grbl streaming code, and openbuilds control interface UX.

    I wanted to be able to automate around gcode streaming in python/shell all the while being able to throw immediate/realtime commands in at any time to have the cli behave like an immediately responsive interface (i.e. like OpenBuilds Control). The HCLI sample application (named "hc") queues up streaming gcode jobs as provided from shell scripts or via piping in a bash terminal.

    It seems to work ok. This is not broadly tested but it works fine on my macos M1 against OpenBuilds BlackBox v1.1g & a CBEAM machine setup.

    HCLI is part of a broader project I've been working on to help yield REST APIs that also present a standard semantic interface (HCLI) that is then interacted with as any *nix style CLI using a client that understands HCLI semantics. See: hcli.io

    There should be more comprehensive installation and setup instructions in the two github code repos above. Otherwise:

    HCLI Server (hcli_core, a falcon WSGI) running on gunicorn and exposing the hc sample application as a REST API (hc is the sample CNC GRBL interface sample application):
    • pip install gunicorn
    • pip install hcli-core
    • pip install hcli-hc
    • gunicorn --workers=1 --threads=1 "hcli_core:connector(\"`hcli_hc path`\")"
    Note: hc does NOT support multi-processing and can thus only run on a single worker on gunicorn since each worker runs in a different process. Multi-processing currently makes it so that the singleton constraint around shared/controlled resources can't be respected (e.g. access to the serial device, the job queue or the controller).

    HCLI Client (Huckle) that you point at the HCLI Server to "behave" as the cli exposed by the server (huckle doesn't "install" anything):
    • pip install huckle
    • huckle cli install localhost:8000
    • eval $(huckle env)
    Then you can use "hc" directly in your *nix shell (I'm making a bash shell assumption) to impersonate the presented HCLI exposed by gunicorn and hcli_core.

    For help:
    • hcli_core help
    • huckle help
    • hc help
    Then with your hcli_core service running you can run somethin like this client-side in some bash terminal (connect to the right serial port on your computer):

    Code:
    #!/bin/bash
    
    hc scan
    hc connect /dev/tty.usbserial-AR0JI0GV
    hc home
    echo '$#' | hc
    cat ~/Documents/References/OpenBuilds/Images\ Of\ Mordamir.gcode | hc -j 'Images of mordamir'
    The hcli_hc python package wrapped sample HCLI application code can otherwise also be used as inspiration for something else (MIT license).

    I'm happy to take merge requests requests and it would be cool to find a grbl controller emulator python library (I found this but it's not python: GitHub - grbl/grbl-sim: Compiles Grbl into an executable for your computer.) so that I can automate tests for the hcli sample application against it. I have automated tests on circleci for huckle and hcli_core and hcli_hc relative to the supported python versions and for a few very simple things hc is more complicated to test because it requires being able to connect to some grbl controller.

    Enjoy!

    Regards,
    Jeff Michaud
     
    #15 Jean-Francois MIchaud, Jul 7, 2023
    Last edited: Apr 30, 2024

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice