Node change from publisher to subscriber under conditional statement











up vote
0
down vote

favorite












I want to subscribe to a geomtery message that is being published. But inside a node that is also a publisher under a condition.



My node publishes a cmd_vel message when an object is near the robot, to tell the robot to stop. and if not, then it moves to a constant speed.



But now I want the robot to subscribe to a cmd_vel that is being published when the condition is not met, for example, to be able to move it by using keyboard or others.



I have tried to write it but nothing is working, so I did not think it is useful to show to you what did not work. Because it does not work. Instead if you could give me help on how to do this, especially with the code part, I will be thankful.



I also want to ask if there is a way so that it publishes on the topic only when the condition is met, and just not publishing when it does not.



#!/usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

class Obstacle():
def __init__(self):
self.LIDAR_ERR = 0.05
self._cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
self.obstacle()

def get_scan(self):
msg = rospy.wait_for_message("scan", LaserScan)
self.scan_filter =
for i in range(360):
if i <= 15 or i > 335:
if msg.ranges[i] >= self.LIDAR_ERR:
self.scan_filter.append(msg.ranges[i])

def obstacle(self):
self.twist = Twist()
while not rospy.is_shutdown():
self.get_scan()

if min(self.scan_filter) < 0.2:
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
self._cmd_pub.publish(self.twist)
rospy.loginfo('Stop!')

else:
self.twist.linear.x = 0.5
self.twist.angular.z = 0.0
rospy.loginfo('distance of the obstacle : %f', min(self.scan_filter))

self._cmd_pub.publish(self.twist)

def main():
rospy.init_node('turtlebot3_obstacle')
try:
obstacle = Obstacle()
except rospy.ROSInterruptException:
pass

if __name__ == '__main__':
main()









share|improve this question
























  • As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
    – Darkproduct
    Nov 23 at 16:51










  • Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
    – 강천사
    Nov 24 at 3:07

















up vote
0
down vote

favorite












I want to subscribe to a geomtery message that is being published. But inside a node that is also a publisher under a condition.



My node publishes a cmd_vel message when an object is near the robot, to tell the robot to stop. and if not, then it moves to a constant speed.



But now I want the robot to subscribe to a cmd_vel that is being published when the condition is not met, for example, to be able to move it by using keyboard or others.



I have tried to write it but nothing is working, so I did not think it is useful to show to you what did not work. Because it does not work. Instead if you could give me help on how to do this, especially with the code part, I will be thankful.



I also want to ask if there is a way so that it publishes on the topic only when the condition is met, and just not publishing when it does not.



#!/usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

class Obstacle():
def __init__(self):
self.LIDAR_ERR = 0.05
self._cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
self.obstacle()

def get_scan(self):
msg = rospy.wait_for_message("scan", LaserScan)
self.scan_filter =
for i in range(360):
if i <= 15 or i > 335:
if msg.ranges[i] >= self.LIDAR_ERR:
self.scan_filter.append(msg.ranges[i])

def obstacle(self):
self.twist = Twist()
while not rospy.is_shutdown():
self.get_scan()

if min(self.scan_filter) < 0.2:
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
self._cmd_pub.publish(self.twist)
rospy.loginfo('Stop!')

else:
self.twist.linear.x = 0.5
self.twist.angular.z = 0.0
rospy.loginfo('distance of the obstacle : %f', min(self.scan_filter))

self._cmd_pub.publish(self.twist)

def main():
rospy.init_node('turtlebot3_obstacle')
try:
obstacle = Obstacle()
except rospy.ROSInterruptException:
pass

if __name__ == '__main__':
main()









share|improve this question
























  • As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
    – Darkproduct
    Nov 23 at 16:51










  • Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
    – 강천사
    Nov 24 at 3:07















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to subscribe to a geomtery message that is being published. But inside a node that is also a publisher under a condition.



My node publishes a cmd_vel message when an object is near the robot, to tell the robot to stop. and if not, then it moves to a constant speed.



But now I want the robot to subscribe to a cmd_vel that is being published when the condition is not met, for example, to be able to move it by using keyboard or others.



I have tried to write it but nothing is working, so I did not think it is useful to show to you what did not work. Because it does not work. Instead if you could give me help on how to do this, especially with the code part, I will be thankful.



I also want to ask if there is a way so that it publishes on the topic only when the condition is met, and just not publishing when it does not.



#!/usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

class Obstacle():
def __init__(self):
self.LIDAR_ERR = 0.05
self._cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
self.obstacle()

