ROS_project_part2

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()

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?