Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

How to quit callback function in Unreal Engine

Hello

I am currently writing code for my Unreal Engine programme using NI-DAQmx C++ code to trigger actions in game when a digital input is triggered but have currently ran into a problem. I have attached the actor code I am using at the bottom of this post. When I press play in the editor, the code functions correctly and activates the Booleans shown in the EveryNCallback1 function, but once the editor is stopped the C++ actor still continues to create output logs from the code:

UE_LOG(LogTemp, Log, TEXT("Value = %f"), 1); which is due to the callback function still being activated I believe.

How do I only have this code activate when the editor is playing?

I have left the DoneCallback1 function blank as I have been trying new functions to solve this issue in there but nothing has seemed to work yet.

 

MyActor.h:

#pragma once


#include "CoreMinimal.h"
#include "ghc.h" //This is #include <NIDAQmx.h> but with slight edits
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class GHC_API AMyActor : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
AMyActor();
UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
bool Scrnsht1=false;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
bool Scrnsht2=false;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
bool Scrnsht3=false;
int main(void);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

};

 

MyActor.cpp

#include "MyActor.h"
#include "Logging/LogMacros.h"
int32 CVICALLBACK EveryNCallback1(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData);
int32 CVICALLBACK DoneCallback1(TaskHandle taskHandle, int32 status, void* callbackData);
bool Scrnsht1;
bool Scrnsht2;
bool Scrnsht3;


AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{

Super::BeginPlay();
main();
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

int AMyActor::main(void) {

int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048] = { '\0' };

/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxCreateTask("", &taskHandle);
(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
(DAQmxCfgSampClkTiming(taskHandle, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000));
(DAQmxCfgDigEdgeStartTrig(taskHandle, "/Dev1/PFI1", DAQmx_Val_Rising));
(DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback1, NULL));
(DAQmxRegisterDoneEvent(taskHandle, 0, DoneCallback1, NULL));
(DAQmxStartTask(taskHandle));


//UE_LOG(LogTemp, Log, TEXT("Value = %f"), 1);
getchar();

 

return 0;
}

int32 CVICALLBACK EveryNCallback1(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
int32 error = 0;
char errBuff[2048] = { '\0' };
static int totalRead = 0;
int32 read = 0;
float64 data[1000];

/*********************************************/
// DAQmx Read Code
/*********************************************/
(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByScanNumber, data, 1000, &read, NULL));
UE_LOG(LogTemp, Log, TEXT("Value = %f"), 1);
if (read > 0) {
if (Scrnsht1) {
if (Scrnsht2) {
if (Scrnsht3) {
UE_LOG(LogTemp, Log, TEXT("Commence Path Planning"), 1);

}
else {
Scrnsht3 = true;
}

}
else {
Scrnsht2 = true;
}

}
else {
Scrnsht1 = true;
}
UE_LOG(LogTemp, Warning, TEXT("Boolean 2 is %s"), Scrnsht2 ? TEXT("true") : TEXT("false"));

DAQmxStopTask(taskHandle);
fflush(stdout);
(DAQmxStartTask(taskHandle));

}


return 0;
}

int32 CVICALLBACK DoneCallback1(TaskHandle taskHandle, int32 status, void* callbackData)
{
int32 error = 0;
char errBuff[2048] = { '\0' };

// Check to see if an error stopped the task.


return 0;
}

Any help would be appreciated 🙂

0 Kudos
Message 1 of 2
(2,881 Views)

In the callback function you stop the task but immediately start it again. So it will trigger another event of course, which will rearm the task again and again. You need somewhere to stop the task and avoid rearming it.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 2
(2,845 Views)