Error in Method data entry



I tried to write my own method, then I get an error in the data entry that is still looking for another in put of time. Please explain my error in either the method creation or the calling of the method

Hi @ROxX ,

You are getting this error because you are not calling the function properly.

It seems that the function move_straight_2 is inside a class in the microproject.py python file.

The error in the error in the picture clearly states that the error is on line 30:
move_straight_2(1.0, 3.0)

This line is implemented wrong. It should be called with reference to a class.
So your function call would be something like class_instance_variable.move_straight_2(1.0, 3.0)
You should instantiate the class as an object before being able to use the function.

If you are calling the function inside the same class, you should call it as shown below:
self.move_straight_2(1.0, 3.0).

This is very basic Python Object-Oriented Programming (OOP) concept that you seem to have not understood. I strongly recommend you to go though the course again on the OOP chapters.

Regards,
Girish




I admit I am struggling with the class / method part of this course.
There is a big jump in information between section 5.3.7 the test exercise 5.1 and chapter 6. I want to use the laser distance data to control the robot moves. But, I don’t see how the self.dist is not available for use. You helped me greatly last week with Gazebo Sim, I realized I have to improve my Python skills before I finish the Gazebo Sim Thanks Tom

Hi @ROxX ,

You get this error with self.dist class variable because you have not declared and initialized it properly in the class constructor def __init__(...).

The self.dist variable must be initialized, so you will write something like self.dist = 0.0.
Unlike in C/C++, where you can leave a variable defined but unintialized, you can’t do that in Python.

The line self.dist, when written without a right hand side, would actually print the value of the variable on the terminal, basically fetching the value for dummy display purpose.
Since at the time of the constructor you don’t have self.dist properly initialized, you are getting the errors shown in your picture.

Initialize the self.dist to 0.0 as self.dist = 0.0 in your constructor function def __init__(...) and your problem should be resolved.

EDIT: Also, your time variable is not initialized. So once you resolve this dist error, you will get the 'time' not defined error, in this line self.time = time.
So there are two fixes for that:

  1. Set self.time = 0.0 in the constructor __init__(...).
    (OR) [this or that but not both]
  2. Initialize time also along with the class constructor like def __init__(self, motion, clockwise, speed, time).
    The choice is yours based on your program design, but just use only one of the two options for time. Don’t implement both!

Regards,
Girish

I understand your explanation. But, I have another question. I thought it was possible to call the methods in RobotControl by using rc. dot method. So I have rc2= RobotControl(). that is defined above the init initiator. But the error highlighted rc2 I am so confused on how to move data between methods or to call methods.

Thank you

Tom
I expected that line to call

def move_straight(self)
: # Initialize velocities
self.cmd.linear.x = 0.5
self.cmd.linear.y = 0
self.cmd.linear.z = 0
self.cmd.angular.x = 0
self.cmd.angular.y = 0
self.cmd.angular.z = 0

Publish the velocity

self.publish_once_in_cmd_vel(

I am trying to apply my skills to my personal project. I want to move my Gazebo robot with this file. I want to use two methods and then call either multiple times. one method for straight and one for turns. Some of the code is based on this course, and the rest if from another course. The publisher functions in a trial that i performed, when i add a more complex method, I get an error. This project is why I took the python course, I have to make this project work





Hi ROxX
you haven’t understood the concept of scope within a class. The rc2 variable that you defined in the class Project is a local variable which is not a member of the class, so you cannot use it as you are using in the screen capture.

Also, you screen captures make very difficult to see the whole code and understand what goes after what. Please copy paste with the proper format your code here so we can understand your code, also copy ourselves and do tests, rather than just watch an image

I agree, I have no idea what the section 6 assignment is about. It seemed like a task that required reading the laser data and making a couple of turns. So, I tried to write my own code. But, in Chap 4, methods return a defined result. In Chap 5 examples, there are no examples on how to call another method or use the “return” from a method. In Chap 6 there is a new script with lots of useful methods. But, still there is no explanation of how they interact. In the attached I tried to use two of the methods, then I get an error message that seems unrelated to the code. So, yes i don’t understand the scope, but I am a serious “student”


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.