Skip to content

chaffybird56/MuscleMate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฆพ MuscleMate โ€” Myoelectric Robotic Sterilization Helper

Myoelectric signals from your forearm drive a robot arm that shuttles surgical instruments into an autoclave with deterministic, safe motion primitives.

Kapture.2025-09-29.at.12.32.14.mp4

๐ŸŒŸ Highlights

  • Modular Python package (musclemate/) with clean separation of hardware adapters, gesture decoding, control logic, and simulated EMG feeds.
  • Command line interface (control.cli) for live runs or scripted demosโ€”no need to touch application code.
  • Configuration via dataclasses with sensible defaults for thresholds, waypoints, speeds, and loop timing.
  • Test suite (tests/) covering gesture decoding logic and scripted EMG playback.
  • Packaging metadata (pyproject.toml) for pip install . deployment.

๐Ÿ—‚๏ธ Project Overview

โ”œโ”€โ”€ control/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py                # CLI entry point
โ”‚   โ”œโ”€โ”€ config.py             # Dataclass configs and timing
โ”‚   โ”œโ”€โ”€ gesture.py            # Debounce + hysteresis + intent decoding
โ”‚   โ”œโ”€โ”€ runner.py             # Main loop orchestrator
โ”‚   โ”œโ”€โ”€ state_machine.py      # Finite-state workflow for sterilization
โ”‚   โ””โ”€โ”€ utils.py              # Shared helpers (clamp, etc.)
โ”œโ”€โ”€ hardware/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ arm.py                # Arm interface + QArm stub
โ”‚   โ””โ”€โ”€ emg.py                # EMG reader protocol + static source
โ”œโ”€โ”€ sim/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ emg_profiles.py       # Scripted EMG scenarios for demos and tests
โ”œโ”€โ”€ tests/                    # Pytest-based unit tests
โ””โ”€โ”€ pyproject.toml            # Packaging metadata

โšก Quick Overview

  1. Flex a muscle โ†’ two EMG channels capture the activity.
  2. Signal cleaning yields a smooth strength value (the envelope).
  3. Dual thresholds with hysteresis decide on/off to avoid chatter.
  4. Debounce, cooldown, and long-press logic produce stable intents.
  5. The robot executes a safe sequence: select bin โ†’ approach โ†’ grip โ†’ lift โ†’ autoclave โ†’ place โ†’ close โ†’ home.

Thatโ€™s the loop: muscle โ†’ intent โ†’ motion until the instrument is safely sterilized.


๐Ÿš€ Quick Start

1. Setup

git clone https://github.com/chaffybird56/MuscleMate.git
cd MuscleMate
python -m venv .venv && source .venv/bin/activate  # optional but recommended
pip install .

2. Run the scripted demo

python -m control.cli --demo
# or simply: musclemate --demo   # after installing the console script
  • Uses a deterministic EMG script to exercise the full sterilization cycle.
  • Logs intents/state transitions while the stub arm tracks pose updates.

3. Connect your hardware

Replace QArmStub and StaticEMGSource with your Quanser QArm / biosignal bindings by subclassing the ArmInterface and EMGReader protocols in hardware.


โš™๏ธ Configuration Cheatsheet

Parameter Location Purpose
Thresholds control.config EMG on/off thresholds, debounce, cooldown, long-press abort
Waypoints control.config Bin/autoclave/home coordinates
Speeds control.config Motion profile speeds & gripper dwell
Sampling control.config Runtime and loop rate defaults

Override values at runtime with CLI flags, e.g.:

python -m control.cli --emg-on 0.7 --cooldown 0.4 --loop-rate 60

๐Ÿง  From Muscle to Motion

  1. Acquire EMG from two channels โ†’ normalize to [0, 1].
  2. Deadband + hysteresis debounce the raw signal into stable "pressed" states.
  3. Gesture mapping converts channel patterns into Intent enums (start, grip, toggle door, abort).
  4. Finite-state machine (State) ensures safe, sequential motion primitives:
IDLE โ†’ SELECT_BIN โ†’ APPROACH โ†’ GRIP โ†’ LIFT โ†’ TRANSIT
     โ†’ OPEN_AUTOCLAVE โ†’ PLACE โ†’ CLOSE_AUTOCLAVE โ†’ HOME
  1. Safety: Long-press on channel 2 issues an ABORT, homing the arm immediately.

๐Ÿงฎ Signal Processing & Decision Flow

Let $x_i[n]$ be raw EMG on channel $i \in {1,2}$ sampled at $f_s$ Hz.

1) Create the envelope

Rectify and low-pass filter to obtain a smooth envelope $u_i[n]$:

