Comparing Values of lists in Python

Hi All,

I wrote a simple script that takes 2 polylines, explodes them into segments, then compares each segment’s length against the length of the other polyline’s segments. From the comparison, the smaller number is divided by the larger to get the ratio of their difference in length. This ratio is then used to modify a variable that represents the speed of a robot arm, so that both robots could theoretically move to each of their control points at the same time.
Here is that script:

#Calculate both Robots' Feed Rates to Match while <= MaxFeed
import rhinoscriptsyntax as rs

#Main Variables
SlaveFeed  = []
MasterFeed = []
Master     = MasterTCP
Slave      = SlaveTCP
#Break ToolPath into individual Moves
Msegs = rs.ExplodeCurves(Master)
Ssegs = rs.ExplodeCurves(Slave)
#Make Sure there are the same number of moves for each robot
if len(Msegs) != len(Ssegs):
    print "Unequal Line Segments!"
    MasterFeed = "Learn to count...."
    SlaveFeed  = "Nope"
#If there are = Moves, Find difference in length
    print "Same number of Moves for both Robots"
    for i in range(len(Msegs)):
        Mseglen = rs.CurveLength(Msegs[i])
        Sseglen = rs.CurveLength(Ssegs[i])
        #If Master Moves Farther than Slave, Slow Slave by length difference Ratio
        if Mseglen > Sseglen:
            Rlen = Sseglen/Mseglen
            SFeed = Rlen*MaxFeed
        #If Slave Moves Farther than Master, Slow Master by length difference Ratio
            Rlen = Mseglen/Sseglen
            MFeed = Rlen*MaxFeed

This script seems to be working fine, but I am now trying to coordinate more that 2 robots. For this I will need to find a way of checking which segment from each robot’s path is the longest, so that it can receive the fastest speed. Once I find the longest, I will need to calculate a ratio for each of the other lengths and append them to a list of Feed Rates. I am not sure how to set up this relationship of comparing more than 2 lengths. Anyone have an idea?

Sorry if the post is long/hard to follow. It is my first question on here.

I think I understand what you are trying to do. Does this work for you?

import rhinoscriptsyntax as rs

def get_length_ratios(ids):
    if len(ids)<2: return
    #create a list of dictionaries for tracking information
    #this would probably be cleaner with classes, but dicts will work
    data = [{'id':id} for id in ids]
    for d in data:
        pieces = rs.ExplodeCurves(d['id'])
        d['pieces'] = pieces
        d['ratios'] = []
    #check that all of the 'pieces' entries have the same length
    piece_count = len(data[0]['pieces'])
    bad_input = False;
    for i in range(1,len(data)):
        if piece_count != len(data[i]['pieces']):
            bad_input = True
    if bad_input:
        print "unequal line segments"
        #clean up
        for d in data: rs.DeleteObjects(d['pieces'])
    for i in range(piece_count):
        max_length = 0
        lengths = []
        for d in data:
            length = rs.CurveLength(d['pieces'][i])
            if length>max_length:
                max_length = length
        for i, d in enumerate(data):
            d['ratios'].append(lengths[i] / max_length)
    #clean up
    for d in data: rs.DeleteObjects(d['pieces'])
    return [d['ratios'] for d in data]

curve_ids = rs.GetObjects("select curves", rs.filter.curve)
if curve_ids:
    ratios = get_length_ratios(curve_ids)
    for ratio in ratios:
        print ratio

1 Like

This is great! I am going to dig into how this is working so I can use it properly, but it looks like it is giving me the good ratios for modifying the speeds of each robot.

Thank you!!