def get_scan(self):
msg = rospy.wait_for_message("scan", LaserScan)
self.scan_filter =
for i in range(360):
if i <= 15 or i > 335:
if msg.ranges[i] >= self.LIDAR_ERR:
self.scan_filter.append(msg.ranges[i])

def obstacle(self):
self.twist = Twist()
while not rospy.is_shutdown():
self.get_scan()

if min(self.scan_filter) < 0.2:
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
self._cmd_pub.publish(self.twist)
rospy.loginfo('Stop!')

else:
self.twist.linear.x = 0.5
self.twist.angular.z = 0.0
rospy.loginfo('distance of the obstacle : %f', min(self.scan_filter))

self._cmd_pub.publish(self.twist)

def main():
rospy.init_node('turtlebot3_obstacle')
try:
obstacle = Obstacle()
except rospy.ROSInterruptException:
pass

if __name__ == '__main__':
main()









share|improve this question















I want to subscribe to a geomtery message that is being published. But inside a node that is also a publisher under a condition.



My node publishes a cmd_vel message when an object is near the robot, to tell the robot to stop. and if not, then it moves to a constant speed.



But now I want the robot to subscribe to a cmd_vel that is being published when the condition is not met, for example, to be able to move it by using keyboard or others.



I have tried to write it but nothing is working, so I did not think it is useful to show to you what did not work. Because it does not work. Instead if you could give me help on how to do this, especially with the code part, I will be thankful.



I also want to ask if there is a way so that it publishes on the topic only when the condition is met, and just not publishing when it does not.



#!/usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

class Obstacle():
def __init__(self):
self.LIDAR_ERR = 0.05
self._cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
self.obstacle()

def get_scan(self):
msg = rospy.wait_for_message("scan", LaserScan)
self.scan_filter =
for i in range(360):
if i <= 15 or i > 335:
if msg.ranges[i] >= self.LIDAR_ERR:
self.scan_filter.append(msg.ranges[i])

def obstacle(self):
self.twist = Twist()
while not rospy.is_shutdown():
self.get_scan()

if min(self.scan_filter) < 0.2:
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
self._cmd_pub.publish(self.twist)
rospy.loginfo('Stop!')

else:
self.twist.linear.x = 0.5
self.twist.angular.z = 0.0
rospy.loginfo('distance of the obstacle : %f', min(self.scan_filter))

self._cmd_pub.publish(self.twist)

def main():
rospy.init_node('turtlebot3_obstacle')
try:
obstacle = Obstacle()
except rospy.ROSInterruptException:
pass

if __name__ == '__main__':
main()






message ros






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 at 3:06









Darkproduct

17915




17915










asked Nov 22 at 3:57









강천사

64




64












  • As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
    – Darkproduct
    Nov 23 at 16:51










  • Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
    – 강천사
    Nov 24 at 3:07




















  • As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
    – Darkproduct
    Nov 23 at 16:51










  • Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
    – 강천사
    Nov 24 at 3:07


















As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
– Darkproduct
Nov 23 at 16:51




As far as I understand your problem, you are trying to pass the control from one node to another under some conditions. Am I correct?
– Darkproduct
Nov 23 at 16:51












Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
– 강천사
Nov 24 at 3:07






Yes, I want to pass the control to another node, if the condition is not met. to be more specific to a Rosbridge that is subscribing to an API and then publishing to cmd_vel topic.
– 강천사
Nov 24 at 3:07














2 Answers
2






active

oldest

votes

















up vote
0
down vote













Subscribing and unsubscribing just like publishing and unpublishing is not a optimal way since it takes time to construct and deconstruct them what you can do is set up both publisher and subscriber and control when they will be updated and published:




  1. control when you publish by adding an if before self._cmd_pub.publish(self.twist).

  2. set up your callback and update your variables with data from callback if your condition mets.






share|improve this answer





















  • Thank you for your help
    – 강천사
    Nov 25 at 14:39


















up vote
0
down vote













You have two options here and they differentiate in what you are trying to archive:





  1. You have different tasks for the robot to do. Take a look into the actionlib.



    E.g. the robot should drive to house x and park in the garage. Then you can split this into multiple tasks:




    • Drive to the garage.

    • Park in the garage.



    This split makes sense because the nature of the maneuvers are very different and you'll probably wont to write another node (or multiple nodes) for only this maneuver.




  2. From your question I'm a little bit confused because it seems like you are trying to implement a "safety" feature, which should be able to take control in all possible cases.



    Unfortunately, ROS has, as far as I know, no good way to differentiate between the publishers of a ROS message. A ROS message can have as many publishers and subscribers as you want and you are not able to select "listen here, but ignore these messages".



    The best way I found to do something like this, is to create a new subscriber in every cmd_vel publisher and if your "safety" node sends "stop" (or bool false/true in this case) the publisher stops publishing.




