Skip to content

Instantly share code, notes, and snippets.

@jhconning
Last active December 24, 2023 03:01
Show Gist options
  • Save jhconning/a6f292d2fa1618c1544d439f259fe671 to your computer and use it in GitHub Desktop.
Save jhconning/a6f292d2fa1618c1544d439f259fe671 to your computer and use it in GitHub Desktop.
Run Jupyter notebook to control ev3 using ev3dev via rpyc server
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Control EV3 via rpyc\n\nOn the ev3 (atfter installing rpyc) type:\n\n rpyc_classic.py\n \nYou should see something like \n\n INFO:SLAVE/18812:server started on [0.0.0.0]:18812\n\nNow we are ready to write code that mostly executes here (except for the parts over a connection to the ev3)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Documentation and notes\n\n[python-ev3dev](http://python-ev3dev.readthedocs.io/en/stable/) Read the docs\n\n[Installing rpyc on ev3](http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/stable/rpyc.html). \n\nIf we've created the `rpyc_server.sh` executable on ev3 then we can start things directly from Brickman on the brick."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The following code isn't that relevant for ipython scripts but could be used at the top of any.py scripts stored on the brickman to make them run either on brickman or remotely."
},
{
"metadata": {},
"cell_type": "raw",
"source": "import socket\nhostname = socket.gethostname()\n\nif hostname == 'ev3dev':\n # We are on the EV3, import required modules directly\n import ev3dev.ev3 as ev3\n\nelse:\n # We are somewhere else. Assume we are using RPyC:\n import rpyc\n conn = rpyc.classic.connect('ev3dev') # host name or IP address of the EV3\n ev3 = conn.modules['ev3dev.ev3']"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Start Here\n\nOnce the rpyc server is running on the EV3 we can start running things below:"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "import rpyc\nconn = rpyc.classic.connect('ev3dev') # host name or IP address of the EV3\nev3 = conn.modules['ev3dev.ev3']",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "m = ev3.LargeMotor('outA')\nm.run_timed(time_sp=1000, speed_sp=600)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "m.run_timed(time_sp=1000, speed_sp=600)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "m.commands",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "['run-forever', 'run-to-abs-pos', 'run-to-rel-pos', 'run-timed', 'run-direct', 'stop', 'reset']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ev3.Sound.speak('Welcome to the E V 3 dev project!').wait()",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "0"
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python [default]",
"language": "python"
},
"hide_input": false,
"language_info": {
"name": "python",
"version": "3.6.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Run Jupyter notebook to control ev3 using ev3dev via rpyc server",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment