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

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.

PRO
Canada
#129  

You're welcome! I'm glad you found the solution helpful and understandable. If you have any more questions or need further assistance in the future, feel free to reach out. Happy robot building!


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

@Athena , Can you explain the following javascript command and when to use it? I don't understand why you used it in the above script you wrote. Is this the only way to set a object as a variable?:

setVarObject();

#131  

@Athena , nevermind answering my last question. I looked in thw support section under the Javascript and found this that explained everything:

setVarObject( variableName, value )

Sets the value from Arc’s public global variable storage of the object. This allows the variable to be available to other robot skills or scripts using getVar(). (Read More). This is different than setVar() because it sets the value as an object without any translation. So you can pass entire objects, such as functions, classes, or multidimensional arrays. - The name of the global variable as a string - The object value that you wish to store in the global variable Example: // Set the multidimensional array to be accessible by other robot skills with getVar() x = [ [0, 1, 2], [3, 4, 5] ]; setVar($MyValue, x);

PRO
Canada
#132  

Great to hear that you found the information you needed!The setVarObject() function is indeed very useful for storing complex data structures like objects and multidimensional arrays in ARC's global variable storage. This allows you to share these complex data structures across different robot skills and scripts within your project.

If you have any more questions or need further assistance, feel free to ask. Happy coding!


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

@Athena I have experimented and double checked with the multiple scripts and have found a glitch. It always starts with first name on the array rather than the start name. It then continues on for the amount of iterations passed the startname. It is supposed to have the startname be the first array that the servos go to. Any recommendations to fix this?

PRO
Canada
#134  

To address the issue where the script always starts with the first name in the array rather than the specified start name, you can modify the script to find the index of the start name and begin the iteration from that index. Here’s how you can do it:

  1. Find the Index of the Start Name: Before iterating through the array, find the index of the start name.
  2. Start Iteration from the Found Index: Use the found index to start the iteration.

Here’s an example of how you can achieve this in JavaScript:

// Define the multidimensional array
var namesAndValues = [
    ["Name1", 10, 20, 30],
    ["Name2", 40, 50, 60],
    ["Name3", 70, 80, 90],
    // Add more entries as needed
];

// Function to find the index of the start name
function findStartIndex(array, startName) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][0] === startName) {
            return i;
        }
    }
    return -1; // Return -1 if the start name is not found
}

// Specify the start name
var startName = "Name2";

// Find the start index
var startIndex = findStartIndex(namesAndValues, startName);

if (startIndex !== -1) {
    // Iterate through the array starting from the start index
    for (var i = startIndex; i < namesAndValues.length; i++) {
        var name = namesAndValues[i][0];
        var x = namesAndValues[i][1];
        var y = namesAndValues[i][2];
        var z = namesAndValues[i][3];

        // Move the servos to the specified positions
        Servo.setPosition("d0", x);
        Servo.setPosition("d1", y);
        Servo.setPosition("d2", z);

        // Print the current name and values for debugging
        print("Moving to " + name + ": x=" + x + ", y=" + y + ", z=" + z);

        // Add a delay if needed
        sleep(1000); // 1 second delay
    }
} else {
    print("Start name not found in the array.");
}

Explanation:

  1. Multidimensional Array: The namesAndValues array contains names and their corresponding x, y, z values.
  2. findStartIndex Function: This function iterates through the array to find the index of the specified start name.
  3. Start Index: The startIndex variable holds the index of the start name.
  4. Iteration: The for loop starts from the startIndex and iterates through the array, moving the servos to the specified positions.

Notes:

  • Ensure that the startName exists in the array to avoid starting from an invalid index.
  • Adjust the servo ports (d0, d1, d2) as per your setup.
  • Add any necessary delays or additional logic as needed.

This approach ensures that the iteration starts from the specified start name and continues for the specified number of iterations. If you have any further questions or need additional assistance, feel free to ask!


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

@Athena I was looking over your array on July 7th  post #127  and that should have worked to start with the startName that was declared in a different script. Here is the code that you posted  var startName = getVar("$Start_Name");

// Find the starting index based on the name
var startIndex = items.findIndex(item => item.name === startName);

As soon as you start the script it should go to the location of the startName . What could be the reason that it is not going there? I double checked the variable with smart variable watcher and it has one of the names in the array in it.

PRO
Canada
#136  

It sounds like the logic should work as intended, but there might be a small issue with how the findIndex function is being used or how the variable is being accessed. Let's go through a few troubleshooting steps to ensure everything is set up correctly.

1. Verify the Array Structure

Ensure that your array structure matches the expected format. For example, if your array is structured as follows:

var items = [
    { name: "Name1", x: 10, y: 20, z: 30 },
    { name: "Name2", x: 40, y: 50, z: 60 },
    { name: "Name3", x: 70, y: 80, z: 90 }
];

2. Verify the Start Name Variable

Ensure that the startName variable is correctly retrieved from the global variable storage:

var startName = getVar("$Start_Name");
print("Start Name: " + startName); // Debugging line to verify the start name

3. Find the Start Index

Ensure that the findIndex function is correctly finding the index:

var startIndex = items.findIndex(item => item.name === startName);
print("Start Index: " + startIndex); // Debugging line to verify the start index

4. Iterate from the Start Index

Ensure that the iteration starts from the correct index:

if (startIndex !== -1) {
    for (var i = startIndex; i < items.length; i++) {
        var name = items[i].name;
        var x = items[i].x;
        var y = items[i].y;
        var z = items[i].z;

        // Move the servos to the specified positions
        Servo.setPosition("d0", x);
        Servo.setPosition("d1", y);
        Servo.setPosition("d2", z);

        // Print the current name and values for debugging
        print("Moving to " + name + ": x=" + x + ", y=" + y + ", z=" + z);

        // Add a delay if needed
        sleep(1000); // 1 second delay
    }
} else {
    print("Start name not found in the array.");
}

Full Example Code

Here is the full example code with debugging lines added:

// Define the multidimensional array
var items = [
    { name: "Name1", x: 10, y: 20, z: 30 },
    { name: "Name2", x: 40, y: 50, z: 60 },
    { name: "Name3", x: 70, y: 80, z: 90 }
];

// Retrieve the start name from the global variable storage
var startName = getVar("$Start_Name");
print("Start Name: " + startName); // Debugging line to verify the start name

// Find the starting index based on the name
var startIndex = items.findIndex(item => item.name === startName);
print("Start Index: " + startIndex); // Debugging line to verify the start index

if (startIndex !== -1) {
    // Iterate through the array starting from the start index
    for (var i = startIndex; i < items.length; i++) {
        var name = items[i].name;
        var x = items[i].x;
        var y = items[i].y;
        var z = items[i].z;

        // Move the servos to the specified positions
        Servo.setPosition("d0", x);
        Servo.setPosition("d1", y);
        Servo.setPosition("d2", z);

        // Print the current name and values for debugging
        print("Moving to " + name + ": x=" + x + ", y=" + y + ", z=" + z);

        // Add a delay if needed
        sleep(1000); // 1 second delay
    }
} else {
    print("Start name not found in the array.");
}

Troubleshooting Steps

  1. Check the Smart Variable Watcher: Ensure that the $Start_Name variable is correctly set and matches one of the names in the array.
  2. Debugging Output: Use the print statements to verify that the startName and startIndex are correctly retrieved and calculated.
  3. Array Structure: Ensure that the array structure matches the expected format with objects containing name, x, y, and z properties.

If the issue persists, please provide more details about the array structure and the exact values being used, and I'll be happy to assist further!


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.