This is my service server code of finding wall, my code runs well but the problem is when I launch my server using roslaunch cmd my service getting start, later when I call my service using rosservice call, it being called but the actual thing is the response getting returned as true before the completion of whole execution of my code, I mean before the completion of wall find the response is getting printed as true
Please give some suggestions to optimize my code
Thank you
Your service callback has no logic to make the robot move, so your callback simply sets the wallfound variable to True and returns immediately!
Fix: Include robot movement inside your service callback.
Also you need to clean your laser callback. You should never overload your subscriber callbacks with robotic movements. That is a very bad practice. Move the entire “robot movement wall finding logic” inside the service callback.
Basically just the data acquisition logic only. Read the laser scan message inside the laser callback and store the values in class variable(s) dedicated to store laser scan readings.
idk whats the problem but my ranges array in servicecallback function is not updating the values so the robot is turning in the loop because it doesn’t reach the minimum value without updating of ranges array in service callback
Your programming logic is unnecessarily complicated for an otherwise easy and small task.
Your robot will always be in a “turning” state because of your first while loop inside your my_callback.
Your robot will be stuck there because the first condition cannot be reached: front_range > self.min_value because of rounding off and accuracy issues.
You need to completely change your programming logic.
Let me give you a simple pseudo-code:
[manually position the robot a bit far from a wall first, but not too far from wall]
service_callback:
get the laser scans
find which is the minimum scan value and get the index
round this minimum range to 3 decimal places
now choose a direction and spin
loop over:
rotate the robot
wait for few secs for spin to complete (0.5 to 1.0 secs)
stop the robot
get laser scans
find minimum range and index
check if the new minimum range is same as the first minimum range
if not the same then continue else break
[at this point the robot should have positioned itself correctly facing the wall]
return with wallfound set to True
Thank you for the response sir but my actual problem is my ranges array in servicecallback function is not updating, so how do i update the ranges array in service callback function? is my code has any wrong in updation of array?
Your laser scan callback is fine. I do not see any problems there.
However you might want to declare self.ranges as empty list rather than None.
Use self.ranges = [] instead of self.ranges = None.
thank you for the response sir I found the actual error, I should update the ranges array by using
ranges = self.ranges
because my while loop is iterating and checking the conditions I should add ranges = self.ranges inside my while loop so that the array gets updated and the while loop checks the condition every time and the robot will stop after it faces the wall.
It got worked for me and everything is running well.