One last recommendation: Try to bundle up your navigation tools to one branch for every different task with only one end node, which publishes cmd_vel. And every safety and control mechanism you need is packed underneath and not "in parallel" so to speak.



This makes everything much easier and you can use the actionlib to its full potential. You can even share nodes between the branches, without interfering, because the end publisher will be silenced if not needed.






share|improve this answer























  • Thank you, I will try it, yes I am thinking of the second case you are saying
    – 강천사
    Nov 25 at 14:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423668%2fnode-change-from-publisher-to-subscriber-under-conditional-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Subscribing and unsubscribing just like publishing and unpublishing is not a optimal way since it takes time to construct and deconstruct them what you can do is set up both publisher and subscriber and control when they will be updated and published:




  1. control when you publish by adding an if before self._cmd_pub.publish(self.twist).

  2. set up your callback and update your variables with data from callback if your condition mets.






share|improve this answer





















  • Thank you for your help
    – 강천사
    Nov 25 at 14:39















up vote
0
down vote













Subscribing and unsubscribing just like publishing and unpublishing is not a optimal way since it takes time to construct and deconstruct them what you can do is set up both publisher and subscriber and control when they will be updated and published:




  1. control when you publish by adding an if before self._cmd_pub.publish(self.twist).

  2. set up your callback and update your variables with data from callback if your condition mets.






share|improve this answer





















  • Thank you for your help
    – 강천사
    Nov 25 at 14:39













up vote
0
down vote










up vote
0
down vote









Subscribing and unsubscribing just like publishing and unpublishing is not a optimal way since it takes time to construct and deconstruct them what you can do is set up both publisher and subscriber and control when they will be updated and published:




  1. control when you publish by adding an if before self._cmd_pub.publish(self.twist).

  2. set up your callback and update your variables with data from callback if your condition mets.






share|improve this answer












Subscribing and unsubscribing just like publishing and unpublishing is not a optimal way since it takes time to construct and deconstruct them what you can do is set up both publisher and subscriber and control when they will be updated and published:




  1. control when you publish by adding an if before self._cmd_pub.publish(self.twist).

  2. set up your callback and update your variables with data from callback if your condition mets.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 at 12:23









Mohammad Ali

30018




30018












  • Thank you for your help
    – 강천사
    Nov 25 at 14:39


















  • Thank you for your help
    – 강천사
    Nov 25 at 14:39
















Thank you for your help
– 강천사
Nov 25 at 14:39




Thank you for your help
– 강천사
Nov 25 at 14:39












up vote
0
down vote













You have two options here and they differentiate in what you are trying to archive:





  1. You have different tasks for the robot to do. Take a look into the actionlib.



    E.g. the robot should drive to house x and park in the garage. Then you can split this into multiple tasks:




    • Drive to the garage.

    • Park in the garage.



    This split makes sense because the nature of the maneuvers are very different and you'll probably wont to write another node (or multiple nodes) for only this maneuver.




  2. From your question I'm a little bit confused because it seems like you are trying to implement a "safety" feature, which should be able to take control in all possible cases.



    Unfortunately, ROS has, as far as I know, no good way to differentiate between the publishers of a ROS message. A ROS message can have as many publishers and subscribers as you want and you are not able to select "listen here, but ignore these messages".



    The best way I found to do something like this, is to create a new subscriber in every cmd_vel publisher and if your "safety" node sends "stop" (or bool false/true in this case) the publisher stops publishing.




One last recommendation: Try to bundle up your navigation tools to one branch for every different task with only one end node, which publishes cmd_vel. And every safety and control mechanism you need is packed underneath and not "in parallel" so to speak.



This makes everything much easier and you can use the actionlib to its full potential. You can even share nodes between the branches, without interfering, because the end publisher will be silenced if not needed.






share|improve this answer























  • Thank you, I will try it, yes I am thinking of the second case you are saying
    – 강천사
    Nov 25 at 14:39















up vote
0
down vote













