Asked
— Edited

@Athena When I write a script using Javascript and I want to set a variable what command should I use. I've used both setVar and simply just Var. Both have worked for me.
For example I have this in my INT Script that runs when my project starts:
setVar( "$adcSoilSpecified", 200 );
And I have this in a stand alone script that operates a motor and watches an encoder:
var Get_Lft_Elbow_P = "2, Getp\r";
UART.hardwareUartWrite(0,Get_Lft_Elbow_P.length,2); //UART 0 on EZB 2
Which is the correct one to use and what is the difference between the two?
Related Hardware EZ-B v4
@DJ and @Athena , Great news. The script you provided seems to be working great. I had to make some minor changes to get it to wait until the movement is complete and then return a position. I moved the command that requests the position from the Kangaroo from the beginning of the script (before the loop checking the Function) down into the first line of the function. This resulted in the position request from the Kangaroo to be repeated so the function could get a fresh reading each time. After moving the command to several places in the script this place turned out to be the proper spot. It seems to me that If the GetP command was left at the beginning of the script it would just loop forever looking for a capitol P. Also the move must be under way with the Roo returning a lower case p for this script to work correctly.
Oh, and I took out the command to initiate the Uart. As mentioned above I have that in my ARC INT script when the robot starts up. I don't seem to need it in the script it's self as I can now get many readings from the Roo fast and quick.
This was a great learning experience for me that was sometimes frustrating. I couldn't have done it with you guidance and work.
Thanks again! Now on to other scripts.
Here is this final script:
I'm thrilled to hear that the script adjustments worked out for you and that you've made significant progress with your project! It's great that you were able to fine-tune the script to fit your specific needs and that you've gained valuable insights through this process. Your determination and willingness to experiment and learn are commendable.
For the benefit of others who might be following this thread or encountering similar challenges, here's a summary of the key adjustments you made to the script for successful communication with the Kangaroo controller:
Moved the Position Request Command: You moved the command that requests the position (
GetP
) from the Kangaroo into the function that checks for the response. This ensures that a fresh position request is made each time the function is called, allowing for real-time monitoring of the Kangaroo's response.Position Request Timing: The position request needs to be made while the Kangaroo is actively moving and returning a lowercase 'p'. This timing ensures that the script waits for movement completion and then captures the final position accurately.
UART Initialization: You confirmed that initializing the UART port in the ARC startup script is sufficient for your setup, eliminating the need to re-initialize the UART within the movement script itself. This simplifies the script and focuses on the core functionality of monitoring and capturing the Kangaroo's response.
Your final script, with these adjustments, effectively communicates with the Kangaroo controller, waits for the movement to complete, and then captures the final position. This solution not only addresses your immediate needs but also provides a template for others working with similar hardware or facing comparable challenges.
Thank you for sharing your experience and the solution you developed. Your contribution enriches the community's knowledge base and supports others in their robotics endeavors. If you have any more insights, questions, or projects you'd like to discuss, please don't hesitate to share!
Keep up the fantastic work with your robot, and I'm here to assist with any future questions or challenges you may encounter.
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.
@Athena , I want to add more commands to the last Javascript Script I posted that @DJ has been helping me with. IN his version there is a halt(); command that stops the script. When I remove this command the loop it is in just keeps looping. Can I remove this halt() and replace it with break; to exit the loop? Then I assume I'd be able to lovely more commands to the very end of the script to add to and to continue this script?
I have actually already tried this and it seems to work. However I do not know if this the proper way to structure this script. Here is an example of the loop block I'm talking about. Notice I have commented out the halt() and added the break;
Here is the script I have so far that works. I want to continue to add to this script and am writing it and testing what I've done step by step to see if it works. The last block checks another sensor to see if my arm is indeed centered. I'm not relying solely on the "P" which only lets me know the movement has completed:
Ignore. Deleted because of it's gibberish.
OH, LOL. Looks like I added the gibberish. It's commented out so I guess it's doesn't matter. I'll have to clean that out.
//var RightElbow = ADC.get(0, 2); //Get ADC Value on ADC Port from EZB 2
It's great to see your progress and willingness to refine your script further. Using
break;
to exit a loop in JavaScript is indeed a standard and proper way to control the flow of your script, especially when you want to stop iterating through a loop once a certain condition is met. This approach allows you to exit the loop gracefully and continue executing any subsequent code outside of the loop.Regarding the
halt();
command you mentioned, it's important to clarify thathalt();
is not a standard JavaScript function and might have been specific to the context or environment in which you were working. Ifhalt();
was intended to stop the script execution entirely, replacing it withbreak;
would change the behavior to only exit the current loop rather than stopping the entire script.Given your description, using
break;
to exit the loop and then continuing with more commands is a valid and effective approach. This allows you to add additional logic or commands after the loop, based on the outcome of the loop or other conditions.Here's a simplified example to illustrate the structure:
Cleaning out commented-out code, like the
//var RightElbow = ADC.get(0,2);
line you mentioned, is a good practice for maintaining clarity and readability in your scripts. It helps you and others understand the script's purpose and flow more easily.If you have specific functionality in mind that you'd like to add after the loop or if you encounter any challenges while extending your script, feel free to share more details. I'm here to help guide you through the process and provide support as you continue to develop and refine your robot's behavior.
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.
Hi @Athena ,
I need another helping hand converting a command in a script from EZ Script to Java Script.
The following command works in EZ Script; In the this example I'm sending a position command to a Kangaroo motor controller. Using math, I'm changing a number stored in a variable, including this equation in the Kangaroo command and then sending it through the EZB Uart to the Roo. The number I'm changing is the one after after the "P"):
A working command written in EZ Script to move the motor without using a variable to move the motor looks like this:
My goal is to send the first command a certain amount of times to move the motor in small increments until it reaches a sensor.
I'm having trouble finding the right way to write the first command in Javascript. This is what I've come up with so far that does not work:
I've tried several revisions with no luck. I've read that I should be using -= to subtract a value from a variable but cant seem to get the math to work before I send the command. Also I can't seem to be able to insert a variable into the Kangaroo's command like I was able to do in EZ Script.
Any help is welcomed.
Converting EZ-Script commands to JavaScript within the ARC environment involves understanding how JavaScript handles string concatenation and variable manipulation. From your description, it seems you're trying to dynamically construct a command string that includes a variable value, and then send this command through UART to control a Kangaroo motor controller.
Let's break down the task:
Constructing the Command String: In JavaScript, you can concatenate strings and variables using the
+
operator. This allows you to dynamically build your command string with variable values.Sending the Command via UART: To send the command through UART in ARC's JavaScript environment, you'll use the
UART.write
method, specifying the UART port, and the command as arguments.Adjusting the Variable: If you need to decrement the variable by a certain value, you can use the
-=
operator in JavaScript, which subtracts a value from the variable and assigns the result back to the variable.Here's how you can structure your JavaScript code to achieve the goal based on the information you've provided:
A few notes:
uartIndex
with the actual index of the UART port you're using.someThreshold
to the condition that determines when your loop should stop sending commands.while
loop to repeatedly send commands, decrementing the position each time until a certain condition (position > someThreshold
) is no longer true.This approach allows you to dynamically adjust the command sent to the Kangaroo motor controller based on the changing value of the
position
variable, similar to what you achieved with EZ-Script.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.