# Example listing

{.example-item}
:Example: [Train the inverted double link cartpole model](notebooks/example_2_link_cartpole/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [cart pole]{.example-tag} [machine learning]{.example-tag}
:Folder: example_2_link_cartpole
:Description: We will now demonstrate training the model using StableBaselines 3

{.example-item}
:Example: [Flexible Slider-Crank](notebooks/example_flexible_slider_crank/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [time domain]{.example-tag} [flexible]{.example-tag} [slider crank]{.example-tag}
:Folder: example_flexible_slider_crank
:Description: Illustrates a slider-crank with a flexible body. This is a system that has both flexible bodies and closed loops.

{.example-item}
:Example: [Cart Pole Reinforcement Learning](notebooks/example_cartpole/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [cart pole]{.example-tag} [machine learning]{.example-tag}
:Folder: example_cartpole
:Description: This notebook demonstrates implementing a custom learning environment using kdFlex. By implementing the simple cart pole scenario in a Gymnasium class, any machine learning library that is compatible with Gymnasium can be trained on the scenario. This tutorial consists of 2 parts: building the environment and training a model on the environment.

{.example-item}
:Example: [Data Logging](notebooks/example_data_logging/notebook.ipynb)
:Tags: [data logging]{.example-tag}
:Folder: example_data_logging
:Description: This notebook explores **kdflex**'s built in data logging capabilities using a 2-link pendulum example multibody. We start with generic non-simulation data logging to H5 files using {py:class}`Karana.KUtils.H5Writer`. Then we will demonstrate automatically logging sim data using the {py:class}`Karana.Models.DataLogger`. This requires going through the typical process of setting up a multibody and running the simulation.

{.example-item}
:Example: [Flexible Double-Wishbone](notebooks/example_flexible_double_wishbone/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [double wishbone]{.example-tag} [constraints]{.example-tag} [cutjoint]{.example-tag} [flexible]{.example-tag} [TA model]{.example-tag}
:Folder: example_flexible_double_wishbone
:Description: This example builds a flexible body double-wishbone example. This example and makes one of its links flexible, adds a spring-damper, and runs a full simulation of the double-wishbone system. This is an example of a multibody that has flexible bodies and constraints.
   _

{.example-item}
:Example: [Visualization](notebooks/example_visualization/notebook.ipynb)
:Tags: [visualization]{.example-tag} [3D graphics]{.example-tag}
:Folder: example_visualization
:Description: In this tutorial we will:
   - Create a two-link pendulum multibody
   - Attach a primitive geometry to a body
   - Attach a 3D mesh geometry to a body
   - Setup web-based 3d visualization
   - Render to a file

   Prerequisites:
   - [2-link Pendulum](../example_2_link_pendulum/notebook.ipynb)

{.example-item}
:Example: [Importing a robot arm multibody model from a URDF file](notebooks/example_urdf/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [urdf]{.example-tag} [import]{.example-tag} [robot]{.example-tag} [WAM]{.example-tag}
:Folder: example_urdf
:Narration: [Video](https://portal.karanadyn.com/tutorial_videos/example_urdf.mp4)
:Description: The purpose of this notebook is to
   - demonstrate the creation of a {py:class}`Karana.Dynamics.Multibody` instance for a robot arm from a URDF model file 
   - show the structure of the loaded model
   - show a 3D visualization of the robot arm
   - serve as a `hello world` script for verifying a proper `kdFlex` installation

   Resources
   - [Other notebook examples](https://portal.karanadyn.com/docs/examples_page.html) 
   - [kdFlex Documentation](https://portal.karanadyn.com/docs/index.html)

   **Acknowledgements**
   The Barrett WAM urdf is sourced from the public repository [barrett-ros2-pkg](https://git.barrett.com/software/barrett-ros2-pkg.git). Thank you to the authors and contributors.

{.example-item}
:Example: [Parallel vehicle propagation](notebooks/example_parallel_vehicles/notebook.ipynb)
:Tags: [time domain]{.example-tag} [parallelization]{.example-tag}
:Folder: example_parallel_vehicles
:Description: This notebook runs two independent vehicle {py:class}`SubTrees <Karana.Dynamics.SubTree>` in parallel via {py:class}`Karana.Models.ParallelVehicles`. {py:class}`ParallelVehicles <Karana.Models.ParallelVehicles>` registers on a global {{ spcls }} and advances a list of vehicle {{ spcls }} instances during the global propagator's hop.

   This example uses small serial-chain vehicles so the setup is easy to see. Each vehicle has its own {py:class}`SubTree <Karana.Dynamics.SubTree>`, its own {{ spcls }}, its own integrator, and a recurring pre-hop event that sleeps for 0.1 seconds. The sleep events make the wall-clock difference between parallel and serial execution visible.

{.example-item}
:Example: [Basic frames usage](notebooks/example_frames/notebook.ipynb)
:Tags: [frames]{.example-tag}
:Folder: example_frames
:Description: Illustrates frames layer usage

{.example-item}
:Example: [Procedural n-link pendulum collision example](notebooks/example_procedural_collision/notebook.ipynb)
:Tags: [pendulum]{.example-tag} [contact]{.example-tag} [time domain]{.example-tag}
:Folder: example_procedural_collision
:Description: This notebook is a more advanced version of the n-link pendulum and 2-link pendulum collision tutorials.In this notebook a pendulum is created using the procedural approach to allow for an n number of linked bodies to be built at once. Collision detection is added in this notebook and visualized using material colors.

   Requirements:
   - [n-link Pendulum](../example_n_link_pendulum/notebook.ipynb)
   - [2-link Pendulum Collision](../example_pendulum_collision/notebook.ipynb)


   In this example we will:
    - Create the n-link pendulum multibody
    - Create the **kdFlex** Scene
    - Setup geometries
    - Set up the state propagator and models
    - Set initial state
    - Register a timed event
    - Run the simulation
    - Clean up the simulation

   ![](../resources/nb_images/procedural_pendulum_collision.PNG)

   For a more in-depth descriptions of **kdflex** concepts see [usage](usage_page).

{.example-item}
:Example: [Basic math usage](notebooks/example_math/notebook.ipynb)
:Tags: [math]{.example-tag} [beginner]{.example-tag}
:Folder: example_math
:Description: This notebook demonstrates how to work with basic math objects using **kdFlex**

{.example-item}
:Example: [ATRVjr Reinforcement Learning](notebooks/example_atrvjr_learning/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [ground vehicle]{.example-tag} [skid steered]{.example-tag} [machine learning]{.example-tag} [ATRV Jr]{.example-tag}
:Folder: example_atrvjr_learning
:Description: This tutorial  trains a model to drive an ATRVjr vehicle to a target. This tutorial consists of 2 parts: building the environment and training a model on the environment.

{.example-item}
:Example: [Serial chain](notebooks/example_serial_chain/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [pendulum]{.example-tag} [gui]{.example-tag} [beginner]{.example-tag}
:Folder: example_serial_chain
:Description: This is an introductory example of creating a serial chain system along with starting up the GUI for introspection.

{.example-item}
:Example: [Double-Wishbone](notebooks/example_double_wishbone/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [double wishbone]{.example-tag} [constraints]{.example-tag} [cutjoint]{.example-tag} [TA model]{.example-tag}
:Folder: example_double_wishbone
:Description: This notebook walks through loading a DARTS Model file of a Double Wishbone multibody. Additionally, it demonstrates how to enforce loop constraints in the graph multibody.

{.example-item}
:Example: [4-bar system simulation](notebooks/example_4_bar/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [cutjoint]{.example-tag} [constraints]{.example-tag} [TA model]{.example-tag} [beginner]{.example-tag} [time domain]{.example-tag} [planar 4-bar]{.example-tag}
:Folder: example_4_bar
:Description: This notebook builds upon the system-level simulation showcased in the [system-level notebook](../example_system_level/notebook.ipynb). If you are not already familiar with the concepts described in that notebook, please go through it before this one.

{.example-item}
:Example: [Import/Export Multibodies](notebooks/example_import_export/notebook.ipynb)
:Tags: [import]{.example-tag} [pendulum]{.example-tag} [time domain]{.example-tag}
:Folder: example_import_export
:Description: Illustrates importing and exporting multibody models files in various formats; also, the use of units and quantities.

{.example-item}
:Example: [Load Mars 2020 rover model from a URDF file](notebooks/example_m2020/notebook.ipynb)
:Tags: [import]{.example-tag} [urdf]{.example-tag} [ground vehicle]{.example-tag}
:Folder: example_m2020
:Description: Illustrates importing a Mars 2020 rover model from a URDF file

{.example-item}
:Example: [Cut-joint loop constraint](notebooks/example_cut_joint_constraint/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [constraints]{.example-tag} [cutjoint]{.example-tag} [TA model]{.example-tag} [time domain]{.example-tag}
:Folder: example_cut_joint_constraint
:Description: This notebook illustrates the use of a cu-joint loop constraint.

{.example-item}
:Example: [Basic 2-link Pendulum](notebooks/example_2_link_pendulum/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [time domain]{.example-tag} [pendulum]{.example-tag}
:Folder: example_2_link_pendulum
:Description: This notebook walks you through every step to manually create a simple two-link pendulum multibody simulation. While the majority of the **kdFlex** software is in C++, Python bindings for the C++ classes are available as Python extensions. This example illustrates the use of the **kdFlex** Python extensions.

{.example-item}
:Example: [$n$-link Pendulum Benchmark Comparison](notebooks/example_rigid_timing/notebook.ipynb)
:Tags: [pendulum]{.example-tag} [time domain]{.example-tag}
:Folder: example_rigid_timing
:Description: In this notebook we benchmark the n-link pendulum example. This notebook is similar to the $n$-link pendulum but is designed for benchmarking **kdFlex**'s simulation performance and demonstrating how **kdFlex**'s multibody simulation performance scales.

   Requirements:
   - [n-link Pendulum](../example_n_link_pendulum/notebook.ipynb)

   In this tutorial we will:
   - Define a method to create the multibody
   - Run the simulation with increasing number of links
   - Benchmark simulation runtimes

   ![](../resources/nb_images/results_plot_SOA.png)


    For a more in-depth descriptions of **kdflex** concepts see [usage](usage_page).

{.example-item}
:Example: [Beginner example of creating a system-level simulation](notebooks/example_system_level/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [pendulum]{.example-tag} [beginner]{.example-tag} [KModels]{.example-tag} [time domain]{.example-tag}
:Folder: example_system_level
:Description: An introductory example of setting up a time-domain simulation of a serial chain system along with a gravity `KModel` and starting up the GUI for introspection.

   This notebook builds upon the time domain simulation showcased in the [time domain notebook](../example_time_domain/notebook.ipynb). If you are not already familiar with the concepts described in that notebook, please go through it before this one.

{.example-item}
:Example: [Robotics Algorithms](notebooks/example_robotics_algorithms/notebook.ipynb)
:Tags: [import]{.example-tag} [math]{.example-tag} [robot]{.example-tag}
:Folder: example_robotics_algorithms
:Description: The purpose of this notebook is to demonstrate how **kdflex**'s various algorithms can be used. This notebook uses the Barret WAM arm to showcase these algorithms.

   Resources
   - [Other notebook examples](https://portal.karanadyn.com/docs/examples_page.html) 
   - [kdFlex Documentation](https://portal.karanadyn.com/docs/index.html)

   **Acknowledgements**
   The Barret WAM urdf is sourced from the public repository [barrett-ros2-pkg](https://git.barrett.com/software/barrett-ros2-pkg.git). Thank you to the authors and contributors.

{.example-item}
:Example: [n-link Pendulum](notebooks/example_n_link_pendulum/notebook.ipynb)
:Tags: [pendulum]{.example-tag} [dynamics]{.example-tag} [time domain]{.example-tag}
:Folder: example_n_link_pendulum
:Description: This notebook walks through the process required to create an n-link pendulum multibody simulation in **kdflex**. This problem is an extension to the 2-link pendulum by creating the pendulum using a procedural approach to allow for an n number of linked bodies to be built at once. This method uses the {py:meth}`Karana.Dynamics.PhysicalBody.addSerialChain` method which can be useful for automating the creation of large multibody systems.

   Requirements:
   - [2-link Pendulum](../example_2_link_pendulum/notebook.ipynb)

   In this tutorial we will:
   - Create the multibody
   - Setup the **kdFlex** scene
   - Add a root visual geometry
   - Set up the state propagator and models
   - Set initial state
   - Register a timed event
   - Run the simulation
   - Clean up the simulation

   ![](../resources/nb_images/n_link_pendulum_flipped.PNG)

   For a more in-depth descriptions of **kdflex** concepts see [usage](usage_page).

{.example-item}
:Example: [Fully Augmented $n$-link  Pendulum Benchmark Comparison](notebooks/example_fully_augmented_benchmark/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [time domain]{.example-tag} [pendulum]{.example-tag} [TA model]{.example-tag}
:Folder: example_fully_augmented_benchmark
:Description: In this notebook, we use the $n$-link pendulum example to compare the performance of **kdFlex's** default SOA-base $O(N)$ dynamics algorithm with the conventional $O(N^{3}$) methods for a *fully augmented (FA)* model.

{.example-item}
:Example: [Spice Frames](notebooks/example_spice_frames/notebook.ipynb)
:Tags: [frames]{.example-tag} [spice]{.example-tag} [ephemerides]{.example-tag}
:Folder: example_spice_frames
:Description: This notebook walks the user through the process of creating Spice Frames. The {py:class}`Karana.Frame.SpiceFrame` is a special type of {py:class}`Karana.Frame.Frame` that is associated with planetary bodies for ephemerides. 

   Requirements:
   - [2-link Pendulum](../example_2_link_pendulum/notebook.ipynb)

   In this tutorial we will:
   - Create SpiceFrames for planetary bodies
   - Create SpiceFrameToFrames to tie together ephemerides
   - Setup the **kdFlex** scene
   - Attach ProxySceneNodes to each planet
   - Query ephemeris data for bodies
   - Run and Clean up the Simulation

{.example-item}
:Example: [Slider crank example](notebooks/example_slider_crank/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [slider crank]{.example-tag} [constraints]{.example-tag} [cutjoint]{.example-tag}
:Folder: example_slider_crank
:Description: This notebook is an example of creating a simple slider crank with loop constraints and graph topology. This demonstrates how to do the following:

   In this tutorial we will:
   - Create a Multibody using DataStruct.DataStruct 
   - Use ConstraintKinematicsSolver solve a system
   - Setup the simulation
   - Run the simulation with constraints

   Prerequisites:
   - [Import/export](../example_import_export/notebook.ipynb)

{.example-item}
:Example: [ATRVjr Driving](notebooks/example_atrvjr_drive/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [ground vehicle]{.example-tag} [skid steered]{.example-tag} [ATRV Jr]{.example-tag}
:Folder: example_atrvjr_drive
:Description: In this notebook we import the ATRVjr rover model into **kdflex** and drive it around using a simple manual applied torque approach.

{.example-item}
:Example: [2-link Pendulum Collision Example](notebooks/example_pendulum_collision/notebook.ipynb)
:Tags: [pendulum]{.example-tag} [contact]{.example-tag} [time domain]{.example-tag}
:Folder: example_pendulum_collision
:Description: This notebook is a more advanced version of the 2-link pendulum example by adding a collision dynamics. This tutorial notebook walks you through the steps to create a 2-link pendulum manually and then adds collision dynamics in addition to extra visualization geometries.

   Requirements:
   - [2-link Pendulum](../example_2_link_pendulum/notebook.ipynb)

   In this example we will:
    - Create the multibody
    - Setup the **kdFlex** Scene
    - Setup geometries
    - Set up the state propagator and models
    - Set initial state
    - Register a timed event
    - Run the simulation
    - Clean up the simulation

   ![](../resources/nb_images/pendulum_collision.PNG)


   For a more in-depth descriptions of **kdflex** concepts see [usage](usage_page).

{.example-item}
:Example: [ATRVjr ROS Integration](notebooks/example_atrvjr_ros/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [ground vehicle]{.example-tag} [skid steered]{.example-tag} [ATRV Jr]{.example-tag} [ROS]{.example-tag}
:Folder: example_atrvjr_ros
:Description: In this notebook we import the ATRVjr rover model into **kdflex** and interface with ROS. We will publish messages to ROS as the sim advances as well as listen for a ROS message to command the vehicle's driving mode.

{.example-item}
:Example: [Closed-loop assignment](notebooks/example_loop_assignment/notebook.ipynb)
:Tags: [dynamics]{.example-tag} [time domain]{.example-tag} [pendulum]{.example-tag} [constraints]{.example-tag} [cutjoint]{.example-tag}
:Folder: example_loop_assignment
:Description: This notebook servers as a simple assignment (with answers) to help you understand how to build a closed-loop kdFlex simulation.


[//]: # (Hidden toctree to make sphinx happy and put notebooks in latex)
```{eval-rst}
.. toctree::
   :hidden:

   notebooks/example_2_link_cartpole/notebook.ipynb
   notebooks/example_flexible_slider_crank/notebook.ipynb
   notebooks/example_cartpole/notebook.ipynb
   notebooks/example_data_logging/notebook.ipynb
   notebooks/example_flexible_double_wishbone/notebook.ipynb
   notebooks/example_visualization/notebook.ipynb
   notebooks/example_urdf/notebook.ipynb
   notebooks/example_parallel_vehicles/notebook.ipynb
   notebooks/example_frames/notebook.ipynb
   notebooks/example_procedural_collision/notebook.ipynb
   notebooks/example_math/notebook.ipynb
   notebooks/example_atrvjr_learning/notebook.ipynb
   notebooks/example_serial_chain/notebook.ipynb
   notebooks/example_double_wishbone/notebook.ipynb
   notebooks/example_4_bar/notebook.ipynb
   notebooks/example_import_export/notebook.ipynb
   notebooks/example_m2020/notebook.ipynb
   notebooks/example_cut_joint_constraint/notebook.ipynb
   notebooks/example_2_link_pendulum/notebook.ipynb
   notebooks/example_rigid_timing/notebook.ipynb
   notebooks/example_system_level/notebook.ipynb
   notebooks/example_robotics_algorithms/notebook.ipynb
   notebooks/example_n_link_pendulum/notebook.ipynb
   notebooks/example_fully_augmented_benchmark/notebook.ipynb
   notebooks/example_spice_frames/notebook.ipynb
   notebooks/example_slider_crank/notebook.ipynb
   notebooks/example_atrvjr_drive/notebook.ipynb
   notebooks/example_pendulum_collision/notebook.ipynb
   notebooks/example_atrvjr_ros/notebook.ipynb
   notebooks/example_loop_assignment/notebook.ipynb
```
