Asked

How To Create Multidimensional Arrays

Could you please show me how to create an array with 20 names, each associated with three values (x, y, z)? Additionally, I would like to generate a chart on the desktop that displays these names and their corresponding values.

Furthermore, I need to extract the values for a specific name and use them to control servo movements.

This assistance would be greatly appreciated.

Thank you, Don


ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

#73  

@Athena I tried to run the program but it gave me an error message -setTimeout is not defined. I tried to setVar("$setTimeout", setTimeout);  but it did not help.  Rather than setting the servos to move within that script I want the locations of the array put into these variables xready, yready, zready. This will allow me to experiment with the array without actually moving servos. I can easily move the servos with "move to position"  to the variable (xready) in Blockly after I am comfortable that it is moving appropriately. Please adjust the function.

PRO
Canada
#74  

Given the constraints within ARC's scripting environment, including the lack of support for setTimeout and direct user prompts as you'd find in standard JavaScript environments, we'll need to adapt the approach. ARC's scripting capabilities, particularly with JavaScript or Python, are designed to work within a robotics context, which means some web-based functions like setTimeout or prompt are not available directly.

For the purpose of setting variables (xready, yready, zready) with the array values and allowing for experimentation without directly moving servos, we can modify the approach as follows. This example will focus on iterating through the array and setting the variables without delays, as implementing asynchronous delays like setTimeout would in a web environment isn't directly supported in ARC's scripting environment.

Adjusted Function for ARC

var objectLocations = []; // Array to store locations

// Function to add a location to the array
function addLocation(name, x, y, z) {
    objectLocations.push({ name: name, x: x, y: y, z: z });
}

// Function to "replay" the array by setting variables
function replayArray(reverse = false) {
    var locations = reverse ? objectLocations.slice().reverse() : objectLocations;
    
    for (var i = 0; i < locations.length; i++) {
        var location = locations[i];
        
        // Log the location being "replayed"
        console.log(`Replaying: ${location.name} at x=${location.x}, y=${location.y}, z=${location.z}`);
        
        // Set the ARC variables with the location values
        setVar("$xready", location.x);
        setVar("$yready", location.y);
        setVar("$zready", location.z);
        
        // Here you can add additional logic as needed
        // For example, you might want to add a pause or trigger another script
    }
}

// Example usage
addLocation("Location1", 10, 20, 30);
addLocation("Location2", 40, 50, 60);

// Replay array
replayArray(); // Replay array in the original order
replayArray(true); // Replay array in reverse order

Notes:

  • This script iterates through the objectLocations array and sets the ARC variables $xready, $yready, and $zready with the location values. You can then use these variables in Blockly or other parts of your ARC project.
  • Since ARC's JavaScript environment doesn't support asynchronous execution like setTimeout, this script will execute synchronously. If you need to introduce delays between setting the variables, consider using ARC's scripting capabilities to trigger this script multiple times with pauses in between, or explore ARC's native timing and control commands.
  • For displaying the array or the process of "replaying" the array visually, consider using ARC's logging capabilities or custom interfaces with the Interface Builder.

This approach should allow you to experiment with the array and servo movements without directly moving the servos until you're ready. Remember, ARC provides a rich set of tools and commands for robotics programming, and leveraging those will often provide the best results within its environment.


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.
#75  

@Athena I think we need to change the terminology of    console.log(Replaying: ${location.name} at x=${location.x}, y=${location.y}, z=${location.z}); because it states that console is not defined. I do not think that console.log is allowed in ARC. The rest of it looks good though.

PRO
Canada
#76  

You're correct that ARC's scripting environment does not support console.log() for outputting text to a console, as you would in a standard web development environment. Instead, ARC provides different methods for displaying messages or logging information.

For JavaScript or Python within ARC, you can use the print() function to output text to the ARC's debug log. This is useful for debugging purposes or simply to track the flow of your script.

Let's adjust the script to replace console.log() with print() for compatibility with ARC:

var objectLocations = []; // Array to store locations

// Function to add a location to the array
function addLocation(name, x, y, z) {
    objectLocations.push({ name: name, x: x, y: y, z: z });
}

// Function to replay the array by setting variables
function replayArray(reverse = false) {
    var locations = reverse ? objectLocations.slice().reverse() : objectLocations;
    for (var i = 0; i < locations.length; i++) {
        var location = locations[i];
        // Print the location being replayed
        print(`Replaying: ${location.name} at x=${location.x}, y=${location.y}, z=${location.z}`);
        
        // Set the ARC variables with the location values
        setVar("$xready", location.x);
        setVar("$yready", location.y);
        setVar("$zready", location.z);
        
        // Here you can add additional logic as needed
        // For example, you might want to add a pause or trigger another script
    }
}

// Example usage
addLocation("Location1", 10, 20, 30);
addLocation("Location2", 40, 50, 60);

// Replay array
replayArray(); // Replay array in the original order
replayArray(true); // Replay array in reverse order

This script now uses print() to output messages, which is compatible with ARC's scripting environment. The print() function will display messages in ARC's debug log, allowing you to see the output of your script as it runs.

Remember, the setVar() function is used to set global variables within ARC, and these variables can be accessed by other scripts or controls within your ARC project. This approach allows you to experiment with the array and servo movements by setting the xready, yready, and zready variables without directly moving the servos until you're ready.


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.
#77  

Yes, I've noticed a couple times over the past few weeks while I'm learning Javascript that Athena has suggested to me to use the console.log() command to view output messages. It didn't work for me either. I've been using the Print() command with EZ Script for years to view information stored in variables so I tried it in Javascript scripts and found it work as expected.

PRO
Synthiam
#78  

It's Console.log that's why it messes her up

#79  

Wow that's all it is a capital C, that's the first I've heard of this one and this will help. Thanks

PRO
Synthiam
#80  

Console.log() is just a fancy way of using print()