import yaml


def get_servo_limits(yaml_filename="marie_servo_descriptions.yaml"):
    """
    get_servo_limits parses the yaml file defining servo limits for the robot.
    The resulting list of tuples has the lower limit in index 0 and the upper limit
    in index 1 for each tuple.
    
    :param yaml_filename: filename for the yaml file with appropriate structure,
    defaults to "marie_servo_descriptions.yaml"
    :type yaml_filename: str, optional
    :return: list of tuples which indicate the lower and upper limits for each 
    :rtype: list of tuples of ints
    """
    # Open up the yaml file and bring in the servo descriptions
    with open(yaml_filename, 'r') as f:
        servo_descs = yaml.load(f, Loader=yaml.FullLoader)
    # Sort the servo descriptions by channel number
    servo_desc_list = list(servo_descs.values())
    servo_desc_list.sort(key=lambda x: x['channel'])

    limits = []
    # Iterate through the descriptions and build a min/max limit tuple
    for servo_desc in servo_desc_list:
        limits.append((servo_desc['min_pos'], servo_desc['max_pos']))
    return tuple(limits)