Not totally clear about Exe_8.13

Hi there all,
I was trying to solve exercise 8.13 and I got stuck. I solved it getting help from solution. But there I didn’t understand a few things. I would be grateful if I get clarity on them.

1.
def init(self):
# creates the action server
self._as = actionlib.SimpleActionServer(“move_drone_square_as”, TestAction, self.goal_callback, False)
self._as.start()
self.ctrl_c = False
self.rate = rospy.Rate(10)

What is the use of self._as.start() here ?

2.
def publish_once_in_cmd_vel(self, cmd):
“”"
This is because publishing in topics sometimes fails teh first time you publish.
In continuos publishing systems there is no big deal but in systems that publish only
once it IS very important.
“”"
while not self.ctrl_c:
connections = self._pub_cmd_vel.get_num_connections()
if connections > 0:
self._pub_cmd_vel.publish(cmd)
rospy.loginfo(“Publish in cmd_vel…”)
break
else:
self.rate.sleep()

Although something is given in the comment about the above code, it’s still not clear to me. As I go through it’s logic and try to read it, it gets more confusing. If you could explain it and make it more clear ?
And specially, we even call this in stop_drone() turn_drone() and move_forward_drone() functions. Why do we call it in these functions ?

3.
In the pic below, what does the sideSecods = goal.goal mean ? What is goal.goal, specially why this second goal mentioned here ?

4.
In the code below, what does mentioning this second feedback mean ? and is .publish_feedback just a name given here ?
# build and publish the feedback message
self._feedback.feedback = i
self._as.publish_feedback(self._feedback)
# the sequence is computed at 1 Hz frequency
r.sleep()

5.
In the pic, there are lines of code I see in a lot of python documents.
Screenshot (812)
I’ve heard that it’s used to check if the program runs directly or it’s imported
But Can I get a clear explanation on it with an example ? Either the same example or some other.

Thank you for doing all this
Regards,
Abdul Basit Dost

Hello @abdulbasitisdost,

Let’s go by parts:

1.- The start() function is starting (launching) the Action Server. In the previous line the AS is being configured, but not explicitly started. See: http://docs.ros.org/en/melodic/api/actionlib/html/classactionlib_1_1simple__action__server_1_1SimpleActionServer.html#ab1d26031978ae7a1a5e04a79ad63d3c1

2.- This function is used to publish only 1 message into a topic. Publishing only one message into a topic sometimes fails, so this function makes sure that this is done properly. See: https://www.theconstructsim.com/ros-qa-158-publish-one-message-topic/

3.- The first goal contains the whole goal message. The 2nd goal accesses the actual variable inside the goal part of the message, which is named goal in this case. You can check this by having a look at the message you are using here:

roscd actionlib
cd action
cat Test.action

4.- This is the same case as before, the first feedback makes reference to the whole feedback message, which is defined at the beginning of the program, here:

_feedback = TestFeedback()

and the 2nd feedback actually accesses the variable that contains the feedback, as you can see in the Test.action message. The publish_feedback() is the function used by the Action Server in order to publish feedback. See: http://docs.ros.org/en/melodic/api/actionlib/html/classactionlib_1_1simple__action__server_1_1SimpleActionServer.html#a4c5e2e6c8c55a18ab46699f2e6e93bc2

5.- Check: https://www.freecodecamp.org/news/if-name-main-python-example/

Best,

2 Likes

Thank you so much Professor Alberto @albertoezquerro for your clear explanation and all the links you shared. Thank you for being there
One doubt, dear Professor…In point 5, where the main condition is checked, the node name is rospy.init_node('move_square') …But in our launch file, the name is different i-e name="move_drone_square"
The code still runs, how ?

Professor Alberto @albertoezquerro
Would you please help me with this ?

Or anyone else, please ?

Hi Abdul,

In a launch file, you can type anything in the name=" " argument, and it will still work. This name is the one that is listed in the command
rosnode list.

Rodrigo

But shouldn’t the name of the rospy.init_node() and the name in the .launch file be same to work ?
I’m asking this because, in the previous notes I’ve seen that both were same.

No, the names don’t have to be the same in order to work. In the line
rospy.init_node('move_square')
the default node name is being passed, while what is being done on the launch file is actually a remaping of the name into something else (it could be any name), in this case name="move_drone_square".

Personally I keep both the same to avoid confusion, but there are some cases where it’s useful to remap the node name in a launch file.

2 Likes