pymodi 1.2.0
Python API for controlling modular electronics, MODI.
License: MIT
Keywords: python,modi

<p align="center">
	<img src="https://github.com/LUXROBO/pymodi/blob/master/docs/_static/img/logo.png?raw=true" width="500" height="150">
</p>

--------
<div align="center">

[![Python Versions](https://img.shields.io/pypi/pyversions/pymodi.svg?style=flat-square)](https://pypi.python.org/pypi/pymodi)
[![PyPI Release (latest by date)](https://img.shields.io/github/v/release/LUXROBO/pymodi?style=flat-square)](https://pypi.python.org/pypi/pymodi)
[![Read the Docs (version)](https://img.shields.io/readthedocs/pymodi/master?style=flat-square)](https://pymodi.readthedocs.io/en/master/?badge=master)
[![GitHub Workflow Status (Build)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Build%20Status/master?style=flat-square)](https://github.com/LUXROBO/pymodi/actions)
[![Coveralls Coverage](https://img.shields.io/coveralls/github/LUXROBO/pymodi/master?style=flat-square)](https://coveralls.io/github/LUXROBO/pymodi)
[![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/LUXROBO/pymodi/master?style=flat-square)](https://www.codefactor.io/repository/github/luxrobo/pymodi/overview/master)
[![GitHub LICENSE](https://img.shields.io/github/license/LUXROBO/pymodi?style=flat-square&color=blue)](https://github.com/LUXROBO/pymodi/blob/master/LICENSE)
[![Lines of Code](https://img.shields.io/tokei/lines/github/LUXROBO/pymodi?style=flat-square)](https://github.com/LUXROBO/pymodi/tree/master/modi)

</div>

Description
===========
> Python API for controlling modular electronics, MODI.

Promotion Video
---------------
[![PyMODI Intro Video](https://github.com/LUXROBO/pymodi/blob/master/docs/_static/video/pymodi-intro.gif)](https://www.youtube.com/watch?v=7ciGw8V-8sM)

Features
--------
PyMODI provides a control of modular electronics, [MODI](https://modi.luxrobo.com/en)
* Platform agnostic control of modules through serial connection
* Explicit CAN communication to MODI modules via magnetic connector
* Utilities of wireless connection with BLE (Bluetooth Low Engery)
* Update of MODI firmware consisting of both ESP32 and STM32 modules
* Direct manipulation of MODI network module using [MODI Play](https://play.google.com/store/apps/details?id=com.luxrobo.modiplay&hl=en_US)

Build Status
------------
|master|develop|
|:---:|:---:|
| [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Build%20Status?label=master&branch=master&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) | [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Build%20Status?label=develop&branch=develop&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions)

System Support
--------------
| System | 3.6 | 3.7 | 3.8 |
| :---: | :---: | :---: | :---: |
| Linux | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Ubuntu)/master?label=Unit%20Test%20%28Ubuntu%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Ubuntu)/master?label=Unit%20Test%20%28Ubuntu%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) |[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Ubuntu)/master?label=Unit%20Test%20%28Ubuntu%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions)
| Mac OS | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(macOS)/master?label=Unit%20Test%20%28macOS%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) |[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(macOS)/master?label=Unit%20Test%20%28macOS%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(macOS)/master?label=Unit%20Test%20%28macOS%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions)
| Windows | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Windows)/master?label=Unit%20Test%20%28Windows%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Windows)/master?label=Unit%20Test%20%28Windows%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions) | [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/LUXROBO/pymodi/Unit%20Test%20(Windows)/master?label=Unit%20Test%20%28Windows%29&logo=github&style=flat-square)](https://github.com/LUXROBO/pymodi/actions)

Contribution Guidelines
-----------------------
We appreciate all contributions. If you are planning to report bugs, please do so [here](https://github.com/LUXROBO/pymodi/issues). Feel free to fork our repository to your local environment, and please send us feedback by filing an issue.

If you want to contribute to pymodi, be sure to review the contribution guidelines. This project adheres to pymodi's code of conduct. By participating, you are expected to uphold this code.

[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=flat-square)](CODE_OF_CONDUCT.md)

Installation
------------
> When installing PyMODI package, we highly recommend you to use Anaconda to manage the distribution.
> With Anaconda, you can use an isolated virtual environment, solely for PyMODI.

[Optional] Once you install [Anaconda](https://docs.anaconda.com/anaconda/install/), then:
```commandline
# Install new python environment for PyMODI package, choose python version >= 3.6
conda create --name pymodi python=3.6

# After you properly install the python environment, activate it
conda activate pymodi

# Ensure that your python version is compatible with PyMODI
python --version
```

Install the latest PyMODI if you haven't installed it yet:
```commandline
python -m pip install pymodi --user --upgrade
```

You can also install PyMODI at develop branch (containing latest changes but it can be unstable) with:
```commandline
python -m pip install git+https://github.com/LUXROBO/pymodi.git@develop --user --upgrade
```
Alternatively, you can install a stable copy of PyMODI at a tag (representing a previous release) with:
```commandline
python -m pip install git+https://github.com/LUXROBO/pymodi.git@v1.0.0 --user --upgrade
```

Usage
-----
Import modi package and create MODI object (we call it "bundle", a bundle of MODI modules).
```python
# Import modi package
import modi

"""
Create MODI object, make sure that you have connected your network module
to your machine while other modules are attached to the network module
"""
bundle = modi.MODI()
```

[Optional] Specify how you would like to establish the connection between your machine and the network module.
```python
# 1. Serial connection (via USB), it's the default connection method
bundle = modi.MODI(conn_type="ser")

# 2. CAN connection (via CAN shield, not recommended to use it directly)
bundle = modi.MODI(conn_type="can")

# 3. BLE (Bluetooth Low Energy) connection, it's wireless! But it can be slow :(
bundle = modi.MODI(conn_type="ble", network_uuid="YOUR_NETWORK_MODULE_UUID")
```

List and create connected modules' object.
```python
# List connected modules
print(bundle.modules)

# List connected leds
print(bundle.leds)

# Pick the first led object from the bundle
led = bundle.leds[0]
```

Visualize how modules are connected.
```python
# Print topology map without indicating module id
bundle.print_topology_map()

# Print topology map with module id printed
bundle.print_topology_map(print_id=True)
```

Let's blink the LED 5 times.
```python
import time

for _ in range(5):
    # turn on for 0.5 second
    led.turn_on()
    time.sleep(0.5)

    # turn off for 0.5 second
    led.turn_off()
    time.sleep(0.5)
```

If you are still not sure how to use PyMODI, you can play PyMODI tutorial over REPL:
```commandline
$ python -m modi --tutorial
```
As well as an interactive usage examples:
```commandline
$ python -m modi --usage
```

Moreover, we provide [api documentation](https://pymodi.readthedocs.io/en/master/), [usage and creation examples](examples), and a [descriptive web page](https://luxrobo.github.io/pymodi).

Additional Usage
----------------
To update MODI network module (the network module must be connected on serial),
```commandline
$ python -m modi --update_network
```

To update MODI network module's base (as above, the network module must be connected on serial),
```commandline
$ python -m modi --update_network_base
```

To update MODI modules (all modules but the network module),
```commandline
$ python -m modi --update_modules
```

To diagnose MODI modules (helpful to find existing malfunctioning modules),
```commandline
$ python -m modi --inspect
```

To debug MODI modules with PyMODI debugger,
```commandline
$ python -m modi --debug
```

To check the performance of PyMODI on your machine,
```commandline
$ python -m modi --performance
```

To initialize MODI modules implicitly (set `i` flag to enable REPL mode),
```commandline
$ python -im modi --initialize
```

To see what other commands are available,
```commandline
$ python -m modi --help
```

Sponsored by
------------
[![Ministry of SMEs and StartUps](https://github.com/LUXROBO/pymodi/blob/master/docs/_static/img/ministry_of_smes_and_startups.png?raw=true)](https://www.mss.go.kr/site/eng/main.do)

History
==

0.1.0 (2018-06-28)
--
1. Release initial version of the package on in-house GitLab

0.1.1 (2018-07-20)
--
1. Fix requirements
2. Fix include path

0.1.2 (2018-07-23)
--
1. Python 3.4 support

0.2.0 (2018-07-25)
--
1. Implement Speaker module
2. Implement Display module

0.2.1 (2018-07-27)
--
1. Implement speaker module's tune() and off() methods
2. Remove duplicated codes

0.3.0 (2018-07-28)
--
1. Support python 2.7
2. Speed up the performance

0.3.1 (2018-07-28)
--
1. Fix requirements

0.4.0 (2018-08-20)
--
1. Improve performance
2. Complete first documentation

0.5.0 (2018-08-22)
--
1. Change the project name to pyMODI

0.5.1 (2018-11-07)
--
1. Fix "No MODI network module connected" issue

0.5.2 (2019-10-11)
--
1. Can control a pair of motors of the motor module by torque(), speed(),
   angle()

0.6.0 (2019-10-18)
--
1. Improve performance

0.7.0 (2020-01-23)
--
1. Implement individual motor control
2. Implement set variable method in display
3. Implement test cases for each modi modules
4. Implement multiprocessing version of pymodi
5. Bug fixes

0.7.1 (2020-02-07)
--
1. Refactor file structures
2. Bug fixes

0.8.0 (2020-05-07)
--
1. Implement Can Message Handler
2. Implement Topology Manager
3. Improve connection reliability by separating communication functions
4. Bug fixes

0.9.0 (2020-07-07)
--
1. Implement Module Firmware Updater
2. Implement bluetooth connection via SPP
3. Implement MODI Priority Queue to increase response speed
4. Implement PyMODI tutor for teaching new-comers
5. Support multi MODI network modules given appropriate COM ports
6. Refactor getter/setter for each MODI module
7. Refactor motor module to use set_motor_channel internally
8. Update property values when setting properties
9. Add usage/creation example to the repository
10. Bug fixes

1.0.0 (2020-09-15)
--
1. Implement Network Firmware Updater
2. Implement BLE connection (excluding macOS support)
3. Implement MODI Play support via network module
4. Refactor module getter/setter interface
5. Enhance topology manager to work with battery module
6. Add functionality to reset existing user code on the modules
7. Bug fixes

1.0.1 (2020-10-06)
--
1. Implement experimental BLE functionality on Windows and macOS
2. Improve BLE connection stability in Linux Platform
3. Implement module usage manual (i.e. quick-start)
4. Implement module inspection functionality (i.e. diagnose)
5. Refactor GUI debugger design, fix initialization issue on macOS

1.1.0 (2020-12-17)
--
1. Implement GUI Firmware Updater in PyMODI
2. Implement network base (STM32) firmware updater
3. Implement GD32 firmware updater
4. Implement [VirtualMODI](https://github.com/luxrobo/virtual-modi) integration code

1.2.0 (2022-02-16)
--
1. Detach GUI Firmware Updater from PyMODI to individual repo
2. Implement web client method for MODI json connection
3. Package PyMODI into a dockerfile via GHCR
4. Fix minor bug in BLE connection