CARLA
 
载入中...
搜索中...
未找到
V2XSensor.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"
12#include <string.h>
13#include <algorithm>
14#include "V2X/CaService.h"
15#include "V2XSensor.h"
16#include "V2X/PathLossModel.h"
17std::list<AActor *> AV2XSensor::mV2XActorContainer;
19
20AV2XSensor::AV2XSensor(const FObjectInitializer &ObjectInitializer)
21 : Super(ObjectInitializer)
22{
23 PrimaryActorTick.bCanEverTick = true;
24 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
25
26 // Init path loss model
29}
30
32{
33 UE_LOG(LogCarla, Warning, TEXT("V2XSensor: called setowner with %p"), Owner);
34 if (GetOwner() != nullptr)
35 {
36 AV2XSensor::mV2XActorContainer.remove(GetOwner());
37 UE_LOG(LogCarla, Warning, TEXT("V2XSensor: removed old owner %p"), GetOwner());
38 }
39
40 Super::SetOwner(Owner);
41
42 // Store the actor into the static list if the actor details are not available
43 if (Owner != nullptr)
44 {
46 {
47 AV2XSensor::mV2XActorContainer.push_back(Owner);
48 UE_LOG(LogCarla, Warning, TEXT("V2XSensor: added owner, length now %d"), AV2XSensor::mV2XActorContainer.size());
49 }
50 UWorld *world = GetWorld();
51 CaServiceObj->SetOwner(world, Owner);
53 }
54}
55
60
61/* Function to add configurable parameters*/
62void AV2XSensor::Set(const FActorDescription &ActorDescription)
63{
64 UE_LOG(LogCarla, Warning, TEXT("V2XSensor: Set function called"));
65 Super::Set(ActorDescription);
66 UActorBlueprintFunctionLibrary::SetV2X(ActorDescription, this);
67}
68
69void AV2XSensor::SetCaServiceParams(const float GenCamMin, const float GenCamMax, const bool FixedRate)
70{
71 // forward parameters to CaService Obj
72 CaServiceObj->SetParams(GenCamMin, GenCamMax, FixedRate);
73}
74
75void AV2XSensor::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{
91 PathLossModelObj->SetPathLossModel(path_loss_model);
92}
93
95{
97}
98
99/*
100 * Function stores the actor details in to the static list.
101 * Calls the CaService object to generate CAM message
102 * Stores the message in static map
103 */
104void AV2XSensor::PrePhysTick(float DeltaSeconds)
105{
106 Super::PrePhysTick(DeltaSeconds);
107 // Clear the message created during the last sim cycle
108 if (GetOwner())
109 {
110 AV2XSensor::mActorV2XDataMap.erase(GetOwner());
111
112 // Step 0: Create message to send, if triggering conditions fulfilled
113 // this needs to be done in pre phys tick to enable synchronous reception in all other v2x sensors
114 // Check whether the message is generated
115 if (CaServiceObj->Trigger(DeltaSeconds))
116 {
117 // If message is generated store it
118 // make a pair of message and sending power
119 // if different v2x sensors send with different power, we need to store that
123 AV2XSensor::mActorV2XDataMap.insert({GetOwner(), cam_pw});
124 }
125 }
126}
127
132
133void AV2XSensor::SetGNSSDeviation(const float noise_lat_stddev,
134 const float noise_lon_stddev,
135 const float noise_alt_stddev,
136 const float noise_head_stddev,
137 const float noise_lat_bias,
138 const float noise_lon_bias,
139 const float noise_alt_bias,
140 const float noise_head_bias)
141{
142 CaServiceObj->SetGNSSDeviation(noise_lat_stddev,
143 noise_lon_stddev,
144 noise_alt_stddev,
145 noise_head_stddev,
146 noise_lat_bias,
147 noise_lon_bias,
148 noise_alt_bias,
149 noise_head_bias);
150}
151
152void AV2XSensor::SetVelDeviation(const float noise_vel_stddev)
153{
154 CaServiceObj->SetVelDeviation(noise_vel_stddev);
155}
156
157void AV2XSensor::SetYawrateDeviation(const float noise_yawrate_stddev, const float noise_yawrate_bias)
158{
159 CaServiceObj->SetYawrateDeviation(noise_yawrate_stddev, noise_yawrate_bias);
160}
161
162/*
163 * Function takes care of sending messages to the current actor.
164 * First simulates the communication by calling LOSComm object.
165 * If there is a list present then messages from those list are sent to the current actor
166 */
167void AV2XSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
168{
169 TRACE_CPUPROFILER_EVENT_SCOPE(AV2XSensor::PostPhysTick);
170 if (GetOwner())
171 {
172 // Step 1: Create an actor list which has messages to send targeting this v2x sensor instance
173 std::vector<ActorPowerPair> ActorPowerList;
174 for (const auto &pair : AV2XSensor::mActorV2XDataMap)
175 {
176 if (pair.first != GetOwner())
177 {
178 ActorPowerPair actor_power_pair;
179 actor_power_pair.first = pair.first;
180 // actor sending with transmit power
181 actor_power_pair.second = pair.second.Power;
182 ActorPowerList.push_back(actor_power_pair);
183 }
184 }
185
186 // Step 2: Simulate the communication for the actors in actor list to current actor.
187 if (!ActorPowerList.empty())
188 {
189 UCarlaEpisode *carla_episode = UCarlaStatics::GetCurrentEpisode(GetWorld());
190 PathLossModelObj->Simulate(ActorPowerList, carla_episode, GetWorld());
191 // Step 3: Get the list of actors who can send message to current actor, and the receive power of their messages.
192 ActorPowerMap actor_receivepower_map = PathLossModelObj->GetReceiveActorPowerList();
193 // Step 4: Retrieve the messages of the actors that are received
194
195 // get registry to retrieve carla actor IDs
196 const FActorRegistry &Registry = carla_episode->GetActorRegistry();
197
198 AV2XSensor::V2XDataList msg_received_power_list;
199 for (const auto &pair : actor_receivepower_map)
200 {
201 // Note: AActor* sender_actor = pair.first;
202 carla::sensor::data::CAMData send_msg_and_pw = AV2XSensor::mActorV2XDataMap.at(pair.first);
203 carla::sensor::data::CAMData received_msg_and_pw;
204 // sent CAM
205 received_msg_and_pw.Message = send_msg_and_pw.Message;
206 // receive power
207 received_msg_and_pw.Power = pair.second;
208
209 msg_received_power_list.push_back(received_msg_and_pw);
210 }
211
212 WriteMessageToV2XData(msg_received_power_list);
213 }
214 // Step 5: Send message
215
216 if (mV2XData.GetMessageCount() > 0)
217 {
218 auto DataStream = GetDataStream(*this);
219 DataStream.SerializeAndSend(*this, mV2XData, DataStream.PopBufferFromPool());
220 }
221 mV2XData.Reset();
222 }
223}
224
225/*
226 * Function the store the message into the structure so it can be sent to python client
227 */
229{
230 for (const auto &elem : msg_received_power_list)
231 {
233 }
234}
std::pair< AActor *, float > ActorPowerPair
std::map< AActor *, float > ActorPowerMap
EScenario
EPathLossModel
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.
void Set(const FActorDescription &ActorDescription) override
Definition V2XSensor.cpp:62
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)
Definition V2XSensor.cpp:75
virtual void PrePhysTick(float DeltaSeconds) override
static ActorV2XDataMap mActorV2XDataMap
Definition V2XSensor.h:72
static std::list< AActor * > mV2XActorContainer
Definition V2XSensor.h:67
void SetGNSSDeviation(const float noise_lat_stddev, const float noise_lon_stddev, const float noise_alt_stddev, const float noise_head_stddev, const float noise_lat_bias, const float noise_lon_bias, const float noise_alt_bias, const float noise_head_bias)
void SetAccelerationStandardDeviation(const FVector &Vec)
void SetPathLossModel(const EPathLossModel path_loss_model)
Definition V2XSensor.cpp:89
static FActorDefinition GetSensorDefinition()
Definition V2XSensor.cpp:56
PathLossModel * PathLossModelObj
Definition V2XSensor.h:69
CaService * CaServiceObj
Definition V2XSensor.h:68
std::map< AActor *, carla::sensor::data::CAMData > ActorV2XDataMap
Definition V2XSensor.h:26
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime) override
AV2XSensor(const FObjectInitializer &ObjectInitializer)
Definition V2XSensor.cpp:20
void SetScenario(EScenario scenario)
Definition V2XSensor.cpp:94
void SetOwner(AActor *Owner) override
Definition V2XSensor.cpp:31
void SetYawrateDeviation(const float noise_yawrate_stddev, const float noise_yawrate_bias)
FV2XData mV2XData
Definition V2XSensor.h:73
std::vector< carla::sensor::data::CAMData > V2XDataList
Definition V2XSensor.h:27
void SetCaServiceParams(const float GenCamMin, const float GenCamMax, const bool FixedRate)
Definition V2XSensor.cpp:69
void SetVelDeviation(const float noise_vel_stddev)
void WriteMessageToV2XData(const V2XDataList &msg_received_power_list)
void SetGNSSDeviation(const float noise_lat_stddev, const float noise_lon_stddev, const float noise_alt_stddev, const float noise_head_stddev, const float noise_lat_bias, const float noise_lon_bias, const float noise_alt_bias, const float noise_head_bias)
void SetVelDeviation(const float noise_vel_stddev_x)
void SetOwner(UWorld *world, AActor *Owner)
Definition CaService.cpp:44
bool Trigger(float DeltaSeconds)
void SetAccelerationStandardDeviation(const FVector &Vec)
void SetYawrateDeviation(const float noise_yawrate_stddev, const float noise_yawrate_bias)
CAM_t GetCamMessage()
void SetParams(const float GenCamMin, const float GenCamMax, const bool FixedRate)
Definition CaService.cpp:90
A registry of all the Carla actors.
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 SetV2X(const FActorDescription &Description, AV2XSensor *V2X)
A simulation episode.
const FActorRegistry & GetActorRegistry() const
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
void WriteMessage(CAMData message)
Definition V2XData.h:61
size_t GetMessageCount() const
Definition V2XData.h:49
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.