Migrating from EZ-Script to JavaScript

Description

How to move your code from EZ-Script to JavaScript. JavaScript offers faster execution and more features.

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)

User-inserted image

EZ-Script (2 minutes, 5 seconds)

User-inserted image


ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

PRO
Synthiam
#9  

That's what super heros are made of!

#10   — Edited

Actually i was adding some script codes for my Init servos start up and other start up functions, then realized I used EZ script instead of Javascript. Since Javascript is way faster ,what is the easy way to convert the EZ script over to Javascript? right now it just wants to delete the whole EZ script when I move into Javascript window. Like if I copy and paste it in, will it convert? I guess I can just start over line by Line with the cheat helper 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.

PRO
Synthiam
#11  

*Note: I moved your question to the appropriate thread because it was asked on a feature request for Blockly copy and paste

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.

#12  

Ahh yes That was what I was looking for thanks again, sorry for asking twice,LOL!

Kyrgyzstan
#13  

How to  include a javascript library ?

PRO
Synthiam
#14  

There used to be an option for that. Hmmm. I can't seem to find it now. I'll have to take a better look as I don't see it in the documentation on the support section.

PRO
USA
#15  

....just stumbled onto this...thank you so much.

#16   — Edited

OK, I'm having trouble wrapping my mind around converting my EZ Script to JavaScript. I do think I understand some of the basics ways to write in JS like loops, classes and variables. However I I have a very complicated but working EZ script I wrote a few years ago I'm trying to convert to JS but I don't understand some of the proper ways to write commands.

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:

uartWrite(2, 2, "2,p805 s300", 0x0d)

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?

UART.uartWrite(2,"2,p805 s300", 0x0d,2);

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:

# Get Right ARS Roo Position
uartWrite(2, 2, "1, Getp", 0x0d)

:waitFor_Rt_Data
Sleep(30)

$y = UartAvailable(2, 2)
# Sleep(30)
# print("Bytes in buffer:" + $y)
if ($y < 6)
# Go back and wait for data again because we did not receive the least number of expected bytes.
goto(waitFor_Rt_Data)
endif

$Get_Rt_Carrage_P = UARTRead(2, 2, $y)
# Sleep(30)
# print("Received:" + $Get_Rt_Carrage_P)
Sleep( 150 )

# -------------------------------
# Find out (and print if wanted) if the P is upper or lower case.
# Upper case means the motor has finished moving and stopped.
# Lower Case means that the motor is still moving.
$P_or_p = GetCharAt($Get_Rt_Carrage_P, 2) #Assign a variable to either a capital P or Lowercase p returned by Kangaroo.
# Print("P_or_p:" + $P_or_p)

# -----------------------------
# # Defeat the P_or_p because it must die!
if ($P_or_p = "p")
$Rt_Carrage_Position = Split($Get_Rt_Carrage_P, "p",1) #1,pXXXX is returned. Get everything after the Lowercase p (P is case sensitive).
ELSE ($P_or_p = "P")
$Rt_Carrage_Position = Split($Get_Rt_Carrage_P, "P",1) #1,pXXXX is returned. Get everything after the Upper case P (P is case sensitive).
endif

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.

if ($Rt_Carrage_Position >= 751 and $Lft_Carrage_Position >= 751)
$Both_ARS_Extended = 1 #Rt Carriage fully out. Set this variable so the waiting Automation Script knows the carriage is fully out.
ELSE ($Rt_Carrage_Position < 750 and $Lft_Carrage_Position < 750)
Goto (Wait_ARS_Loop) # Go back and do this again because the carriage is not fully out.
endif
# Arms should now be fully out and arms can safely move.

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.

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!

# Move Both Carriages Out of torso
# -----------------------------------------------------------------
$Both_ARS_Extended = 0 #Sets Variable to both ARS still in torso. 0=in, 1=out
ClearVariable("$Get_Lft_Carrage_P") #Clears Variable in case it's in an Array
ClearVariable("$Get_Rt_Carrage_P") #Clears Variable in case it's in an Array

Sleep(100)

uartWrite(2, 2, "1,p805 s300", 0x0d) #Move Rt ARS Carriage full out
Sleep(50)
uartWrite(2, 2, "2,p805 s300", 0x0d) #Move Lft ARS Carriage full out

