Agrarsense
CollisionSensor.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "CollisionSensor.h"
7#include "Collision.h"
8
10
11ACollisionSensor::ACollisionSensor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
12{
13 PrimaryActorTick.bCanEverTick = false;
14}
15
16void ACollisionSensor::Init(AActor* NewOwner, bool SimulateSensor)
17{
18 SetSimulateSensor(SimulateSensor);
19
20 if (NewOwner)
21 {
22 NewOwner->OnActorHit.AddDynamic(this, &ACollisionSensor::OnCollisionEvent);
23 CurrentOwner = NewOwner;
24 }
25 CollisionMessage = MakeShared<ROSMessages::std_msgs::String>();
26
28}
29
31{
32 Super::BeginPlay();
33}
34
35void ACollisionSensor::EndPlay(const EEndPlayReason::Type EndPlayReason)
36{
37 Super::EndPlay(EndPlayReason);
38
39 if (CurrentOwner.IsValid())
40 {
41 CurrentOwner->OnActorHit.RemoveDynamic(this, &ACollisionSensor::OnCollisionEvent);
42 }
43
44 CurrentOwner.Reset();
45 CollisionMessage.Reset();
46}
47
48void ACollisionSensor::OnCollisionEvent(AActor* Actor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
49{
50 if (!CanSimulateSensor() || !Actor || !OtherActor)
51 {
52 return;
53 }
54
55 NormalImpulse *= 1e-2;
56 if (NormalImpulse.Size() > 200)
57 {
58 CollisionData Collisiondata =
59 {
60 Actor->GetName(),
61 OtherActor->GetName(),
62 NormalImpulse
63 };
64
65 SendCollisionData(Collisiondata.ToString());
66
67 FString MsgHumanReadable = Collisiondata.ToStringHumanReadable();
68 WriteToLogFile(MsgHumanReadable); // Log to separate file
69 SimulatorLog::Log(MsgHumanReadable); // Log to ROS info topic and Simulator log file
70 }
71}
72
73void ACollisionSensor::SendCollisionData(const FString& CollisionDataString)
74{
75 UTopic* Topic = GetROSTopic();
76 if (IsROSConnected() && CollisionMessage.IsValid() && Topic)
77 {
78 CollisionMessage->_Data = CollisionDataString;
79 Topic->Publish(CollisionMessage);
80 }
81}
void SendCollisionData(const FString &CollisionDataString)
TSharedPtr< ROSMessages::std_msgs::String > CollisionMessage
void Init(AActor *NewOwner, bool SimulateSensor=true)
ACollisionSensor(const FObjectInitializer &ObjectInitializer)
virtual void BeginPlay() override
void OnCollisionEvent(AActor *Actor, AActor *OtherActor, FVector NormalImpulse, const FHitResult &Hit)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
TWeakObjectPtr< AActor > CurrentOwner
bool CanSimulateSensor() const
Definition: Sensor.h:161
void SetSimulateSensor(bool SimulateSensor)
Definition: Sensor.h:151
UTopic * GetROSTopic() const
Definition: Sensor.h:141
bool IsROSConnected() const
Definition: Sensor.h:192
void WriteToLogFile(const FString &Message)
Definition: Sensor.cpp:242
virtual void CreateROSTopic()
Definition: Sensor.cpp:146
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
FString ToStringHumanReadable()
FString ToString()