Welcome to Our Community

Some features disabled for guests. Register Today.

Tech support for Javascript Macros in CONTROL

Discussion in 'Control Software' started by sharmstr, Jan 4, 2021.

  1. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
    hey peter. any way to inspect a specific element quickly? I'm used to doing it with a right click which is disabled in control.
     
  2. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Fire up localhost:3000 in a browser, when you are outside of the Electron window, the normal devtools work again

    upload_2021-1-4_14-40-25.png
     
    NickEng and sharmstr like this.
  3. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
    Updated the code above so that the macro cant run twice (adds a hidden div on first run) and added a dialog box at the end to let you know what happened. Also added show z buttons when in continuous jog mode.
     
    #3 sharmstr, Jan 5, 2021
    Last edited: Jan 6, 2021
  4. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
    Hey Peter. Any advice on loading a local image into a dialog? I havent tried, but I'm assuming that control updates wipes out the OpenBuildsCONTROL directory. That's why I was going to put it somewhere else.
     
  5. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Base64 var directly in the macro? Base64 Image Encoder
    Can also embed SVG (open the file as text, put the XML into a var)
     
    #5 Peter Van Der Walt, Jan 28, 2021
    Last edited: Jan 28, 2021
  6. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
  7. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
  8. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
    yep yep.

    unfortunately they dont have an image of a cutting board with a juice groove :)
     
    Peter Van Der Walt likes this.
  9. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    :) no that is oddly specific lol!
     
    sharmstr likes this.
  10. Laurens Metz

    Builder

    Joined:
    Aug 18, 2020
    Messages:
    2
    Likes Received:
    0
    Hello,
    Could you please help me with the JS code that opens a file and then runs it?

    I have already this:

    window.open("file:///C:\Users\laure\OneDrive\Bureaublad\gcode.gcode");

    socket.off('prbResult'); // Disable old listeners

    socket.emit("runJob", {
    data: editor.getValue(),
    isJob: false,
    completedMsg: false
    });

    So it is a fixed path/file that I am overwriting all the time,
    and I would like to automaticly open and run the last saved version of this file.

    Thanks
     
  11. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
  12. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    David the swarfer likes this.
  13. Laurens Metz

    Builder

    Joined:
    Aug 18, 2020
    Messages:
    2
    Likes Received:
    0
  14. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    With the onClick function, how do I get that to run a javascript macro I have under Macros? Is there a name I have to give in the macro function?
     
  15. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    Code:
    var sizebtn = `<button id="grblXYSizeMenu" class="ribbon-button" onclick="??????">
                     <span class="icon">
                       <span class="fa-layers fa-fw">
                         <i class="fas fa-tape fa-rotate-180 fg-blue"></i>
                       </span>
                     </span>
                     <span class="caption grblmode">XY Size</span>
                   </button>`
    $( "#grblXYSizeMenu" ).after( chkSize );
     
  16. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    Does this code also need to go in a new macro along side the macro or does the code to add the button go in the same macro as the macro that shows me the size?
     
  17. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    I have tried putting it all in one macro and defining a function to run the result. It however can not see the function when I click on the button.

    Code:
    function XYSizeCheck(){
    if (typeof object !== 'undefined') {
       var string = ""
       if (object.userData.inch) {
           string += "<span style='font-size:42px;'>X:&nbsp" + Math.ceil(object.userData.bbbox2.max.x - object.userData.bbbox2.min.x) + "inch" + "<br>" + "Y:&nbsp;" + (object.userData.bbbox2.max.y - object.userData.bbbox2.min.y) + "inch</span>";
       } else {
           string += "<span style='font-size:42px;'>X:&nbsp" + Math.ceil(object.userData.bbbox2.max.x - object.userData.bbbox2.min.x) + "mm" + "<br>" + "Y:&nbsp;" + (object.userData.bbbox2.max.y - object.userData.bbbox2.min.y) + "mm</span>";
       }
       console.log(string)
       Metro.dialog.create({
           title: "Stock Size needed",
           clsDialog: "dark",
           content: `
              To run this job, you need a piece of stock:<hr>
              ` + string + `
          `,
           actions: [{
               caption: "Cancel",
               cls: "js-dialog-close alert",
           }]
       });
    }
    }
    
    var sizerbtntpl = `<button id="grblSizerMenu" class="ribbon-button" onclick="XYSizeCheck();">
                     <span class="icon">
                       <span class="fa-layers fa-fw">
                         <i class="fas fa-tape fa-rotate-180 fg-red"></i>
                       </span>
                     </span>
                     <span class="caption grblmode">XY</span>
                   </button>`
    $( "#grblProbeMenu" ).after( sizerbtntpl );
     
  18. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Check the example in post #21 above - you have to scope the new functions as window.function... to allow global access to it
     
    sharmstr likes this.
  19. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    Got it! Thank you!
     
  20. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    Is it possible to send GCode via a macro? I am looking to make the Z go up to the homed top, then once it has safely done that - move the X and Y to the very back of my machine (say X3600 Y2000 based on the distance from homed zero not current zero set in UI) so that I can load up my next sheet of material. Thanks for any help.

    I am keen to write a macro that homes then does a z probe in one operation. I am going to embed my touch plate into my wasteboard and touch off the Z each time the machine is homed.
     
    #20 SeanD, May 18, 2022
    Last edited by a moderator: May 18, 2022
  21. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Yes you can. See page 1 of this thread and the tips in Devtools
     
  22. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    I think something is broken. Ctrl + Shift + I doesn't look the same as on page one. I just get a console but not Devtools.

    I tried just a GCode macro and used G53 X100 F5000 but the machine just moved the X positive until it hit the wall at the end.

    In the code on the first page I can see:

    <button class="button outline sawStopActionBtn warning" onclick="sendGcode('M8');">APPLY BRAKE</button>
    <button class="button outline sawStopActionBtn primary" onclick="sendGcode('M9');">RELEASE BRAKE</button>
    <hr>
    <button class="button outline sawStopActionBtn secondary" onclick="sendGcode('$H');">HOME</button>
    <button class="button outline sawStopActionBtn secondary" onclick="sendGcode('G10 P0 L20 X0');">SETZERO</button>

    for the function of sendGcode. If I am writing this into a function should I use multiple sendGcode commands after each other or use a ; in the sendGcode("do first part; do second part;") sort of like a single line of multiple commands?

    I am not sure how to trigger the Z probe but I will work on that later.
     

    Attached Files:

  23. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Do add a G0 or G1 too, also add you G20/G21s and G90/G91s too - best to not rely on assumed modals

    G53X100 is 100mm beyond the limit switch: See Frequently Asked Questions · gnea/grbl Wiki (Machine Coordinates are always negative)

    G38.2 is the probe command


    sendGcode is for Single commands

    For "jobs" use runJob (the backend handles the queuing etc): See the basic tips in the into when you open devtools:

    upload_2022-5-19_8-2-22.png
     

    Attached Files:

  24. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    I don't think you need JS at all though: Can probably do what you need to do with gcode only

    G21; Millimeter mode
    G90; Absolute Positioning
    G53 G0 Z-5; lift z up
    G53 G0 X-20 Y-20 ;Back right corner
    G53 G0 Z-80; Rapid down to just above probe plate
    G38.2 Z-20 F100 ; Probe Z down max of 20mm
    G10 P0 L20 Zxx ; Set Z-Zero relative offset
     
    David the swarfer and SeanD like this.
  25. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    There is no devtools :( I attached the image to show. It isn't there.

    I read through the FAQ you linked. I now know that all of the G53 commands should be negative, but is that from my max distance/length of the machine? It looks from there that to get to X500 using G53 coords I should be using G53 X-500, but I am wrong.

    Doing a G53 X-500 goes to 500mm from the end of my machine. It's like the homing sequence completes and sets my machine zero to the other end of the X and Y. It looks to be getting that setting from my machine settings in Openbuilds Control. I thought the homing would set G53 to: 0,0 (which from the documentation you linked is what a lot of people assume wrong). Instead it appears to be setting it to: {negative value specified X length},{negative value specified Y length}.

    So to move to the back of my machine, I can just use G53 X0 and that does the trick.

    Attached my settings backup to show what I have set. Maybe I should have this in it's own thread to not muddy this one up as this is very specific to me and my lack of knowledge/understanding is the issue.
     

    Attached Files:

  26. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    Or just use your perfect suggestion :) I do want to add a button to the interface like I did with getting the X and Y size (works perfectly now thanks for the help on that one). I am not sure if I can add a macro straight on there or just send the commands via a function. The commands are what I needed for the most part. I wish I could duplicate your brain and put it in a jar to ask questions of instead of your time educating me and others that might find this helpful one day :)
     
  27. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    G53 X0 = far right
    G53 Y0 = far back
    G53 Z0 = Z lifted up all the way

    Negative directions. Away from 0 means more left, more front, more down. Higher number = further away from back, right, top

    Regardless of where the switches are, 0 0,0 is always in the same spot: at axis maximums.
     
  28. SeanD

    SeanD Well-Known
    Builder

    Joined:
    Mar 23, 2019
    Messages:
    207
    Likes Received:
    65
    It’s not intuitive but if that was the standard at the start of it all it makes sense why they keep it that way. Thanks.
     
    Peter Van Der Walt likes this.
  29. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,039
    Likes Received:
    1,434
    There will be on first connect. If you've hit F5 (refresh) at any time, there wont be.
     
    Peter Van Der Walt likes this.
  30. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,037
    Likes Received:
    4,122
    Fair enough, has to be a JS macro to inject the button.
    But refine the sequence of commands as a gcode macro first - once it does it perfectly - wrap the gcode up in a
    Code:
    var myCustomProbeMacro = `
    G21; Millimeter mode
    G90; Absolute Positioning
    G53 G0 Z-5; lift z up
    G53 G0 X-20 Y-20 ;Back right corner
    G53 G0 Z-80; Rapid down to just above probe plate
    G38.2 Z-20 F100 ; Probe Z down max of 20mm
    G10 P0 L20 Zxx ; Set Z-Zero relative offset
    `
    
    Which you then send out using"
    Code:
    socket.emit("runJob", {
      data: myCustomProbeMacro,
      isJob: false,
      completedMsg: false,
    });
    
    as part of your JS macro
     

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