CARLA
CarlaSubscriberListener.cpp
Go to the documentation of this file.
1 #define _GLIBCXX_USE_CXX11_ABI 0
2 
4 #include <iostream>
5 
6 #include <fastdds/dds/subscriber/DataReader.hpp>
7 #include <fastdds/dds/subscriber/DataReaderListener.hpp>
8 #include <fastdds/dds/core/status/SubscriptionMatchedStatus.hpp>
9 #include <fastdds/dds/subscriber/SampleInfo.hpp>
13 
14 namespace carla {
15 namespace ros2 {
16 
17  namespace efd = eprosima::fastdds::dds;
18  using erc = eprosima::fastrtps::types::ReturnCode_t;
19 
20  class CarlaSubscriberListenerImpl : public efd::DataReaderListener {
21  public:
23  efd::DataReader* reader,
24  const efd::SubscriptionMatchedStatus& info) override;
25  void on_data_available(efd::DataReader* reader) override;
26 
27  int _matched {0};
28  bool _first_connected {false};
31  };
32 
33  void CarlaSubscriberListenerImpl::on_subscription_matched(efd::DataReader* reader, const efd::SubscriptionMatchedStatus& info)
34  {
35  if (info.current_count_change == 1) {
36  _matched = info.total_count;
37  _first_connected = true;
38  } else if (info.current_count_change == -1) {
39  _matched = info.total_count;
40  if (_matched == 0) {
42  }
43  } else {
44  std::cerr << info.current_count_change
45  << " is not a valid value for PublicationMatchedStatus current count change" << std::endl;
46  }
47  }
48 
49  void CarlaSubscriberListenerImpl::on_data_available(efd::DataReader* reader)
50  {
51  efd::SampleInfo info;
52  eprosima::fastrtps::types::ReturnCode_t rcode = reader->take_next_sample(&_message, &info);
53  if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
54  VehicleControl control;
55  control.throttle = _message.throttle();
56  control.steer = _message.steer();
57  control.brake = _message.brake();
58  control.hand_brake = _message.hand_brake();
59  control.reverse = _message.reverse();
60  control.gear = _message.gear();
62  _owner->ForwardMessage(control);
63  }
64  if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
65  std::cerr << "RETCODE_ERROR" << std::endl;
66  }
67  if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
68  std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
69  }
70  if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
71  std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
72  }
73  if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
74  std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
75  }
76  if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
77  std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
78  }
79  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
80  std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
81  }
82  if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
83  std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
84  }
85  if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
86  std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
87  }
88  if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
89  std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
90  }
91  if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
92  std::cerr << "RETCODE_TIMEOUT" << std::endl;
93  }
94  if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
95  std::cerr << "RETCODE_NO_DATA" << std::endl;
96  }
97  if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
98  std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
99  }
100  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
101  std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
102  }
103  }
104 
106  _impl->_owner = owner;
107  }
108 
110  _impl(std::make_unique<CarlaSubscriberListenerImpl>()) {
111  _impl->_owner = owner;
112  }
113 
115 
116 }}
eProsima_user_DllExport void throttle(float _throttle)
This function sets a value in member throttle.
eProsima_user_DllExport void hand_brake(bool _hand_brake)
This function sets a value in member hand_brake.
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unique_ptr< CarlaSubscriberListenerImpl > _impl
This class represents the structure CarlaEgoVehicleControl defined by the user in the IDL file...
eProsima_user_DllExport void steer(float _steer)
This function sets a value in member steer.
void on_data_available(efd::DataReader *reader) override
CarlaEgoVehicleControlSubscriber * _owner
carla_msgs::msg::CarlaEgoVehicleControl _message
eProsima_user_DllExport void manual_gear_shift(bool _manual_gear_shift)
This function sets a value in member manual_gear_shift.
void on_subscription_matched(efd::DataReader *reader, const efd::SubscriptionMatchedStatus &info) override
void SetOwner(CarlaEgoVehicleControlSubscriber *owner)
CarlaSubscriberListener(CarlaEgoVehicleControlSubscriber *owner)
eProsima_user_DllExport void reverse(bool _reverse)
This function sets a value in member reverse.
eProsima_user_DllExport void gear(int32_t _gear)
This function sets a value in member gear.
eProsima_user_DllExport void brake(float _brake)
This function sets a value in member brake.