반응형

When a node want to listen topics from other node, and control his own robot..

Using python lambda

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace_format = "/px4_{}"
 
class OffboardControl(Node):
    """Node for controlling a vehicle in offboard mode."""
 
    def __init__(self, model_id=1-> None:
        self._id = model_id
        super().__init__("px4_{}".format(self._id))
        model_name = namespace_format.format(self._id)
        print("Model Name : ", model_name)
 
        # Configure QoS profile for publishing and subscribing
        qos_profile = QoSProfile(
            reliability=ReliabilityPolicy.BEST_EFFORT,
            durability=DurabilityPolicy.TRANSIENT_LOCAL,
            history=HistoryPolicy.KEEP_LAST,
            depth=1
        )
 
        # Number of drones around me
        self._num_of_drones = 4
        
        # Subscribe specific topic list
        self.p_ned = list() # vehicle local position
        self.vehicle_local_position_subscriber = list()
        for item in range(self._num_of_drones):
            id = item + 1
            print("Attach to ", namespace_format.format(id))
            self.p_ned.append(VehicleLocalPosition())
            self.vehicle_local_position_subscriber.append(
                self.create_subscription(VehicleLocalPosition,
                    namespace_format.format(id) + '/fmu/out/vehicle_local_position',
                    lambda pos : self.vehicle_local_position_callback(vehicle_local_position=pos, item=item),
                    qos_profile))
 
    def vehicle_local_position_callback(self, vehicle_local_position, item):
        """Callback function for vehicle_local_position topic subscriber."""
        self.p_ned[item] = vehicle_local_position
cs

 

 

*** EOF ***

 

728x90

+ Recent posts