To modify the Python script in accordance with your requirements, we will integrate a client that communicates with the rack_detection_server service you described in the C++ code. This Python client will send a request to the service once it receives the signal from another source (which you should implement as per your application's communication logic, for example, a topic subscription that triggers the request).
In this implementation, once the signal is received, the client will send a request to the rack_detection service and process the response. Let's add this logic into your code:
python
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
import numpy as np
import joblib
from rclpy.qos import QoSProfile, QoSReliabilityPolicy, QoSHistoryPolicy
from ref_slam_interface.srv import RackDetection # Import the service interface
print("已经修改了代码0.2.2")
depth_shelves = 1
compare_num = 0.5
class LidarStorageDetection(Node):
def __init__(self):
super().__init__('lidar_storage_detection')
qos_profile = QoSProfile(
reliability=QoSReliabilityPolicy.BEST_EFFORT,
history=QoSHistoryPolicy.KEEP_LAST,
depth=1,
durability=rclpy.qos.QoSDurabilityPolicy.VOLATILE
)
self.subscription = self.create_subscription(
LaserScan,
'scan',
self.lidar_callback,
qos_profile)
self.subscription
self.model = joblib.load('/home/byd/ros_tmp/lidar_RandomForest_model.pkl')
self.get_logger().info("Model loaded successfully.")
# Create a client for RackDetection service
self.client = self.create_client(RackDetection, 'rack_detection')
# Wait for service to become available
while not self.client.wait_for_service(timeout_sec=1.0):
self.get_logger().info('Waiting for service to become available...')
# Example request initialization
self.request = RackDetection.Request()
self.response = None
def send_request(self):
# Set the request parameters as needed
self.request.detecting = 1 # Example: start detection
# Call the service and store the future
self.future = self.client.call_async(self.request)
# Handle the response in a separate callback
self.future.add_done_callback(self.handle_response)
def handle_response(self, future):
try:
response = future.result()
self.response = response
self.get_logger().info(f"Response from service: current_status = {response.current_status}, racks = {response.racks}")
# You can further process racks as needed
# Example placeholder for processing one rack
example_rack = response.racks[0]
x, y, z, status = example_rack.x, example_rack.y, example_rack.z, example_rack.state
self.process_rack_data(x, y, z, status)
except Exception as e:
self.get_logger().error(f'Service call failed: {e}')
def process_rack_data(self, x, y, z, status):
# Process the rack data here as needed
self.get_logger().info(f"Received rack data: x={x}, y={y}, z={z}, status={status}")
def lidar_callback(self, msg):
# Placeholder for the current implementation of lidar callback
# After processing lidar data, send a request (example trigger)
self.send_request()
# Your existing code for processing LaserScan data goes here...
def main(args=None):
rclpy.init(args=args)
lidar_storage_detection = LidarStorageDetection()
rclpy.spin(lidar_storage_detection)
lidar_storage_detection.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
Changes and Logic:
Service Client Setup: The code imports the RackDetection service interface and initializes a client node that communicates with the C++ service node (rack_detection_server).
Service Call: The method send_request() sends a request to the service. It waits for the service to become available before making a call. This method should be called when a signal (like receiving a specific message) is received.
Response Handling: The method handle_response() processes the response asynchronously. It extracts the data—the rack positions—and calls process_rack_data() for further processing (which you will modify according to your requirements).
Dummy Triggers: I've included a boost function send_request() after processing lidar data to simulate when you might want to send this request. You should modify or replace this behavior with whatever event should trigger your request in your system.
This modified script includes the integration for making service calls and handling responses while keeping the original Lidar data processing logic intact. You will still need to adapt the data processed from lidar_callback() depending on when you truly need to send the request to the C++ service. Adjust the trigger as per your specific use case.