Skip to content

Commit

Permalink
Merge pull request #40 from zabertech/tracking-example
Browse files Browse the repository at this point in the history
Adding motion tracking example.
  • Loading branch information
martinzak-zaber authored Dec 10, 2024
2 parents 5ad21a0 + 6d2a7a3 commit 31c1af5
Show file tree
Hide file tree
Showing 10 changed files with 571 additions and 0 deletions.
162 changes: 162 additions & 0 deletions examples/motion_tracking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm-project.org/#use-with-ide
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
62 changes: 62 additions & 0 deletions examples/motion_tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Tracking Motion Using PID Controller

This example demonstrates how Zaber stages can track analog input signals using a PID controller.
A PID controller is a control loop feedback mechanism widely used in industrial control systems.
It calculates the error between a desired setpoint and a measured process variable and applies a correction based on proportional, integral, and derivative terms.
In Zaber's case, the controller can move the stage to track the setpoint of the analog input signal.
Zaber only supports the PI controller, which is a simplified version of the PID controller without derivative term.

The following video demonstrates the concept:

![video.mp4](img/video.mp4)

In the video above there is a load cell mounted on a Zaber stage.
The stage is tracking the analog input signal from the load cell keeping a constant tension on the spring.
When disrupted, the stage moves to compensate for the change in the force until it reaches the setpoint again.

## Code

### Dependencies

The script uses `pdm` to manage the virtual environment and dependencies:

Instructions on how to install it can be found on the official `pdm` project page [here](https://github.com/pdm-project/pdm).

The dependencies are listed in `pyproject.toml`.

### Running the Script

Make sure to change `SERIAL_PORT` constant in `main.py` file to the serial port that your device is connected to.

Afterwards, you can run the example:

```shell
cd examples/motion_tracking/
pdm install
pdm run example
```

### Explanation

<https://github.com/zabertech/zaber-examples/blob/main/examples/motion_tracking/src/motion_tracking/main.py#L17-L32>

After establishing a connection with the device, the script prints the PI controller parameters.

Then based on the user input "track" the scripts starts the PI controller by issuing the `move track` command to the axis.
After that, the stage will track the analog input signal indefinitely until the user inputs "stop".

## Tuning the Controller

Properly tuning the controller requires a basic understanding of control theory and is beyond the scope of this example.
Typically, you adjust the Kp and Ki parameters to achieve the desired response.
You may also need to change the direction (setting `motion.tracking.dir`) if the input signal is inverted.
In this example, the Kp = 0, Ki = 1000, and the direction is set to 1.
We recommend using Oscilloscope application in Zaber Launcher to visualize the input signal and the stage position.

## Additional Information

There is also a `move track once` variant of the `move track` command that reaches the setpoint once and then stops.
For this variant a settings `motion.tracking.settle.tolerance` and `motion.tracking.settle.period` determines
when the stage is considered to have settled at the setpoint.

You can find the exhaustive list of commands and settings in the [ASCII Protocol Manual](https://www.zaber.com/protocol-manual?protocol=ASCII).
9 changes: 9 additions & 0 deletions examples/motion_tracking/article.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
authors:
- Martin Zak
date: 2024-12-05
category: Motion
tags:
- PID
- Tracking
- Python
picture: img/chart.png
Binary file added examples/motion_tracking/img/chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/motion_tracking/img/video.mp4
Binary file not shown.
Loading

0 comments on commit 31c1af5

Please sign in to comment.