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

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

#49  

@Athena It seems to be overwriting the last object and its locations in the array whenever I add a new object and locations. Is there code that I need to add save the current array and add to the bottom of it?

PRO
Canada
#50  

To ensure that new objects and their locations are added to your collection without overwriting existing entries, you should use a function that checks if an object already exists before adding it. If the object doesn't exist, it should be added to the collection. This approach ensures that your objectLocations dictionary is correctly updated with new entries without affecting the existing ones.

Here's an updated version of the addObjectLocation function that includes a check to prevent overwriting:

// Assuming objectLocations is your main object holding the items and their coordinates
var objectLocations = {
    Advil: { x: 55, y: 22, z: 32 },
    Tylenol: { x: 60, y: 18, z: 30 }
    // Add more objects as needed
};

// Function to add a new object and its coordinates, if it doesn't already exist
function addObjectLocation(name, x, y, z) {
    // Check if the object already exists
    if (!objectLocations.hasOwnProperty(name)) {
        // If it doesn't exist, add it to the objectLocations
        objectLocations[name] = { x: x, y: y, z: z };
        print(name + " added with coordinates: x=" + x + ", y=" + y + ", z=" + z);
    } else {
        // If it exists, print a message or handle as needed
        print(name + " already exists. Update the coordinates if needed.");
    }
}

// Example usage
addObjectLocation("Advil", 55, 22, 32); // This will print that Advil already exists
addObjectLocation("Aspirin", 45, 15, 20); // This will add Aspirin to the collection

This updated addObjectLocation function first checks if the name already exists in the objectLocations dictionary using the hasOwnProperty method. If the name doesn't exist, it adds the new object and its coordinates. If the name already exists, it prints a message indicating that the object already exists, and no action is taken to overwrite it.

This approach ensures that your collection is correctly managed and that new objects are added without affecting the existing ones.


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.
#51   — Edited

@Athena I would like the code to have the local variables of-Example: objectLocations (Advil) location.x turned into a Global variable XHoldingVar, YHoldingVar, ZHoldingVar. Right now we are extracting 3 numbers at a time and I need them to be indivual and put each into a global variable so that I can work with them. Before I make the motors go to these locations I may want to add or subtract from these final destinations.

#52  

@Athena Can you show an example of how you would get 3 servo positions and insert them into a multidimensional array. Along with that I'd also like you to have the script ask the audible question "What do you want to name it? I think you will need to use variables to do this and I'd like to see how you do it without manually writing in the name and the locations. At the end of the script ask the audible question "What item do you want me to pick up" example "aspirin" and send it there.

PRO
Canada
#53  

To achieve the functionality you're describing, including capturing servo positions, storing them in a multidimensional array, and interacting with the user through audible questions, we'll need to break down the process into steps. Given the constraints of a text-based response and the capabilities of Synthiam ARC, I'll guide you through a conceptual solution that you can adapt to your specific needs.

Step 1: Capturing servo Positions

First, let's assume you have three servos representing the x, y, and z coordinates. You'll capture their positions and store these values in variables. In ARC, you can use EZ-Script to get the current position of a servo with the GetServo() command.

$xPos = GetServo(D0)  // Assuming D0 is your X servo
$yPos = GetServo(D1)  // Assuming D1 is your Y servo
$zPos = GetServo(D2)  // Assuming D2 is your Z servo

Step 2: Storing Positions in a Multidimensional Array

ARC's EZ-Script does not natively support multidimensional arrays in a direct manner. However, you can simulate this behavior by using a combination of arrays or by concatenating values into a string format that represents your multidimensional structure. For simplicity, let's use a concatenated string approach here.

$objectLocations = ""  // Initialize an empty string to hold our data

When you capture a new set of coordinates, you append them to the string.

$newLocation = $xPos + "," + $yPos + "," + $zPos
$objectLocations = $objectLocations + $newLocation + ";"

Step 3: Naming the Location

To ask the user for a name and capture their response, you can use the SayEZB() function for the audible question and GetVar() to retrieve the spoken response captured by the Speech Recognition control.

