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: grand refactoring #143

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

demos: grand refactoring #143

wants to merge 13 commits into from

Conversation

MariusBgm
Copy link
Collaborator

@MariusBgm MariusBgm commented Nov 6, 2024

Demo Refactoring

Review / Tests:

  • Code review
    • ApplicationBase.hpp
    • Demo implementations
  • Run Demos without command line args + sil-kit-system-controller
    • Log output formatting
    • Something unexpected
  • Run Demos with -aA (async, autonomous)
  • Run Demos together with CANoe
  • Docs (demos.rst)

Central modifications / guidelines

  • Removed the original Can, Ethernet, Lin, Flexray, PubSub and Rpc Demos
  • Removed the yaml/json configurations. Instead, a new folder Demos/SampleConfigurations with useful yaml configs is provided.
  • Split into one demo per participant
  • No usage of std::cout, std::cerr except printing command line argument usage
  • Use the SIL Kit Logger for printing
  • Header-only SignalHandler and CommandLineParser in Demos/include.
  • No usage of Press any key
  • Demos work without requiring any command line arguments at all (with time sync/cooridinated start)

The demos

  • use the new ApplicationBase that provides general SIL Kit functionality (e.g. lifecycle, threading, command line args)
  • common behavior per femo is in a separate header Common.hpp. Participant specific code is implemented in the participant specific demo.

Minimal demo SimpleCan

The SimpleCan demo is not using the ApplicationBase to showcase basic SIL Kit usage. It is sync-only, only takes a participant name as command line argument and sends/receives CAN frames. Focus here is on minimizing lines of code. Such a simple demo could be unique for CAN and is not necessary for each bus system.

The ApplicationBase

  • Implements a set of default command line arguments (except --network which is an extension provided by the CAN Demo) :

    .\SilKitDemoCanReader.exe --help
    
    SIL Kit Demo - Can: Log received frames
    
    Arguments:
    -h, --help                   | Get this help.
    -n, --name <name>            | The participant name used to take part in the simulation.
                                   Defaults to 'CanReader'.
    -u, --registry-uri <uri>     | The registry URI to connect to.
                                   Defaults to 'silkit://localhost:8500'.
    -l, --log <level>            | Log to stdout with level:
                                   'trace', 'debug', 'warn', 'info', 'error', 'critical' or 'off'.
                                   Defaults to 'info'.
                                   Cannot be used together with '--config'.
    -c, --config <filePath>      | Path to the Participant configuration YAML or JSON file.
                                   Cannot be used together with '--log'.
                                   Will always run as fast as possible.
    -a, --async                  | Run without time synchronization mode.
                                   Cannot be used together with '--sim-step-duration'.
    -A, --autonomous             | Start the simulation autonomously.
                                   Without this flag, a coordinated start is performed
                                   which requires the SIL Kit System Controller.
    -d, --sim-step-duration <us> | The duration of a simulation step in microseconds.
                                   Defaults to 1us.
                                   Cannot be used together with '--async'.
    -f, --fast                   | Run the simulation as fast as possible.
                                   By default, the execution is slowed down to two work cycles per second.
                                   Cannot be used together with '--config'.
    -N, --network <name>         | Name of the CAN network to use.
                                   Defaults to 'CAN1'.
    -H, --hex                    | Print the CAN payloads in hexadecimal format.
                                   Otherwise, the payloads are interpreted as strings.
    
    
  • The constructor also takes a struct with the arguments that can be used to define defaults per demo implementation (e.g. Can Demos should run with stepSize = 5ms)

  • Provides an interface (pure virtual functions) that must be implemented by the demo:

    • AddCommandLineArgs(): Inject additional demo args. Here, the CAN demo adds a --network arg to set a network name.
    • EvaluateCommandLineArgs(): Evaluate the additional args after parsing. Here, the --network is evaluated and used in the controller creation
    • CreateControllers(): Called before lifecycle setup
    • InitControllers(): Called in CommunicationReadyHandler()
    • DoWorkAsync(): Called in the worker thread in async mode
    • DoWorkSync(std::chrono::nanoseconds now): Called in the SimulationStepHandler
  • A SignalHandler is used that stops the demo in any state on CTRL-C

  • Public methods:

    • SetupCommandLineArgs(...): To split command line arg setup and the simulation run. Optionally, a set of default args can be excluded.
    • Run(): The actual simulation run. The workflow is:
              // Create participant
              SetupParticipant();
    
              // Stop() on Ctrl-C
              SetupSignalHandler();
    
              // app: Controller creation
              CreateControllers();
    
              // React on valid start / simulation abort
              // app: Controller initialization in CommunicationReadyHandler
              SetupLifecycle();
    
              // sync:  Create timeSyncService and simulationStepHandler
              //        app: DoWorkSync
              // async: Create thread and couple to starting handler
              //        app: DoWorkASync
              SetupDoWork();
    
              // Start lifecycle
              Launch();
    
              // Wait for lifecycle to end
              // async: Join worker thread
              WaitUntilDone();
    
  • Demo main then is as simple as:

    int main(int argc, char** argv)
    {
      Arguments args;
      args.participantName = "CanWriter";
      args.duration = 5ms;
      CanWriter app{args};
      app.SetupCommandLineArgs(argc, argv, "Command Line Description");
    
      return app.Run();
    }
    

@MariusBgm MariusBgm changed the title Dev silkit app refactor demos into a generic application and bus specific apps Nov 7, 2024
@KonradBkd KonradBkd changed the title refactor demos into a generic application and bus specific apps demos: grande refactoring Nov 15, 2024
@KonradBkd KonradBkd changed the title demos: grande refactoring demos: grand refactoring Nov 15, 2024
@KonradBkd KonradBkd marked this pull request as ready for review November 15, 2024 13:47
Demos: Overhaul SilKitApplication

Demos: Add signal handling, more command line args, renaming

demos: Improve application demo base

demos: Remove IApp.hpp, pure virtual methods are defined in BaseApplication

demos: Add license headers

demos: Fix include

demos: Include cctype

demos: Add SimpleCan Demo

demos: Remove CanDemo, restructure new Can Demos

demos: Add sample participant configurations

demos: Reorganized common CAN behavior; Review suggestions

demos: Fix command line help message

demos: CanWriter: Always send CAN FD; ApplicationBase: Fix command line help

demos: Use arg struct at construction; Always send CAN FD in demo

demos: Always run as-fast-as-possible with --config
Copy link
Member

@VDanielEdwards VDanielEdwards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more suggestions, mostly just fluff. I'm going to look into the LIN demos specifically now 👍 Overall fantastic stuff!

Demos/communication/Flexray/FlexrayDemoCommon.hpp Outdated Show resolved Hide resolved
Demos/communication/Ethernet/EthernetWriterDemo.cpp Outdated Show resolved Hide resolved
Demos/communication/Can/CanWriterDemo.cpp Outdated Show resolved Hide resolved
docs/CHANGELOG.rst Outdated Show resolved Hide resolved
docs/demos/abstracts.rst Outdated Show resolved Hide resolved
docs/demos/build.rst Outdated Show resolved Hide resolved
docs/demos/build.rst Outdated Show resolved Hide resolved
docs/demos/build.rst Outdated Show resolved Hide resolved
Demos/CMakeLists.txt Show resolved Hide resolved
Demos/CMakeLists.txt Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants