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. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    Misterg likes this.
  2. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    Thanks Peter - I'd found that line, but wasn't sure what the definition of 'Recently' was, so I was going to have a play around and see what it did. :)
     
  3. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    "we saw a $H come by, and haven't picked up a reset or alarm since" doesn't mean its still "homed" just that it was recently Homed (the action of homing). Fair enough naming could have been better
     
  4. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    No, I think the naming is fine - I couldn’t find the code that set the status though, so was resorting to trial & error! :)
     
  5. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    As per Peters post above the link to function JobNeedsHoming is already built in to the control software.
    so couldn't you just call it before the run of pop up window and it will flash a message.



    function jobNeedsHoming() {
    if (editor.getValue().lastIndexOf("G53") != -1 || editor.getValue().lastIndexOf("g53") != -1) {
    if (laststatus !== undefined) {
    if (laststatus.machine.modals.homedRecently == false) {
    var dialog = Metro.dialog.create({
    clsDialog: 'dark',
    title: "<i class='fas fa-exclamation-triangle'></i> Job uses Machine Coordinates:",
    content: "<i class='fas fa-exclamation-triangle fg-darkRed'></i> Tip: The GCODE file you loaded contains G53 commands. Please make sure to HOME the machine to establish the Machine Coordinate (G53) System properly to prevent crashes.",
    actions: [{
    caption: "Close",
    cls: "js-dialog-close",
    onclick: function() {
    //
    }
    }]
    });
    }
    }
    }
    }
     
  6. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    Not quite, because it also relies on G53 being present in the gcode, and you'd want this macro to work with mpos/wpos as well as wpos-only jobs.
    This function was meant to help with people using Fusion360 gcode that uses machine coordinates for Z-safe moves, but then forgetting to home :)

    It is a good example of how to build the needed function though, but not directly reusable

    PS guys, mind if I move all these posts out of here to the correct thread? We are cluttering the Library thread now

     
  7. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    No, because I want to keep it independent of any GCode.

    It's straightforward enough (and simpler) to check laststatus.machine.modals.homedRecently directly (which is what that function checks, anyway), especially now that Peter has confirmed how it works. I just need to make some time to do it and test it afterwards :)

    Apologies to Peter for the drift - yes, please feel free to move.
     
    Peter Van Der Walt likes this.
  8. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    Hey Misterg, I was testing this again after setting the macro to run on startup which it did fine.
    homed the machine, ran the macro from the button, and it didn't have the reference tool populated so I did a manual probe, reran the macro and the reference tool had it set. moved to tool change location, then hit the new tool button and it moved back to G30 and proceeded to dive into the probe.. not sure what changed because it worked fine the other day. now getting alarm 2 and alarm 9.

    excerpt from serial log here
    [12:35:30] Tool Offset Dialog Opened
    [12:35:40] [ G21 ] ok
    [12:35:40] [ $G ] [GC:G38.3 G54 G17 G21 G90 G94 M5 M9 T0 F50]
    [12:35:40] [ $G ] ok
    [12:35:40] [ G53 G0 Z-5 ] ok
    [12:35:40] [ G30 G91 X0 Y0 ] ok
    [12:35:40] [ $G ] [GC:G0 G54 G17 G21 G91 G94 M5 M9 T0 F50]
    [12:35:40] [ $G ] ok
    [12:35:40] [ JOB COMPLETE ] Job completed in 00h00m
    [12:35:40] [ G30 G91 Z0 ] ok
    [12:35:40] [ $G ] [GC:G0 G54 G17 G21 G91 G94 M5 M9 T0 F50]
    [12:35:40] [ $G ] ok
    [12:35:49] [ G21 ] ok
    [12:35:49] [ $G ] [GC:G0 G54 G17 G21 G91 G94 M5 M9 T0 F50]
    [12:35:49] [ $G ] ok
    [12:35:49] [ ALARM ] ALARM: 2 - G-code motion target exceeds machine travel. Machine position safely retained. Alarm may be unlocked. [ G38.3 Z-50 F200 ]
    [12:35:50] [ ] ALARM: 2 - G-code motion target exceeds machine travel. Machine position safely retained. Alarm may be unlocked. [ G38.3 Z-50 F200 ]
    [12:35:50] [ G38.3 Z-50 F200 ] ALARM:2
    [12:35:50] [ G38.3 Z-50 F200 ] [MSG:Reset to continue]
    [12:35:50] [ G38.3 Z-50 F200 ] ok
    [12:35:50] [ G4 P0.4 ] Grbl 1.1h Evolution 4 ['$' for help]
    [12:35:50] [ G4 P0.4 ] [MSG:'$H'|'$X' to unlock]
    [12:35:50] [ ] error: 9 - G-code locked out during alarm or jog state [ G4 P0.4 ]<hr>This error may just be a symptom of an earlier event:<br> ALARM: 2 - G-code motion target exceeds machine travel. Machine position safely retained. Alarm may be unlocked.
    [12:35:50] [ ERROR ] error: 9 - G-code locked out during alarm or jog state [ G4 P0.4 ]
    This error may just be a symptom of an earlier event:
    ALARM: 2 - G-code motion target exceeds machine travel. Machine position safely retained. Alarm may be unlocked.
    [12:35:50] [ G4 P0.4 ] error:9
    [12:36:00] [ ] Grbl 1.1h Evolution 4 ['$' for help]
    [12:36:00] [ ] [MSG:'$H'|'$X' to unlock]
    [12:36:00] [ ] [MSG:Caution: Unlocked]
    [12:36:00] [ ] ok
    [12:36:01] Tool Offset Dialog Exited

    had a look at the console log as well
    Uncaught TypeError: Cannot read properties of undefined (reading 'expand')
    at probezplatetab (probev2.js:254:55)
    at probetype (probev2.js:305:5)
    at openProbeDialog (probev2.js:62:5)
    at HTMLButtonElement.onclick ((index):234:104)
    websocket.js:407 error: 3 - Grbl '$' system command was not recognized or supported. [ $J=G91G21Z5F1000 ]
    websocket.js:369 ALARM: 4 - Probe fail. The probe is not in the expected initial state before starting probe cycle, where G38.2 and G38.3 is not triggered and G38.4 and G38.5 is triggered. [ G38.3 Z-7 F50 ]
    websocket.js:407 error: 9 - G-code locked out during alarm or jog state [ G4 P0.4 ]<hr>This error may just be a symptom of an earlier event:<br> ALARM: 4 - Probe fail. The probe is not in the expected initial state before starting probe cycle, where G38.2 and G38.3 is not triggered and G38.4 and G38.5 is triggered.
    websocket.js:369 ALARM: 3 - Reset while in motion. Grbl cannot guarantee position. Lost steps are likely. Re-homing is highly recommended. [ undefined ]
    VM134:258 New Tool Probe
    VM134:259 Measured Tool Offset-57.673
    VM134:260 Current WCS Z offset -77.74
    VM134:261 Previous Tool Offset -57.023
    VM134:262 Previous WCS Offset -77.76
    VM134:263 New WCS Offset -78.390
    websocket.js:253 jobComplete
    Object
    websocket.js:269 Completed: complete / Filename: / Estimated Runtime: 00h:00m / Streaming Runtime: 00h00m / Start Date: Sat Apr 01 2023 12:35:39 GMT-0400 (Eastern Daylight Time) / End Date: Sat Apr 01 2023 12:35:40 GMT-0400 (Eastern Daylight Time)

    Ok so after reading this over , I think it boils down to the g38.3 is not finding the probe because I reset the g30 location and stopped it to high above the probe block. gonna reset g30 and try again.
    I apologize for any issues. I am really trying to get this to work for me, and I will, just a bit slow......
     
    #68 Martin wade, Apr 1, 2023
    Last edited: Apr 2, 2023
  9. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    Hi, sorry for your problems -

    The entire excerpt from the serial log is consistent with G30 being set too low for the travel requested by the macro during the probe. By default, the distance that the macro asks for is 50mm (tc_seekdistance = 50 in the macro). So if G30 height is set at less than 50mm from the bottom of the Z travel, this error will flag up.

    If you need / want to set G30 that low, then the value of tc_seekdistance in the macro can be reduced to suit, so that the maximum potential travel of the Z axis during probing stays within the limits of the machine.

    (As an aside, you should only need to set G30 / G28 once, as the settings are retained when the machine is switched off in the same manner as the work offsets - G54, etc. so provided the machine is homed, they will be in the same place every time.)

    I'm not sure what you mean by

    Did you do a probe using the "normal" probe button in Control? I didn't think that this could be done while the macro window is open, but if you managed to, I'd be interested to know how :).

    Having the reference tool length showing as set in the macro **should** only be possible following a successful probe using the macro...I've tried to deliberately break this, but havent found a way of doing it yet. Any probing using the built in wizards **shouldn't** affect it.

    The first bit of the console log (below), I think, has been generated by Control in response to earlier problems unrelated to the macro [apologies in advance to the OB team if I am wrong] (possibly a jog command ? and the using the probing wizard with the probe being triggered at the start of the probing cycle) - the errors stick around in the console until cleared:

    This output from the console log is as I would expect from setting a new tool:

    It's possible that if you scroll further up the log, there will be some messages relating to the setting of the reference tool - something like this (a sample from my machine):

    (Look in the message category 'Other')

    So, the headline problem is that G30 was set lower than the requested probing distance, however this should just have generated an alarm without any lasting effects or damage. I can't *see* any bugs in the macro that would contribute to this, but it doesn't mean that they're not there!

    I will continue to investigate this - I hope to add the homing check to the macro this week, so will work through the code again then. In the meantime, proceed with caution, and if anything untoward happens again, please let me know, with as much info as possible on the sequence of events so I can try and recreate it.

    Thanks for being the guinnea-pig ;)
     
    #69 Misterg, Apr 2, 2023
    Last edited: Apr 2, 2023
  10. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    I did as you suggested and reset G30 higher than it was. I had come to the same conclusion after the errors using the macro. As for my earlier comment about setting the reference tool with the regular probe macro it was done after canceling your macro. It did retain reference tool settings. so after making sure it had the 50mm or so. Everything worked well. I had an issue with consistent measurements of the tool. Which I think is due to my placement of the probe. I had earlier milled a spot on the wasteboard to accurately place the probe but I am getting inconsistent results measuring the same tool multiple times. Several thou differences each time. Tomorrow I will try a few things short of permanently fixing the probe to a spot. Much the same as a bit setter would be set up. Stay tuned. And I am glad I’m able to help shake this out.
     
    Misterg likes this.
  11. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    So part of my consistency problem was the acme nut is worn out on my Zscrew. I had about 3 mm play along the z. I have it torn apart and waiting for a new one. Will get back to it after new nut and re-tram.
     
    Misterg likes this.
  12. Jimc137

    Jimc137 New
    Builder

    Joined:
    Nov 24, 2022
    Messages:
    4
    Likes Received:
    0
    I am new to Macros so I tried this one but when it ran, the units were in inches (which was fine) but there were 15 decimals! Is there a way to round it off to 2 decimal places or even a fraction?
     
  13. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    Yes, you can append toFixed(2) to values where you need it: JavaScript toFixed() Method
     
  14. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    Ok, it has been a while, but I've updated the macro to check the 'homedRecently' status and that the spindle isn't running, etc.

    Code here

    The homedRecently status errs on the side of caution, and shows that the machine needs homing after every error (e.g. syntax error in gcode file, attempting to jog beyond the machine limits, etc.) even though the position is reliable. Because of this, I've given the option to carry on regardless, and also the option to disable the check in the macro, as it was getting on my nerves :rolleyes: (set tc_ignorehomestatus to "yes").

    I've also corrected the reporting where the WCS has been changed but the tool offset hasn't, tidied up the formatting, and set the default values for the probing cycle to be 'lower and slower'. It should also be possible to re-run the macro now which will update the settings without having to exit completely from Control.

    If the probe isn't in the expected state before probing, or the requested probe travel is > machine travel these generate hard errors that my macro can't trap. If they happen, you will need to close the dialog and re-open it.

    ETA: It isn't possible to pause a program to use this macro, as (I think) the comms port isn't released while the program is paused, so each tool still needs its separate file.

    Let me know how it goes.
     
    #74 Misterg, Apr 11, 2023
    Last edited: Apr 11, 2023
  15. Jimc137

    Jimc137 New
    Builder

    Joined:
    Nov 24, 2022
    Messages:
    4
    Likes Received:
    0
    I appreciate your quick reply, but I am still a little lost. From the little I recall about JAVAscript, you said that with the toFixed() method, I need to set the output variable to a fixed # of decimals but I am not sure what the variable is or where to add the function in the macro.
     
  16. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    I think you would add it as follows, but note that I haven't tested this.

    Code:
    if (typeof object !== 'undefined') {
       var string = ""
       if (object.userData.inch) {
           string += "Width (X): " + (object.userData.bbbox2.max.x - object.userData.bbbox2.min.x).toFixed(2)+ "inch" + " / " + "Height (Y): " + (object.userData.bbbox2.max.y - object.userData.bbbox2.min.y).toFixed(2) + "inch";
       } else {
           string += "Width (X): " + (object.userData.bbbox2.max.x - object.userData.bbbox2.min.x).toFixed(0) + "mm" + " / " + "Height (Y): " + (object.userData.bbbox2.max.y - object.userData.bbbox2.min.y).toFixed(0) + "mm";
       }
       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",
           }]
       });
    }
    
     
  17. Ivo Beltchev

    Builder

    Joined:
    Apr 17, 2023
    Messages:
    32
    Likes Received:
    15
    Hi. I've been using the Control software for a few months now, and I started thinking about some usability improvements. One thing that I would love to have is a button to reload the last g-code file. However looking at the source code I don't see it being stored anywhere - just the name and extension.

    This will be a trivial change to the software's source code, but I'd like to have it as a macro if possible. My idea is to clone the readFile function with one that actually stores the full file path. This requires copying the socket.on("openFile") handler as well.

    And that's where I hit the first snag (possibly of many more to come) - I can't figure out how to get dialog.showOpenDialog to work. I tried multiple syntaxes inside a macro, and nothing worked.

    I am not an expert in Javascript, and completely new to electron and node.js. So as anyone would do in this situation, I turned to StackOverflow. I found a simple example of a message box, that also didn't work inside a macro:

    Code:
    const { dialog, BrowserWindow } = require ("electron");
    
    dialog.showMessageBox (BrowserWindow.getFocusedWindow(), {
      type: "warning",
      message: "You have been warned.",
      buttons: ["OK"]
    });
    I get the error "Uncaught TypeError: Cannot read properties of undefined (reading 'showMessageBox')"

    Is there a trick to getting the dialog library to work from a macro?

    Also, is my goal doomed to fail? I'd really like to avoid editing the source code if possible.
     
  18. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,059
    Likes Received:
    1,448
  19. sharmstr

    sharmstr OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 23, 2018
    Messages:
    2,059
    Likes Received:
    1,448
  20. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    Correct scripts can't access file system, only user actions. Security reasons in chromium
     
    sharmstr likes this.
  21. Ivo Beltchev

    Builder

    Joined:
    Apr 17, 2023
    Messages:
    32
    Likes Received:
    15
    No, I don't mean across sessions. Just to improve the iteration loop - tweak something in Fusion, post-process, then click a button to reload.

    I will pour over the examples. Thanks!

    P.S. I am using your tweak to remove the dropdown menu from the file button, which saves me a click when reloading. Thanks for that! :)
     
    #81 Ivo Beltchev, Apr 18, 2023
    Last edited: Apr 18, 2023
  22. Ivo Beltchev

    Builder

    Joined:
    Apr 17, 2023
    Messages:
    32
    Likes Received:
    15
    So it is true that even if I solve the file dialog issue, there is no way I would be able to do operations like fs.readFile and such?

    Out of curiosity, is there a way to get the dialog module to work inside a macro? That's not directly related to accessing the file system.
     
  23. Peter Van Der Walt

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

    Joined:
    Mar 1, 2017
    Messages:
    14,734
    Likes Received:
    4,261
    Without also modifying the backend, probably not. At the moment it is a little secure (we don't want baddies using CONTROL to do bad things)

    Oh for that, checkout this
    Developers: API to Inject GCODE into CONTROL

    There was a fusion plugin somewhere that used that to post gcode to CONTROL during Post. Can't remember where it was, but was a modified version of GitHub - tapnair/UGS_Fusion: Fusion 360 Universal Gcode Sender Plugin

    So instead of accessing files, let Fusion push upload it to CONTROL through the https API
     
    #83 Peter Van Der Walt, Apr 18, 2023
    Last edited: Apr 18, 2023
  24. Ivo Beltchev

    Builder

    Joined:
    Apr 17, 2023
    Messages:
    32
    Likes Received:
    15
    Yeah, I ended up modifying index.js. It works perfectly. The hardest part was figuring out which icon to use for the button.

    upload_2023-4-18_0-44-10.png
     
  25. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    Hi misterg, I just tried your new version and the only comment I have is. It always resets back to z0. If I had previously set z0 to top of stock. The machine coordinates are not remembered correctly. It saves x and y 0’s but goes back to the waste board z 0. So as a tool change procedure in the middle of a job it requires you to reset using the probe again, and if your original settings were top of stock if kinda fails. The offset seems to work correctly. Have a think. Let me know if it’s possible to save the machine z setting as well.
     
  26. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    Hi, thanks for the feedback - it should preserve the Z height (it does for me) - if you have Z0 set at the waste board with the old tool, the Z0 will still be at the wasteboard with the new tool, even though it might have a different length - likewise if Z0 is set on top of the stock (or anywhere else), then the tip of the new tool will be at the same place.

    I have been using it this way for a while now, and it has worked OK. I wonder if there may be some subtle differences in the way that we're using it?
     
  27. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    Maybe. Having said all that I think I just realized that I was doing this without an actual piece of stock. Give me a few minutes and I’ll get back to you.
     
  28. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    Ok my apologies. It does work. I’m not not sure what I was thinking. Hit the top of stock perfectly. Not enough coffee I guess. Keep up the good work. I am gonna try with the homed status set to yes.
     
    Misterg likes this.
  29. Misterg

    Misterg Veteran
    Staff Member Moderator Builder Resident Builder

    Joined:
    Aug 27, 2022
    Messages:
    344
    Likes Received:
    268
    Glad to hear that! :)

    As I said, I’ve used it quite a bit recently, and have even started to trust it!
     
  30. Martin wade

    Builder

    Joined:
    Dec 18, 2018
    Messages:
    11
    Likes Received:
    6
    well I'm almost there. As i said before my machine was out of service for a couple of weeks. so just getting back to it. Thanks for your patience. I'm gonna share this macro with my group of cnc-ers at BobsCNC. you get all the credit.
     
    Misterg likes this.

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