Ros2 real robot testing

I was testing my code in turtlebot3. I did my wall finding algorithm using with only /scan values and in simulation it works for the real robot conditions are checked but it doesnt continue. So my main question is i am using multiple mutually exclusive callback group group for every node. 2 for odom recorder 4 for wallfollowing(main) and 2 for wall finding can it be the reason or any guesses? the code seems to get stuck at this stage. Only for turtlebot.

self.group1 = MutuallyExclusiveCallbackGroup()
self.group2 = MutuallyExclusiveCallbackGroup()
self.srv = self.create_service(
            FindWall, 'find_wall', self.find_wall_callback, callback_group=self.group1)
self.lasersubscriber_ = self.create_subscription(LaserScan, '/scan', self.laser_callback, QoSProfile(
            depth=10, reliability=ReliabilityPolicy.RMW_QOS_POLICY_RELIABILITY_RELIABLE), callback_group=self.group2)

self.distance_tolerance = 0.06

 def find_wall_callback(self, request, response):
        self.get_logger().info('find_wall_callback working')
        msg = Twist()
        while not self.nearest_wall_found:
            self.get_logger().info('Waiting for wall info')
            msg.linear.x = 0.0
            msg.angular.z = 0.0
            self.cmdpubliser_.publish(msg)
            time.sleep(0.5)

        if self.nearest_wall_found == True and self.first_turn_done == False:

            self.get_logger().info('Turning to wall')

            while not self.distance_to_wall-self.distance_tolerance < self.distance_to_nearest_wall < self.distance_to_wall+self.distance_tolerance:
                if (self.distance_to_nearest_wall_idx) >= 360:
                    msg.linear.x = 0.0
                    msg.angular.z = 0.2
                elif (self.distance_to_nearest_wall_idx) < 360:
                    msg.linear.x = 0.0
                    msg.angular.z = -0.2
                self.cmdpubliser_.publish(msg)
               
            self.first_turn_done = True
            msg.linear.x = 0.0
            msg.angular.z = 0.0
            self.cmdpubliser_.publish(msg)

Could you state clearly what is working in the simulation and now working on the real robot? Then we can start figuring out the reason for that.

Sure and i am sorry about the post being complicated
First of all in simulation the wall following can call wall finder node and it can find the closest wall turns to it by comparing the front laserscan value get close to it turns it right. After that the odom recorder starts and wall following function starts and robot follows the wall as expected.
For the real robot it can call wall finder and find the closest wall and searches it according to printing value i checked it can do both correctly but even if the conditions become true the robot does not stop searching

Thanks for the clarification. Only that I don’t understand this part:

You didn’t mention anything about this in the simulation part.

Perhaps this is applicable and can help. One fundamental difference in the way topics work in the robot vs simulation is that:

in the simulation when you publish successfully to a topic, it remembers the last message and keeps acting on it. But in the real robot, it only acts on it once, so you have to keep publishing.

For instance, if you want to robot to keep moving in a specific direction in the simulation, you just need to publish the message once and it keeps moving. But in the real robot, you have to keep publishing the message or it would move and then stop.

The condition was the scan values of front laser being in between shortest distance- tolerance value and shotest distance+tolerance value it is both same in simulation and real robot. In code this looks something like this

if self.nearest_wall_found == True and self.first_turn_done == False:
            self.get_logger().info('Turning to wall')
            while not self.distance_to_wall-self.distance_tolerance < self.distance_to_nearest_wall < self.distance_to_wall+self.distance_tolerance:
                if (self.distance_to_nearest_wall_idx) >= 360:
                    msg.linear.x = 0.0
                    msg.angular.z = 0.2
                elif (self.distance_to_nearest_wall_idx) < 360:
                    msg.linear.x = 0.0
                    msg.angular.z = -0.2
                self.cmdpubliser_.publish(msg)
            self.first_turn_done = True
            msg.linear.x = 0.0
            msg.angular.z = 0.0
            self.cmdpubliser_.publish(msg)

And for keep publishing after stopping to motion and getting closer i used this part

elif self.first_turn_done:
            while self.distance_to_wall > 0.3:
                msg.linear.x = 0.03
                msg.angular.z = 0.0
                self.cmdpubliser_.publish(msg)
            while self.rightdistance_to_wall > 0.3:
                msg.linear.x = 0.0
                msg.angular.z = 0.2
                self.cmdpubliser_.publish(msg)
            self.get_logger().info('Wall found and turned right to it!')
            msg.linear.x = 0.0
            msg.angular.z = 0.0
            self.cmdpubliser_.publish(msg)
            response.wallfound = True
            self.task_finished = True
            return response

so it should keep publish the message but in real robot it cant get out of the first loop even when the condition was true. I was thinking that maybe my use of multiple mutually exclusive callback groups are blocking it, is something like that possible?

For those who stumple here changing mutually exclusive group to reentrant group solved the problem for me

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.