Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demos: lin: fix 'async' mode of the dynamic demo #141

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading