Grant (and Pyro-users),

Thanks for the bug report! You were right, and in fact there were a few issues with that little bit of code. I've updated CVS to include the following in pyrobot/robot/device.py:

        elif type(item) == types.SliceType:
            if item.step:
                step = item.step
            else:
                step = 1
            if item.start == None:
                if step > 0:
                    start = 0
                else:
                    start = len(self) - 1
            else:
                start = item.start
            if item.stop == None:
                if step > 0:
                    stop = len(self)
                else:
                    stop = -1
            elif item.stop > len(self):
                stop = len(self)
            else:
                stop = item.stop
            if start < 0 and step > 0:
                start = len(self) + start
            if stop < 0 and step > 0:
                stop = len(self) + stop
return [self.getSensorValue(p) for p in xrange(start, stop, step)]

This allows it to act much more similarly to Python's slice behavior, including:

self.robot.sonar[0][:]
self.robot.sonar[0][:-1]
self.robot.sonar[0][-3:-1]
self.robot.sonar[0][::2]

Let me know if you see anything wrong with this. Thanks again!

-Doug

Grant Braught wrote:
Hi Doug,

We've been working on a summer research project here using Pyro and seem to have come across a bug. There is an easy work-around so it's not too big a deal but I thought I'd let you know. It seems that when using list slices to access a range of sensor values, the value of the sensor with the maximum index is never included. Here's some sample code that illustrates the bug:

===

from pyrobot.brain import Brain

class BugBrain(Brain):

   def step(self):

       rrangea = [s.value for s in self.robot.sonar[0]['all']]
       rrangeb = [s.value for s in self.robot.sonar[0][0:16]]

       print len(rrangea)  # prints 16
       print rrangea  # prints all 16 sonar values.
       print
       print len(rrangeb)  # prints 15
       print rrangeb  # prints all but the last sonar value.
       print
       print

# Create the brain and return it to the Pyrobot program.
def INIT(engine):
   return BugBrain('BB', engine)

===

It looks like this is caused by some code in the __getItem__ method in the Device class.

        elif type(item) == types.SliceType:
            if item.stop >= len(self): stop = len(self) - 1
            else:                      stop = item.stop
            step = 1
            if item.step:
                step = item.step
return [self.getSensorValue(p) for p in xrange(item.start, stop, step)]

This seems to cut off the last element when a slice is being used to access the sensor values.

===

Since the range names work we can just use those with no problem. We actually discovered this using the bumpers where we wanted to get the rear bumpers ([s.value for s in self.robot.bumpers[0][5:10]) and then check each one to see if it was triggered. That list turned out to only have 4 elements in it, causing out of bounds errors when we checked the [4]th index.

===

Thanks again for all your hard work in putting the Pyro stuff together and maintaining it! It is a great tool!

Grant


_______________________________________________
Pyro-users mailing list
[email protected]
http://emergent.brynmawr.edu/mailman/listinfo/pyro-users

Reply via email to