How to ensure my callback fully executed then only continue on the other.

asked 2021-08-29 03:10:59 -0500

JoeLoh gravatar image

Hi, I am new to ROS. Basically i am trying to publishing my cmd_vel to turtlebot after getting the lidar scan data from the other node. As an example, the turtlebot shall perform a 90 degree turn after detected certain distance while the turn function is using get milli second concept to achieve same purpose as delay. Here comes the problem where the program not fully executed the turning function then it will execute another statement due to new scan messages keep passing into which result other conditions were fulfilled. So i was wonder is there anything that ensure my callback is executed then only receive new message from scan data.Below is my code, please point to me if i made any mistake or misunderstanding, thank you.

!/usr/bin/env python3

import rospy import time import math import numpy from sensor_msgs.msg import LaserScan from geometry_msgs.msg import Twist

prev_time1=int(round(time.time())1000) prev_time2=int(round(time.time())1000) lstep=0 rstep=0

def leftturn(): global prev_time1 global prev_time2 global lstep

current_time=int(round(time.time())*1000)
if (current_time-prev_time1 <=1000) and (lstep==0):
 print (" left turning:",current_time-prev_time1)
 move.linear.x = 0
 move.angular.z= 0
 pub.publish(move) 
else:
 prev_time1=current_time 
 lstep=1

if (current_time-prev_time2<=6000) and (lstep==1):
 print (" left turning:",current_time-prev_time2)
 move.linear.x = 0
 move.angular.z= 0.9
 pub.publish(move)  
else:
 prev_time2=current_time
 lstep=0

def rightturn(): global prev_time1 global prev_time2 global rstep

current_time=int(round(time.time())*1000)
if (current_time-prev_time1 <=1000) and (rstep==0):
 print (" right stopping:",current_time-prev_time1)
 move.linear.x = 0
 move.angular.z= 0
 pub.publish(move) 
else:
 prev_time1=current_time 
 rstep=1

if (current_time-prev_time2<=6000) and (rstep==1):
 print (" right turning:",current_time-prev_time2)
 move.linear.x = 0
 move.angular.z= -0.9
 pub.publish(move)  
else:
 prev_time2=current_time
 rstep=0

def callback(msg):

if msg.ranges[270]>=1.6 and msg.ranges[280]>=1.6 and msg.ranges[260]>=1.6 and msg.ranges[290]>=1.6 and msg.ranges[250]>=1.6:
    leftturn()
    print ("statement1")


elif msg.ranges[270]<1.5 or msg.ranges[260]<1.5 or msg.ranges[280]<1.5 or msg.ranges[255]<1.5 or msg.ranges[285]<1.5:
    if (msg.ranges[0]<=0.75 or msg.ranges[355]<=0.75 or msg.ranges[5]<=0.75 or msg.ranges[350]<=0.75 or msg.ranges[10]<=0.75 or msg.ranges[345]<=0.75 or msg.ranges[15]<=0.75 ):   
        rightturn()
        pub.publish(move) 
        print ("statement2")

    elif (msg.ranges[0]>0.75 or msg.ranges[355]>0.75 or msg.ranges[5]>0.75 or msg.ranges[350]>0.75 or msg.ranges[10]>0.75 or msg.ranges[345]>0.75 or msg.ranges[15]>0.75 ):
        move.linear.x = -0.5
        move.angular.z= 0.0
        pub.publish(move) 
        print ("statement2")

else:
    move.linear.x = -0.5
    move.angular.z= 0.0
    pub.publish(move) 
    print ...
(more)
edit retag flag offensive close merge delete