Skip to content

Commit a82fe38

Browse files
committed
wip #164
1 parent 6a32faa commit a82fe38

20 files changed

+24
-36
lines changed

coderbot/api.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,25 @@
55

66
import os
77
import subprocess
8-
import shutil
98
import logging
109
import connexion
1110
from werkzeug.datastructures import Headers
1211
from flask import (request,
1312
send_file,
1413
Response)
15-
import connexion
1614
import picamera
17-
import pigpio
1815
import urllib
16+
import connexion
1917

20-
from cachetools import cached, TTLCache
21-
from coderbot import CoderBot
2218
from program import ProgramEngine, Program
2319
from config import Config
2420
from activity import Activities
2521
from camera import Camera
2622
from cnn.cnn_manager import CNNManager
2723
from musicPackages import MusicPackageManager
2824
from audio import Audio
29-
from event import EventManager
3025
from coderbotTestUnit import run_test as runCoderbotTestUnit
26+
from coderbot import CoderBot
3127
from balena import Balena
3228

3329
BUTTON_PIN = 16
@@ -56,7 +52,6 @@ def get_serial():
5652

5753
return cpuserial
5854

59-
@cached(cache=TTLCache(maxsize=1, ttl=10))
6055
def get_status():
6156
"""
6257
Expose CoderBot status:
@@ -87,7 +82,6 @@ def get_status():
8782
'temp': temp,
8883
'uptime': uptime}
8984

90-
@cached(cache=TTLCache(maxsize=1, ttl=60))
9185
def get_info():
9286
"""
9387
Expose informations about the CoderBot system.

coderbot/program.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,28 @@ def prog_list(self):
101101
def save(self, program):
102102
query = Query()
103103
self._program = program
104-
program_db_entry = program.as_dict()
105-
program_db_entry["filename"] = os.path.join(PROGRAM_PATH, PROGRAM_PREFIX + program.name + PROGRAM_SUFFIX)
104+
program_db_entry = self._program.as_dict()
106105
if self._programs.search(query.name == program.name) != []:
107106
self._programs.update(program_db_entry, query.name == program.name)
108107
else:
109108
self._programs.insert(program_db_entry)
110-
f = open(program_db_entry["filename"], 'w+')
111109
json.dump(program.as_dict(), f)
112110
f.close()
113111

114112
def load(self, name):
115113
query = Query()
116114
program_db_entries = self._programs.search(query.name == name)
117115
if len(program_db_entries) > 0:
118-
logging.debug(program_db_entries[0])
119-
f = open(program_db_entries[0]["filename"], 'r')
120-
self._program = Program.from_dict(json.load(f))
116+
prog_db_entry = program_db_entries[0]
117+
logging.debug(prog_db_entry)
118+
self._program = Program.from_dict(prog_db_entry)
121119
return self._program
122120
return None
123121

124122
def delete(self, name):
125123
query = Query()
126124
program_db_entries = self._programs.search(query.name == name)
127-
logging.info("deleting: " + name + " program: " + str(program_db_entries))
128-
if program_db_entries != []:
129-
os.remove(program_db_entries[0]["filename"])
130-
self._programs.remove(query.name == name)
125+
self._programs.remove(query.name == name)
131126

