Firmata

From EIK wiki


Setting up Arduino

Make sure your user is in dialout group:

gpasswd -a $USER dialout

If necessary, log out and log in again. When you run following the 'dialout' should appear in the list:

groups

Clone Arduino library to your machine:

git clone http://github.com/firmata/arduino ~/Documents/Arduino/libraries/Firmata

Select Examples -> Firmata -> StandardFirmata from the menu and hit the Upload button.

Classic Python 3.4

Make sure you have pip:

sudo apt-get install python3-pip

Install PyMata:

git clone https://github.com/MrYsLab/PyMata
cd PyMata
sudo python3 setup.py install

Code example for test.py:

import signal
import sys
from PyMata.pymata import PyMata

def signal_handler(sig, frame):
    if board is not None: board.reset()
    sys.exit(0)
    
signal.signal(signal.SIGINT, signal_handler)

board = PyMata()
board.set_pin_mode(5, board.PWM, board.DIGITAL)

while True:
    for i in range(0, 255):
        board.analog_write(5, i)
    for i in range(255, 0, -1):
        board.analog_write(5, i)

Run the example:

python3 test.py


Asynchronous Python 3.5

For Ubuntu 14.04 first add Python 3.5 repository:

 sudo add-apt-repository ppa:fkrull/deadsnakes
 sudo apt-get update
 sudo apt-get install python3.5 python3-serial

Ubuntu 15.10 already has Python 3.5 available, simply install it:

 sudo apt-get install python3.5 python3-serial

Ubuntu 16.04 will ship Python 3.5 by default just add serial module:

 sudo apt-get install python3-serial

Install PyMata for Python 3.5:

 git clone https://github.com/MrYsLab/pymata-aio
 cd pymata-aio
 sudo python3.5 setup.py install

Create test.py with following code:

from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants

# instantiate the pymata_core API
board = PyMata3()

# set the pin mode
board.set_pin_mode(13, Constants.PWM)

for j in range(0, 10):
    for i in range(0, 255):
        board.analog_write(13, i)
    for i in range(255, 0, -1):
        board.analog_write(13, i)

# reset the board and exit
board.shutdown()

Plug the Arduino to your machine via USB cable and run the code:

python3.5 test.py


Driving triple omniwheel robot

import signal
import sys
from PyMata.pymata import PyMata

# Pin mapping
MOTOR_1_PWM = 2
MOTOR_1_A   = 3
MOTOR_1_B   = 4
MOTOR_2_PWM = 5
MOTOR_2_A   = 6
MOTOR_2_B   = 7
MOTOR_3_PWM = 8
MOTOR_3_A   = 9
MOTOR_3_B   = 10

 
def signal_handler(sig, frame):
    if board is not None: board.reset()
    sys.exit(0)
 
signal.signal(signal.SIGINT, signal_handler)
 
board = PyMata()
board.set_pin_mode(MOTOR_1_PWM, board.PWM,    board.DIGITAL)
board.set_pin_mode(MOTOR_1_A,   board.OUTPUT, board.DIGITAL)
board.set_pin_mode(MOTOR_1_B,   board.OUTPUT, board.DIGITAL)
board.digital_write(MOTOR_1_A, 1) # This motor goes forward
board.digital_write(MOTOR_1_B, 0)

board.set_pin_mode(MOTOR_2_PWM, board.PWM,    board.DIGITAL)
board.set_pin_mode(MOTOR_2_A,   board.OUTPUT, board.DIGITAL)
board.set_pin_mode(MOTOR_2_B,   board.OUTPUT, board.DIGITAL)
board.digital_write(MOTOR_2_A, 1) # This motor also goes forward
board.digital_write(MOTOR_2_B, 0)

board.set_pin_mode(MOTOR_3_PWM, board.PWM,    board.DIGITAL)
board.set_pin_mode(MOTOR_3_A,   board.OUTPUT, board.DIGITAL)
board.set_pin_mode(MOTOR_3_B,   board.OUTPUT, board.DIGITAL)
board.digital_write(MOTOR_3_A, 0) # This motor goes in reverse
board.digital_write(MOTOR_3_B, 1)

while True:
    for i in range(0, 250):
        board.analog_write(MOTOR_1_PWM, i)
        board.analog_write(MOTOR_2_PWM, i)
        board.analog_write(MOTOR_3_PWM, i)
    for i in range(250, 0, -1):
        board.analog_write(MOTOR_1_PWM, i)
        board.analog_write(MOTOR_2_PWM, i)
        board.analog_write(MOTOR_3_PWM, i)