Skip to content

Instantly share code, notes, and snippets.

@JohnGriffiths
Last active August 29, 2015 14:09
Show Gist options
  • Save JohnGriffiths/bba47112025ec91f2201 to your computer and use it in GitHub Desktop.
Save JohnGriffiths/bba47112025ec91f2201 to your computer and use it in GitHub Desktop.
Get your bot on code
(gist naming file; ignore)
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"css": [
""
],
"name": "",
"signature": "sha256:799225d5b83c773de13af5daca51a595862551b7789d598fe587e6f84c56c3ab"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"A subsumption architecture for behaviour-based control\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"####Table of Contents\n",
"* [A subsumption architecture for behaviour-based control\n",
"](#A-subsumption-architecture-for-behaviour-based-control)\n",
"\t* [Class](#Class)\n",
"\t* [Initialize](#Initialize)\n",
"\t* [Clear](#Clear)\n",
"\t* [Cruise forward ](#Cruise-forward)\n",
"\t* [Spin](#Spin)\n",
"\t* [Zigzag](#Zigzag)\n"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Imports"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
"IPython.load_extensions('calico-spell-check', 'calico-document-tools', 'calico-cell-tools');"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"javascript": [
"IPython.load_extensions('calico-spell-check', 'calico-document-tools', 'calico-cell-tools');"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0xa807f4c>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the class that controls the robot"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"\n",
"import os\n",
"\n",
"from IPython.lib import backgroundjobs as bg\n",
"from IPython import display\n",
"\n",
"from copy import deepcopy\n",
"import datetime\n",
"import sys\n",
"import pandas as pd\n",
"\n",
"import seaborn as sns\n",
"\n",
"from matplotlib import pyplot as plt\n",
"%matplotlib inline"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Robot brain"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Class"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class robotbrain(object):\n",
" \n",
" def __init__(self, is_sim = True, username = 'pi', server = '10.100.102.4'):\n",
" \n",
" #behaviours_list = None, behaviours_dict=None):\n",
" \n",
" #self.jobs = bg.BackgroundJobManager()\n",
" #self.behaviours_dict = behaviours_dict # not using?\n",
" #self.behaviours_list = behaviours_list\n",
"\n",
" self.username = username\n",
" self.server = server\n",
" \n",
" # pins \n",
" self.left_forward_pin = 11\n",
" self.right_forward_pin= 15\n",
" self.left_back_pin = 13\n",
" self.right_back_pin = 7\n",
" \n",
" self.ssh_cmd = ' ssh %s@%s ' %(self.username,self.server)\n",
" self.on_cmd = ' sudo gpio -1 write %s 1 ' \n",
" self.off_cmd = ' sudo gpio -1 write %s 0 '\n",
" \n",
" self.clear_cmd = 'STILL NEED TO DO THIS.' #(alternative = turn pins off)\n",
"\n",
" self.is_sim = is_sim # if simulation, the ssh commands are note executed\n",
" \n",
" \n",
" # set pins to be outputs\n",
" print '\\nsetting pins as outputs:' \n",
" _cmds = ['%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.left_forward_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.right_forward_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.left_back_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.right_back_pin)]\n",
" for c in _cmds: \n",
" print '\\ncommand: %s' %c\n",
" if not self.is_sim: os.system(c)\n",
" \n",
" \n",
" self.mode = 'play'\n",
" self.mood = 'blah??'\n",
" \n",
" \n",
" \n",
" # Actions / states: \n",
" \n",
" # Level 2: Motor sequences\n",
" \n",
" \n",
" # Primitives:\n",
" # =========== \n",
" \n",
" def left_forward(self, onoff):\n",
" if onoff is 'on': _cmd = self.ssh_cmd + self.on_cmd %self.left_forward_pin\n",
" elif onoff is 'off': _cmd = self.ssh_cmd + self.off_cmd %self.left_forward_pin\n",
" print '\\n left forward is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
" \n",
" def right_forward(self, onoff): \n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.right_forward_pin)\n",
" elif onoff is 'off':_cmd = self.ssh_cmd + (self.off_cmd %self.right_forward_pin)\n",
" print '\\n right forward is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
"\n",
" def left_back(self, onoff):\n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.left_back_pin)\n",
" elif onoff is 'off':_cmd = self.ssh_cmd + (self.off_cmd %self.left_back_pin)\n",
" print '\\n left back is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
" \n",
" def right_back(self, onoff): \n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.right_back_pin)\n",
" elif onoff is 'off': _cmd = self.ssh_cmd + (self.off_cmd %self.right_back_pin)\n",
" print '\\n right back is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
"\n",
" def clear_actions(self):\n",
" \n",
" _cmds = [ self.ssh_cmd + self.off_cmd %self.left_forward_pin,\n",
" self.ssh_cmd + self.off_cmd %self.right_forward_pin,\n",
" self.ssh_cmd + self.off_cmd %self.left_back_pin,\n",
" self.ssh_cmd + self.off_cmd %self.right_back_pin ]\n",
"\n",
" print '\\n clearing action pins\\n commands: \\n'\n",
" for c in _cmds: \n",
" print '\\n %s ' %c\n",
" if not self.is_sim: os.system(c)\n",
" \n",
"\n",
" # Basic actions\n",
" # ==============\n",
" def go_forward(self):\n",
" self.left_forward('on')\n",
" self.right_forward('on')\n",
" \n",
" def go_back(self):\n",
" self.left_back('on')\n",
" self.right_back('on')\n",
" \n",
" def spin_left(self):\n",
" self.right_forward('on')\n",
" self.left_back('on')\n",
" \n",
" def spin_right(self):\n",
" self.left_forward('on')\n",
" self.right_back('on')\n",
"\n",
" \n",
" \n",
" # Patterns (basic actions, in combinatoins, with a duration)\n",
" # =========\n",
"\n",
" def pause(self, duration):\n",
" time.sleep(duration)\n",
" \n",
" \n",
" def turn_90deg(self, direction, dur90deg = 1.2): \n",
" # dur is the time it takes to spin 90degress\n",
" # may depend on speed. need to find it out. \n",
" \n",
" print 'turning %s 90 degrees. Assuming that %s is correct duration' %(direction, dur90deg)\n",
" if direction is 'left': self.spin_left()\n",
" elif direction is 'right': self.spin_right()\n",
" time.sleep(dur90deg)\n",
" self.clear_actions()\n",
" \n",
" def cruise(self, direction, duration):\n",
"\n",
" print 'cruising for %s ms ' %duration\n",
" if direction is 'forward': self.go_forward()\n",
" elif direction is 'back': self.go_back()\n",
" time.sleep(duration)\n",
" self.clear_actions()\n",
" \n",
"\n",
" def spin(self, direction, duration):\n",
" \n",
" print 'spinning for %s ms ' %duration\n",
" if direction is 'left': self.spin_left()\n",
" elif direction is 'right': self.spin_right()\n",
" time.sleep(duration)\n",
" self.clear_actions()\n",
" \n",
" \n",
" # Combo patterns (don't need to clear actions for these)\n",
" \n",
" \n",
" def zigzag(self, direction, duration, nzags=8):\n",
" \n",
" print 'zigzagging %s, %s times, for %s ms ' %(direction,nzags,duration)\n",
" dur_frac = np.floor(duration/nzags)\n",
" \n",
" if direction is 'left': \n",
" first = 'left'; second = 'right';\n",
" elif direction is 'right':\n",
" first = 'right'; second = 'left'; \n",
" nz = 0\n",
" while nz < nzags:\n",
" self.turn_90deg(first)\n",
" self.cruise('forward', dur_frac); \n",
" self.turn_90deg(second); \n",
" self.cruise('forward', dur_frac)\n",
" nz+=1\n",
"\n",
" \n",
" def back_and_forth(self, duration, ntimes):\n",
" \n",
" \n",
" print 'going back and forth %s times, for %s ms' %(ntimes, duration)\n",
" dur_frac = np.floor(duration/ntimes)\n",
" n = 0\n",
" while n<ntimes:\n",
" self.cruise('forward', dur_frac/2)\n",
" self.cruise('back', dur_frac/2)\n",
" n+=1\n",
" \n",
" \n",
" def arc(self, direction):\n",
" # an arc is a zigzag with 2 zags and long durations\n",
" self.zigzag(direction, duration, nzags=2)\n",
" \n",
" \n",
" def check_fuckup(selff, behav, params):\n",
" # check whether a behaviour wtill bre problematic: \n",
" # if go forward, is there something in the way\n",
" # ... ?\n",
" return False\n",
"\n",
"\n",
" \n",
" def select_play_behav(self):\n",
" # check mood...\n",
" options = [{'behav': self.zigzag, 'params': {'nzags': [10,20,30], 'duration': [1,2,3]} },\n",
" {'behav': self.zigzag, 'params': {'nzags': [10,20,30], 'duration': [1,2,3]} } ]\n",
" \n",
" behav_choice_ind = np.random.randn(0, len(behav_options.keys()) ) # this needs to be an integer\n",
" blah = options.items()[behav_choice_ind]\n",
" behav = blah['behav']\n",
" \n",
" params = {}\n",
" for k,v in blah['params'].items():\n",
" choice_ind = np.random.randn(0,len(v))\n",
" params[k] = v[choice_ind]\n",
" \n",
" return behav, params \n",
" \n",
" def choose_behav(self):\n",
" \n",
" if self.mode is 'play': \n",
" behav,params = self.select_play_behav() # (also return args?)\n",
" if self.check_fuckup(behav,params) is False:\n",
" behav(**params)\n",
" \n",
" elif self.mode is 'follow':\n",
" behav = self.select_follow_behav() \n",
" \n",
" \n",
" "
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Initialize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize brain"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R = robotbrain(is_sim=False)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"setting pins as outputs:\n",
"\n",
"command: ssh pi@10.100.102.4 sudo gpio -1 mode 11 out\n",
"\n",
"command: ssh pi@10.100.102.4 sudo gpio -1 mode 15 out"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"command: ssh pi@10.100.102.4 sudo gpio -1 mode 13 out"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"command: ssh pi@10.100.102.4 sudo gpio -1 mode 7 out"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Clear"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.clear_actions()\n"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" clearing action pins\n",
" commands: \n",
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Primitives"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Cruise forward "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cruise forward for 2 seconds"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.cruise('forward', 2)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cruis backward for 2 seconds"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.cruise('back', 2)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cruising for 2 ms \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 \n",
"\n",
" right back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 7 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Spin"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Spin in circles"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.spin('left', 10)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"spinning for 10 ms \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.cruise('forward', 5)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cruising for 5 ms \n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.left_forward('on')"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.left_forward('off')"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" left forward is off; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n"
]
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.right_forward('on')"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Zigzag"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.zigzag('left', 10, 8)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"zigzagging left, 8 times, for 10 ms \n",
"turning left 90 degrees. Assuming that 5 is correct duration\n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning right 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 7 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning left 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning right 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 7 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning left 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning right 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 7 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"turning left 90 degrees. Assuming that 5 is correct duration"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"cruising for 1.0 ms "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 "
]
}
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.spin_right()"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" left forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 11 1 \n",
"\n",
" right back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 7 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.turn_90deg('left', 10)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"turning left 90 degrees. Assuming that 10 is correct duration\n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"R.spin('left', 20)"
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"spinning for 20 ms \n",
"\n",
" right forward is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 15 1 \n",
"\n",
" left back is on; \n",
"command: ssh pi@10.100.102.4 sudo gpio -1 write 13 1 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" clearing action pins\n",
" commands: \n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 11 0 \n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 15 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 13 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
" ssh pi@10.100.102.4 sudo gpio -1 write 7 0 "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Graveyard"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%writefile robotbrain.py\n",
"\n",
"import time\n",
"\n",
"import os\n",
"\n",
"from IPython.lib import backgroundjobs as bg\n",
"from IPython import display\n",
"\n",
"from copy import deepcopy\n",
"import datetime\n",
"import sys\n",
"\n",
"class robotbrain(object):\n",
" \n",
" def __init__(self, is_sim = True, username = 'pi', server = '10.100.102.4'):\n",
" \n",
" #behaviours_list = None, behaviours_dict=None):\n",
" \n",
" #self.jobs = bg.BackgroundJobManager()\n",
" #self.behaviours_dict = behaviours_dict # not using?\n",
" #self.behaviours_list = behaviours_list\n",
"\n",
" self.username = username\n",
" self.server = server\n",
" \n",
" # pins \n",
" self.left_forward_pin = 11\n",
" self.right_forward_pin= 15\n",
" self.left_back_pin = 13\n",
" self.right_back_pin = 7\n",
" \n",
" self.ssh_cmd = ' ssh %s@%s ' %(self.username,self.server)\n",
" self.on_cmd = ' sudo gpio -1 write %s 1 ' \n",
" self.off_cmd = ' sudo gpio -1 write %s 0 '\n",
" \n",
" self.clear_cmd = 'STILL NEED TO DO THIS.' #(alternative = turn pins off)\n",
"\n",
" self.is_sim = is_sim # if simulation, the ssh commands are note executed\n",
" \n",
" \n",
" # set pins to be outputs\n",
" print '\\nsetting pins as outputs:' \n",
" _cmds = ['%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.left_forward_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.right_forward_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.left_back_pin),\n",
" '%s sudo gpio -1 mode %s out' %(self.ssh_cmd,self.right_back_pin)]\n",
" for c in _cmds: \n",
" print '\\ncommand: %s' %c\n",
" if not self.is_sim: os.system(c)\n",
" \n",
" \n",
" self.mode = 'play'\n",
" self.mood = 'blah??'\n",
" \n",
" \n",
" \n",
" # Actions / states: \n",
" \n",
" # Level 2: Motor sequences\n",
" \n",
" \n",
" # Primitives:\n",
" # =========== \n",
" \n",
" def left_forward(self, onoff):\n",
" if onoff is 'on': _cmd = self.ssh_cmd + self.on_cmd %self.left_forward_pin\n",
" elif onoff is 'off': _cmd = self.ssh_cmd + self.off_cmd %self.left_forward_pin\n",
" print '\\n left forward is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
" \n",
" def right_forward(self, onoff): \n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.right_forward_pin)\n",
" elif onoff is 'off':_cmd = self.ssh_cmd + (self.off_cmd %self.right_forward_pin)\n",
" print '\\n right forward is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
"\n",
" def left_back(self, onoff):\n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.left_back_pin)\n",
" elif onoff is 'off':_cmd = self.ssh_cmd + (self.off_cmd %self.left_back_pin)\n",
" print '\\n left back is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
" \n",
" def right_back(self, onoff): \n",
" if onoff is 'on': _cmd = self.ssh_cmd + (self.on_cmd %self.right_back_pin)\n",
" elif onoff is 'off': _cmd = self.ssh_cmd + (self.off_cmd %self.right_back_pin)\n",
" print '\\n right back is %s; \\ncommand: %s' %(onoff, _cmd)\n",
" if not self.is_sim: os.system(_cmd)\n",
"\n",
" def clear_actions(self):\n",
" \n",
" _cmds = [ self.ssh_cmd + self.off_cmd %self.left_forward_pin,\n",
" self.ssh_cmd + self.off_cmd %self.right_forward_pin,\n",
" self.ssh_cmd + self.off_cmd %self.left_back_pin,\n",
" self.ssh_cmd + self.off_cmd %self.right_back_pin ]\n",
"\n",
" print '\\n clearing action pins\\n commands: \\n'\n",
" for c in _cmds: \n",
" print '\\n %s ' %c\n",
" if not self.is_sim: os.system(c)\n",
" \n",
"\n",
" # Basic actions\n",
" # ==============\n",
" def go_forward(self):\n",
" self.left_forward('on')\n",
" self.right_forward('on')\n",
" \n",
" def go_back(self):\n",
" self.left_back('on')\n",
" self.right_back('on')\n",
" \n",
" def spin_left(self):\n",
" self.right_forward('on')\n",
" self.left_back('on')\n",
" \n",
" def spin_right(self):\n",
" self.left_forward('on')\n",
" self.right_back('on')\n",
"\n",
" \n",
" \n",
" # Patterns (basic actions, in combinatoins, with a duration)\n",
" # =========\n",
"\n",
" def pause(self, duration):\n",
" time.sleep(duration)\n",
" \n",
" \n",
" def turn_90deg(self, direction, dur90deg = 5): \n",
" # dur is the time it takes to spin 90degress\n",
" # may depend on speed. need to find it out. \n",
" \n",
" print 'turning %s 90 degrees. Assuming that %s is correct duration' %(direction, dur90deg)\n",
" if direction is 'left': self.spin_left()\n",
" elif direction is 'right': self.spin_right()\n",
" time.sleep(dur90deg)\n",
" self.clear_actions()\n",
" \n",
" def cruise(self, direction, duration):\n",
"\n",
" print 'cruising for %s ms ' %duration\n",
" if direction is 'forward': self.go_forward()\n",
" elif direction is 'back': self.go_back()\n",
" time.sleep(duration)\n",
" self.clear_actions()\n",
" \n",
"\n",
" def spin(self, direction, duration):\n",
" \n",
" print 'spinning for %s ms ' %duration\n",
" if direction is 'left': self.spin_left()\n",
" elif direction is 'right': self.spin_right()\n",
" time.sleep(duration)\n",
" self.clear_actions()\n",
" \n",
" \n",
" # Combo patterns (don't need to clear actions for these)\n",
" \n",
" \n",
" def zigzag(self, direction, duration, nzags=8):\n",
" \n",
" print 'zigzagging %s, %s times, for %s ms ' %(direction,nzags,duration)\n",
" dur_frac = np.floor(duration/nzags)\n",
" \n",
" if direction is 'left': \n",
" first = 'left'; second = 'right';\n",
" elif direction is 'right':\n",
" first = 'right'; second = 'left'; \n",
" nz = 0\n",
" while nz < nzags:\n",
" self.turn_90deg(first)\n",
" self.cruise('forward', dur_frac); \n",
" self.turn_90deg(second); \n",
" self.cruise('forward', dur_frac)\n",
" nz+=1\n",
"\n",
" \n",
" def back_and_forth(self, duration, ntimes):\n",
" \n",
" \n",
" print 'going back and forth %s times, for %s ms' %(ntimes, duration)\n",
" dur_frac = np.floor(duration/ntimes)\n",
" n = 0\n",
" while n<ntimes:\n",
" self.cruise('forward', dur_frac/2)\n",
" self.cruise('back', dur_frac/2)\n",
" n+=1\n",
" \n",
" \n",
" def arc(self, direction):\n",
" # an arc is a zigzag with 2 zags and long durations\n",
" self.zigzag(direction, duration, nzags=2)\n",
" \n",
" \n",
" def check_fuckup(selff, behav, params):\n",
" # check whether a behaviour wtill bre problematic: \n",
" # if go forward, is there something in the way\n",
" # ... ?\n",
" return False\n",
"\n",
"\n",
" \n",
" def select_play_behav(self):\n",
" # check mood...\n",
" options = [{'behav': self.zigzag, 'params': {'nzags': [10,20,30], 'duration': [1,2,3]} },\n",
" {'behav': self.zigzag, 'params': {'nzags': [10,20,30], 'duration': [1,2,3]} } ]\n",
" \n",
" behav_choice_ind = np.random.randn(0, len(behav_options.keys()) ) # this needs to be an integer\n",
" blah = options.items()[behav_choice_ind]\n",
" behav = blah['behav']\n",
" \n",
" params = {}\n",
" for k,v in blah['params'].items():\n",
" choice_ind = np.random.randn(0,len(v))\n",
" params[k] = v[choice_ind]\n",
" \n",
" return behav, params \n",
" \n",
" def choose_behav(self):\n",
" \n",
" if self.mode is 'play': \n",
" behav,params = self.select_play_behav() # (also return args?)\n",
" if self.check_fuckup(behav,params) is False:\n",
" behav(**params)\n",
" \n",
" elif self.mode is 'follow':\n",
" behav = self.select_follow_behav() \n",
" \n",
" \n",
" \n",
" \n"
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Writing robotbrain.py\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \n",
" \n",
" # interact? \n",
" \n",
" \n",
" def spin_left(self):\n",
" self.changeval(self.left_forward, \n",
" \n",
" self.spinning_right=True\n",
" \n",
" def left_forward(self):\n",
" \n",
" _cmd = self.ssh_cmd + self.on_cmd %self.left_forward\n",
" \n",
" def right_forward\n",
" def left_back\n",
" def right_back\n",
" \n",
"\n",
" \n",
" \n",
" def spin_right(self): \n",
" \n",
" _cmd = self.ssh_cmd + self.on_cmd %self.left_forward\n",
" \n",
" \n",
" \n",
" ' %self.left_forward \\\\\n",
" + ' | ' + self.on_cmd %self.right_back) \n",
" print '\\nspinning right: %s' %_cmd\n",
" if not self.is_sim: os.system(_cmd)\n",
" self.spinning_right=True\n",
" \n",
" def spin_left(self): \n",
" \n",
" _cmd = self.ssh_cmd + self.on_cmd + ' %s ' %self.left_back \\\\\n",
" + ' | ' + self.on_cmd %self.right_forward)\n",
" print '\\nspinning left: %s' %_cmd\n",
" if not self.is_sim: os.system(_cmd)\n",
" self.spinning_left=True\n",
" \n",
" def go_forward(self):\n",
" \n",
" _cmd = self.ssh_cmd + self.on_cmd + ' %s ' %self.left_forward \\\\\n",
" + ' | ' + self.on_cmd %self.right_forward)\n",
" print '\\ngoing forward: %s' %_cmd\n",
" if not self.is_sim: os.system(_cmd)\n",
" self.going_forward=True\n",
" \n",
" \n",
" def go_back(self):\n",
" \n",
" _cmd = self.ssh_cmd + self.on_cmd + ' %s ' %self.left_back \\\\\n",
" + ' | ' + self.on_cmd %self.right_back)\n",
" print '\\ngoing back: %s' %_cmd\n",
" if not self.is_sim: os.system(_cmd)\n",
" self.going_back=True\n",
" \n",
"\n",
" # Behaviours\n",
" # ===========\n",
" \n",
" def follow(self):\n",
" \n",
" self.go_forward()\n",
" \n",
" \n",
" \n",
" def decide_from_list(self):\n",
" for b in self.behaviours_list:\n",
" exec 'self.%s()' %b\n",
"\n",
" \n",
" def decide_from_dict(self): # (deprecated?)\n",
" for b in self.behaviours_dict:\n",
" newstr = self.behaviours_dict[b].replace('.', 'self.')\n",
" exec newstr\n",
" \n",
" def start(self, reps=10, interval=1):\n",
" self.running=True\n",
" \n",
" for n in range(reps):\n",
" \n",
" time.sleep(interval)\n",
" \n",
" print '\\n\\nstep %s:\\n\\n' %n\n",
" \n",
" print '\\nmaking decisions'\n",
" self.decide()\n",
" \n",
" self.print_status()\n",
" \n",
" \n",
" def start_in_bg(self):\n",
" self.jobs.new('self.start())')\n",
" \n",
" \n",
" \n",
" def stop(self):\n",
" print 'stop the job now'\n",
" self.running=False\n",
" \n",
" def print_status(self): \n",
" \n",
" display.clear_output(wait=True)\n",
" sys.stdout.flush()\n",
" print 'be_playful: %s' %self.be_playful\n",
" print 'move_to_light %s' %self.move_to_light\n",
" print 'avoid_wall %s' %self.avoid_wall\n",
" \n",
" def check_jobs(self):\n",
" print 'check that all the background jobs are still running properly'\n",
" \n",
"\n",
" \n",
" # States:\n",
" self.nothing_in_the_way = True\n",
" \n",
" \n",
" # Level 1: Don't fuck up\n",
" def obstacle_check(self):\n",
" if self.camera_light == 'oh dear': \n",
" self.obstacle = True\n",
" else: \n",
" self.obstacle = False\n",
"\n",
" \n",
" \n",
"\n",
" \n",
" # Level 3: Mode\n",
" \n",
" # Level 4: Mood\n",
" \n",
" "
],
"language": "python",
"metadata": {
"run_control": {
"state": "d"
}
},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# behaviours from dict: \n",
"\n",
"behavs = {'1. move to light': 'if .is_light and not .is_wall: .move = True',\n",
" '2. avoid wall': 'if .is_wall: .move = False',\n",
" '3. be playful': 'if .is_light and not .is_wall: .be_playful = True'}\n",
"\n",
"thisone = arbitrator(behavs)\n",
"thisone.start()"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def run_sim_in_bg(self,sim_length=10):\n",
" self.jobs.new('self.run_sim(%s)' %(sim_length)) \n",
" \n",
" \n",
" \n",
"for n in range(reps):\n",
" time.sleep(interval)\n",
" for doplot,name in zip([plot_tavg,plot_bold],['tavg', 'bold']):\n",
" if doplot:\n",
" t,d = self.get_avg_arrs(name)\n",
" if d is not np.nan:\n",
"\n",
" df = pd.DataFrame(d,index=t)[roi_nums]\n",
" fig, ax = plt.subplots() \n",
" ax.axvline(x=t[-1], ymin=vline_lims[0],ymax=vline_lims[1],\n",
" linewidth=2, color='k', axes=ax,linestyle='dashed')\n",
" ax = df.plot(title='Sim state at %s second(s)' %(n+1), ax=ax,\n",
" **plot_params);\n",
" \n",
" display.clear_output(wait=True)\n",
" display.display(plt.gcf()) \n",
" sys.stdout.flush()\n",
" plt.close()\n",
" \n",
" \n",
" "
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class BackgroundSim(object):\n",
" \n",
" \"\"\"\n",
" Usage:\n",
" -----\n",
" \n",
" bgs = BackgrounSim()\n",
" \n",
" bgs.run_sim()\n",
" bgs.run_sim_in_bg()\n",
" bgs.make_graph()\n",
" \n",
" \"\"\"\n",
" def __init__(self, int_dt=2**-2, oscillator_params=None,sim=None):\n",
" \n",
" if not sim: \n",
" if not oscillator_params: \n",
" oscillator_params = {'a':1.05,'b':-1.0,'c':0,'d':0.1,'e':0,'f':1/3,'g':1,\n",
" 'I':0,'alpha':1,'beta':0.2,'gamma':-1,'tau':1.25} \n",
" oscillator = models.Generic2dOscillator()\n",
" white_matter = connectivity.Connectivity(load_default=True)\n",
" white_matter.speed = numpy.array([8.0])\n",
" white_matter_coupling = coupling.Linear(a=0.0152)\n",
" my_seed = 13\n",
" my_random_state = numpy.random.RandomState(my_seed)\n",
" hiss = noise.Additive(nsig=0.08)\n",
" heunint = integrators.HeunStochastic(dt=int_dt, noise=hiss)\n",
" heunint.configure()\n",
" mama = monitors.TemporalAverage(period=2 ** -1)\n",
" mimi = monitors.Bold()\n",
" what_to_watch = (mama,mimi) #(momo, mama, mimi)\n",
" sim = simulator.Simulator(model=oscillator, connectivity=white_matter,\n",
" coupling=white_matter_coupling,\n",
" integrator=heunint, monitors=what_to_watch)\n",
" sim.configure()\n",
" self.sim = sim\n",
" self.rls = sim.connectivity.region_labels \n",
" self.bold_time = [] \n",
" self.bold_data = [] \n",
" self.tavg_time = [] \n",
" self.tavg_data = [] \n",
" self.jobs = bg.BackgroundJobManager()\n",
"\n",
"\n",
" def run_sim(self,sim_length=10):\n",
" self.sim_running=True\n",
" for tavg,bold in self.sim(simulation_length=sim_length):\n",
" if bold is not None:\n",
" self.bold_time.append(bold[0])\n",
" self.bold_data.append(bold[1])\n",
" if tavg is not None:\n",
" self.tavg_time.append(tavg[0])\n",
" self.tavg_data.append(tavg[1])\n",
" self.running=False\n",
" \n",
" def run_sim_in_bg(self,sim_length=10):\n",
" self.jobs.new('self.run_sim(%s)' %(sim_length)) \n",
" \n",
" def list_arr_size(self,interval=1, reps=5):\n",
" for n in range(reps):\n",
" time.sleep(interval)\n",
" \n",
" display.clear_output(wait=True)\n",
" display.display(plt.gcf()) \n",
" sys.stdout.flush()\n",
" \n",
" def get_avg_arrs(self,varname):\n",
" if varname=='tavg':\n",
" tl = self.tavg_time\n",
" ta = np.squeeze(np.array(self.tavg_time))[:len(tl)]\n",
" da = np.squeeze(np.array(self.tavg_data))[:len(tl),:]\n",
" elif varname=='bold':\n",
" tl = self.bold_time\n",
" if len(tl)>0:\n",
" ta = np.squeeze(np.array(self.bold_time))[:len(tl)]\n",
" da = np.squeeze(np.array(self.bold_data))[:len(tl),:]\n",
" else:\n",
" ta,da = np.nan,np.nan\n",
" \n",
" return ta,da\n",
"\n",
"\n",
" def clock_time_report(self,sim_params,run_in_bg=False):\n",
" import time\n",
" start = time.time()\n",
" if run_in_bg: self.run_sim_in_bg(**sim_params)\n",
" else: self.run_sim(**sim_params)\n",
" finish = time.time()\n",
" duration = (finish-start)*1000\n",
" clock_ratio = sim_params['sim_length']/duration\n",
" return duration, clock_ratio\n",
"\n",
"\n",
" \n",
" def make_graph(self,interval=1, reps=5,xlim=None,ylim=None,make_movie_frames=False,plot_params=None,\n",
" plot_tavg=True,plot_bold=False,vline_lims = [-1,1],\n",
" roi_nums=[0,10,20,30,40,50,60]):\n",
" import time\n",
" import numpy as np\n",
" for n in range(reps):\n",
" time.sleep(interval)\n",
" for doplot,name in zip([plot_tavg,plot_bold],['tavg', 'bold']):\n",
" if doplot:\n",
" t,d = self.get_avg_arrs(name)\n",
" if d is not np.nan:\n",
"\n",
" df = pd.DataFrame(d,index=t)[roi_nums]\n",
" fig, ax = plt.subplots() \n",
" ax.axvline(x=t[-1], ymin=vline_lims[0],ymax=vline_lims[1],\n",
" linewidth=2, color='k', axes=ax,linestyle='dashed')\n",
" ax = df.plot(title='Sim state at %s second(s)' %(n+1), ax=ax,\n",
" **plot_params);\n",
" \n",
" display.clear_output(wait=True)\n",
" display.display(plt.gcf()) \n",
" sys.stdout.flush()\n",
" plt.close()\n"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- have a set of behaviours\n",
"- have an arbitrator\n",
"- 'current state' runs as a background job\n",
"- 'update' state runs on a clock cycle\n",
"- 'current state' checks values of arbitrator when saying what to do next\n",
"- arbitrator rank-orders behaviours and/or does logic on combinations\n",
"\n",
"\n",
"- behaviours range from low level to high level; from reactive (sensors) to (something else)\n",
"\n",
"\n",
"\n",
"- behaviours: \n",
" - move towards light\n",
" - follow person\n",
" - respond to sound\n",
" - do 'play' (1,2,3,4..X)\n",
" - hierarchy?\n",
"\n",
"\n",
"- different time-scales of evaluation\n",
" - mood / play / behavioural state is a slower time scale\n",
" \n",
"\n",
"- Program them now and print out values as text or something\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Styling"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"css_file = '/media/sf_SharedFolder/Code/git_repos_of_mine/bitbucket/tvb-notebooks/styles/CFDPython_css_modified.css'\n",
"HTML(open(css_file, \"r\").read())"
],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": [
{
"html": [
"<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>\n",
"<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>\n",
"<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400' rel='stylesheet' type='text/css'>\n",
"<style>\n",
" @font-face {\n",
" font-family: \"Computer Modern\";\n",
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
" }\n",
" div.cell{\n",
" width:800px;\n",
" margin-left:16% !important;\n",
" margin-right:auto;\n",
" }\n",
" h1 {\n",
" font-family: 'Alegreya Sans', sans-serif;\n",
" }\n",
" h2 {\n",
" font-family: 'Fenix', serif;\n",
" }\n",
" h3{\n",
"\t\tfont-family: 'Fenix', serif;\n",
" margin-top:12px;\n",
" margin-bottom: 3px;\n",
" }\n",
"\th4{\n",
"\t\tfont-family: 'Fenix', serif;\n",
" }\n",
" h5 {\n",
" font-family: 'Alegreya Sans', sans-serif;\n",
" }\t \n",
" div.text_cell_render{\n",
" font-family: 'Alegreya Sans',Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
" line-height: 135%;\n",
" font-size: 120%;\n",
" width:600px;\n",
" margin-left:auto;\n",
" margin-right:auto;\n",
" }\n",
" .CodeMirror{\n",
" font-family: \"Source Code Pro\";\n",
"\t\t\tfont-size: 90%;\n",
" }\n",
"/* .prompt{\n",
" display: None;\n",
" }*/\n",
" .text_cell_render h1 {\n",
" font-weight: 200;\n",
" font-size: 50pt;\n",
"\t\tline-height: 100%;\n",
" color:#CD2305;\n",
" margin-bottom: 0.5em;\n",
" margin-top: 0.5em;\n",
" display: block;\n",
" }\t\n",
" .text_cell_render h5 {\n",
" font-weight: 300;\n",
" font-size: 16pt;\n",
" color: #CD2305;\n",
" font-style: italic;\n",
" margin-bottom: .5em;\n",
" margin-top: 0.5em;\n",
" display: block;\n",
" }\n",
" \n",
" .warning{\n",
" color: rgb( 240, 20, 20 )\n",
" } \n",
"</style>\n",
"<script>\n",
" MathJax.Hub.Config({\n",
" TeX: {\n",
" extensions: [\"AMSmath.js\"]\n",
" },\n",
" tex2jax: {\n",
" inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
" displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
" },\n",
" displayAlign: 'center', // Change this to 'center' to center equations.\n",
" \"HTML-CSS\": {\n",
" styles: {'.MathJax_Display': {\"margin\": 4}}\n",
" }\n",
" });\n",
"</script>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"<IPython.core.display.HTML at 0xac2144c>"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {
"run_control": {
"state": "n"
}
},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment