I have a 2-segments robotic arm that needs to reach a specific point by setting up its joints (angles).
Here is a drawing of the setup:
My arm is positioned in the middle of the sketch so my origin is (width/2,0). These are the values that I know: Lengths: First segment (L1): 140 mm. Second segment (L2): 180 mm. Distance between the point of origin and the target point. (using dist() ).
These lengths form a triangle. So, using laws of trigonometric I can find the angles of the triangle which are the angles I want to use to position my arm so it reaches the target point.
Now, I want to draw the triangle on screen using Processing environment for simulation. I applied some transformations to draw the triangle but I am not getting the correct drawing.
This is my code:
void draw(){
background(100);
translate(width/2,0); // origin
// target
float penX = mouseX-width/2;
float penY = mouseY;
// draw points
ellipse(penX, penY, 20, 20);
ellipse(0,0,20,20);
line(0,0,penX,penY);
// distance from origin to target
float distance = dist(0,0,penX,penY);
// first angle (part of S1)
float b = asin(penY/distance);
arc(0,0,100,100,0,b);
// second angle (part of S1)
float a = acos((pow(L1,2)+pow(distance,2)-pow(L2,2))/(2*L1*distance));
// Angle representing first joint
S1 = a + b; // first joint angle
// Angle representing second joint
S2 = acos((pow(L1,2)+pow(L2,2)-pow(distance,2))/(2*L1*L2)); //second joint angle
//Drawing Triangle:
rotate(S1);
line(0,0,120,0);
translate(120,0);
rotate(-S2);
line(0,0,180,0);}
I hope my writing is clear and sorry for any confusion.