132127
def create(self, name, code):
133128
self._program = Program(name, code)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name": "cat_follower", "dom_code": "<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable id=\",?YI{f}$o~t3-v%!oOh`\">object</variable><variable id=\"9layCHw;-1P!)]pX+m2W\">class</variable><variable id=\"80w]HhW?e7QM%yfJ*[BS\">position</variable><variable id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</variable></variables><block type=\"controls_whileUntil\" id=\"[^a!+/Zsc!iXdJ*Z-Go#\" x=\"43\" y=\"19\"><field name=\"MODE\">WHILE</field><value name=\"BOOL\"><block type=\"logic_boolean\" id=\"2Qi5#s+x(vg-{l2glVGI\"><field name=\"BOOL\">TRUE</field></block></value><statement name=\"DO\"><block type=\"variables_set\" id=\"0Y_GS_fw!h^YTb,XeKz#\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"_GYqAA5bQtVd@ICQGW(U\"><mutation statement=\"false\" at=\"false\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FIRST</field><value name=\"VALUE\"><block type=\"coderbot_adv_cnn_detect_objects\" id=\"m(x+AUHK}k[f:xAb;!iH\"><field name=\"MODEL\">generic_object_detect</field></block></value></block></value><next><block type=\"variables_set\" id=\":mq1jiC7D(V_yjV]p+bj\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"gg?Rm?i]1V[@Sk0~,6}U\"><mutation statement=\"false\" at=\"false\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FIRST</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"jWBE:9@+HD)M5w(R6-+R\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value></block></value><next><block type=\"controls_if\" id=\"SoONo/Qzd^PSRs3kDLW4\"><mutation else=\"1\"></mutation><value name=\"IF0\"><block type=\"logic_compare\" id=\"girWO4BMe2k@24#mK9W|\"><field name=\"OP\">EQ</field><value name=\"A\"><block type=\"variables_get\" id=\"9oEqGs$i`cb!|tDMGcUs\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field></block></value><value name=\"B\"><block type=\"text\" id=\"57hUIBHt5-};Ur^Ym(`p\"><field name=\"TEXT\">cat</field></block></value></block></value><statement name=\"DO0\"><block type=\"variables_set\" id=\":krqJ`zM+j//B~motZ@5\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"n*i|7Ey@DqvX+8X;^2fb\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"$p#z76Ff-*8z$I:6(%:F\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\"?s]?2J@-Pc6i1f-y{[09\"><field name=\"NUM\">3</field></block></value></block></value><next><block type=\"variables_set\" id=\"[:tR1VyS6qeSU?!D)`sN\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field><value name=\"VALUE\"><block type=\"math_arithmetic\" id=\"*U,nQR1[HxPKn8W%pkCz\"><field name=\"OP\">DIVIDE</field><value name=\"A\"><block type=\"math_arithmetic\" id=\"uc.~ggYr?xqXV/.F7s1u\"><field name=\"OP\">ADD</field><value name=\"A\"><block type=\"lists_getIndex\" id=\"kuISxM{Klu36vQ]Jo+P@\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"?gyq($9~Uq.~3XY8_J9)\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\"(1G%d4(*1Vqq?1xk.^{t\"><field name=\"NUM\">1</field></block></value></block></value><value name=\"B\"><block type=\"lists_getIndex\" id=\".Xk?/{p+tLXc=}|OA82n\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"|IdyV?|5py+=3,0!:z_A\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\",Me8:/`3uBpfuBZrA)sp\"><field name=\"NUM\">3</field></block></value></block></value></block></value><value name=\"B\"><block type=\"math_number\" id=\"m~K8F`G6T$ha/a^LdD5Z\"><field name=\"NUM\">2</field></block></value></block></value><next><block type=\"text_print\" id=\"|,^zQ|VUQq/(lt,K`8y0\"><value name=\"TEXT\"><block type=\"variables_get\" id=\"#QdSbUp(4Hn;Xk(4xe0G\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field></block></value><next><block type=\"controls_if\" id=\"}1i(Om9l5~9.WOIq!=W`\"><mutation elseif=\"1\" else=\"1\"></mutation><value name=\"IF0\"><block type=\"logic_compare\" id=\"~.k].tdW8FF4#-(AeXsM\"><field name=\"OP\">LT</field><value name=\"A\"><block type=\"variables_get\" id=\")KUmL+N6#~?rM~GnqH[G\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field></block></value><value name=\"B\"><block type=\"math_number\" id=\"l*=6l=SR/g6%(|]$i;}C\"><field name=\"NUM\">40</field></block></value></block></value><statement name=\"DO0\"><block type=\"coderbot_adv_move\" id=\":YSGyu*XL_u{)zQBD:{X\"><field name=\"ACTION\">LEFT</field><value name=\"SPEED\"><block type=\"math_number\" id=\"de??^JX,i_mkb$RsmNXs\"><field name=\"NUM\">60</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"zs-Cre_[w%5@VNoMwi;l\"><field name=\"NUM\">0.1</field></block></value></block></statement><value name=\"IF1\"><block type=\"logic_compare\" id=\"+S5!K$WfFhj@QhRSokhV\"><field name=\"OP\">GT</field><value name=\"A\"><block type=\"variables_get\" id=\"J%|{#:u3(!lcF4.B~gQt\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field></block></value><value name=\"B\"><block type=\"math_number\" id=\"[gg?3(+hO-?.%qW,`@t~\"><field name=\"NUM\">60</field></block></value></block></value><statement name=\"DO1\"><block type=\"coderbot_adv_move\" id=\"vA*_d?-Bwh55o,{*%v:G\"><field name=\"ACTION\">RIGHT</field><value name=\"SPEED\"><block type=\"math_number\" id=\"W}pFMYn/}2RnG+E86e~v\"><field name=\"NUM\">60</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"as/ZY,XwrTw`@Z~O9B`$\"><field name=\"NUM\">0.1</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"coderbot_adv_move\" id=\"Xs:ewoKw*Fg3UK*sRCNm\"><field name=\"ACTION\">FORWARD</field><value name=\"SPEED\"><block type=\"math_number\" id=\"qSFO9:h}?s~cD:hj(r:(\"><field name=\"NUM\">100</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"Nh^fXOiu1$x8[$i1C2IV\"><field name=\"NUM\">0.2</field></block></value></block></statement></block></next></block></next></block></next></block></statement><statement name=\"ELSE\"><block type=\"text_print\" id=\"PAYJ[1S)`0Gl#=M`cX=g\"><value name=\"TEXT\"><block type=\"variables_get\" id=\"2nPmKkqF|Y:u3?r=5y|n\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value></block></statement></block></next></block></next></block></statement></block></xml>", "code": "object2 = None\nclass2 = None\nposition = None\npos_x = None\n\n\nwhile True:\n get_prog_eng().check_end()\n object2 = get_cam().cnn_detect_objects(\"generic_object_detect\")[0]\n class2 = object2[0]\n if class2 == 'cat':\n position = object2[2]\n pos_x = (position[0] + position[2]) / 2\n get_cam().set_text(class2)\n if pos_x < 40:\n get_bot().left(speed=60, elapse=0.1)\n elif pos_x > 60:\n get_bot().right(speed=60, elapse=0.1)\n else:\n get_bot().forward(speed=100, elapse=0.2)\n else:\n get_cam().set_text(object2)\n", "default": false}
1+
{"name": "cat_follower", "dom_code": "<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable id=\",?YI{f}$o~t3-v%!oOh`\">object</variable><variable id=\"9layCHw;-1P!)]pX+m2W\">class</variable><variable id=\"80w]HhW?e7QM%yfJ*[BS\">position</variable><variable id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</variable></variables><block type=\"controls_whileUntil\" id=\"[^a!+/Zsc!iXdJ*Z-Go#\" x=\"43\" y=\"19\"><field name=\"MODE\">WHILE</field><value name=\"BOOL\"><block type=\"logic_boolean\" id=\"2Qi5#s+x(vg-{l2glVGI\"><field name=\"BOOL\">TRUE</field></block></value><statement name=\"DO\"><block type=\"variables_set\" id=\"0Y_GS_fw!h^YTb,XeKz#\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"_GYqAA5bQtVd@ICQGW(U\"><mutation statement=\"false\" at=\"false\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FIRST</field><value name=\"VALUE\"><block type=\"coderbot_adv_cnn_detect_objects\" id=\"m(x+AUHK}k[f:xAb;!iH\"><field name=\"MODEL\">generic_object_detect</field></block></value></block></value><next><block type=\"variables_set\" id=\":mq1jiC7D(V_yjV]p+bj\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"gg?Rm?i]1V[@Sk0~,6}U\"><mutation statement=\"false\" at=\"false\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FIRST</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"jWBE:9@+HD)M5w(R6-+R\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value></block></value><next><block type=\"controls_if\" id=\"SoONo/Qzd^PSRs3kDLW4\"><mutation else=\"1\"></mutation><value name=\"IF0\"><block type=\"logic_compare\" id=\"girWO4BMe2k@24#mK9W|\"><field name=\"OP\">EQ</field><value name=\"A\"><block type=\"variables_get\" id=\"9oEqGs$i`cb!|tDMGcUs\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field></block></value><value name=\"B\"><block type=\"text\" id=\"57hUIBHt5-};Ur^Ym(`p\"><field name=\"TEXT\">cat</field></block></value></block></value><statement name=\"DO0\"><block type=\"variables_set\" id=\":krqJ`zM+j//B~motZ@5\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field><value name=\"VALUE\"><block type=\"lists_getIndex\" id=\"n*i|7Ey@DqvX+8X;^2fb\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"$p#z76Ff-*8z$I:6(%:F\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\"?s]?2J@-Pc6i1f-y{[09\"><field name=\"NUM\">3</field></block></value></block></value><next><block type=\"variables_set\" id=\"[:tR1VyS6qeSU?!D)`sN\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field><value name=\"VALUE\"><block type=\"math_arithmetic\" id=\"*U,nQR1[HxPKn8W%pkCz\"><field name=\"OP\">DIVIDE</field><value name=\"A\"><block type=\"math_arithmetic\" id=\"uc.~ggYr?xqXV/.F7s1u\"><field name=\"OP\">ADD</field><value name=\"A\"><block type=\"lists_getIndex\" id=\"kuISxM{Klu36vQ]Jo+P@\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"?gyq($9~Uq.~3XY8_J9)\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\"(1G%d4(*1Vqq?1xk.^{t\"><field name=\"NUM\">1</field></block></value></block></value><value name=\"B\"><block type=\"lists_getIndex\" id=\".Xk?/{p+tLXc=}|OA82n\"><mutation statement=\"false\" at=\"true\"></mutation><field name=\"MODE\">GET</field><field name=\"WHERE\">FROM_START</field><value name=\"VALUE\"><block type=\"variables_get\" id=\"|IdyV?|5py+=3,0!:z_A\"><field name=\"VAR\" id=\"80w]HhW?e7QM%yfJ*[BS\">position</field></block></value><value name=\"AT\"><block type=\"math_number\" id=\",Me8:/`3uBpfuBZrA)sp\"><field name=\"NUM\">3</field></block></value></block></value></block></value><value name=\"B\"><block type=\"math_number\" id=\"m~K8F`G6T$ha/a^LdD5Z\"><field name=\"NUM\">2</field></block></value></block></value><next><block type=\"text_print\" id=\"|,^zQ|VUQq/(lt,K`8y0\"><value name=\"TEXT\"><block type=\"variables_get\" id=\"#QdSbUp(4Hn;Xk(4xe0G\"><field name=\"VAR\" id=\"9layCHw;-1P!)]pX+m2W\">class</field></block></value><next><block type=\"controls_if\" id=\"}1i(Om9l5~9.WOIq!=W`\"><mutation elseif=\"1\" else=\"1\"></mutation><value name=\"IF0\"><block type=\"logic_compare\" id=\"~.k].tdW8FF4#-(AeXsM\"><field name=\"OP\">LT</field><value name=\"A\"><block type=\"variables_get\" id=\")KUmL+N6#~?rM~GnqH[G\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field></block></value><value name=\"B\"><block type=\"math_number\" id=\"l*=6l=SR/g6%(|]$i;}C\"><field name=\"NUM\">40</field></block></value></block></value><statement name=\"DO0\"><block type=\"coderbot_adv_move\" id=\":YSGyu*XL_u{)zQBD:{X\"><field name=\"ACTION\">LEFT</field><value name=\"SPEED\"><block type=\"math_number\" id=\"de??^JX,i_mkb$RsmNXs\"><field name=\"NUM\">60</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"zs-Cre_[w%5@VNoMwi;l\"><field name=\"NUM\">0.1</field></block></value></block></statement><value name=\"IF1\"><block type=\"logic_compare\" id=\"+S5!K$WfFhj@QhRSokhV\"><field name=\"OP\">GT</field><value name=\"A\"><block type=\"variables_get\" id=\"J%|{#:u3(!lcF4.B~gQt\"><field name=\"VAR\" id=\"fRn/=2s}a[[jcCloxlyH\">pos_x</field></block></value><value name=\"B\"><block type=\"math_number\" id=\"[gg?3(+hO-?.%qW,`@t~\"><field name=\"NUM\">60</field></block></value></block></value><statement name=\"DO1\"><block type=\"coderbot_adv_move\" id=\"vA*_d?-Bwh55o,{*%v:G\"><field name=\"ACTION\">RIGHT</field><value name=\"SPEED\"><block type=\"math_number\" id=\"W}pFMYn/}2RnG+E86e~v\"><field name=\"NUM\">60</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"as/ZY,XwrTw`@Z~O9B`$\"><field name=\"NUM\">0.1</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"coderbot_adv_move\" id=\"Xs:ewoKw*Fg3UK*sRCNm\"><field name=\"ACTION\">FORWARD</field><value name=\"SPEED\"><block type=\"math_number\" id=\"qSFO9:h}?s~cD:hj(r:(\"><field name=\"NUM\">100</field></block></value><value name=\"ELAPSE\"><block type=\"math_number\" id=\"Nh^fXOiu1$x8[$i1C2IV\"><field name=\"NUM\">0.2</field></block></value></block></statement></block></next></block></next></block></next></block></statement><statement name=\"ELSE\"><block type=\"text_print\" id=\"PAYJ[1S)`0Gl#=M`cX=g\"><value name=\"TEXT\"><block type=\"variables_get\" id=\"2nPmKkqF|Y:u3?r=5y|n\"><field name=\"VAR\" id=\",?YI{f}$o~t3-v%!oOh`\">object</field></block></value></block></statement></block></next></block></next></block></statement></block></xml>", "code": "object2 = None\nclass2 = None\nposition = None\npos_x = None\n\n\nwhile True:\n get_prog_eng().check_end()\n object2 = get_cam().cnn_detect_objects(\"generic_object_detect\")[0]\n class2 = object2[0]\n if class2 == 'cat':\n position = object2[2]\n pos_x = (position[0] + position[2]) / 2\n get_cam().set_text(class2)\n if pos_x < 40:\n get_bot().left(speed=60, elapse=0.1)\n elif pos_x > 60:\n get_bot().right(speed=60, elapse=0.1)\n else:\n get_bot().forward(speed=100, elapse=0.2)\n else:\n get_cam().set_text(object2)\n"}

0 commit comments

Comments
 (0)