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
Implementing a counter in your loop to prevent infinite looping and ensure the script stops after a certain number of iterations is a straightforward task. Based on your description, you want the script to attempt re-centering the arm, and if it doesn't hit the opto switch within 25 to 35 loops, the script should stop to prevent reaching the end limit.
Here's how you can modify your loop to include a counter and a maximum number of attempts. This example assumes you have a function or condition to check if the opto switch is triggered (
optoSwitchIsTriggered()
as a placeholder) and a way to move the arm in small increments (moveArmIncrement()
as a placeholder):In this example, the loop will continue to attempt re-centering the arm by calling
moveArmIncrement()
until either the opto switch is triggered (indicating successful re-centering) or the maximum number of attempts (maxAttempts
) is reached. If the maximum number of attempts is reached without triggering the opto switch, the script logs a message, and you can add any additional error handling or recovery actions in that section.Remember to replace
optoSwitchIsTriggered()
andmoveArmIncrement()
with your actual implementation for checking the opto switch status and moving the arm. Thesleep(250)
function is used to add a delay between attempts, which you can adjust or remove based on your needs.This approach ensures that the script will not loop indefinitely and provides a mechanism to handle situations where the arm cannot be re-centered as expected.
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.
I do believe I have this script working now with all the guidance from @DJ and @Athena. Thanks once more. Between your help and the studying I'm doing in the internet in places like W3 Schools I'm learning a lot. I'm even starting to be able to write the Jacascript in proper styles. Spaces, indents and Semicolons aren't such a mystery anymore. Of course I still have lots to learn but it's coming.
So here's the completed and working block of code I (we) wrote. I'm using a Do/While loop as is seems easier for me to see what's going on and seems more logical to me to use here. So, once the arm move is complete to the center of it's ARC it will check the opto switch closed through the ADC reading. It will also keep a count of the loops it takes to move the motor to the Opto. I know it's about 20 or so loops using the speed and sleep setting I've written in. If it counts more then 25 loops the whole script will stop. That will bring the whole robot to a stop and at this time that's OK. I do not want the arm trying retract into the robot or the robot to move around with the arm sticking out. Until i come up with a safe reset retinue this will be great.
I'm just attaching the working block of this script that will center and check the left arm. if it's not centered on the first move then it will move it a little ways away and slowly retry. Thanks for looking and all the help. I'm always open to listen to suggestions or constructive criticism.
It's fantastic to hear that you've made significant progress with your script and are gaining confidence in writing JavaScript! Your willingness to learn and apply new concepts is commendable. The approach you've taken with the do/while loop and incorporating a counter for loop iterations is a solid strategy for ensuring your robot's arm movements are precise and safe.
Here's a general review and suggestion for your described approach:
Do/While Loop: Using a do/while loop is indeed a good choice when you want the loop to execute at least once before checking the condition. This fits well with your scenario where you want to attempt to center the arm and then check if the operation was successful.
Loop Counter: Implementing a loop counter to prevent infinite looping is a best practice, especially in robotics where physical movements are involved. Setting a reasonable threshold for the number of attempts helps ensure safety and prevents potential damage.
Opto Switch Check: Using an opto switch as a feedback mechanism for arm positioning is a smart way to achieve precise control. It's great that you're checking the opto switch status to confirm the arm's position.
Error Handling: Having a plan for what to do if the arm doesn't center correctly is important. Stopping the robot and preventing further movements until the issue is resolved is a safe approach. In the future, you might consider adding more sophisticated error recovery routines, such as attempting to re-center the arm a certain number of times before giving up.
Code Readability: It's great to hear that you're focusing on writing clean and readable code. Proper formatting, such as consistent indentation, spacing, and the use of semicolons, makes your code easier to understand and maintain.
Without seeing the specific code block, these are general observations and suggestions based on your description. If you have any specific questions or need further assistance with your script, feel free to share more details or ask for help.
Keep up the great work, and remember that learning to program effectively is a journey filled with continuous improvement. Your dedication to learning and applying best practices will undoubtedly lead to success in your robotics projects.
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, Is there a way to clear or delete a global variable that is set into my ARC project using Javascritp? EZ Script had a way to clear a variable but I can't see one for Javascript that seems right.
I have a global variable called $RtArmRailPosition and it's value is 800. I don't think it matters but this value was created and placed into a Javascript VAR variable from a reading of a motor encoder. I used the following javascript command to then set the Javascript VAR variable into my ARC project as a Global $ variable:
This global variable may interfear with other scripts that will run and I want to delete, reset or clear it. Any suggestions?
Edit to my last post: I may not need to clear the variable I was talking about above. I hope I fixed a script that was causing this issue and the variable will update it's value instead. It's a learning process. LOL. Still I'm interested in finding out the answer to my question above.
Remember that a global variable allows access between scripts. If you're not using the value of that variable in other scripts, don't make a it global.
Also, use a different variable name if you don't want them to interfere. Because "clearing it" just means setting it to a different incorrect value that the other script would use.
Lastly, using the same global variable across scripts that share it is a bad idea because they can run at the same time.
You didn't say why it was global, but if it doesn't need to be, don't make it global. Don't use setVar() or getVar() on it.
Thanks @DJ. That all makes sense and helps.
@Athena ,
I've repurposed the script below for use watching a different motor controlled by a Kangaroo motor controller and a Sabertooth. I've made the correct changes to reflect the new ports and EZB numbers and simple serial commands being sent to the Kangaroo through an EZB Uart port. Everything works as expected. Except......
On the new motor setup the motor reading being retrieved from the Kangaroo goes below zero to -20. So the position numbers for the range of motion are between -20 and 800. The script runs correctly and returns the expected motor position as long as the returned value stays above zero. Once the returned value drops into the negative numbers I get back incorrect position values. Also the bytes being read in the Uart have changed from 9 to 8 but I think I've got that fixed by changing that value to 8 in these lines. I think:
I've tried many different changes to this script but can't seem to get it to work once the position value drops below zero. Can you advise on the proper changes to get it to work with negative returned numbers? Also am I correct about changing the value in the Uart read and available lines? Thanks.