Created
June 12, 2015 15:46
-
-
Save cfobel/9b175c0f813c1be5190f to your computer and use it in GitHub Desktop.
Interactive Firmata motor control notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:8e253071404ae11008b94449a1ef11f4af455b2221f1e2d2d4c4cb2f5e4ef148" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from threading import Thread\n", | |
"import time\n", | |
"import pyfirmata\n", | |
"from IPython.html.widgets import interactive\n", | |
"\n", | |
"\n", | |
"g_on_period = 1\n", | |
"g_off_period = 1\n", | |
"g_active = False\n", | |
"\n", | |
"\n", | |
"def motor_control():\n", | |
" global g_on_period\n", | |
" global g_off_period\n", | |
" global g_active\n", | |
" \n", | |
" g_on_period = 1\n", | |
" g_off_period = 1\n", | |
" g_active = False\n", | |
" \n", | |
" board = pyfirmata.Board('/dev/ttyACM0')\n", | |
" control_pin = board.digital[13]\n", | |
" control_pin.mode = pyfirmata.OUTPUT\n", | |
" \n", | |
" while True:\n", | |
" if g_active:\n", | |
" control_pin.write(1)\n", | |
" time.sleep(g_on_period)\n", | |
" control_pin.write(0)\n", | |
" time.sleep(g_off_period)\n", | |
" else:\n", | |
" control_pin.write(0)\n", | |
" time.sleep(.1)\n", | |
" \n", | |
" \n", | |
"def set_motor_parameters(on_period=1., off_period=1., active=False):\n", | |
" global g_on_period\n", | |
" global g_off_period\n", | |
" global g_active\n", | |
" \n", | |
" print on_period, off_period, active\n", | |
" g_on_period = on_period\n", | |
" g_off_period = off_period\n", | |
" g_active = active" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"control_thread = Thread(target=motor_control)\n", | |
"control_thread.daemon = True\n", | |
"control_thread.start()\n", | |
"\n", | |
"interactive(set_motor_parameters,\n", | |
" on_period=(0, 10, .01),\n", | |
" off_period=(0, 10, .01))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1.0 1.0 False\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll need to install
pyfirmata
before running the notebook.