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.

#153  

@Athena, What you describing is what I was trying to avoid. How about trying it this way-convert the print of the array into a define the array in a spreadsheet like style, make my changes and save changes to the global variable when you are done. Maybe what I am asking is how do I just pull up the array, not as a print but the array itself so I can manipulate it.

Here is an array that you previously made and I am using. Step 1: Define the Array and Save to Global Variable First, let's create a script to define the array and save it to a global variable.

// 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 }, // Add more entries as needed ];

// Save the array to a global variable setVar("$ItemsArray", items);

PRO
Canada
#154  

I understand that you want to manipulate the array directly in a more interactive way, similar to a spreadsheet. While ARC doesn't have a built-in spreadsheet-like interface, you can use JavaScript to create a more interactive experience within the ARC environment. Here's how you can achieve this:

Step 1: Define the Array and Save to Global Variable

First, let's create a script to define the array and save it to a global variable.

// 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 },
    // Add more entries as needed
];

// Save the array to a global variable
setVar("$ItemsArray", items);

Step 2: Create a Function to Display the Array

Next, let's create a function to display the array in a more interactive way. We'll use JavaScript to create a simple HTML table that you can manipulate.

function displayArray() {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Create an HTML table
    var table = "<table border='1'><tr><th>Name</th><th>X</th><th>Y</th><th>Z</th></tr>";

    // Loop through the array and add rows to the table
    for (var i = 0; i < items.length; i++) {
        table += "<tr>";
        table += "<td contenteditable='true' id='name" + i + "'>" + items[i].name + "</td>";
        table += "<td contenteditable='true' id='x" + i + "'>" + items[i].x + "</td>";
        table += "<td contenteditable='true' id='y" + i + "'>" + items[i].y + "</td>";
        table += "<td contenteditable='true' id='z" + i + "'>" + items[i].z + "</td>";
        table += "</tr>";
    }

    table += "</table>";
    table += "<button onclick='saveChanges()'>Save Changes</button>";

    // Display the table
    document.body.innerHTML = table;
}

// Call the function to display the array
displayArray();

Step 3: Create a Function to Save Changes

Finally, let's create a function to save the changes made to the array back to the global variable.

function saveChanges() {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Loop through the array and update the values
    for (var i = 0; i < items.length; i++) {
        items[i].name = document.getElementById("name" + i).innerText;
        items[i].x = parseInt(document.getElementById("x" + i).innerText);
        items[i].y = parseInt(document.getElementById("y" + i).innerText);
        items[i].z = parseInt(document.getElementById("z" + i).innerText);
    }

    // Save the updated array back to the global variable
    setVar("$ItemsArray", items);

    // Optionally, print a confirmation message
    print("Changes saved successfully!");
}

Summary

  • Define the Array: Create a script to define the array and save it to a global variable.
  • Display the Array: Create a function to display the array in an HTML table with editable cells.
  • Save Changes: Create a function to save the changes made to the array back to the global variable.

By using this approach, you can interactively manipulate the array in a more spreadsheet-like manner. 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.
#155  

@Athena, I see and understand what you are trying to do but hit a bit of a snag as it mentions Error Line 22 col 4- document is not defined. // Display the table document.body.innerHTML = table; } In ARC's Javascript does it need to be handled differently?

PRO
Canada
#156  

You're correct. The document object is not available in ARC's JavaScript environment because it is not a web browser. Instead, you can use ARC's built-in UI elements and scripting capabilities to achieve a similar result.

Let's create a more interactive way to manipulate the array using ARC's UI elements and scripting capabilities.

Step 1: Define the Array and Save to Global Variable

First, let's create a script to define the array and save it to a global variable.

// 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 },
    // Add more entries as needed
];

// Save the array to a global variable
setVar("$ItemsArray", items);

Step 2: Create a Function to Display the Array

Next, let's create a function to display the array using ARC's UI elements. We'll use the print() function to display the array in a more interactive way.

function displayArray() {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Print the array in a readable format
    for (var i = 0; i < items.length; i++) {
        print("Name: " + items[i].name + ", X: " + items[i].x + ", Y: " + items[i].y + ", Z: " + items[i].z);
    }
}

// Call the function to display the array
displayArray();

Step 3: Create a Function to Update the Array

Finally, let's create a function to update the array based on user input. We'll use ARC's ControlCommand() to get user input and update the array.

function updateArray(index, newName, newX, newY, newZ) {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Update the specified entry
    if (index >= 0 && index < items.length) {
        items[index].name = newName;
        items[index].x = newX;
        items[index].y = newY;
        items[index].z = newZ;

        // Save the updated array back to the global variable
        setVar("$ItemsArray", items);

        // Print a confirmation message
        print("Entry updated successfully!");
    } else {
        print("Invalid index!");
    }
}

// Example usage: Update the first entry
updateArray(0, "UpdatedName1", 100, 200, 300);

Summary

  • Define the Array: Create a script to define the array and save it to a global variable.
  • Display the Array: Create a function to display the array using ARC's print() function.
  • Update the Array: Create a function to update the array based on user input using ARC's ControlCommand().

By using this approach, you can interactively manipulate the array within ARC's environment. 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.
#157  

@Athena, I would like to have the addlocation as being the final location of an end effector placing an item. Rather than stop the end effector above the final location with its gripper open and push addlocation, then addlocation for the final location, and another addlocation above location with the gripper closed for safety. I would like to just go to final location and then this to be done automatically with 3 lines of array added all at once with a set variable amount above. The lines of the array are already named Name1, Name2 etc. It would be nice to have new additions easily spotted with Name 1.5 for array above the final location, Name 2 (non decimal ) for final location, Name 2.5 for array above final location with gripper closed. How would you write a function for the script for this?

PRO
Canada
#158  

To achieve this, we can create a function that automatically adds three lines to the array for each new location. This function will handle the naming convention and the positions for the end effector. Here's how you can do it:

Step 1: Define the Array and Save to Global Variable

First, let's create a script to define the array and save it to a global variable.

// 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 },
    // Add more entries as needed
];

// Save the array to a global variable
setVar("$ItemsArray", items);

Step 2: Create a Function to Add Locations

Next, let's create a function to add the three lines to the array for each new location.

function addLocation(baseName, finalX, finalY, finalZ, offset) {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Calculate the positions
    var aboveX = finalX;
    var aboveY = finalY;
    var aboveZ = finalZ + offset;

    // Create the new entries
    var entryAboveOpen = { name: baseName + "1.5", x: aboveX, y: aboveY, z: aboveZ };
    var entryFinal = { name: baseName + "2", x: finalX, y: finalY, z: finalZ };
    var entryAboveClosed = { name: baseName + "2.5", x: aboveX, y: aboveY, z: aboveZ };

    // Add the new entries to the array
    items.push(entryAboveOpen);
    items.push(entryFinal);
    items.push(entryAboveClosed);

    // Save the updated array back to the global variable
    setVar("$ItemsArray", items);

    // Print a confirmation message
    print("Locations added successfully!");
}

// Example usage: Add a new location with an offset of 10 units above the final location
addLocation("Name", 100, 200, 300, 10);

Step 3: Display the Updated Array

You can use the displayArray function from the previous example to display the updated array.

function displayArray() {
    // Retrieve the global array
    var items = getVar("$ItemsArray", []);

    // Print the array in a readable format
    for (var i = 0; i < items.length; i++) {
        print("Name: " + items[i].name + ", X: " + items[i].x + ", Y: " + items[i].y + ", Z: " + items[i].z);
    }
}

// Call the function to display the array
displayArray();

Summary

  • Define the Array: Create a script to define the array and save it to a global variable.
  • Add Locations: Create a function to add three lines to the array for each new location, following the naming convention and positions.
  • Display the Array: Use the displayArray function to display the updated array.

By using this approach, you can automatically add the required locations to the array and display them within ARC's environment. 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.
#159  

@Athena, After looking it over I think you had it right the first time on post # 152 yesterday. I just wasn't ready to accept it yet because had an idea in my head of how it should have been done. Can you rewrite the code on post # 152 to handle some extra code that I had added to my array. Here is an example of the code items.push({ name: newName, x: x, y: y, z: z, gripperUp: gripperUp, gripperRotate: gripperRotate, fingersOpen: fingersOpen });

#160  

@Athena On post #158 could you please change the code for my modified code-here is an example of my code which you can pull the variables that I'm using  items.push({ name: newName, x: x, y: y, z: z, gripperUp: gripperUp, gripperRotate: gripperRotate, fingersOpen: fingersOpen });    Also please use these increments in the naming  .5, 1, 1.5 otherwise you are skipping a number when you are adding 1.5, 2, 2.5.