ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Keep getting that Rate is not defined

asked 2019-01-04 04:46:30 -0500

stevemartin gravatar image

updated 2019-01-04 05:23:11 -0500

I am having some strange problem. When I run the python program I get an error that Rate is not defined:

line 30, in EncoderNodeLeft
    rate.sleep()
NameError: name 'rate' is not defined

Here is my code:

import time
#import ASUS.GPIO as GPIO
import rospy
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path
from std_msgs.msg import Int16


class EncoderNodeLeft:

    def __init__(self,encoder):
        self.tick = 0
        self.encoder = encoder
        self.pub = rospy.Publisher("lwheel", Int16, queue_size=1000)

    def tick_callback(self,channel):
        self.tick += 1
        if self.tick > 32766:
            self.tick = 0
        self.pub.publish(self.tick)

    def run(self):
        #GPIO.add_event_detect(self.encoder, GPIO.RISING, callback=self.tick_callback)
        rate = rospy.Rate(10) # 10hz
        while not rospy.is_shutdown():

            rate.sleep()

if __name__ == "__main__":
    try:
        rospy.init_node("encoder_node_left")
        encoder = 224
        encoder_node_left = EncoderNodeLeft(encoder)
        encoder_node_left.run()
    except rospy.ROSInterruptException:
        rospy.loginfo("There was an exception in encoder_node_left")
edit retag flag offensive close merge delete

Comments

Both tick_callback(..) and run() are functions, not methods of class EncoderNodeLeft: they are missing the self argument. So self.tick (in the cb) will fail fi.

Not saying that is necessarily the cause, but it's a potential problem in any case.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-04 05:13:57 -0500 )edit

@gvdhoorn yes you are right, stupid mistake. But this is not a cause

stevemartin gravatar image stevemartin  ( 2019-01-04 05:22:50 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-01-04 05:31:30 -0500

stevemartin gravatar image

Some Python indentation problems I guess. I rewrote everything and it worked, Lol.

edit flag offensive delete link more

Comments

1

Good to hear you fixed your problem. In the future, perhaps a forum dedicated to Python development would be more suited for these types of problems, as they are not ROS problems, but generic Python ones.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-04 06:52:30 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-04 04:46:30 -0500

Seen: 660 times

Last updated: Jan 04 '19