You have two options here and they differentiate in what you are trying to archive:





  1. You have different tasks for the robot to do. Take a look into the actionlib.



    E.g. the robot should drive to house x and park in the garage. Then you can split this into multiple tasks:




    • Drive to the garage.

    • Park in the garage.



    This split makes sense because the nature of the maneuvers are very different and you'll probably wont to write another node (or multiple nodes) for only this maneuver.




  2. From your question I'm a little bit confused because it seems like you are trying to implement a "safety" feature, which should be able to take control in all possible cases.



    Unfortunately, ROS has, as far as I know, no good way to differentiate between the publishers of a ROS message. A ROS message can have as many publishers and subscribers as you want and you are not able to select "listen here, but ignore these messages".



    The best way I found to do something like this, is to create a new subscriber in every cmd_vel publisher and if your "safety" node sends "stop" (or bool false/true in this case) the publisher stops publishing.




One last recommendation: Try to bundle up your navigation tools to one branch for every different task with only one end node, which publishes cmd_vel. And every safety and control mechanism you need is packed underneath and not "in parallel" so to speak.



This makes everything much easier and you can use the actionlib to its full potential. You can even share nodes between the branches, without interfering, because the end publisher will be silenced if not needed.






share|improve this answer























  • Thank you, I will try it, yes I am thinking of the second case you are saying
    – 강천사
    Nov 25 at 14:39













up vote
0
down vote










up vote
0
down vote









You have two options here and they differentiate in what you are trying to archive:





  1. You have different tasks for the robot to do. Take a look into the actionlib.



    E.g. the robot should drive to house x and park in the garage. Then you can split this into multiple tasks:




    • Drive to the garage.

    • Park in the garage.



    This split makes sense because the nature of the maneuvers are very different and you'll probably wont to write another node (or multiple nodes) for only this maneuver.




  2. From your question I'm a little bit confused because it seems like you are trying to implement a "safety" feature, which should be able to take control in all possible cases.



    Unfortunately, ROS has, as far as I know, no good way to differentiate between the publishers of a ROS message. A ROS message can have as many publishers and subscribers as you want and you are not able to select "listen here, but ignore these messages".



    The best way I found to do something like this, is to create a new subscriber in every cmd_vel publisher and if your "safety" node sends "stop" (or bool false/true in this case) the publisher stops publishing.




One last recommendation: Try to bundle up your navigation tools to one branch for every different task with only one end node, which publishes cmd_vel. And every safety and control mechanism you need is packed underneath and not "in parallel" so to speak.



This makes everything much easier and you can use the actionlib to its full potential. You can even share nodes between the branches, without interfering, because the end publisher will be silenced if not needed.






share|improve this answer














You have two options here and they differentiate in what you are trying to archive:





  1. You have different tasks for the robot to do. Take a look into the actionlib.



    E.g. the robot should drive to house x and park in the garage. Then you can split this into multiple tasks:




    • Drive to the garage.

    • Park in the garage.



    This split makes sense because the nature of the maneuvers are very different and you'll probably wont to write another node (or multiple nodes) for only this maneuver.




  2. From your question I'm a little bit confused because it seems like you are trying to implement a "safety" feature, which should be able to take control in all possible cases.



    Unfortunately, ROS has, as far as I know, no good way to differentiate between the publishers of a ROS message. A ROS message can have as many publishers and subscribers as you want and you are not able to select "listen here, but ignore these messages".



    The best way I found to do something like this, is to create a new subscriber in every cmd_vel publisher and if your "safety" node sends "stop" (or bool false/true in this case) the publisher stops publishing.




One last recommendation: Try to bundle up your navigation tools to one branch for every different task with only one end node, which publishes cmd_vel. And every safety and control mechanism you need is packed underneath and not "in parallel" so to speak.



This makes everything much easier and you can use the actionlib to its full potential. You can even share nodes between the branches, without interfering, because the end publisher will be silenced if not needed.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 at 20:32

























answered Nov 24 at 19:26









Darkproduct

17915




17915












  • Thank you, I will try it, yes I am thinking of the second case you are saying
    – 강천사
    Nov 25 at 14:39


















  • Thank you, I will try it, yes I am thinking of the second case you are saying
    – 강천사
    Nov 25 at 14:39
















Thank you, I will try it, yes I am thinking of the second case you are saying
– 강천사
Nov 25 at 14:39




Thank you, I will try it, yes I am thinking of the second case you are saying
– 강천사
Nov 25 at 14:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423668%2fnode-change-from-publisher-to-subscriber-under-conditional-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)