# ------------------
# Loop to Read & send Carriage position info for Waiting Automation Arm
# Script so it can safely start moving arms when Carriage is fully out.
# The following will get the position of the LFt & Rt Kangaroo positions

# --------------------

:Wait_ARS_Loop

# Get Right Carriage Roo Position
uartWrite(2, 2, "1, Getp", 0x0d)

:waitFor_Rt_Data
Sleep(30)

$y = UartAvailable(2, 2)
# Sleep(30)
# print("Bytes in buffer:" + $y)
if ($y < 6)
  # Go back and wait for data again because we did not receive the least number of expected bytes.
  goto(waitFor_Rt_Data)
endif

$Get_Rt_Carrage_P = UARTRead(2, 2, $y)
# Sleep(30)
# print("Received:" + $Get_Rt_Carrage_P)
Sleep( 150 )

# -------------------------------
# Find out (and print if wanted) if the P is upper or lower case.
# Upper case means the motor has finished moving and stopped.
# Lower Case means that the motor is still moving.
$P_or_p = GetCharAt($Get_Rt_Carrage_P, 2) #Assign a variable to either a capital P or Lowercase p returned by Kangaroo.
# Print("P_or_p:" + $P_or_p)

# -----------------------------
# # Defeat the P_or_p because it must die!

if ($P_or_p = "p")
  $Rt_Carrage_Position = Split($Get_Rt_Carrage_P, "p",1) #1,pXXXX is returned. Get everything after the Lowercase p (P is case sensitive).
ELSE ($P_or_p = "P")
  $Rt_Carrage_Position = Split($Get_Rt_Carrage_P, "P",1) #1,pXXXX is returned. Get everything after the Upper case P (P is case sensitive).
endif
Sleep(50)

# -----------------------------------------------------------
# Get Lft ARS Roo position

uartWrite(2, 2, "2, Getp", 0x0d)

:waitFor_Lft_Data
Sleep(30)

$y_lft = UartAvailable(2, 2)
# Sleep(30)
# print("Bytes in buffer:" + $y_lft)

if ($y_lft < 6)
  # Go back and wait for data again because we did not receive the least number of expected bytes.
  goto(waitFor_Lft_Data)
endif

$Get_Lft_Carrage_P = UARTRead(2, 2, $y_lft)

# Sleep(30)
# print("Received: " + $Get_Lft_Carrage_P)
Sleep( 150 )

# -------------------------------

# Find out (and print if wanted) if the P is upper or lower case.
# Uppercase means the motor has finished moving and stopped.
# Lower Case means that the motor is still moving. We need to know so we can move past this to the rest of the script.

$P_lft_or_p_lft = GetCharAt($Get_Lft_Carrage_P, 2) #Assign a variable to either a capital P or Small case p returned by Kangaroo.
# Print("P_lft_or_p_lft:" + $P_lft_or_p_lft)

# -----------------------------

# Defeat the P_lft_or_p_lft

if ($P_lft_or_p_lft = "p")
  $Lft_Carrage_Position = Split($Get_Lft_Carrage_P, "p",1) #1,pXXXX is returned. Get everything after the Lowercase p (P is case sensitive).
ELSE ($P_lft_or_p_lft = "P")
  $Lft_Carrage_Position = Split($Get_Lft_Carrage_P, "P",1) #1,pXXXX is returned. Get everything after the Upper case P (P is case sensitive).
endif

  # ------------------------------
  # The following will loop and read both of the actual ARS carriage positions
  # until both carriages are full out past 750 (830 is actually full out but
  # this starts the animation a little early to take away any delay).
  # Sleep(150)
  # Get$Rt_Carrage_Position
  # Get$Lft_Carrage_Position
  # Sleep(500)
Print($Rt_Carrage_Position)
Print($Lft_Carrage_Position)

if ($Rt_Carrage_Position >= 751 and $Lft_Carrage_Position >= 751)
  $Both_ARS_Extended = 1 #Rt Carriage fully out. Set this variable so the waiting Automation Script knows the carriage is fully out.
ELSE ($Rt_Carrage_Position < 750 and $Lft_Carrage_Position < 750)
  Goto (Wait_ARS_Loop)  # Go back and do this again because the carriage is not fully out.
endif
  # Arms should now be fully out and arms can safely move.