kinematics and storing foot location

I want to use a joystick to drive the foot of a leg around an x,y axis.

I can probably map the joystick inputs to a multiplier, and then use that multiplier to add or subract from the current x or y axis. ,,

I can probably set constraints on the possible x,y locations, so I can probably manage the first phase of the experiment,,, but how will I be able to know where the foot is? I can probably serial print the foot location for phase one,, but then I will want to first move one leg, then the next leg, etc. .,, I can use a get operator to collect it from ,,, the last cycle of the function for one leg,, but how about multiple legs?? Do variables store in this way,

Is it possible to know the servo angles with a function, and then just program a forward kinematics equation that reads the servos, and then figures the location once a leg is selected??

well, I didn’t get the joysticks to do anything I can watch happen and feel good about. However, I did manage to use 2 10k potentiometers mapped to x,y coordinates and get to move the foot around the x,y axis.

There are some things about this experiment. I am using buggy servos that randomly go haywire for no reason other than some minor voltage change caused by unknown power variation, and then another interesting thing I noticed. Although the code should not change the value of both Analog inputs when only one pot is turned, both potentiometers are hooked to the same power rail, and then to ground. I suppose there is a way to add fixed resistors to my circuit to affect the stability of the voltage, but I do not know what would happen to the analogRead results. Mostly one potentiometer was affected the most by the other being turned, but I’m sure that the reason both values were changing is because as one potentiometer gained or lost resistance, it altered the amount of current that flowed through the other potentiometer, them being essentially wired in parallel. (although the center taps are going to two seperate, but likely very similar pins, the paths to ground are parallel, and the paths to voltage are parallel, so the amount of available power would be affected. It probably matters that I am driving my servos from arduino 5v, but there’s only two of them and they’re small, but I suppose they are drawing enough current that when I move the potentiometers, which is when they try to move, the combined activity causes a change in the voltage present at the pin for the potentiomter I am not changing.

Anyway, my experiment DOES have one success, and that is my code does facilitate for changing the position of a foot with x,y coordinates.

//servo leg experiment. 2 servos moving on a 2 dimensional x,y coordinate plane. hip servo at 0,0
//hip servo oriented such that 0 degrees is straight downward, and 180 is straight upward
//knee servo oriented so that 0 is directly toward femur, and that 180 is directly away from tibia (so 180 fully extends the leg)
//my model is backward, with the leg extending to my left, so I input negative values and it seems to want to work
//there are still problems with this code, the solutions it may pick may not be within the range of motion of the servos. 
//all measurements are in milimeters.
//open serial monitor to see angle B then angle A, B is hip angle so printed first

#include <Servo.h>
#include <math.h>

Servo hip; 
Servo knee;

int femur = 100;
int tibia = 90;

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:
int joyx = analogRead(A0);
int joyy = analogRead(A1);
int x = map(joyx, 0, 1023, -180, 0);
int y = map(joyy, 0, 1023, -150, 150);
Serial.print (x);
Serial.print ("    ");
Serial.println (y);
//assuming servo hip is oriented, 0 straight down, 180 straight up, y is inverted, so -y is above the x axis 
 float L = sqrt((x*x)+(y*y));
 float A = acos(((femur*femur)+(tibia*tibia)-(L*L))/(2*femur*tibia));//outputs number in radians as A
 float B = acos(((femur*femur)+(L*L)-(tibia*tibia))/(2*femur*L))+acos(((L*L)+(y*y)-(x*x))/(2*L*y));//outputs number in radians as B
Serial.print(degrees(B));//converts radians to degrees
Serial.print("    ");
Serial.print(degrees(A));//converts radians to degrees
Serial.println ("----------------------");
hip.write(degrees(B));//converts radians to degrees
knee.write(degrees(A));//converts radians to degrees

It probably matters that I am driving my servos from arduino 5v, but there's only two of them and they're small

Even a micro servo can draw 650mA according to this.

but how about multiple legs?? Do variables store in this way,

It sounds like you need to read up on arrays. One array for each value that you need to store each with one level for each leg. Give the arrays names to make them easier to use and number the legs, perhaps clockwise from front right to make them easier to understand. Better still, use an enum to give meaning to the leg numbers.

Assuming you are using the Servo library, the Servo objects remember the last value written to the servo and will return it to you with or servo.readMicroseconds(). They will NOT report the current position if the servo is powered down or detached and moved manually.

It's faster to just keep track of each position in an array.

Assuming you are using the Servo library, the Servo objects remember the last value written to the servo

Just to make it 100% clear to OP, "written" is the operative word. A normal servo has no way of knowing its actual position, so if it was mechanically prevented from arriving at the written position, there's no way of knowing that.