Asked — Edited

Converting EZ Script To Javascript That Jumps Around With Goto Commands &Amp; Labels

EDIT: I screwed up and inserted the wrong script but have now replaced it with the proper one. This one does repeat 3 times. I'll do a septate post with the script that only runs once. Sorry for any confusion.

Hi, as requested in another thread I'm posting the following script and asking @Athena to help convert it to Javascript. It's very long at over 1400 lines. I'm posting the entire script because as it's executed it jumps around a lot from top to middle, then back towards the top then to the end.

*This EZ Script lets my B9 robot from the TV show Lost In Space do impersonations (or impressions) of all the characters of that show (sound clips stored in various ARC Soundboards) including Dr Smith, Will Robinson and his family. It will also do impressions of other famous robots like Hal, Robby the Robot, Bender, R2D2 and other film and TV robots along with a few special effect sounds. Each character has many different sound clips (from 4 to as many as 30 or more) that can be spoken. *Each sound clip is assigned it's own number and they are selected using the random number generation command in ARC. This gives statements, answers and the impersonations process a truly random feel.  *Each time the script runs it will have the robot say three different random characters impersonations. Each character's impersonations is said only once (using sound clips stored in various ARC Soundboards) and each of the clips will be selected randomly.

I've written this EZ Script using GoTo commands, Labels, Return, Repeat, If Statements and a lot of random generation of numbers to choose sound files that make the robot respond differently each time and his impersonations random. I also use Controlcomand() to start up other scripts and AutoPosition controls that run anamations, servos and DC motors. In addition it relies heavily on Global Variables. Kind of like this:

"Robot, Do an impersonation of Dr Smith".

*The EZ Script pauses the Speech Recognition and Personality Generator then a GoTo command will send it to the section where the robot says a random acknowledgment that he heard you then it will return to where is came from.  *Then it will be sent to the random character section (Each character has his own section). *This will repeat three times with the robot making a statment between each impression. This is done using the GoTo command. *Once the impersonation process has repeated three times the Goto command will send the script to the end of the script for the robot to do a closing routine and return all motors and functions to a rest or starting position.  *Finally all Variables will be reset.

The following is the EZ Script I need to convert to JavaScript. @Athena , can you help me convert it? The script includes Goto statements, Labels, Return, Repeat, If Statements and a lot of random generation of numbers for subroutines and random selection of states. I would appreciate your guidance on how to translate this functionality into JavaScript:

I welcome any advice from anyone else. Thanks!

File: original ezscript.txt


ARC Pro

Upgrade to ARC Pro

Experience the transformation – subscribe to Synthiam ARC Pro and watch your robot evolve into a marvel of innovation and intelligence.

PRO
Canada
#17  

Certainly! In JavaScript, curly braces {} are used to define the start and end of a block of code associated with control structures such as if, else, for, while, and functions. Here are some examples to illustrate their use in the context of ARC JavaScript:

If Statement

if (condition) {
  // Code to execute if the condition is true
} else {
  // Code to execute if the condition is false
}

For Loop

for (initialization; condition; increment) {
  // Code to execute on each loop iteration
}

While Loop

while (condition) {
  // Code to execute as long as the condition is true
}

Function Definition

function myFunction(parameters) {
  // Code to execute when the function is called
}

Example in ARC JavaScript

Here's an example of how you might use these structures in an ARC JavaScript script:

// Define a function to move a servo to a random position
function moveServoRandomly(port) {
  var randomPosition = Math.floor(Math.random() * 180) + 1; // Random position between 1 and 180
  Servo.setPosition(port, randomPosition); // Move servo to the random position
}

// Check a condition and call the function if true
if (getVar("$IsSpeaking") === false) {
  moveServoRandomly("D0"); // Call the function with the servo port
} else {
  console.log("The robot is currently speaking.");
}

// Loop through a set of actions
for (var i = 0; i < 5; i++) {
  moveServoRandomly("D1"); // Move a different servo
  sleep(1000); // Wait for 1 second
}

In this example, we define a function moveServoRandomly that moves a servo to a random position. We then use an if statement to check if the robot is not speaking before moving a servo. We also use a for loop to move another servo five times, pausing for one second between each move.

Remember that in JavaScript, the use of curly braces is essential for defining the scope of code blocks, and every opening brace { must have a corresponding closing brace }.


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.
#18  

@DJ, I installed this last revised you worked on for me and it runs beautifully. I only had to go in and define the soundboard sound tracks that the ConrtolComands called. I just needed to add parentheses to define them.

Just for the record, this is an example of what I had that didn't work in the JS Script:

controlCommand("Soundboard v4", Track_64)

This is the correct format with the parentheses added that defines the action that works:

controlCommand("Soundboard v4", "Track_64")

I didn't have the parentheses in EZ Script and everything worked fine. I'm surprised. I do see in the EZ Script Cheat Sheet that this command should have the parentheses. It must be one of your magic tricks you have hidden behind the EZ Script curtain. LOL

Again, thanks for all your work on this and the wonderful JavaScript lesson.

PRO
Synthiam
#19   — Edited

Nice - that's great to hear. Sorry i forgot about the plain text arguments.

In real programming languages, plain text arguments that are strings need to be surrounded by "quotes".

So, you can't do something like this

print(Hello World")

because  Hello World is a string parameter and therefore needs to be in quotes like this...

print("Hello World")

The same thing applies to any function that takes string arguments. In your case, it was the control command. Therefore, the ControlCommand() needed a string as a parameter which is Track_XXXX. Therefor, you need to surround "Track_XXXX" in quotes.

#20  

@DJ, No need to be sorry for anything. You went above and beyond for me in this conversion and the lesions you taught me. Again, many thanks. 

#21  

Love this thread.  It is inspiring me to start using JavaScript now!  Thanks Dave and DJ!

PRO
USA
#22  

Dave.  I have a question did you convert from EzScript the commands to move your torso?   If you did could you provide me a piece of code.  I cant seem to get the EzScript to convert correclty.  Here is my EzScript  uartWrite(4, 1, "1, p32455 s10000", 0x0d)    i've tried all the commands in the uart and i cant figure out the conversion.

#23   — Edited

Hi @dbeard. It's good to hear form you. I Hope all is well in your world.

I assume you're converting your EZ Scripts into JavaScript? Judging from the command you are using you're sending this command to a Sabertooth with a Kangaroo for position and speed control. If so, here's what I have for a JavaScript command to move my torso through my Kangaroo controller:

UART.hardwareUartWriteString(0,"1, p113261 s10000\r",1); // Move Waist Motor to 1/2 right Position, EZB 1, Uart Port 0

A few things to look at here;

*The first 0 is the Uart Port. This placement is different then in EZ Script (make sure you are Initializing your EZB Uart ports before you try to use them. You only have to do this once).

*The commands inside the parentheses " " are the Kangaroo commands. Notice I'm now using the \r as a return instead of 0x0d. The Kangaroo always needs a Return at the end of a command. Also, I think JavaScript may have issues with the 0x0d format but \r works.(make sure you are starting the Kangaroo Channels you are powering the motor with before you try to use them. You should only have to do this once).

*The last 1 is the EZB connection number. This placement is different then in EZ Script.

*The last ; is like punctuation in a sentence. It makes the commands easer to read but are not really necessary. Like some people never use in posts in social media. Don't get me started on that. LOL.

*Everything after the // are my remarks.

I hope this helps.