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

#! #keepAttributeError: 'Float32' object has no attribute 'humidity' [ERROR] [1604594380.746952]: bad callback: <function callback at 0x7f72bc562488> Traceback (most recent call last)

asked 2020-11-05 10:50:42 -0600

hafeez360 gravatar image

updated 2020-11-05 13:57:51 -0600

Code below:

#!/usr/bin/env python
import time
import rospy
from std_msgs.msg import Float32
from std_msgs.msg import Int32


def callback(humidity):
    motor(humidity)


def motor(humidity):
    pub=rospy.Publisher('Servo', Int32, queue_size=10) #rospy.Publisher(Topic name, Type, queue_size )
    #rospy.init_node('motor',anonymous=True) #rospy.init_node(Node name,anonymous=True)
    rate=rospy.Rate(10) #10Hz
    if  humidity.humidity > 50:
        pub.publish(180)  
        rospy.loginfo(humidity_value.humidity)

    else:
        pub.publish(0)
        rospy.loginfo(humidity_value.humidity)  
    rate.sleep()

def listener():
    rospy.init_node('listener', anonymous=True) #anonymus let us have many node named the same way running simultaneously
    rospy.Subscriber('humidity', Float32,callback) #Callback name of the function you need to call when you get data
    rospy.spin() #keeps the program running until the node is stopped

if __name__=='__main__':
    listener()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-11-05 14:15:58 -0600

There are a number of issues I see in your code.

The most obvious is the error you are getting: if humidity.humidity > 50: needs to be if humidity.data > 50: because

> rosmsg show Float32
[std_msgs/Float32]:
float32 data

and humidity, like the error says, is a Float32 object -- i.e., the python object representing the Float32 ROS message type.

It also seems like humidity_value is not defined anywhere in your code.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-11-05 10:50:42 -0600

Seen: 193 times

Last updated: Nov 05 '20