Data update frequency

Hello,

I have been off the site for a little while but am trying to get started again!
I restarted my program and noticed that while my sensor is distance is decreasing, it is doing so very slowly and not keeping up with my gazebo robot. By the time my turtebot hits the wall, I am at a distance of 0.87 or so. Is it not updating fast enough? Codes below

Also - not as critical, but im never able to get to the 4th checkpoint ( print point 4 completed) on my action client. It isnt creating issues but is driving my crazy!

actionclinet:

#! /usr/bin/env python

import rospy
import actionlib
import turtle2.msg
from turtle2.msg import turtlemvmtAction, turtlemvmtGoal, turtlemvmtResult, turtlemvmtFeedback
import rospy
import sensor_msgs.msg
from geometry_msgs.msg import Twist 
from sensor_msgs.msg import LaserScan
from std_msgs.msg import String






class ActionClient(): #prereq instead of of object , or self???
    


    def __init__(self, dist): #Constructors are used to initialize the object’s state. The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
        self.dist = dist
        self.sub = rospy.Subscriber('/scan', LaserScan, self.callback)
        rate = rospy.Rate(100)
        
    def callback(self, msg) :
        self.dist = (msg.ranges[180])
        print ("point 1", self.dist)
        self.call_server()        
        
                
    def feedback_cb(self):
        print ("point 4: completed")

    
    def call_server(self):
        print ("point 2, server called")

        print ("point 3", self.dist)

        self.client = actionlib.SimpleActionClient('sharedspace', turtlemvmtAction)

        self.client.wait_for_server()

        self.goal = turtlemvmtGoal()

        self.goal.ranges = float(self.dist)

        print ("point3a", self.goal.ranges)        
    
        self.client.send_goal(self.goal, feedback_cb = self.feedback_cb)

        self.client.wait_for_result(timeout=rospy.Duration(1))

        result = self.client.get_result()

        return result
       

    

 

if __name__ == '__main__':
    try:
        rospy.init_node('action_client') 
        j = ActionClient(dist=[])
        print ('The result is:', )
        rospy.spin()
    except rospy.ROSInterruptException as e:
        print ('Something went wrong:', e)

actionserver:

#! /usr/bin/env python

import turtle2.msg
from turtle2.msg import turtlemvmtAction, turtlemvmtFeedback, turtlemvmtResult, turtlemvmtGoal
import rospy
from geometry_msgs.msg import Twist 
from sensor_msgs.msg import LaserScan
from std_msgs.msg import String
import actionlib
import actionlib_tutorials.msg


class ActionServer(object):
    # create messages that are used to publish feedback/result
    #feedback = turtle2.msg.turtlemvmtFeedback()
    #result = turtle2.msg.turtlemvmtResult()
    #goal = turtle2.msg.turtlemvmtGoal()

    def __init__(self):
        self._as = actionlib.SimpleActionServer('sharedspace', turtlemvmtAction, execute_cb=self.execute_cb, auto_start = False)
        self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.Twistobj = Twist()
        self._as.start()
        print ("executing1 @ ",)

    def execute_cb(self, goal):
        print ("executing2")
        success = True 
        feedback = turtlemvmtFeedback()
        result = turtlemvmtResult()
        rate = rospy.Rate(100)
        while True: 

            if self._as.is_preempt_requested():
              self._as.set_preempted()
              success = False
              break

            if float(goal.ranges) > 0.3:
                self.Twistobj.linear.x = .002
                self.Twistobj.linear.y = 0
                self.Twistobj.linear.z = 0
                self.Twistobj.angular.x = 0
                self.Twistobj.angular.y = 0
                self.Twistobj.angular.z = 0
                self.pub.publish(self.Twistobj)
                print("goal 1 @", goal.ranges)
                continue   
            elif float(goal.ranges) <0.2:
                self.Twistobj.linear.x = .01
                self.Twistobj.linear.y = 0
                self.Twistobj.linear.z = 0
                self.Twistobj.angular.x = 20
                self.Twistobj.angular.y = 0
                self.Twistobj.angular.z = 0
                self.pub.publish(self.Twistobj)
                print("goal 2 @", goal.ranges)
                continue  
            elif 0.2 < float(goal.ranges) < 0.5:
                self.Twistobj.linear.x = .02
                self.Twistobj.linear.y = 0
                self.Twistobj.linear.z = 0
                self.Twistobj.angular.x = 20
                self.Twistobj.angular.y = 0
                self.Twistobj.angular.z = 0
                self.pub.publish(self.Twistobj)
                print("goal 3 @", goal.ranges)
                continue  

            feedback.last_dish_washed = last_dish_washed
            result.dishes_washed.append(last_dish_washed)
            self._as.publish_feedback(feedback)
            rate.sleep()


            if success:
                self._as.set_succeeded(self._result)

    

           
if __name__ == '__main__':
    rospy.init_node('action_server')
    server = ActionServer()
    rospy.spin()





























Hello @r.franklinismail

Is it a rosject related to the final project?
Could you share the rosject with me?

Hello!
I didnt realize you replied - I didnt get the email, sorry I am responding so late. Once i figure out how to share I will do so!!

I cannot share because it says its forked from a private rosject but I dont have any other rosjects

Hello @r.franklinismail ,

What’s the goal of your program (I mean what’s the behavior you want to achieve)? Are you following the instructions for an exercise?

I want it to turn when it senses a wall. I am following the instructions as best I can. I cant seem to share the project but I can send the zip drive if that helps?

Just wanted to see if there were any thoughts/updates?

Hello @r.franklinismail ,

Then, you should start by creating a simple ROS program that does that (without putting it inside an Action). Just create a simple ROS program based on Subscribers and Publishers that reads the data from the laser and, depending on the readings, it reacts in one way or another. Trying to work with an Action from the beginning it just adds innecessary complexity to it, specially if you don’t know very well how actions work.

Some comments from quickly reviewing your program:

  • You are calling the Action Server for every reading of the laser sensor:
def callback(self, msg) :
        self.dist = (msg.ranges[180])
        print ("point 1", self.dist)
        self.call_server()

This is a very bad practice, since you are continuously calling the Action Server.

  • The logic of your program is contradictory:
if float(goal.ranges) > 0.3:
elif float(goal.ranges) <0.2:
elif 0.2 < float(goal.ranges) < 0.5:

If goal.ranges is, for instance, 0.4, then 2 conditions in your logic are met, thus the robot is receiving multiple commands for that.

  • Also, an angular velocity of 20, self.Twistobj.angular.x = 20, is way too high. You should test the robot beforehand in order to see what velocities values are the correct ones for moving the robot properly.

Hope this helps you,