# ROS\_project\_part2

**URL:** <https://get-help.theconstruct.ai/t/ros-project-part2/9199>\
**Category:** ROS Basics In 5 Days (Python)\
**Created:** [April 2, 2021, 9:45am UTC](https://get-help.theconstruct.ai/t/ros-project-part2/9199 "2021-04-02T09:45:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToniSolo](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/t/e95f7d/32.png) [@ToniSolo](https://get-help.theconstruct.ai/u/ToniSolo)\
**Post date:** [April 2, 2021, 9:45am UTC](https://get-help.theconstruct.ai/t/ros-project-part2/9199/1 "2021-04-02T09:45:35Z")

</div>

Hello,  
Part 2 of basic ROS course project needs to call a service with an empty message. However, it also needs to read laserscan readings in order to execute the service routine. How can I call the service with empty message, but also provide the callback with LaserScan readings?

Here is a sample code below that may be close to what i am saying.  
Assume a custom message with empty request.  
I think something is wrong with the last lines, specifically at my\_service and sub.  
Also ignore the routine inside the def callback.  
I want to combine an empty message to just call the service once, but also insert LaserScan readings while service runs, to be able to execute its routine

#! /usr/bin/env python

import rospy  
from geometry\_msgs.msg import Twist  
from sensor\_msgs.msg import LaserScan  
from custom.srv import custom, customResponse  
import time

def callback(request):

#length=len(msg.ranges)  
#print('length: ',length)  
print(msg.ranges[359])  
print(msg.ranges[270])  
print(msg.ranges[180])  
print(msg.ranges[90])  
print(msg.ranges[0])

if msg.ranges[180] \< 0.5:  
move.linear.x = 0  
move.angular.z = 0.25  
time.sleep(1)  
print(‘mode1’)  
#pub.publish(move)  
elif msg.ranges[90] \> 0.3:  
move.linear.x = 0.1  
move.angular.z = -0.25  
time.sleep(1)  
print(‘mode2’)  
#pub.publish(move)  
elif msg.ranges[90] \< 0.2:  
move.linear.x = 0.1  
move.angular.z = 0.25  
time.sleep(1)  
print(‘mode3’)  
#pub.publish(move)  
elif msg.ranges[90] \> 0.2 and msg.ranges[90] \< 0.3:  
move.linear.x = 0.1  
move.angular.z = 0  
time.sleep(1)  
print(‘mode4’)  
#pub.publish(move)

pub.publish(move)  
response = customResponse()  
response.success = True  
return response

rospy.init\_node(‘reach\_wall\_node\_service’)  
my\_service = rospy.Service(’/reach\_wall’, custom, callback)  
sub = rospy.Subscriber(’/scan’, LaserScan, callback)  
pub = rospy.Publisher(’/cmd\_vel’, Twist, queue\_size=1)  
move = Twist()  
rate = rospy.Rate(1)  
rospy.spin()

---

<div class="post-metadata">

**Author:** ![roalgoal](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/roalgoal/32/7280_2.png) [@roalgoal](https://get-help.theconstruct.ai/u/roalgoal)\
**Post date:** [April 6, 2021, 8:19am UTC](https://get-help.theconstruct.ai/t/ros-project-part2/9199/2 "2021-04-06T08:19:44Z")

</div>

Hi, could you please format your code so I can take a better look at what you have? It’s difficult to see where functions start/end with this format. You can either share screenshots or use the “Preformatted text” button on the window where you type here in the forum. From what I can see here, you can consider separating the service call request and the callback and test your program. What behavior have you gotten with this code?
