'Path' object has no attribute 'currentPosition'
I have a very strange case here. I have a python node that has a class with several attributes one of which is currentPosition. My code runs without problems for the initial iterations and very often breaks before the last iteration. I have identified the problem segment and where everything goes wrong, it is in the constructor!
print "path.currentPosition and path.speed",path.currentPosition,path.speed
newPath=Path(path.currentPosition,path.speed)
print "newPath.currentPosition and newPath.speed",newPath.currentPosition,newPath.speed
I will copy the function is which I see the error:
def computePathWithoutWaypoints(self, path, waypointsToExclude):
'''! Compute the path based on the current path but without considering some tasks
It have to recompute the path in some cases using the motion_planner so it can be slow.
Necessary to estimateRobotLossWithoutWaypoints()
'''
print "path.currentPosition, path.speed",path.currentPosition, path.speed
newPath=Path(path.currentPosition, path.speed)
print "NEW path.currentPosition, path.speed", newPath.currentPosition,newPath.speed
i=0
lastAdded=-1
conflict=False
for waypoint in path:
# print waypoint
if waypoint in waypointsToExclude:
conflict=True
i+=1
continue
if conflict==False:
newPath.append(path.waypointsPlanned[i],path.timePlanned[i])
elif i-lastAdded==1:
newPath.append(path.waypointsPlanned[i],updateTime=True)
else:
pathToWaypoint,timeToWaypoint=self.computePathToWaypoint(waypoint, startingPoint=newPath.lastPlannedPosition(),startingTime=newPath.lastPlannedTime())
newPath.append(pathToWaypoint,timeToWaypoint)
i+=1
lastAdded=i-1
return newPath
Here is my Class (off course parts of it): class Path:
waypointsPlanned=0
timePlanned=0
currentPosition=0
speed=0.3
def __init__(self,currentPosition,speed):
self.waypointsPlanned=[]
self.timePlanned=[]
self.currentPosition=currentPosition
self.speed=speed
Now, I've checked all the indentation and made sure there is not a problem there. I can't understand what is happening inside this constructor. I get meaningful and correct values in the first print and upon second one I get:
> print newPath.currentPosition,newPath.speed AttributeError: 'Path' object has no attribute 'currentPosition'
Please let me know what could be the problem.
Thanks, Zeynab
Asked by zeinab on 2017-06-06 10:32:14 UTC
Answers
The class Path is ill-formed. All class variables should be instantiated in the __init__
constructor. It looks like you have variables defined in between the class Path:
line and the __init__
constructor. Take these out: nothing inside a class should ever be defined outside functions. Class-wide variables are defined in the __init__
constructor. The variables inside the __init__
look correct, including the prepended self.
which people often forget.
Just to be sure, is path
a member of another class, which has the function computePathWithoutWaypoints
? Also, the line for waypoint in path
seems odd to me, as you're iterating over a class?
Asked by ufr3c_tjc on 2017-06-06 18:15:35 UTC
Comments
I don't think that this can be answered from the small code given. Also it looks more a python question then ros. Btw calling "Path(path.currentPosition,path.speed)" looks a bit strange. Do you effectively want to copy your object?
Asked by Humpelstilzchen on 2017-06-06 11:11:51 UTC
Thanks for your reply. Well, its not copying but rather creating a new object with similar fields for the variables of
currentPosition
andspeed
. I agree it's more a python problem and the code is small. But it really looks like the issue happens in this one line of calling the constructor.Asked by zeinab on 2017-06-06 11:44:21 UTC
Please let me know what you would like to see more.
Asked by zeinab on 2017-06-06 11:45:04 UTC
Maybe attach a complete script that can reproduce the problem
Asked by Humpelstilzchen on 2017-06-06 15:25:10 UTC
I'll leave this open as there are already comments and answers, but it should really have been closed as off-topic.
@zeinab: ROS Answers is for questions on ROS only. We try to keep this forum as on-topic as possible, as we already have almost 35000 questions and there are better places ..
Asked by gvdhoorn on 2017-06-07 00:42:43 UTC
.. to post these kind of programming / Python questions.
Please post similar question to such places in the future.
Thank you.
Asked by gvdhoorn on 2017-06-07 00:43:22 UTC
Sure, but I thought there might be an element of ROS such as threads that could lead to this, since I could not think of anything else.
Asked by zeinab on 2017-07-21 07:32:41 UTC