$$ u_i[n] = (1-\alpha)u_i[n-1] + \alpha |x_i[n]|,\qquad \alpha = 1 - e^{-2\pi f_c/f_s}. $$

  • $f_c$: 3โ€“6โ€ฏHz cutoff keeps the envelope slow and stable.
  • Intuition: a moving average of the absolute EMG suppresses spikes.

2) Normalize (z-score)

From a rest segment, compute baseline $\mu_i, \sigma_i$ and normalize:

$$ z_i[n] = \frac{u_i[n] - \mu_i}{\sigma_i}. $$

  • Different electrodes, different baselinesโ€”z-scores compare apples to apples.

3) Apply hysteresis

Use on/off thresholds $(\theta_{\text{on}}, \theta_{\text{off}})$ with $\theta_{\text{on}} > \theta_{\text{off}}$:

$$ c_i[n] = \begin{cases} 1, & z_i[n] \ge \theta_{\text{on}} \text{ or } (c_i[n-1] = 1 \text{ and } z_i[n] > \theta_{\text{off}}),\\ 0, & \text{otherwise.} \end{cases} $$

  • Once โ€œon,โ€ the signal must fall below $\theta_{\text{off}}$ to turn offโ€”no flicker at the boundary.

4) Debounce and cooldown

  • Debounce: require $c_i[n] = 1$ for $T_d$ seconds โ†’ $N_d = \lceil T_d / (1/f_s) \rceil$ consecutive samples.
  • Cooldown: ignore new gestures for $T_c$ seconds after accepting an intent.

5) Long-press abort

If channel 2 stays active for $T_\ell \approx 1.2$โ€ฏs, issue an ABORT and send the arm home.


๐Ÿ”ฌ Typical Values & Calibration

Symbol Meaning Typical
$f_s$ EMG sampling rate 200โ€“1000โ€ฏHz
$f_c$ Envelope cutoff 3โ€“6โ€ฏHz
$\theta_{\text{on}}$ On threshold (z-score) 0.65โ€“0.9
$\theta_{\text{off}}$ Off threshold (z-score) 0.30โ€“0.5
$T_d$ Debounce time 0.10โ€“0.20โ€ฏs
$T_c$ Cooldown 0.30โ€“0.40โ€ฏs
$T_\ell$ Long-press (abort) โ‰ˆ 1.2โ€ฏs

Calibration tip: Take a rest recording, set $\theta_{\text{on}}$ near the 95th percentile + margin, and $\theta_{\text{off}}$ at roughly half that. Adjust until confident contractions trigger reliably without false positives.


๐Ÿงช Testing

pip install pytest
pytest

Unit tests validate gesture decoding (debounce, cooldown, long-press) and deterministic EMG playback. Extend with hardware-in-the-loop tests as you integrate real sensors.

Hardware-in-the-loop (HIL) smoke test

  • Connect your EMG acquisition and arm bindings.
  • Run python -m control.cli --loop-rate 60 --runtime 30 to exercise the loop at control-rate.
  • Monitor logs for state transitions; abort safely with a long press.
  • Add assertions around ControllerEvent streams to automate pass/fail checks.

๐Ÿงช What You Should See

  • Reliable pick/place of mock instruments into the autoclave (sim + hardware).
  • Long-press abort always returns to home.
  • Stable control thanks to the envelope, hysteresis, and debounce stages.

Limitations. EMG varies day-to-day; revisit thresholds periodically. The autoclave door dwell is simulated in the stub implementation. Expanding the gesture set will require additional channels or a classifier.


๐Ÿ› ๏ธ Extending MuscleMate

  • Add new intents by expanding Intent and updating the transition logic in state_machine.py.
  • Integrate real EMG hardware by implementing EMGReader.
  • Plug in motion planning or collision checking in SterilizationController.move_to.
  • Log or stream ControllerEvent instances via the optional sink in run_controller.

๐Ÿ”ง Customization Checklist

  • Extend the Intent enum and FSM transitions for new gestures.
  • Wire up your EMG backend through EMGReader.
  • Enhance SterilizationController.move_to with planners or collision avoidance.
  • Attach a telemetry sink to run_controller for logging or streaming events.

๐Ÿ“š Glossary

  • EMG โ€” Electromyography; electrical activity from muscles.
  • Envelope โ€” Smoothed strength signal after rectification + low-pass filtering.
  • Hysteresis โ€” Using two thresholds to avoid flicker around the setpoint.
  • Debounce โ€” Minimum press duration to confirm state changes.
  • Cooldown โ€” Timeout to prevent rapid re-triggering.
  • Waypoint โ€” Predefined robot pose used to build motion sequences.

๐Ÿ“œ License

MIT โ€” see LICENSE.

About

Muscle-powered robotic arm for surgical sterilization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages