I am taking this edx course on robotics using ROS and our first project is "writing a node that subscribes to a topic and publishes to another. Your code will subscribe to a topic called 'two_ints', on which a custom message containing two integers can be broadcast. Make sure to familiarize yourself with the message format of this topic:
int16 a
int16 b
. Those two integers are to be added and the result published to topic 'sum' as an Int16 from std_msgs. "
ok this is what I have so far from stringing other examples I have seen together and guessing... very lost now though about how to connect these and also what the 'callback' thing does or if I need to define that..
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int16
from project1_solution.msg import TwoInts
def listener():
rospy.init_node('project1_solution', anonymous=True)
rospy.Subscriber("two_ints", TwoInts, callback)
rospy.spin()
while not rospy.is_shutdown():
msg = TwoInts()
solution = msg.a + msg.b
def talker():
pub = rospy.Publisher('Sum', Int16, queue_size=10)
rospy.init_node('project1_solution', anonymous=True)
rate = rospy.Rate(0.5) #.5hz
while not rospy.is_shutdown():
msg = Int16()
pub.publish(msg)
rate.sleep()
thank you, any tips or help would be great!
↧