I'm trying to take gyroscope readings (pitch and roll) and use them to control two sets of servo motors seperately. The problem is that when I switch from controlling set 0 (pitch_pos[0] & roll_pos[0]) to set 1 (pitch_pos[1] & roll_pos[1]), move the sensor and then go back to set 0, those motors will jerk into a different position instantly.
I triend to come up with an equation that would save the last value in each set before switching and using it with together with the current gyroscope angle to keep motors in current set in the same position until the sensor is moved in that set only. I know this should be simple but I can't get my head around it.
//STATE 0 - BASE, ELBOW, WRIST-VERTICAL, GRIPPER OPEN
if(flex_raw > 540) { //[0] //flex_raw = flex sensor value
pitch_old[1] = pitch_pos[1];
roll_old[1] = roll_pos[1];
pitch_pos[0] = pitch - (pitch_old[0] - pitch_old[1]); //doesn't work
roll_pos[0] = roll - (roll_old[0] - roll_old[1]); //doesn't work
pos[0] = map(roll_pos[0], -75, 75,allowed_min[0], allowed_max[0]); //M1 (Base)
pos[2] = map(pitch_pos[0], -75, 75, allowed_min[2], allowed_max[2]); //M3 (Elbow)
pos[3] = map(pitch_pos[0], -75, 75, allowed_min[3], allowed_max[3]); //M4 (Wrist - Vertical)
pos[5] = allowed_min[5];
//pitch_now[0] = pitch_pos[0];
//roll_now[0] = roll_pos[0];
}
//STATE 1 - SHOULDER, WRIST-ROTATION, GRIPPER OPEN
if(flex_raw <= 540 && flex_raw > 510) { //[1]
pitch_old[0] = pitch;
roll_old[0] = roll;
pitch_pos[1] = pitch - (pitch_old[1] - pitch_old[0]); //doesn't work
roll_pos[1] = roll - (roll_old[1] - roll_old[0]); //doesn't work
pos[1] = map(pitch_pos[1], -75, 75, allowed_min[1], allowed_max[1]); //M2 (Shoulder)
pos[4] = map(roll_pos[1], -75, 75, allowed_min[4], allowed_max[4]); //M5 (Wrist - Rotation)
pos[5] = allowed_min[5];
//pitch_now[1] = pitch_pos[1];
//roll_now[1] = roll_pos[1];
}
//STATE 2 - BASE, ELBOW, WRIST-VERTICAL, GRIPPER CLOSED
if(flex_raw <= 510 && flex_raw > 480) { //[2]
pitch_old[1] = pitch_pos[1];
roll_old[1] = roll_pos[1];
pitch_pos[0] = pitch - (pitch_old[0] - pitch_old[1]); //doesn't work
roll_pos[0] = roll - (roll_old[0] - roll_old[1]); //doesn't work
pos[0] = map(roll_pos[0], -75, 75,allowed_min[0], allowed_max[0]); //M1 (Base)
pos[2] = map(pitch_pos[0], -75, 75, allowed_min[2], allowed_max[2]); //M3 (Elbow)
pos[3] = map(pitch_pos[0], -75, 75, allowed_min[3], allowed_max[3]); //M4 (Wrist - Vertical)
pos[5] = allowed_max[5];
//pitch_now[0] = pitch_pos[0];
//roll_now[0] = roll_pos[0];
}
//STATE 3 - SHOULDER, WRIST-ROTATION, GRIPPER CLOSED
if(flex_raw <= 480) { //[3]
pitch_old[0] = pitch_pos[0];
roll_old[0] = roll_pos[0];
pitch_pos[1] = pitch - (pitch_old[1] - pitch_old[0]); //doesn't work
roll_pos[1] = roll - (roll_old[1] - roll_old[0]); //doesn't work
pos[1] = map(pitch_pos[1], -75, 75, allowed_min[1], allowed_max[1]); //M2 (Shoulder)
pos[4] = map(roll_pos[1], -75, 75, allowed_min[4], allowed_max[4]); //M5 (Wrist - Rotation)
pos[5] = allowed_max[5];
//pitch_now[1] = pitch_pos[1];
//roll_now[1] = roll_pos[1];
}