// Determine which way we want to go based on how far away we are. // If we are less than 180 degrees away, then make the target // position the smaller number (reversed), otherwise go forward // since it's less to move. int target; if (offset <= 180) target = absPos - offset; else target = absPos + (360 - offset);

// Finally, determine if the encoder position is positive or // negative, and return the position accordingly. if (motorEncoderPos > 0) return target; else return -target;}