Asked

Sketch Pad Program For Servos

@Athena, could you help me develop an ARC JavaScript program using the Sketch Pad robot skill to plot the movements of a large gantry machine that I have constructed?

The machine operates with the following axes:

  • X-axis: Controlled by servo V1, with a range from 1 to 130,000.
  • Y-axis: Controlled by servo V60, with a range from 1 to 100,000.
  • Z-axis: This axis manages the up and down movement, with a range from 1 to 140,000.

I would like the program to utilize the Sketch Pad robot skill to visually represent the movements of servos V1 and V60. Specifically, I want the Sketch Pad plot to display in blue when the Z-axis value is less than 42,000, indicating that the machine is moving to the next location without cutting. When the Z-axis value is greater than 42,000, the plot should be in green, signifying that the machine has lowered and started cutting. Additionally, I would like the lines representing these movements to be wide enough to be easily visible.

Thank you for your assistance!


Related Hardware EZ-B v4

ARC Pro

Upgrade to ARC Pro

With Synthiam ARC Pro, you're not just programming a robot; you're shaping the future of automation, one innovative idea at a time.

#1  

We have moved your comment into its own thread to get the attention it deserves from the Athena AI. We also edited your question to add specifics which will help Athena with details, such as using servos functions and using the sketch pad robot skill.

@athena please perform the request.

PRO
Canada
#2  

To create a JavaScript program in ARC that uses the Sketch Pad robot skill to plot the movements of your gantry machine, we can follow these steps:

  1. Initialize the Sketch Pad: Set up the Sketch Pad to draw the movements.
  2. Monitor servo Positions: Continuously check the positions of servos V1 (X-axis) and V60 (Y-axis).
  3. Draw on the Sketch Pad: Use the Sketch Pad to draw lines based on the servo positions, changing the color based on the Z-axis value.

Here's a sample JavaScript program to achieve this:

// Initialize variables for previous positions
var prevX = 0;
var prevY = 0;

// Function to update the Sketch Pad
function updateSketchPad() {
    // Get current positions of the servos
    var currentX = Servo.getPositionRealtime("V1");
    var currentY = Servo.getPositionRealtime("V60");
    var currentZ = Servo.getPositionRealtime("V2"); // Assuming V2 is the Z-axis servo

    // Determine the color based on the Z-axis value
    var color = (currentZ < 42000) ? "blue" : "green";

    // Draw a line on the Sketch Pad
    controlCommand("Sketch Pad", "CanvasDrawLine", 5, prevX, prevY, currentX, currentY, color);

    // Update previous positions
    prevX = currentX;
    prevY = currentY;
}

// Set an interval to update the Sketch Pad every 100 milliseconds
setInterval(updateSketchPad, 100);

Explanation:

  • Servo.getPositionRealtime: This function retrieves the real-time position of the servos. Ensure that your servos support this feature.
  • controlCommand: This command is used to draw on the Sketch Pad. The CanvasDrawLine command draws a line between two points with a specified thickness and color.
  • setInterval: This function repeatedly calls updateSketchPad every 100 milliseconds to update the drawing based on the servo positions.

Notes:

  • Ensure that the servos are correctly configured and connected to the EZ-B v4.
  • Adjust the servo port numbers if they differ from the example.
  • The CanvasDrawLine command's parameters include the line thickness and color, which you can adjust as needed.

Feel free to modify the code to suit your specific requirements or let me know if you need further assistance!


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

@Athena I think you may have forgot something you taught me about 6 months ago. The canvas is only 2 to the 16th power so these numbers that I wanted to use will not work for it. A simple solution is to just scale it to half size and it will be ok.  Here is what you wrote. I had a problem with it because it did not accept the ControlCommand for some reason, I think it may have been a negative number or something. I could not shut it off and had to delete it.

// Set the canvas size to 32767x32767
controlCommand("Sketch Pad", "CanvasSize", 32767, 32767);

// Function to plot a point on the canvas
function plotPoint(x, y, color) {
    controlCommand("Sketch Pad", "CanvasDrawPixel", x, y, color);
}

// Function to scale servo positions from 1-50000 to 0-32767
function scalePosition(position) {
    return Math.floor((position - 1) * (32767 / 49999));
}

// Initialize previous positions
var prevX = -1;
var prevY = -1;

// Main loop to track and plot positions
while (true) {
    // Get the current positions of V26 and V27
    var currentX = Servo.getPosition(V26);
    var currentY = Servo.getPosition(V27);

    // Scale the positions
    var scaledX = scalePosition(currentX);
    var scaledY = scalePosition(currentY);

    // Check if the positions have changed
    if (scaledX !== prevX || scaledY !== prevY) {
        // Plot the new position
        plotPoint(scaledX, scaledY, "red");

        // Update previous positions
        prevX = scaledX;
        prevY = scaledY;
    }

    // Small delay to prevent excessive CPU usage
    sleep(100);
}
#4  

*Note: Above Post edited to add code tags for correct formatting.

PRO
Canada
#6  

Thank you for pointing that out! Let's address the issue with scaling and ensure the control commands are correctly implemented. We'll also add error handling to prevent negative values.

Here's an updated version of the JavaScript program:

// Set the canvas size to 32767x32767
controlCommand("Sketch Pad", "CanvasSize", 32767, 32767);

// Function to plot a point on the canvas
function plotPoint(x, y, color) {
    // Ensure x and y are within valid range
    x = Math.max(0, Math.min(32767, x));
    y = Math.max(0, Math.min(32767, y));
    controlCommand("Sketch Pad", "CanvasDrawPixel", x, y, color);
}

// Function to scale servo positions from 1-130,000 to 0-32767
function scalePosition(position, maxPosition) {
    return Math.floor((position - 1) * (32767 / (maxPosition - 1)));
}

// Initialize previous positions
var prevX = -1;
var prevY = -1;

// Main loop to track and plot positions
while (true) {
    // Get the current positions of V1 and V60
    var currentX = Servo.getPosition(V1);
    var currentY = Servo.getPosition(V60);
    var currentZ = Servo.getPosition(V2); // Assuming V2 is the Z-axis servo

    // Scale the positions
    var scaledX = scalePosition(currentX, 130000);
    var scaledY = scalePosition(currentY, 100000);

    // Determine the color based on the Z-axis value
    var color = (currentZ < 42000) ? "blue" : "green";

    // Check if the positions have changed
    if (scaledX !== prevX || scaledY !== prevY) {
        // Plot the new position
        plotPoint(scaledX, scaledY, color);

        // Update previous positions
        prevX = scaledX;
        prevY = scaledY;
    }

    // Small delay to prevent excessive CPU usage
    sleep(100);
}

Key Changes:

  • Scaling Function: The scalePosition function now takes the maximum position as a parameter to accommodate different ranges for X and Y axes.
  • Error Handling: Added checks to ensure x and y are within the valid range of 0 to 32767 before plotting.
  • Color Logic: The color is determined based on the Z-axis value, as per your requirement.

Notes:

  • Ensure that the servo ports (V1, V60, and V2) are correctly configured in your setup.
  • The controlCommand function should be used with the correct skill name, which is "Sketch Pad" in this case.
  • If you encounter any issues with the control commands, double-check the skill names and parameters.

Feel free to adjust the code further or let me know if you need additional assistance!


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

I follow what you are doing and understand it, just haven't had a chance to experiment and verify it yet but it looks like it should work nicely. Thanks.