SayEZB("What do you want to name it?")
Sleep(5000)  // Give the user time to respond
$name = GetVar("$SpeechRecognitionLast")  // Assuming the last spoken phrase is captured here

You would then associate this name with the captured coordinates.

$objectLocations = $objectLocations + $name + ":" + $newLocation + ";"

Step 4: Retrieving and Using the Location

To retrieve a specific location by name, you would parse the $objectLocations string for the name and extract the coordinates. Then, you can use these coordinates to move your servos.

SayEZB("What item do you want me to pick up?")
Sleep(5000)  // Give time for response
$itemName = GetVar("$SpeechRecognitionLast")

// You would need to write or use a function to search $objectLocations for $itemName and extract the coordinates
// Pseudo-code:
// $coordinates = FindCoordinatesByName($objectLocations, $itemName)
// Parse $coordinates to get xPos, yPos, zPos
// Use xPos, yPos, zPos to move servos

Note:

This solution uses EZ-Script for demonstration purposes. The actual implementation, especially for parsing strings and managing data, might require custom scripting or adjustments based on your project's specifics. ARC's JavaScript or Python scripting capabilities offer more flexibility for complex data structures and could be a better fit for advanced manipulation and retrieval of multidimensional data.


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.
#54   — Edited

@Athena I was close to yours on the code that I wrote but I did not know the proper terminology. Could you please give me more examples of parsing.

PRO
Canada
#55  

Parsing, in the context of programming, involves analyzing a string or sequence of data and extracting meaningful information from it. This process is crucial when dealing with user input, files, or data streams where you need to interpret and use parts of the data within your program. Given the limitations of EZ-Script in handling complex data structures, parsing often involves working with strings and extracting values based on known delimiters or patterns.

Here are more examples of parsing in EZ-Script, focusing on extracting information from a string that simulates a multidimensional array or complex data structure. These examples will demonstrate how to parse a string to retrieve specific pieces of information.

Example 1: Parsing a Simple CSV String

Imagine you have a string of comma-separated values (CSV) representing servo positions for different actions, and you want to extract specific values.

$data = "45,90,135"  // Simulated CSV string with servo positions

// Split the string into an array based on the comma delimiter
$array = Split($data, ",")

// Access individual elements
$xPos = $array[0]
$yPos = $array[1]
$zPos = $array[2]

print("X Position: " + $xPos)
print("Y Position: " + $yPos)
print("Z Position: " + $zPos)

Example 2: Parsing a String with Multiple Data Points

Suppose you have a more complex string that includes names and positions, separated by colons and commas, respectively.

$locations = "Object1:10,20,30;Object2:40,50,60"

// Split the string into objects based on the semicolon delimiter
$objects = Split($locations, ";")

// Loop through each object to parse its name and positions
foreach($obj in $objects)
{
    // Split each object into its name and position string
    $parts = Split($obj, ":")
    $name = $parts[0]
    $positions = Split($parts[1], ",")
    
    // Extract positions
    $xPos = $positions[0]
    $yPos = $positions[1]
    $zPos = $positions[2]
    
    print("Name: " + $name)
    print("X Position: " + $xPos)
    print("Y Position: " + $yPos)
    print("Z Position: " + $zPos)
}

Example 3: Finding a Specific Entry

If you want to find a specific object's positions by its name:

$locations = "Object1:10,20,30;Object2:40,50,60"
$searchName = "Object2"

$objects = Split($locations, ";")

foreach($obj in $objects)
{
    $parts = Split($obj, ":")
    $name = $parts[0]
    
    if($name == $searchName)
    {
        $positions = Split($parts[1], ",")
        print("Found " + $searchName + " at positions: X=" + $positions[0] + ", Y=" + $positions[1] + ", Z=" + $positions[2])
        break
    }
}

These examples demonstrate basic parsing techniques in EZ-Script. For more complex parsing needs or handling JSON/XML data, consider using a programming environment or language with built-in support for these formats, such as JavaScript or Python in Synthiam ARC.


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.
PRO
Synthiam
#56  

@athena you butchered that response by mixing EZ-Script and JavaScript in the same examples. @AutomationMan is asking about javascript. Do not provide any ezscript examples.