Skip to main content

Arduino-style motor and servo control using Firmata — a combined MegaWrapper library

Project description

MegaWrapper

A combined DC-motor + servo control library for Arduino via Firmata.

from megawrapper import Board

board = Board("/dev/ttyUSB0")
motor = board.attach_motor(2, 4, 11)
motor.forward(100)
board.close()

Installation

pip install -e /path/to/MegaWrapper

Requires Python 3.8+. Primary dependency: pyfirmata2. Optional: pyserial (for the alternative Firmata backend), adafruit-circuitpython-vl53l0x (for the ToF sensor).

Documentation

Full API reference, wiring diagrams, and usage guides are on the project wiki.

Examples

Ready-to-run scripts are in examples/:

File What it shows
single_motor.py Basic motor forward/stop
dual_motor.py Two motors with names
tank_drive.py Differential steering pattern
keyboard_control.py Interactive CLI motor control
endless_sweep.py Servo sweep via sweep()
move_smooth.py Servo motion with move_smooth()
pyserial_control.py Using the pyserial Firmata backend
vl53l0x_sensor.py Single VL53L0X distance sensor readout
vl53l0x_multi_sensor.py Multiple VL53L0X sensors via auto_address

Run any example with python examples/<file>.py.

API Overview

Board

Board(port, stby=None)      # Connect to Arduino (stby = optional STBY pin)
Board.get_active_board()    # Get the most recently created Board
Board.has_active_board()    # True if any Board is active
board.attach_motor(d1, d2, pwm, name=None)
board.stop_all()
board.wake() / board.sleep()   # Requires stby pin
board.close()                  # Stop motors + release board

Motor

motor.forward(speed)    # 0–100
motor.backward(speed)   # 0–100
motor.stop()            # Coast stop
motor.brake()           # Active brake
motor.set_speed(n)      # Change speed without changing direction
motor.speed             # Current speed (0–100)
motor.direction         # "forward" | "backward" | "stopped" | "braked"

Servo

servo = Servo()
servo.attach(pin)               # Must create a Board first
servo.write(angle)              # 0–180 (clamped)
servo.read()                    # Returns last commanded angle
servo.move_smooth(target, delay_ms=15)
servo.sweep(start=0, end=180, step=1, delay_ms=15)
servo.detach()

VL53L0X Time-of-Flight Sensor

from megawrapper.sensor import VL53L0X

# Single sensor (default I²C address 0x29)
sensor = VL53L0X()
distance = sensor.distance               # cm

# Multiple sensors with auto-address
sensors = VL53L0X.auto_address([17, 27, 22])  # GPIO pins for XSHUT
for s in sensors:
    print(s.distance)
  • VL53L0X(i2c=None, address=0x29) — initialise at given I²C address
  • sensor.distance — returns distance in centimetres (range mm / 10)
  • VL53L0X.auto_address(xshut_pins, i2c=None) — sequences XSHUT pins to assign unique addresses 0x30, 0x31, … (up to 16 sensors)
  • Independent of Board/Firmata — runs on the host's I²C bus (Raspberry Pi GPIO). Requires the [sensor] extra: pip install 'megawrapper[sensor]'

Utilities

delay(ms)      # Blocking sleep in milliseconds
millis()       # Monotonic millisecond counter

Backend Modes

set_mode("pyserial")            # Use built-in pyserial Firmata client
set_mode("pyfirmata2")          # Default — use pyfirmata2 library
get_mode()                      # Current mode

Exceptions

MegaWrapperError, InvalidSpeedError, BoardConnectionError, StandbyNotConfiguredError

Testing

python -m pytest tests/ -v

All tests mock the Arduino — no hardware required. 92 tests, ~1.5 s.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

megawrapper-0.2.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

megawrapper-0.2.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file megawrapper-0.2.1.tar.gz.

File metadata

  • Download URL: megawrapper-0.2.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for megawrapper-0.2.1.tar.gz
Algorithm Hash digest
SHA256 07e35139a2f0f886c90374e324a1c1c21beeb6ddccbc01e169f5595921fe92bb
MD5 79b5732d162c7c3643ebfabadac58c09
BLAKE2b-256 63d9fb990a34c090ace29ddbf8da38498a686def1793ad2b587a93ebbe4e8a55

See more details on using hashes here.

Provenance

The following attestation bundles were made for megawrapper-0.2.1.tar.gz:

Publisher: publish.yml on vihaanvp/MegaWrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file megawrapper-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: megawrapper-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for megawrapper-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9a15bcc5ee3f0801e47181b4b792733a22d1a49d4cf5db9b13afc0c5555a9258
MD5 46a10b7772ed351df3fed85b9834e8c4
BLAKE2b-256 7611c11c19c4d811e2ba686d44c221127cf83459be429c71185d68c1a101e636

See more details on using hashes here.

Provenance

The following attestation bundles were made for megawrapper-0.2.1-py3-none-any.whl:

Publisher: publish.yml on vihaanvp/MegaWrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page