• Tags
  • Documentation
  • Order
  • Register
  • Login
Duet3D Logo Duet3D
  • Tags
  • Documentation
  • Order
  • Register
  • Login

Custom Java Script in config file

Scheduled Pinned Locked Moved
DueUI
2
7
471
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • undefined
    stuartofmt
    last edited by stuartofmt 29 Mar 2024, 17:16

    I started this conversation in another thread but it warrants being in its own thread so others may search more easily.

    I'm trying to execute some javascript inside DueUI. There is an example in the wiki that I'm following but without success.

    Note that my javascript is "code by example" - so I do not really know what I'm doing 🙂

    I'd appreciate any pointers.

    The top of my DueUIConfig class looks like this:

    class DueUIConfig {
    getProbeNum(state) {
    var myprobe = "No Probe";
    if (Array.isArray(state.sensors.probes[0])) {
    myprobe = 0;
    } else if (Array.isArray(state.sensors.probes[1])) {
    myprobe = 1;
    }
    console.log("Probe Number is - ", myprobe);
    console.log("Probe ", myprobe);
    return myprobe;
    };

    Later on I do this:

    "field": "${state.sensors.probes[DueUIConfig.getProbeNum(state)].value}"
    

    The error I get claims that :

    {timestamp: Fri Mar 29 2024 11:07:37 GMT-0600 (Mountain Daylight Time), severity: 'E', message: 'DueUIConfig.getProbeNum is not a function: `${stat…rs.probes[DueUIConfig.getProbeNum(state)].value}`'}
    
    1 Reply Last reply Reply Quote 0
    • undefined
      gtj0
      last edited by 29 Mar 2024, 17:41

      I'm going to test it again later today but I think you're going to have to declare getProbeNum as static...

      class DueUIConfig {
      static getProbeNum(state) {
      var myprobe = "No Probe";
      if (Array.isArray(state.sensors.probes[0])) {
      undefined 1 Reply Last reply 29 Mar 2024, 21:55 Reply Quote 0
      • undefined
        stuartofmt @gtj0
        last edited by 29 Mar 2024, 21:55

        @gtj0
        declaring as static moved a step closer but "no cigar" 😞

        I tried a bit of debugging. Line 4 (below) gives this error (note there is no probe 0 ). I expected a null or undefined - I suspect deeper in dueui its simply not returning anything on error ??

        {timestamp: Fri Mar 29 2024 14:28:00 GMT-0600 (Mountain Daylight Time), severity: 'E', message: "Cannot set properties of null (setting 'value'): `…rs.probes[DueUIConfig.getProbeNum(state)].value}`"}
        

        Test code:

        static getProbeNum(state) {
        var myprobe = "1"; // set to one for testing. If statement below is failing
        console.log("Checking Probe");
        console.log (typeof(state.sensors.probes[0].value));
        console.log (state.sensors.probes[1].value);
        if (Array.isArray(state.sensors.probes[0])) {
        console.log("Probe 0 found");
        myprobe = 0;
        } else if (Array.isArray(state.sensors.probes[1])) {
        console.log("Probe 1 found");
        myprobe = 1;
        }
        console.log("Probe Number is - ", myprobe);
        return myprobe;
        };

        I ended up backing away from Array.isArray and just using this. I'm unsure if the use of the global myprobenum to exit the function early on successive calls buys me much ....

        static getProbeNum(state) {
        var myprobe = null;
        // If already know the probe number just return it
        if (myprobenum != null) {
        return myprobenum;
        };
        // otherwise Find the active probe
        console.log("Checking Probe");
        if (state.sensors.probes[0] != null) {
        console.log("Probe 0 found");
        myprobe = 0;
        } else if (state.sensors.probes[1] != null) {
        console.log("Probe 1 found");
        myprobe = 1;
        };
        myprobenum = myprobe; // assign to the global variable
        return myprobe;
        };
        undefined 1 Reply Last reply 30 Mar 2024, 16:10 Reply Quote 0
        • undefined
          gtj0 @stuartofmt
          last edited by 30 Mar 2024, 16:10

          @stuartofmt You've got a couple of issues...

          When you were using Array.isArray, you were testing state.sensors.probes[0] and state.sensors.probes[0] which are the elements of the probes array, not arrays themselves so both tests will always return false.

          Your second attempt using the global variable will never return 1 because probes, being an array, can't have an array element at index 1 without there being one at 0.

          I'm guessing you're using the K parameter on M574 to set the probe number? That value only appears in the state.sensors.endstops object as the probe value. It doesn't control where in either the endstops or probes arrays the probe appears. If you only have 1 probe, that probe will always be state.sensors.probes[0] no matter what you have the K value set to.

          If I guessed wrong and you're doing something else, let me know and I'll see if I can help.

          undefined 1 Reply Last reply 30 Mar 2024, 18:35 Reply Quote 0
          • undefined
            stuartofmt @gtj0
            last edited by 30 Mar 2024, 18:35

            @gtj0

            My somewhat strange use-case stems from some experiments I was doing with a secondary calibration code for auto Z-offset. Because of some limitations in DSF - I could only use probe 0 for the calibration code and therefore relegated my BLT to probe 1.

            When not running an auto-z-offset macro (still a wip): The default state is state.sensors.probes[0] = null and state.sensors.probes[1] is the active probe (and hence has a .value). The object model does indeed have a element at index zero but it reports null.

            Aesthetically - the second code attempt is a bit ugly (makes several assumptions) but does work as expected.

            you were testing state.sensors.probes[0] and state.sensors.probes[0] which are the elements of the probes array, not arrays themselves

            I had assumed (incorectly) that each element of the probes array was itself an array. I should have looked at the object model more closely 🙂

            undefined 1 Reply Last reply 30 Mar 2024, 19:49 Reply Quote 0
            • undefined
              gtj0 @stuartofmt
              last edited by 30 Mar 2024, 19:49

              @stuartofmt said in Custom Java Script in config file:

              : The default state is state.sensors.probes[0] = null

              Interesting. So there is an entry at index 0 but it's null? Can you share the relevant config bits that produce that situation? If it's too complicated, no worries, I'm just curious.

              undefined 1 Reply Last reply 30 Mar 2024, 21:53 Reply Quote 0
              • undefined
                stuartofmt @gtj0
                last edited by 30 Mar 2024, 21:53

                @gtj0

                This should clarify:
                Screenshot of Object Model from DWC

                8f83b5fe-4bcc-4c0d-a6eb-593c6e4afc24-image.png

                Relevant setting on config.g (global.zprobe_index = 1)

                ;Setup BLTouch as probe 1. This is to allow probe zero to be free for analogue sensor e.g. simple piezo
                echo "Setting BLTouch"
                ; K = probe number P= probe type R=Settle time before probing H=Dive Height F=Probe Speed
                ; T=Speed between probes A=Max probes at each pont S=Repeat deviation
                M558 K{global.zprobe_index} P9 C"io2.in" R1 H10 F120 T6000 A5 S-1 ;probe 5 times and average
                M950 S{global.zprobe_index} C"io2.out" ; This is the mod probe - used for deploy and retract
                G31 K{global.zprobe_index} P25 ; set trigger threshold
                M280 P{global.zprobe_index} S160 ; reset any error conditions```
                1 Reply Last reply Reply Quote 0
                5 out of 7
                • First post
                  5/7
                  Last post
                Unless otherwise noted, all forum content is licensed under CC-BY-SA