CARLA
 
载入中...
搜索中...
未找到
CustomV2XSensor.cpp
浏览该文件的文档.
1// Copyright (c) 2024 Institut fuer Technik der Informationsverarbeitung (ITIV) at the
2// Karlsruhe Institute of Technology
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "Carla.h"
11#include <string>
12#include <cstring>
13#include <algorithm>
14#include "CustomV2XSensor.h"
15#include "V2X/PathLossModel.h"
16
17std::list<AActor *> ACustomV2XSensor::mV2XActorContainer;
19
20ACustomV2XSensor::ACustomV2XSensor(const FObjectInitializer &ObjectInitializer)
21 : Super(ObjectInitializer)
22{
23 PrimaryActorTick.bCanEverTick = true;
24 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
25
26 // Init path loss model
28}
29
31{
32 UE_LOG(LogCarla, Warning, TEXT("CustomV2XSensor: called setowner with %p"), Owner);
33 if (GetOwner() != nullptr)
34 {
35 ACustomV2XSensor::mV2XActorContainer.remove(GetOwner());
36 UE_LOG(LogCarla, Warning, TEXT("CustomV2XSensor: removed old owner %p"), GetOwner());
37 }
38
39 Super::SetOwner(Owner);
40
41 // Store the actor into the static list if the actor details are not available
42 if(Owner != nullptr)
43 {
45 {
47 UE_LOG(LogCarla, Warning, TEXT("CustomV2XSensor: added owner, length now %d"), ACustomV2XSensor::mV2XActorContainer.size());
48 }
49
50 }
51
53
54 UCarlaEpisode* CarlaEpisode = UCarlaStatics::GetCurrentEpisode(GetWorld());
55 FCarlaActor* CarlaActor = CarlaEpisode->FindCarlaActor(Owner);
56 if (CarlaActor != nullptr)
57 {
58 mStationId = static_cast<long>(CarlaActor->GetActorId());
59 }
60}
61
66
67/* Function to add configurable parameters*/
68void ACustomV2XSensor::Set(const FActorDescription &ActorDescription)
69{
70 UE_LOG(LogCarla, Warning, TEXT("CustomV2XSensor: Set function called"));
71 Super::Set(ActorDescription);
73}
74
75void ACustomV2XSensor::SetPropagationParams(const float TransmitPower,
76 const float ReceiverSensitivity,
77 const float Frequency,
78 const float combined_antenna_gain,
79 const float path_loss_exponent,
80 const float reference_distance_fspl,
81 const float filter_distance,
82 const bool use_etsi_fading,
83 const float custom_fading_stddev)
84{
85 // forward parameters to PathLossModel Obj
86 PathLossModelObj->SetParams(TransmitPower, ReceiverSensitivity, Frequency, combined_antenna_gain, path_loss_exponent, reference_distance_fspl, filter_distance, use_etsi_fading, custom_fading_stddev);
87}
88
90 PathLossModelObj->SetPathLossModel(path_loss_model);
91}
92
97
98/*
99 * Function stores the actor details in to the static list.
100 * Calls the CaService object to generate CAM message
101 * Stores the message in static map
102 */
103void ACustomV2XSensor::PrePhysTick(float DeltaSeconds)
104{
105 Super::PrePhysTick(DeltaSeconds);
106 // Clear the message created during the last sim cycle
107 if (GetOwner())
108 {
109 ACustomV2XSensor::mActorV2XDataMap.erase(GetOwner());
110
111 // Step 0: Create message to send, if triggering conditions fulfilled
112 // this needs to be done in pre phys tick to enable synchronous reception in all other v2x sensors
113 // Check whether the message is generated
115 {
116 // If message is generated store it
117 // make a pair of message and sending power
118 // if different v2x sensors send with different power, we need to store that
120 message_pw.Message = CreateCustomV2XMessage();
121
123 ACustomV2XSensor::mActorV2XDataMap.insert({GetOwner(), message_pw});
124 }
125 }
126}
127
129{
130 CustomV2XM_t message = CustomV2XM_t();
131
132 CreateITSPduHeader(message);
133 std::strcpy(message.message,mMessageData.c_str());
134 mMessageDataChanged = false;
135 return message;
136}
137
145
146/*
147 * Function takes care of sending messages to the current actor.
148 * First simulates the communication by calling LOSComm object.
149 * If there is a list present then messages from those list are sent to the current actor
150 */
151void ACustomV2XSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
152{
153 TRACE_CPUPROFILER_EVENT_SCOPE(ACustomV2XSensor::PostPhysTick);
154
155 // Step 1: Create an actor list which has messages to send targeting this v2x sensor instance
156 std::vector<ActorPowerPair> ActorPowerList;
157 for (const auto &pair : ACustomV2XSensor::mActorV2XDataMap)
158 {
159 if (pair.first != GetOwner())
160 {
161 ActorPowerPair actor_power_pair;
162 actor_power_pair.first = pair.first;
163 // actor sending with transmit power
164 actor_power_pair.second = pair.second.Power;
165 ActorPowerList.push_back(actor_power_pair);
166 }
167 }
168
169 // Step 2: Simulate the communication for the actors in actor list to current actor.
170 if (!ActorPowerList.empty())
171 {
172 UCarlaEpisode *carla_episode = UCarlaStatics::GetCurrentEpisode(GetWorld());
173 PathLossModelObj->Simulate(ActorPowerList, carla_episode, GetWorld());
174 // Step 3: Get the list of actors who can send message to current actor, and the receive power of their messages.
175 ActorPowerMap actor_receivepower_map = PathLossModelObj->GetReceiveActorPowerList();
176 // Step 4: Retrieve the messages of the actors that are received
177
178 // get registry to retrieve carla actor IDs
179 const FActorRegistry &Registry = carla_episode->GetActorRegistry();
180
181 ACustomV2XSensor::V2XDataList msg_received_power_list;
182 for (const auto &pair : actor_receivepower_map)
183 {
185 carla::sensor::data::CustomV2XData received_msg_and_pw;
186 // sent CAM
187 received_msg_and_pw.Message = send_msg_and_pw.Message;
188 // receive power
189 received_msg_and_pw.Power = pair.second;
190
191 msg_received_power_list.push_back(received_msg_and_pw);
192 }
193
194 WriteMessageToV2XData(msg_received_power_list);
195 }
196 // Step 5: Send message
197
198 if (mV2XData.GetMessageCount() > 0)
199 {
200 auto DataStream = GetDataStream(*this);
201 DataStream.SerializeAndSend(*this, mV2XData, DataStream.PopBufferFromPool());
202 }
203 mV2XData.Reset();
204}
205
206/*
207 * Function the store the message into the structure so it can be sent to python client
208 */
210{
211 for (const auto &elem : msg_received_power_list)
212 {
214 }
215}
216
217
218void ACustomV2XSensor::Send(const FString message)
219{
220 //note: this is unsafe!
221 //should be fixed to limit length somewhere
222 mMessageData = TCHAR_TO_UTF8(*message);
223 mMessageDataChanged = true;
224}
225
struct CustomV2XM CustomV2XM_t
std::pair< AActor *, float > ActorPowerPair
std::map< AActor *, float > ActorPowerMap
EScenario
EPathLossModel
const long mMessageId
void Set(const FActorDescription &ActorDescription) override
void SetOwner(AActor *Owner) override
PathLossModel * PathLossModelObj
std::string mMessageData
void Send(const FString message)
std::map< AActor *, carla::sensor::data::CustomV2XData > ActorV2XDataMap
void SetPathLossModel(const EPathLossModel path_loss_model)
void WriteMessageToV2XData(const ACustomV2XSensor::V2XDataList &msg_received_power_list)
CustomV2XM_t CreateCustomV2XMessage()
void SetPropagationParams(const float TransmitPower, const float ReceiverSensitivity, const float Frequency, const float combined_antenna_gain, const float path_loss_exponent, const float reference_distance_fspl, const float filter_distance, const bool use_etsi_fading, const float custom_fading_stddev)
virtual void PrePhysTick(float DeltaSeconds) override
void SetScenario(EScenario scenario)
static FActorDefinition GetSensorDefinition()
const long mProtocolVersion
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime) override
std::vector< carla::sensor::data::CustomV2XData > V2XDataList
ACustomV2XSensor(const FObjectInitializer &ObjectInitializer)
static std::list< AActor * > mV2XActorContainer
void CreateITSPduHeader(CustomV2XM_t &message)
static ACustomV2XSensor::ActorV2XDataMap mActorV2XDataMap
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.
A registry of all the Carla actors.
A view over an actor and its properties.
Definition CarlaActor.h:25
IdType GetActorId() const
Definition CarlaActor.h:81
ActorPowerMap GetReceiveActorPowerList()
void Simulate(const std::vector< ActorPowerPair > ActorList, UCarlaEpisode *CarlaEpisode, UWorld *World)
void SetScenario(EScenario scenario)
void SetPathLossModel(const EPathLossModel path_loss_model)
void SetParams(const float TransmitPower, const float ReceiverSensitivity, const float Frequency, const float combined_antenna_gain, const float path_loss_exponent, const float reference_distance_fspl, const float filter_distance, const bool use_etsi_fading, const float custom_fading_stddev)
float GetTransmitPower()
void SetOwner(AActor *Owner)
static void SetCustomV2X(const FActorDescription &Description, ACustomV2XSensor *V2X)
A simulation episode.
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
const FActorRegistry & GetActorRegistry() const
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
void WriteMessage(CustomV2XData message)
Definition V2XData.h:93
ITSContainer::ItsPduHeader_t header
Definition LibITS.h:769
char message[100]
Definition LibITS.h:770
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.