Welcome to Our Community

Some features disabled for guests. Register Today.

How to feed G-Code to Blackbox via Arduino Uno

Discussion in 'Controller Boards' started by Nathan Allaire, Aug 11, 2021.

  1. Nathan Allaire

    Builder

    Joined:
    Aug 9, 2021
    Messages:
    3
    Likes Received:
    0
    I am in the process of building a CNC that is controlled by the Openbuilds Blackbox. Instead of using the Openbuilds CONTROL to pass the G-Code to the Blackbox, I would like to have an Arduino Uno pass the (manual coded for now) G-Code on through the AUX1 port on the Blackbox.

    Everything is wired up properly and when I enter the raw line into the Openbuilds CONTROL the CNC moves properly, however when I send the line through the Arduino (with the code below), the CNC doesn't move at all. I feel like it is likely an error in the code itself.

    I would greatly appreciate some help trying to get the code to work for this simple test, thank you!


    #include <SoftwareSerial.h>
    SoftwareSerial mySerial(0, 1); // RX, TX

    void setup() {
    // Open serial communications and wait for port to open:
    mySerial.begin(115200);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
    }
    mySerial.begin(115200);
    }


    void loop() { // run over and over
    mySerial.println("G91 G21 Y10 F2500");
    delay(2000);
    mySerial.println("G91 G21 Y-10 F2500");
    delay(2000);
    }
     
  2. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    What version of BlackBox do you have? V2.3-2.7: May, may need to prep it first: see section 2.1.1 of docs.openbuilds.com/interface to get the Aux port to switch properly
     
  3. David the swarfer

    David the swarfer OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 6, 2013
    Messages:
    3,489
    Likes Received:
    1,925
    I think the pins 0 and 1 are the hardware serial port on the Uno, so you cannot softwareserial them, the ordinary 'Serial.println' is already using them.

    You really need to send a line and wait for the BB to send back 'OK' before sending the next line.
    See Grbl v1.1 Interface · gnea/grbl Wiki
     
    Peter Van Der Walt likes this.
  4. Nathan Allaire

    Builder

    Joined:
    Aug 9, 2021
    Messages:
    3
    Likes Received:
    0
    It is a V2.7, would I need to jumper the ICSP Header as shown?
     
  5. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Not for the Uno, with the Interface we are powering the Interface off the same rails, so the ICSP board helps filter any spikes from that. Your Uno is not powered off the BB (I hope) just GND/TX/RX
    Should still remove Q25 though
     
  6. Nathan Allaire

    Builder

    Joined:
    Aug 9, 2021
    Messages:
    3
    Likes Received:
    0
    Okay, on the page about preparing the BB it notes that removing the Q25 will not void the warranty. Is that specific to older boards or will it not void the warranty of a V2.7 board as well?
     
  7. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Warranty still valid
     
  8. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Jumping in on this older conversation to say I am having a similar issue currently and wanted to see if I could get some additional guidance. I am trying to send GCODE to a Blackbox using an Arduino Mega, through the second additional serial port provided. I apologize for asking easy to answer questions, but I am very new to this and the above wiki was not helpful to me. I was wondering what I need to do differently in my code below? I also included pictures of my connection to the Blackbox.

    char grbl_out;
    void setup() {
    Serial.begin(115200);
    Serial2.begin(115200);
    delay(2000);
    Serial2.println('\r\n\r\n');
    delay(2000);
    grbl_out = Serial2.read();
    Serial.print(grbl_out);
    delay(2000);
    Serial2.println('G0 X10 Y10 Z0\r\n');
    delay(2000);
    grbl_out = Serial2.read();
    Serial.print(grbl_out);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    }
     

    Attached Files:

  9. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Applies indeed. You need to build a proper queue management system, managing Grbl over serial is at least a couple hundred lines of code. Keeping track of OKs and other responses to know when to send, managing the buffer, using a ping pong or character counting queue etc. You have to control the flow of bytes manually to prevent the Grbl buffer from both overflow and starvation and also manage your own application - what did you need to display etc
     
  10. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    The current functionality for what I am trying to do is simply just send the Blackbox simple linear movement commands, to move an end effector to different locations within the scope of the gantry. The end effector is controlled separately from the gantry, but exists on the same codebase. I am really inexperienced with serial communication in general, and have read this wiki already in depth. Is there any more specific resource you could point me to in order to write a super stripped down, basic code that would correctly write GCODE to the serial pins of the Mega and consequently to the Blackbox? I wasn’t even aware that hundreds of lines of code were needed to do such a thing.
     
  11. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Additionally, there’s no interface needed for what I am trying to do with this. The endgame for the program is to execute the program in one shot, rather than wait for real time user input.
     
  12. David the swarfer

    David the swarfer OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 6, 2013
    Messages:
    3,489
    Likes Received:
    1,925
    make sure you unplug the USB cable from the BB so the Mega can use the serial pins unhindered.
    this delay (and the others) will give you troubles, you have to do a wait loop that waits until characters are available, then reads them and check that they are 'OK' to confirm that the BB is happy before sending a new command.
    This grbl/simple_stream.py at master · gnea/grbl is the simplest command streamer, and does not do error checking (bad).
    In that same folder is the more complicated version.
    You should look at the serial comms code for something like Candle GitHub - Denvi/Candle: GRBL controller application with G-Code visualizer written in Qt. to see how it has been done properly in C++
     
    Peter Van Der Walt likes this.
  13. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Wouldn't it be simpler to control the motors directly from your code then? See AccelStepper: AccelStepper library for Arduino
    No need for Grbl in between
     
  14. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    I took a look at all of these files and developed the below code, which unfortunately still generates no response. There is no "error" or "ok" returned. I am trying to understand the Denvi/Candle serial communication code, however like I said I am not super experienced with this. Are there any glaring issues in this code you can see?

    bool check_mode;
    void shutdown()
    {
    // optionally do stop motors dim the LED's etc.
    // Serial.print("stopped"); // or other warning
    while(1);
    }
    void setup()
    {
    //Initialize
    Serial.begin(9600);
    Serial1.begin(115200);
    //Wake up grbl
    Serial.print("Initializing Grbl...");
    Serial1.write("\r\n\r\n");
    //Wait for grbl to initialize and flush startup text in serial input
    delay(2000);
    check_mode = false;
    if (check_mode);
    Serial.print("Enabling Grbl Check-Mode: SND: [$C]");
    Serial1.write("$C\n");
    while (1);
    if (Serial1.find('error') >= 0)
    {
    Serial.print("REC: error!");
    Serial.print("Failed to set Grbl check-mode. Aborting...");
    shutdown();
    }
    else if (Serial1.find('ok') >= 0);
    {
    Serial.print('REC: ok');
    }
    }
     
  15. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Unfortunately this is not an option for me at this point. This project is for a Capstone project for my mech engineering degree, and the project is due in just over a month. When we purchased the 1515 and the Blackbox, it had seemed like it was much easier to stream code to the Blackbox via Arduino serial communication. The Arduino is controlling two additional steppers besides the 1515 steppers, plus a robotic arm via serial communication which at present date works just fine.
     
    Peter Van Der Walt likes this.
  16. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Waits forever here, nothing after this happens

    Your code segment hasn't sent any command to expect an OK back for yet?
     
    #16 Peter Van Der Walt, Apr 1, 2022
    Last edited: Apr 1, 2022
  17. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    I’ll change the while loop that was a mistake placeholder. I did send the Serial1.write(“$C\n”); right before the while loop, do I need to put this underneath the while loop?
     
  18. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Fair enough I read that as check_mode=false but I now see you didn't bracket the check mode send correctly so that didn't apply.

    Should be
    if (check_mode) {
    Serial.print("Enabling Grbl Check-Mode: SND: [$C]");
    Serial1.write("$C\n");
    }
    Which would not have executed

    While was where your sketch stops though - it never moves on to the check
     
  19. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3

    OK so there is some progress made! I made those changes and now the program returns the following:

    Failed to set Grbl check-mode. Aborting...28523REC: error!

    I changed the code to the following, with "check" being a boolean value set to true. Is this number a specific error code? I looked up the GRBL error codes and they seem to only go up to two digits.

    check_mode = false;
    if (check_mode){
    Serial.print("Enabling Grbl Check-Mode: SND: [$C]");
    Serial1.write("$C\n");
    }
    while (check){
    if (Serial1.find('error') >= 0)
    {
    Serial.print("REC: error!");
    Serial.print("Failed to set Grbl check-mode. Aborting...");
    //shutdown();
    }
    else if (Serial1.find('ok') >= 0);
    {
    Serial.print('REC: ok');
    }
    }
     
  20. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    What is "check" - and your use of the while loop has no exit (never sets "check" to false) thus the while still runs forever
     
  21. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Yeah I meant to set it that way for now, “check” is just a boolean i set to true earlier in the program. All I’m looking for currently is for the Blackbox to return “ok” and the rest I can figure out on my own.
     
  22. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    Remembered to unplug BlackBox USB cable to switch it to AUX mode?
     
  23. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Peter, this is precisely what I need. Thank you very very much I’ve been searching forever on Google for something like this! I suppose I didn’t put the right search terms in. I’ll give this a shot next week!
     
  24. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
    I edited my post (guess not before you saw it) and removed the link - it doesn't actually do queue management it seems (from the code, nothing checking for OKs etc)
     
  25. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    15,189
    Likes Received:
    4,346
  26. Lukas Butler

    Builder

    Joined:
    Mar 29, 2022
    Messages:
    10
    Likes Received:
    3
    Peter,

    Finally got back in the shop to try this out, and it worked like a charm. Thank you so much for your help and for bearing with my lack of knowledge!
     

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