Skip to content

Commit

Permalink
demos: lin: fix 'async' mode of the dynamic demo (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
VDanielEdwards authored Dec 6, 2024
1 parent 8663d38 commit 3a517c3
Showing 1 changed file with 59 additions and 32 deletions.
91 changes: 59 additions & 32 deletions Demos/Lin/LinDemoDynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,20 @@ try

std::cout << "Creating participant '" << participantName << "' with registry " << registryUri << std::endl;
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri);
auto* lifecycleService = participant->CreateLifecycleService({OperationMode::Coordinated});
auto* timeSyncService = lifecycleService->CreateTimeSyncService();

ILifecycleService* lifecycleService{nullptr};
ITimeSyncService* timeSyncService{nullptr};

if (runSync)
{
lifecycleService = participant->CreateLifecycleService({OperationMode::Coordinated});
timeSyncService = lifecycleService->CreateTimeSyncService();
}
else
{
lifecycleService = participant->CreateLifecycleService({OperationMode::Autonomous});
}

auto* linController = participant->CreateLinController("LIN1", "LIN1");

// Set a Stop and Shutdown Handler
Expand All @@ -409,8 +421,12 @@ try

if (participantName == "LinMaster")
{
lifecycleService->SetCommunicationReadyHandler(
[&participantName, linController]() { InitLinMaster(linController, participantName); });
if (runSync)
{
lifecycleService->SetCommunicationReadyHandler(
[&participantName, linController]() { InitLinMaster(linController, participantName); });
}

linController->AddFrameStatusHandler(
[&master](ILinController* linController, const LinFrameStatusEvent& frameStatusEvent) {
master.FrameStatusHandler(linController, frameStatusEvent);
Expand Down Expand Up @@ -450,21 +466,25 @@ try
std::thread workerThread;
auto now = 0ms;

workerThread = std::thread{[&]() {
while (lifecycleService->State() == ParticipantState::ReadyToRun
|| lifecycleService->State() == ParticipantState::Running)
{
master.DoAction(now);
now += 1ms;
std::this_thread::sleep_for(200ms);
}
if (!isStopRequested)
{
std::cout << "Press enter to end the process..." << std::endl;
}
}};
lifecycleService->SetStartingHandler([&] {
workerThread = std::thread{[&]() {
while (lifecycleService->State() == ParticipantState::ReadyToRun
|| lifecycleService->State() == ParticipantState::Running)
{
master.DoAction(now);
now += 1ms;
std::this_thread::sleep_for(200ms);
}

if (!isStopRequested)
{
std::cout << "Press enter to end the process..." << std::endl;
}
}};
});

lifecycleService->StartLifecycle();

std::cout << "Press enter to leave the simulation..." << std::endl;
std::cin.ignore();

Expand All @@ -485,8 +505,11 @@ try
}
else if (participantName == "LinSlave")
{
lifecycleService->SetCommunicationReadyHandler(
[&participantName, linController]() { InitLinSlave(linController, participantName); });
if (runSync)
{
lifecycleService->SetCommunicationReadyHandler(
[&participantName, linController]() { InitLinSlave(linController, participantName); });
}

linController->AddFrameStatusHandler(
[&slave](ILinController* linController, const LinFrameStatusEvent& frameStatusEvent) {
Expand Down Expand Up @@ -532,21 +555,25 @@ try
std::thread workerThread;
auto now = 0ms;

workerThread = std::thread{[&]() {
while (lifecycleService->State() == ParticipantState::ReadyToRun
|| lifecycleService->State() == ParticipantState::Running)
{
slave.DoAction(now);
now += 1ms;
std::this_thread::sleep_for(200ms);
}
if (!isStopRequested)
{
std::cout << "Press enter to end the process..." << std::endl;
}
}};
lifecycleService->SetStartingHandler([&] {
workerThread = std::thread{[&]() {
while (lifecycleService->State() == ParticipantState::ReadyToRun
|| lifecycleService->State() == ParticipantState::Running)
{
slave.DoAction(now);
now += 1ms;
std::this_thread::sleep_for(200ms);
}

if (!isStopRequested)
{
std::cout << "Press enter to end the process..." << std::endl;
}
}};
});

lifecycleService->StartLifecycle();

std::cout << "Press enter to leave the simulation..." << std::endl;
std::cin.ignore();

Expand Down

0 comments on commit 3a517c3

Please sign in to comment.