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
Converting an EZ-Script with Goto statements, Labels, Return, Repeat, If Statements, and random number generation to JavaScript can be quite involved, especially for a script as long as 1400 lines. However, I can guide you through the process and provide you with the JavaScript equivalents of these constructs.
In JavaScript, instead of using Goto and Labels, we use functions to organize code into reusable blocks. The Return statement is used to exit a function and optionally return a value. Loops in JavaScript can be created using
for
,while
, ordo...while
statements. Conditional logic is handled withif
,else if
, andelse
statements. Random numbers can be generated usingMath.random()
.Here's a simplified example of how you might start converting your EZ-Script to JavaScript:
EZ-Script:
JavaScript Equivalent:
In this JavaScript example, we define functions for each section of your EZ-Script. The
start
function begins the process, andrandomImpersonation
decides which character to impersonate based on a random number. Each character has its own function, such asimpersonateDrSmith
andimpersonateWillRobinson
.Please note that this is a simplified example, and your actual script will likely be more complex. You'll need to translate each part of your EZ-Script into JavaScript functions, loops, and conditional statements. Additionally, you'll need to handle global variables and integrate with ARC's JavaScript API for controlling your robot.
Given the complexity and length of your script, it may be best to break down the conversion process into smaller, manageable parts and test each part thoroughly before moving on to the next. If you encounter specific issues or need help with particular parts of the script, feel free to ask, and I'll do my best to assist you.
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.
Ugh, this is why ezscript needs to be shot. Your code doesn't work as you think it does because ezscript is so broken.
Here is an example...
A condition consists of IF, ELSE IF, and ELSE. An ELSE cannot have a condition included because it's the "all else that's left over"...
In your code, this should NOT compile and, therefore, does not work as you think it does. The $resposne_dr_smith = 2 will never reach anything. This doesn't actually work at all...
There's a lot of code there... but it's mostly just many IF conditions. So that can be replaced with javascript easily - however, you'll need to type a lot. I can provide a bit of help in a future response after i look at it for a bit.
Canceled. Nothing to see here.
Why did you delete the last script?
Thanks DJ. Actually I did not realize the script wasn't working correctly. It would run and respond with different characters and responses. I did think over time that I really didn't get enough responses from the Dr Smith section though. I really want to get this converted and will be studying and working on it soon. In the mean time I guess I need to look at the problem you found and get it fixed. LOL. Thanks!
Argh, i started to edit your original script but noticed you changed it. So i just did a bunch of work for nothing. But anyway. I was able to use search and replace to change most of it. The only thing left is to change the goto's to functions. And then add more opening braces { on the end of the IF conditions. I even fixed the broken ELSE conditions.
revised: revised script 1.txt
But, that's the old version of teh script you uploaded so i dunno to do now.
@DJ,
Woops, I couldn't see it on my side. I thought it didn't post for some reason. I'll try again.Here's a second long EZ Script I want to work on converting to JavaScript. It's the same impersonation skit but only runs once with one character's impression but selects randomly. Foy your approval and amusement. LOL:
Hahah here's your code as a file: SAVE FILES INSTEAD OF GIANT PASTES.txt
I can't wrap my head around how fast you were able to convert and fix this to this level. You must have super powers.
I'll study what you sent in that link and try to learn so I can finish it up.
Thanks again my friend. You're the best.
Okay so this is almost perfect. I haven't tested any logic but most of the code has been converted: revised script 2.js
Hey @DJ, if you're still there, I noticed in your changes that you also removed the Endif commands after my IF, ElseIf statements. After looking around tutorials on the internet I don't see any mention of having to use Endif with JS. Was this just an EZ Script thing?
Wow, nice. I'm flabbergasted that you did this for me. Using this I think I can learn much easier how to proceed with converting my other scripts. They are a lot less complex. Thanks for all your work. I wish I could buy you a beer or something to show my gratitude.
Okay i think this is everything. Take a quick look and see if there's something i missed or any errors. I can't run it becuase i don't have the main project but it looks fine: revised script 2.js
Actually here, this has some additional comments that might make more sense to you: revised script 3.js
Again, I can't say how much I appreacheate your work on this.
I did notice there were a few GoTo's in your first revision but they are gone now. Looking over this quickly it looks good as far as I know. Your work has helped me see how everything needs to be. I still need to do more studying to really grasp it though.
I'll have to wait till tomorrow and run this new script on the robot to see if it flows.
I'll let you know how it goes.
Just remember the only difference is how if conditions and loops use curly braces.
@athena give Dave examples of how curly braces are used in JavaScript with arc
Certainly! In JavaScript, curly braces
{}
are used to define the start and end of a block of code associated with control structures such asif
,else
,for
,while
, and functions. Here are some examples to illustrate their use in the context of ARC JavaScript:If Statement
For Loop
While Loop
Function Definition
Example in ARC JavaScript
Here's an example of how you might use these structures in an ARC JavaScript script:
In this example, we define a function
moveServoRandomly
that moves a servo to a random position. We then use anif
statement to check if the robot is not speaking before moving a servo. We also use afor
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.
@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:
This is the correct format with the parentheses added that defines the action that works:
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.
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
because Hello World is a string parameter and therefore needs to be in quotes like this...
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.
@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.
Love this thread. It is inspiring me to start using JavaScript now! Thanks Dave and DJ!
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.
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:
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.