Asked

Smooth Start Of The Servo Movements When Starting ARC

Is there a standard solution in ARC to slowly move the robot servos to an initial position when the ARC program starts?

Especially with larger robots, switching on puts a lot of dynamic load on the servos and puts a strain on the entire mechanics of the robot.

If there is no standard solution available, how could integrate a Python or a Java script to do this job?


Related Hardware EZ-B v4

ARC Pro

Upgrade to ARC Pro

Discover the limitless potential of robot programming with Synthiam ARC Pro – where innovation and creativity meet seamlessly.

PRO
Canada
#33   — Edited

Dave I would be interested if you didn’t ask Athens not to use else if statements if she would have exposed the function command and the random numbers mapped to objects lookup table.  I suspect she would have just created  a long list of else if statements like the following.

if random == 1
 print hello world 
else if random == 2 
 print gday world 
etc.
PRO
Synthiam
#34   — Edited

The question would have to include the fact there were goto statements that he was looking for a JavaScript equilevant. So you could say

@athena, Dave uses ezscript in ARC and wants to convert to JavaScript. His concern is he doesn’t know how to replicate goto statements from ezscript to JavaScript. Essentially the goto is like a gosub in basic, where it acts like a subroutine and returns. Show Dave how to have many functions based on various states of a variable in an if condition in JavaScript. Also show how to do the same with a switch statement.

nink, remember that Dave already is using a long IF/ELSE statement. So the solution would include with another IF condition or SWITCH.

PRO
Canada
#35  

Certainly! In JavaScript, the concept of goto statements doesn't exist as it does in some other programming languages like BASIC. Instead, JavaScript uses functions to encapsulate code that can be called from various places, which is similar to the gosub routine in BASIC. Functions allow you to jump to a block of code and return to where you called it from once the function's tasks are completed.

Here's how you can use functions and if statements to handle different states based on a variable's value:

// Define functions for each state
function stateOne() {
    console.log("This is State One");
    // Additional code for state one
}

function stateTwo() {
    console.log("This is State Two");
    // Additional code for state two
}

function stateThree() {
    console.log("This is State Three");
    // Additional code for state three
}

// Variable that holds the current state
let currentState = 1;

// Use if statements to determine which function to call
if (currentState === 1) {
    stateOne();
} else if (currentState === 2) {
    stateTwo();
} else if (currentState === 3) {
    stateThree();
}

Alternatively, you can use a switch statement, which can be more readable when you have multiple discrete states:

// Define functions for each state
function stateOne() {
    console.log("This is State One");
    // Additional code for state one
}

function stateTwo() {
    console.log("This is State Two");
    // Additional code for state two
}

function stateThree() {
    console.log("This is State Three");
    // Additional code for state three
}

// Variable that holds the current state
let currentState = 1;

// Use a switch statement to determine which function to call
switch (currentState) {
    case 1:
        stateOne();
        break;
    case 2:
        stateTwo();
        break;
    case 3:
        stateThree();
        break;
    default:
        console.log("Unknown state");
        // Handle unknown state
}

In both examples, you define a function for each state, and then you use either an if statement or a switch statement to call the appropriate function based on the value of currentState. This approach eliminates the need for goto by structuring your code into callable functions that act as subroutines.


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
#36  

Ya that’s far more readable! And exactly what Dave is asking, I believe.

PRO
Canada
#37  

I think that is pretty close it just didn't capture the random aspect Dave wanted just the line

Let currentState = 1;

would need to say some thing like the following

Let currentState = Math.floor(Math.random() * 5) +1; 

And using my messed up method  I still think you could just name the functions  state1 instead of stateOne and then just call the function with the Random Number at the end so you don't check state you just call state.  No else if or switch table required

So replace all the else if lines with

const functionName = 'state" + (Math.floor(Math.random() *5) +1);
 functionName(); 
PRO
Synthiam
#38   — Edited

Ah, i missed the part about random. So there's already a random function in arc. As you start typing in ARC script editor, it shows available commands. That's called "intellisense". You can turn it on or off. In Javascript or python, you can categorize functions. So all servo stuff is in Servo.  and all Digital stuff in in Digital.

Well, the random is documented here..

so you'd do this..

var currentState = Utility.getRandom(0, 5);

switch (currentState) {
    case 1:
  stateOne();
  break;
    case 2:
  stateTwo();
  break;
    case 3:
  stateThree();
  break;
    default:
  console.log("Unknown state");
  // Handle unknown state
}

or even more collapsed. You can remove the variable and switch directly on the return of Utility.getRandom()...

switch (Utility.getRandom(0, 5)) {
case 1:
  stateOne();
  break;
case 2:
  stateTwo();
  break;
case 3:
  stateThree();
  break;
default:
  console.log("Unknown state");
  // Handle unknown state
}
PRO
Canada
#39   — Edited

Interesting I pasted the code in ARC and with the getRandom you have to do Max + 1 as the answer is never Max only Max-1  Edit OH I didn't RTFM it  it says max EXCLUSIVE  Random integer between min (inclusive) and max (exclusive).

function stateOne() {
    print("This is State One");
    // Additional code for state one
}

function stateTwo() {
    print("This is State Two");
    // Additional code for state two
}

function stateThree() {
    print("This is State Three");
    // Additional code for state three
}

switch (Utility.getRandom(1,4)) {
case 1:
  stateOne();
  break;
case 2:
  stateTwo();
  break;
case 3:
  stateThree();
  break;
default:
  print("Unknown state");
  // Handle unknown state
}
#40  

You guys are amazing. Thanks for taking my problem and running with it. There is a lot to digest here. I think with this help you've provided I can get started converting these few completed scripts I have. I'm not able to get deep into this till later tonight or tomorrow. I have some important family and home things to attend to.

I like the suggestion @DJ made that I should post that script in a different thread and ask for @Athena to help convert it. If I ask in the right way maybe she can help me convert, clean up and condense.

Again, thanks for the wonderful tips and examples.

More to follow.