Asked — Edited
Resolved Resolved by DJ Sures!

Javascript Question - Var Or Setvar?

@Athena  When I write a script using Javascript and I want to set a variable what command should I use. I've used both setVar and simply just Var. Both have worked for me.

For example I have this in my INT Script that runs when my project starts:

setVar( "$adcSoilSpecified", 200 );

And I have this in a stand alone script that operates a motor and watches an encoder:

var Get_Lft_Elbow_P = "2, Getp\r";
UART.hardwareUartWrite(0,Get_Lft_Elbow_P.length,2); //UART 0 on EZB 2

Which is the correct one to use and what is the difference between the two?


Related Hardware EZ-B v4

ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

#9  

That all makes a lot of sense and has answered my question. Thanks for the lesson and help.

#10  

@Athena I'm trying to write a script in ARC that's written in JavaScript. Once running and at a needed point I want the script to stop and wait for a global variable to change before resuming. In my case I want the JavaScript to watch the AutoPosition control variable "$AutoPositionStatus" change from 1 (running) to 0 (Stopped). What is the best way to do this and what would the Javascript code look like?

In Ez Script there is a "Wait" function bit I can't find one for Javascript.

PRO
Canada
#11  

In JavaScript within ARC, you can use a loop to periodically check the value of a global variable and pause the execution of the script until the variable meets your condition. Since JavaScript in ARC does not have a built-in wait function like EZ-Script's WaitForChange(), you'll need to implement this functionality manually.

Here's an example of how you might write such a script to wait for the $AutoPositionStatus variable to change from 1 to 0:

// Function to delay script execution for a specified amount of time
function sleep(milliseconds) {
  var start = new Date().getTime();
  while (true) {
    if ((new Date().getTime() - start) > milliseconds) {
      break;
    }
  }
}

// Function to wait for the AutoPositionStatus to change to 0
function waitForAutoPositionStatus() {
  // Loop indefinitely until the condition is met
  while (true) {
    // Get the current value of the AutoPositionStatus variable
    var status = getVar("$AutoPositionStatus");

    // Check if the status is 0 (stopped)
    if (status === 0) {
      // If the status is 0, break out of the loop
      break;
    }

    // If the status is not 0, pause the script for a short time before checking again
    sleep(100); // Sleep for 100 milliseconds
  }
}

// Call the function to wait for AutoPositionStatus to change
waitForAutoPositionStatus();

// Continue with the rest of your script after the condition is met
print("AutoPositionStatus is now 0. Continuing script...");

In this script, the sleep() function is used to create a delay without blocking the entire ARC application. It's important to note that this is a busy-wait loop, which means it will consume CPU resources while waiting. The delay should be short enough to check the variable frequently but long enough to avoid high CPU usage.

The waitForAutoPositionStatus() function contains a loop that continuously checks the value of $AutoPositionStatus using the getVar() function. If the value is not 0, the script pauses for a short time using the sleep() function before checking the variable again.

Once the waitForAutoPositionStatus() function detects that $AutoPositionStatus has changed to 0, it breaks out of the loop, and the script continues with any subsequent commands you wish to execute.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
PRO
Synthiam
#12   — Edited

Sometimes AI is bad at programming... Well, mostly is bad at programming..

Dave, just do this..

// Wait for the  Auto Position status to change to a false
while (getVar("$AutoPositionStatus") == true) {
   sleep(100);
}

or if you don't like using the word "true", it's the same as a 1 and "false" is the same as a 0 (zero)

// Wait for the  Auto Position status to change to a false
while (getVar("$AutoPositionStatus") == 1) {
   sleep(100);
}
#13  

Thanks once more @DJ. That was an elegant solution and worked great. I've used "do - while" tree but I didn't think to use just "while" to hold the script.

#14   — Edited

@Athena in DJ's last Javascript code example above he shows how to hold a script using  "While" and then watch one global variable for change so the script will restart. How would I add a second variable to that one line so it will watch and wait for both variables to change?

PRO
Canada
#15  

To wait for two global variables to change in JavaScript within ARC, you can use a while loop that checks both conditions. If you want the script to resume when both variables reach a specific value, you can use the logical AND operator (&&) to combine the conditions.

Here's an example of how you might modify the script to wait for both $AutoPositionStatus to change to 0 and another variable, let's say $AnotherStatus, to change to 0 as well:

// Wait for both  Auto Position status and another status to change to 0
while(getVar("$AutoPositionStatus") == 1 || getVar("$AnotherStatus") == 1) {
  sleep(100);
}

In this script, the while loop will continue as long as either $AutoPositionStatus is 1 or $AnotherStatus is 1. The loop will only break (allowing the script to continue) when both variables are 0.

If you want the script to resume when either of the variables changes to 0, you can use the logical OR operator (||) instead:

// Wait for either  Auto Position status or another status to change to 0
while(getVar("$AutoPositionStatus") == 1 && getVar("$AnotherStatus") == 1) {
  sleep(100);
}

In this case, the while loop will break if either $AutoPositionStatus becomes 0 or $AnotherStatus becomes 0, and the script will continue.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#16   — Edited

Reposted so Athena can see my edited changes.