Skip to content

ryanrahman27/GripGuard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyML Slip Detection Pipeline

A complete TinyML training and testing pipeline for slip detection using piezoelectric sensor data. The model uses a Decision Tree classifier optimized to run on Arduino Uno.

Features

  • Decision Tree Model: Lightweight and interpretable model perfect for embedded systems
  • Arduino Uno Compatible: Generated C++ code runs directly on Arduino Uno (no ML framework needed)
  • Complete Pipeline: Training, testing, and deployment code included
  • Memory Efficient: Optimized for Arduino Uno's 2KB SRAM and 32KB Flash constraints

Project Structure

.
├── train_model.py              # Main training script
├── test_model.py               # Model testing script
├── generate_sample_data.py     # Generate synthetic training data
├── arduino_slip_detection.ino  # Arduino inference code
├── arduino_model.h             # Generated Arduino model (created after training)
├── requirements.txt            # Python dependencies
└── README.md                   # This file

Installation

  1. Install Python dependencies:
pip install -r requirements.txt
  1. Install Arduino IDE (if deploying to hardware):

Usage

1. Generate Sample Data

If you don't have training data yet, generate synthetic data:

python generate_sample_data.py --n-samples 1000 --output data/training_data.csv

Note: Replace with your actual piezoelectric sensor data. Expected CSV format:

  • Columns: feature_0, feature_1, ..., feature_N, label
  • label: 0 = no slip, 1 = slip detected

2. Train the Model

Train the decision tree model:

python train_model.py --data data/training_data.csv --max-depth 5

Parameters:

  • --data: Path to training data CSV
  • --max-depth: Maximum tree depth (lower = smaller model, default: 5)
  • --min-samples-split: Minimum samples to split node (default: 10)
  • --min-samples-leaf: Minimum samples in leaf (default: 5)
  • --output-model: Output path for saved model (default: model.pkl)
  • --output-arduino: Output path for Arduino header (default: arduino_model.h)

The script will:

  • Train the model
  • Evaluate performance
  • Save the model (model.pkl)
  • Generate Arduino C++ code (arduino_model.h)

3. Test the Model

Test the trained model:

python test_model.py --model model.pkl --data data/test_data.csv

4. Deploy to Arduino Uno

  1. Copy generated model:

    • Copy arduino_model.h to your Arduino sketch folder
  2. Open Arduino IDE:

    • Open arduino_slip_detection.ino
  3. Hardware Setup:

    • Connect piezoelectric sensor to analog pin A0
    • Optional: Connect LED to pin 13 for visual slip indication
  4. Upload:

    • Select board: Tools → Board → Arduino Uno
    • Select port: Tools → Port → [Your Arduino Port]
    • Click Upload
  5. Monitor:

    • Open Serial Monitor (9600 baud)
    • View real-time predictions

Model Architecture

The decision tree model uses the following features (extracted from sensor data):

Spectral Features:

  • Feature 0: Peak amplitude
  • Feature 1: RMS value
  • Feature 2: Frequency domain energy
  • Feature 3: Signal variance
  • Feature 4: Zero crossing rate

Temporal Features:

  • Feature 5: Signal range (max - min)
  • Feature 6: Mean Absolute Value (MAV)
  • Feature 7: Short-window slope / envelope change

Arduino Code Features

  • Real-time feature extraction from sensor readings
  • Efficient decision tree traversal (no dynamic memory allocation)
  • Serial output for debugging
  • LED indicator for slip detection

Memory Optimization

The generated Arduino code is optimized for Arduino Uno:

  • Static arrays (no dynamic allocation)
  • Fixed-size buffers
  • Minimal floating-point operations
  • Compact tree representation

Customization

Adjusting Model Size

For smaller models (more memory constrained):

python train_model.py --max-depth 3 --min-samples-split 20 --min-samples-leaf 10

For more accurate models (if memory allows):

python train_model.py --max-depth 7 --min-samples-split 5 --min-samples-leaf 2

Feature Extraction

Modify extractFeatures() in arduino_slip_detection.ino to match your sensor characteristics and feature extraction method.

Sensor Calibration

Adjust normalization constants in extractFeatures() based on your piezoelectric sensor's output range.

Troubleshooting

Model too large for Arduino Uno:

  • Reduce --max-depth parameter
  • Increase --min-samples-split and --min-samples-leaf
  • Use fewer features

Poor accuracy:

  • Collect more training data
  • Ensure features are well-separated between slip/no-slip classes
  • Try different tree depths
  • Check feature normalization

Arduino compilation errors:

  • Ensure arduino_model.h is in the same folder as .ino file
  • Check that model was generated successfully
  • Verify Arduino IDE version compatibility

License

MIT License - feel free to use and modify for your projects.

Contributing

Contributions welcome! Please feel free to submit issues or pull requests.

About

A smart shoe attachment that helps prevent slips on icy surfaces. Using a piezoelectric sensor and TinyML, it detects unstable foot contact in real time and deploys a stabilizing claw to increase traction and help the user regain balance.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors