Migrating from EZ-Script to JavaScript
Description
How to move your code from EZ-Script to JavaScript. JavaScript offers faster execution and more features.
Related Robot Skills
Why?
Why migrate from EZ-Script to JavaScript? The answer is performance and functionality. While EZ-Script appears straightforward, it is very similar to JavaScript. The most significant differences will be outlined in this tutorial. However, JavaScript can organize commands in groups, called classes. These command groups make JavaScript more organized and easier to find the correct command that you're looking for.
Performance
The Synthiam implementation of JavaScript in ARC is fast... very fast! It outperforms EZ-Script by a significant magnitude. Take for instance, a benchmark between the two languages by counting to 1,000,000.
JavaScript (1.1 seconds)

EZ-Script (2 minutes, 5 seconds)

Edit.... I think I had a similar question last week about using my old EZ scripts and did you point out some tutorial Link? I seem to have forgot where.
The windows Copy and Paste command will copy the text. There is no way to "convert code" when pasting. It will merely paste what is copied. Once you paste the EZ-Script into the JavaScript window, you can edit the code to make it JavaScript syntax.
Here are a couple of the commands I'm having trouble understanding. I'll show the EZ Script command that works and try to show the way I think the JS command will work. If some one can guide me to the correct syntax I would be grateful:
In this first example I am sending a command through EZB 2, Uart 2 to a Kangaroo Motor controller. The Roo's command is inside the " ". It states which channel on the Roo to start the move on, the position to move to and at what speed. The next command in the line, 0x0d, is the ASCII value for the Return Key on a keyboard. The Roo needs this to accept the command:
Below is what I think the JS Syntax should be??? Notice the 0x0d ASCII value and the uart port and EZB index numbers. Do I need those quote marks? How close am I in being correct?Code:
Code:
Now, My next big problem. How to split and GetCharAt with JS. When the I request a position and the Kangaroo motor controller returns this position to ARC through an EZB Uart, it comes back looking like this: Pxxx or pxxx. Their will be either a upper or lower case P followed by the position number (xxx). An upper case P means the motor is still moving and a lower case means it's stopped. I wrote an EZ script that puts this returned value into a variable, splits the P from the position number, tells me if the P is upper or lower case and what the position number is. The way I'm using this script is that I ignore the P and only need to position number. However I need to split the P from the returned value and ignore it or the script wont work:
Then I have the EZ Script decide if the motor has fully deployed the payload and it's safe to continue or it needs to go loop back and do everything over again until the payload in in position. It then sets a global variable which tells a different script that is waiting on this that i's safe to proceed. This EZ script loops until that safe position is reached (805) and sets that variable.Code:
As you can see above this EZ Script is commanding two motors through one Kangaroo, I have no idea how to do the above with JS. I know I'll have to figure out a different way to do the looping as JS does not use GOTO and labels like this. If I could get some help with splitting returned characters and getting characters at a certain place this would be very helpful.Code:
Here's the complete EZ Script for reference if it helps: Be kind. It's probably messy. LOL. Any ideas or advice are very welcomed that will improve it or help with conversion to JS. Thanks in advance!
Code:
I'm making progress on making things work like checking if variables exist and initializing Uart ports. I'm been able to run several commands without errors so I think I'm catching on. However I've hit a wall.
After initializing the proper Uart port I decided to try to move a motor by sending a command through the port to the Kangaroo Motor controller. Like I mentioned and showed above I've been able to do this using EZ Script. I followed @DJ's tutorial and the online JavaScript API here on this forum in the support section with no luck. The script ran and compilated with out an error but the Kangaroo didn't respond. I think the problem is how I'm sending the string to the roo. It needs a Return (enter) keyed after the position and speed command and I don't think I have that properly written in JS. Here's the EZ Script command that works makes the Kangaroo respond. Notice the 0x0d (ASCII for carrage return) :
Here's the code I wrote in JS that I think is close (from what I read in the JavaScript API in the support section). The script completes and seems to send it through the UART. Again, I don't think I have the 0x0d part of string written properly. But I don't really know.Code:
Any help figuring this out would be welcomed.Code:
I found the answer on the Stackoverflow forum. A guy there was working on a different problem he was having with a Python script that controls his Kangaroo. I looked through his script and saw in his Python script that he has a \r inside his double quotes and the end of his string. I inserted it and it works!!
So the proper script that works (once the UART initialized) is as follows. Note the \r at the end of line 1:
var str = "2,p805 s300\r";
UART.hardwareUartWrite(2,str.length,2);
UART.hardwareUartWriteString(2,str,2);
I do have a question about one of the UART. commands that I hope some one could help explain. When I'm asking to read bytes from the UART buffer I seem to have two commands available:
When I run these commands with different scripts I get the same returned answers:Code:
Script 1 using UART.hardwareUartReadStringAvailable
Script 1 Answers:Code:
Code:
Script 2 using UART.hardwareUartReadString
Script 2 Answers:Code:
As I said, I get the same answers. The only differance I can see is that in UART.hardwareUartReadString I need to state how many bites I am expecting. This has proven a problem because I've notices that sometime the buffer has more then the usual 7 bytes available. Mostly after a failed read.Code:
Can someone explain which is the best command to use and/or why I would have to state how many bytes I'm expecting if I use UART.hardwareUartReadString ?
Thanks!
As I said, I'm a novice to JS and mostly understand how to write a working script in EZ Script. I'm starting to understand some of the basic nuances of JS and have made a converted working script. It looks much neater and runs much smoother and faster then my original EZ Script. At this point I'm satisfied with my result but after seeing how much I don't know about JS I'm not sure it's done as well as possible. However it works nice for my needs and I'm happy. LOL.
My intent is to move two carriages that are powered and controlled by a Sabertooth / Kangaroo motor controller. They must move out of an enclosed robot torso until they reach a save point before the arm servos can start moving around. I could just send the carriages out and hope they reach a safe point but if the arms starts moving around before they are clear they will destroy themselves. This script has a built in fail safe to keep that from happening. It monitors the progress of the carriage motor's encoders, Then when it reaches safety it sets a global variable that tells a different waiting script (it's an EZ Script at this point) that it's safe to start moving the arm servos.
Here it is for your enjoyment. LOL. :
Here's the console readout:Code:
Code: