(examples_page)=
# Examples

 While the majority of the **kdFlex** software is written in
`C++`, Python bindings for the `C++` classes are available as Python
extensions. 
Example Python scripts, named `notebook.py`,  are available within each of the
subfolders within `/usr/share/Karana/Tutorials` in your local
installation (`/opt/homebrew/share/Karana/Tutorials` if using macOS). These scripts illustrate the use of the **kdFlex** Python extension and can be run from within each of these folders by
running `python3 notebook.py`. 

There are also `Jupyter notebook` versions of each of the tutorials, named `notebook.ipynb` within the same folder. The [introduction video](https://portal.karanadyn.com/tutorial_videos/intro_video.mp4) shows how to run these notebooks from a remote machine, from a local machine, and as a normal Python script.

(notebooks_page)=
## Python Tutorial Notebooks

This section contains tutorial examples on using **kdFlex**. The Python
examples are included here, since the associated Jupyter notebooks can be converted to HTML. One can find these 
notebooks tutorials under `/usr/share/Karana/Tutorials` (`/opt/homebrew/share/Karana/Tutorials` if using macOS).
The C++ examples cannot be easily converted to a web format, but are included with the kdflex package
as well under `/usr/share/Karana/Tutorials/example_cc` (or `/opt/homebrew/share/Karana/Tutorials/example_cc`) if 
using macOS.

Jupyter notebook | Folder | Description/License tier | Video walkthrough
------- | ----------- | ----------- | -----------
[Importing a rover arm from a URDF file](generated/notebooks/example_urdf/notebook.ipynb)  | example_urdf  | Illustrates importing a  Barrett WAM robot arm multibody model from a URDF file| [Video Walkthrough](https://portal.karanadyn.com/tutorial_videos/example_urdf.mp4)
[Importing M2020 rover model from a URDF file](generated/notebooks/example_m2020/notebook.ipynb)  | example_m2020  | Illustrates importing a  Mars 2020 rover model from a URDF file | 
[2 link pendulum (basic)](generated/notebooks/example_2_link_pendulum/notebook.ipynb) | example_2_link_pendulum |   Illustrates the step by step creation of the bodies for a 2-link   pendulum, and setting up a time domain simulation for it.| [Video Walkthrough](https://portal.karanadyn.com/tutorial_videos/2_link.mp4)
[n link pendulum (procedural)](generated/notebooks/example_n_link_pendulum/notebook.ipynb) | example_n_link_pendulum | Illustrates   the procedural creation of an n-link pendulum and a time domain  simulation for it.| [Video Walkthrough](https://portal.karanadyn.com/tutorial_videos/n_link.mp4)
[Spice frames (basic)](generated/notebooks/example_spice_frames/notebook.ipynb) | example_spice_frames | Illustrates use of the frames layer with planetary body frames| 
[Import/export multibodies](generated/notebooks/example_import_export/notebook.ipynb)  | example_import_export | Illustrates importing and exporting multibody models files in various formats; also, the use of units and quantities.| 
[Visualization](generated/notebooks/example_visualization/notebook.ipynb) | example_visualization |  Illustrates  the capabilities of the 3D graphics layer.| 
[Data logging](generated/notebooks/example_data_logging/notebook.ipynb) | example_data_logging | Illustrates basic data logging capabilities.| 
[Slider-crank with loop constraints](generated/notebooks/example_slider_crank/notebook.ipynb) | example_slider_crank |  Illustrates creating a slide-crank system with loop constraints and graph topology.| 
[Double wishbone](generated/notebooks/example_double_wishbone/notebook.ipynb) | example_double_wishbone | Illustrates creating a double-wishbone system with loop constraints and graph topology.| 
[2 link pendulum (collisions)](generated/notebooks/example_pendulum_collision/notebook.ipynb) | example_pendulum_collision | Illustrates collision dynamics with a 2-link pendulum example.| 
[Multi-link procedural pendulum (collisions)](generated/notebooks/example_procedural_collision/notebook.ipynb) | example_procedural_collision |  Illustrates collision dynamics with a multi-link pendulum example.| 
[Flexible slider-crank](generated/notebooks/example_flexible_slider_crank/notebook.ipynb) | example_flexible_slider_crank | Illustrates a slider-crank with a flexible body. This is a system that has both flexible bodies and closed loops.| 
[Flexible double-wishbone](generated/notebooks/example_flexible_double_wishbone/notebook.ipynb) | example_flexible_double_wishbone | Illustrates a double-wishbone with a flexible body. This is a system that has both flexible bodies and closed loops.| 
[Rigid body SOA dynamics performance](generated/notebooks/example_rigid_timing/notebook.ipynb) |example_rigid_timing |  Verify O(N)   performance of SOA dynamics by measuring computational time with varying number of bodies. | 
[Rigid body SOA dynamics vs fully augmented performance](generated/notebooks/example_fully_augmented_benchmark/notebook.ipynb) |example_fully_augmented_benchmark |  Compare O(N)   performance of SOA dynamics with O(N^3) performance of conventional full-augmented (FA) dynamics. | 
[Skid-steered driving](generated/notebooks/example_atrvjr_drive/notebook.ipynb) | example_atrvjr_drive | Illustrates setting up and driving the ATRV-Jr, a simple skid-steered four wheel vehicle | 
[Cart-pole reinforcement learning](generated/notebooks/example_cartpole/notebook.ipynb) | example_cartpole | Integrates {{kdflex}} with [gymnasium](https://gymnasium.farama.org/) to train on the classical cart-pole problem | 
[Skid-steered reinforcement learning](generated/notebooks/example_atrvjr_learning/notebook.ipynb) | example_atrvjr_learning | Integrates {{kdflex}} with [gymnasium](https://gymnasium.farama.org/) to drive the ATRV-Jr (a skid-steered four wheel vehicle) to random goals | 
[ROS2 integration](generated/notebooks/example_atrvjr_ros/notebook.ipynb) | example_atrvjr_ros | Integrates {{kdflex}} with [ROS2](https://docs.ros.org/en/rolling/index.html) to monitor and command a vehicle | 

<!--
Disabled TODO notebooks

**Jacobians and Inverse kinematics** (coming soon) |  - |  Illustrates creation of inertial and relative Jacobians for single and multiple nodes; general constraint and inverse kinematics| 
**Zero-crossing detection:** (coming soon) | - | Illustrates zero-crossing detection with a pendulum collision problem| 
**Flexible body dynamics:** (coming soon) | - | Illustrates creation of a 2-link flexible body pendulum | 
**Algorithms showcase:** (coming soon) | - | Illustrates various system level algorithmic methods for the mass matrix, system inertias etc| 
-->

Each notebook has a corresponding `notebook.py`, which is a regular `Python` script. 

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

   generated/notebooks/example_urdf/notebook.ipynb
   generated/notebooks/example_2_link_pendulum/notebook.ipynb
   generated/notebooks/example_m2020/notebook.ipynb
   generated/notebooks/example_n_link_pendulum/notebook.ipynb
   generated/notebooks/example_spice_frames/notebook.ipynb
   generated/notebooks/example_import_export/notebook.ipynb
   generated/notebooks/example_visualization/notebook.ipynb
   generated/notebooks/example_data_logging/notebook.ipynb
   generated/notebooks/example_slider_crank/notebook.ipynb
   generated/notebooks/example_double_wishbone/notebook.ipynb
   generated/notebooks/example_pendulum_collision/notebook.ipynb
   generated/notebooks/example_procedural_collision/notebook.ipynb
   generated/notebooks/example_flexible_slider_crank/notebook.ipynb
   generated/notebooks/example_flexible_double_wishbone/notebook.ipynb
   generated/notebooks/example_rigid_timing/notebook.ipynb
   generated/notebooks/example_fully_augmented_benchmark/notebook.ipynb
   generated/notebooks/example_atrvjr_drive/notebook.ipynb
   generated/notebooks/example_cartpole/notebook.ipynb
   generated/notebooks/example_atrvjr_learning/notebook.ipynb
   generated/notebooks/example_atrvjr_ros/notebook.ipynb
```
