Is it possible create a rosnode in python with both subscriber and publisher? [closed]
Is it possible to make a rosnode with both a publisher an subscriber. Based on the tutorials, it looks like each are running in separate thread or infinite while loop? making it impossible to switch between listener and talker?
Is possible to make one rosnode which both has a subscriber and publish, handled by the same node.
Edit:
The case is as you mention @NEngelhard.. The python script establishes a Modbus TCP connection with an server. The server is currently only able to handle request from one master, which is why need the subscriber and publisher to be within the same node, such that I don't need to establish two connections.
This is the code I am running currently.
#!/usr/bin/env python
import rospy
from std_msgs.msg import Empty
from std_msgs.msg import Int8
from std_msgs.msg import String
from pymodbus.client.sync import ModbusTcpClient
import numpy as np
server = ModbusTcpClient('192.168.0.10', port=502)
connection = server.connect()
pub = rospy.Publisher('/conveyor_status', String)
def callback(data):
result_0 = server.read_coils(0,1)
result_1 = server.read_coils(1,1)
result_2 = server.read_coils(2,1)
result_3 = server.read_coils(3,1)
server.write_coil(0, not int(result_0.bits[0]))
server.write_coil(1, not int(result_1.bits[0]))
server.write_coil(2, not int(result_2.bits[0]))
server.write_coil(3, not int(result_3.bits[0]))
print "Message received"
def bin(s):
return str(s) if s<=1 else bin(s>>1) + str(s&1)
def callback_2(data):
received = bin(data.data)
print received[0] + received[1] + received[2] + received[3]
def listener():
rospy.Subscriber('conveyor_control', Empty, callback)
rospy.Subscriber('conveyor_HMI_control',Int8,callback_2)
pub.publish(String("SASAS")) #This is not the actual message, but if this work it should be ok..
rospy.spin()
if __name__ == '__main__':
print "Running"
listener()
In short: Yes, this is perfectly possible.
How?... would you do this.
What have you tried so far and where exactly is your problem?
making two def the talker and listener and adding them in the "main" loop loop.
The case is as you mention@NEngelhard.. The python script establishes a Modbus TCP connection with an server. The server is currently only able to handle request from one master, which is why need the subscriber and publisher to be within the same node, such that I don't need to establish two conn.