#ifdef __INTIME_CPP20
#include <stdio.h>
#include "rt.h"
#include "rsi.h"
#include "HelperFunctions.h"
constexpr int32_t SYNC_PERIOD = 1;
constexpr int32_t AXIS_COUNT = 2;
constexpr double MICROSECONDS_PER_SECOND = 1000000.0;
constexpr int32_t LOW_PRIORITY = 200;
constexpr int32_t SLOW_LOOP_MILLISECONDS = 50;
double encoderPositions[AXIS_COUNT];
int32_t currentPerformanceCounter = 0;
int32_t previousPerformanceCounter = 0;
int32_t deltaPerformanceCounter = 0;
int32_t iterations = 0;
uint32_t cpuFrequency = 0;
double deltaMicroseconds = 0.0;
double minMicroseconds = 1000000.0;
double maxMicroseconds = 0.0;
int32_t rmpSampleCounter;
int32_t syncInterruptSampleCounter;
int32_t lastSyncInterruptSampleCounter;
bool readyToCleanup = false;
RTHANDLE WaitForSystemNotificationsThread = NULL;
RTHANDLE WaitKeyPressAndPrintThread = NULL;
void SyncInterruptMainLoop()
{
while (!readyToCleanup)
{
if (syncInterruptSampleCounter != (lastSyncInterruptSampleCounter + 1))
{
printf("Sync Interrupt not working as expected. syncCounter %ld, lastSyncCounter %ld\n", syncInterruptSampleCounter, lastSyncInterruptSampleCounter);
readyToCleanup = true;
}
deltaPerformanceCounter = currentPerformanceCounter - previousPerformanceCounter;
previousPerformanceCounter = currentPerformanceCounter;
deltaMicroseconds = (double)(deltaPerformanceCounter * (double)(1 / (double)cpuFrequency)) * MICROSECONDS_PER_SECOND;
if (iterations > 1)
{
if (deltaMicroseconds > maxMicroseconds)
{
maxMicroseconds = deltaMicroseconds;
}
if (deltaMicroseconds < minMicroseconds)
{
minMicroseconds = deltaMicroseconds;
}
}
for (int32_t i = 0; i < AXIS_COUNT; i++)
{
encoderPositions[i] = axes[i]->
EncoderPositionGet(RSIMotorFeedback::RSIMotorFeedbackPRIMARY);
}
lastSyncInterruptSampleCounter = syncInterruptSampleCounter;
iterations++;
}
}
void WaitForWaitKeyPressAndPrint(void)
{
while (controller->
OS->
KeyGet((int32_t)RSIWait::RSIWaitPOLL) < 0 && readyToCleanup ==
false)
{
RtSleep(SLOW_LOOP_MILLISECONDS);
printf("SyncInterrupts processed %ld.\t Time since last interrupt: %0.1lfus Min: %0.0lfus Max: %0.0lfus\r", iterations, deltaMicroseconds, minMicroseconds, maxMicroseconds);
}
printf("Key pressed.\n");
readyToCleanup = true;
}
void WaitForSystemNotifications(void)
{
EVENTINFO eiEventInfo;
while (RtNotifyEvent(RT_SYSTEM_NOTIFICATIONS | RT_EXIT_NOTIFICATIONS,
WAIT_FOREVER,
&eiEventInfo))
{
switch (eiEventInfo.dwNotifyType)
{
case TERMINATE:
case NT_HOST_SHUTDOWN_PENDING:
case KERNEL_STOPPING:
case KERNEL_SHUTDOWN_PENDING:
case RT_CLIENT_DOWN:
case RT_CLIENT_UP:
case NT_HOST_DOWN:
case NT_HOST_UP:
case NT_BLUESCREEN:
readyToCleanup = true;
break;
}
}
}
void Cleanup(void)
{
if (WaitForSystemNotificationsThread != NULL)
{
DeleteRtThread(WaitForSystemNotificationsThread);
}
if (WaitKeyPressAndPrintThread != NULL)
{
DeleteRtThread(WaitKeyPressAndPrintThread);
}
if (controller != nullptr)
{
}
}
int32_t main(int32_t argc, char* argv[])
{
try
{
printf("Hello, RapidCodeRT!\n");
if (axisCount < AXIS_COUNT)
{
printf("You don't have enough Axis objects to run this sample. You have %d but need %d.\n", axisCount, AXIS_COUNT);
exit(1);
}
for (int i = 0; i < AXIS_COUNT; i++)
{
}
printf("CPU Frequency is: %u Hz\n", cpuFrequency);
WaitForSystemNotificationsThread = CreateRtThread(LOW_PRIORITY, (LPPROC)WaitForSystemNotifications, 1024, NULL);
WaitKeyPressAndPrintThread = CreateRtThread(LOW_PRIORITY, (LPPROC)WaitForWaitKeyPressAndPrint, 1024, NULL);
SyncInterruptMainLoop();
Cleanup();
}
catch (std::exception const& e)
{
printf("\n%s\n", e.what());
}
return 0;
}
#endif // __INTIME_CPP20