Skip to content

Commit

Permalink
cherry pick: fix coderbot init, program load (#203)
Browse files Browse the repository at this point in the history
* fix init coderbot
* fix program load
* bump connexion to 3.0.x
* add wifi stub
  • Loading branch information
previ authored Jun 16, 2024
1 parent c437c0f commit 4e0e1ec
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 50 deletions.
11 changes: 1 addition & 10 deletions coderbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@

BUTTON_PIN = 16

config = Config.read()
bot = CoderBot.get_instance(motor_trim_factor=float(config.get('move_motor_trim', 1.0)),
motor_max_power=int(config.get('motor_max_power', 100)),
motor_min_power=int(config.get('motor_min_power', 0)),
hw_version=config.get('hardware_version'),
pid_params=(float(config.get('pid_kp', 1.0)),
float(config.get('pid_kd', 0.1)),
float(config.get('pid_ki', 0.01)),
float(config.get('pid_max_speed', 200)),
float(config.get('pid_sample_time', 0.01))))
bot = CoderBot.get_instance()
audio_device = Audio.get_instance()
cam = Camera.get_instance()

Expand Down
2 changes: 1 addition & 1 deletion coderbot/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pyaudio
import alsaaudio

from six.moves import queue
import queue
# [END import_libraries]

# Audio recording parameters
Expand Down
7 changes: 4 additions & 3 deletions coderbot/coderbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ def exit(self):
s.cancel()

@classmethod
def get_instance(cls, motor_trim_factor=1.0, motor_max_power=100, motor_min_power=0, hw_version="5", pid_params=(0.8, 0.1, 0.01, 200, 0.01)):
def get_instance(cls, motor_trim_factor=1.0, motor_max_power=100, motor_min_power=0, hw_version="5", pid_params=(0.8, 0.1, 0.01, 200, 0.01), from_defaults=True):
if not cls.the_bot:
if from_defaults:
raise ValueError("incorrect CoderBot initialisation")
cls.the_bot = CoderBot(motor_trim_factor=motor_trim_factor, motor_max_power= motor_max_power, motor_min_power=motor_min_power, hw_version=hw_version, pid_params=pid_params)
return cls.the_bot

Expand Down Expand Up @@ -272,5 +274,4 @@ def _cb_button(self, gpio, level, tick):
elif tick - self._cb_last_tick[gpio] > elapse:
self._cb_last_tick[gpio] = tick
logging.info("pushed: %d, %d", level, tick)
cb()

cb()
45 changes: 27 additions & 18 deletions coderbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import picamera
import connexion

from flask_cors import CORS
from connexion.options import SwaggerUIOptions
from connexion.middleware import MiddlewarePosition
from starlette.middleware.cors import CORSMiddleware

from camera import Camera
from motion import Motion
Expand All @@ -22,29 +24,27 @@
# Logging configuration
logger = logging.getLogger()
logger.setLevel(os.environ.get("LOGLEVEL", "INFO"))
# sh = logging.StreamHandler()
# formatter = logging.Formatter('%(message)s')
# sh.setFormatter(formatter)
# logger.addHandler(sh)

## (Connexion) Flask app configuration

# Serve a custom version of the swagger ui (Jinja2 templates) based on the default one
# from the folder 'swagger-ui'. Clone the 'swagger-ui' repository inside the backend folder
options = {"swagger_ui": False}
connexionApp = connexion.App(__name__, options=options)

# Connexion wraps FlaskApp, so app becomes connexionApp.app
app = connexionApp.app
# Access-Control-Allow-Origin
CORS(app)
app.debug = False
swagger_ui_options = SwaggerUIOptions(swagger_ui=True)
app = connexion.App(__name__, swagger_ui_options=swagger_ui_options)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.prog_engine = ProgramEngine.get_instance()

## New API and web application

# API v1 is defined in v1.yml and its methods are in api.py
connexionApp.add_api('v1.yml')
app.add_api('v1.yml')

def button_pushed():
if app.bot_config.get('button_func') == "startstop":
Expand All @@ -67,8 +67,16 @@ def run_server():
try:
try:
app.bot_config = Config.read()

bot = CoderBot.get_instance()
bot = CoderBot.get_instance(motor_trim_factor=float(app.bot_config.get('move_motor_trim', 1.0)),
motor_max_power=int(app.bot_config.get('motor_max_power', 100)),
motor_min_power=int(app.bot_config.get('motor_min_power', 0)),
hw_version=app.bot_config.get('hardware_version'),
pid_params=(float(app.bot_config.get('pid_kp', 1.0)),
float(app.bot_config.get('pid_kd', 0.1)),
float(app.bot_config.get('pid_ki', 0.01)),
float(app.bot_config.get('pid_max_speed', 200)),
float(app.bot_config.get('pid_sample_time', 0.01))),
from_defaults=False)

try:
audio_device = Audio.get_instance()
Expand All @@ -78,6 +86,7 @@ def run_server():
logging.warning("Audio not present")

try:
logging.info("starting camera")
cam = Camera.get_instance()
Motion.get_instance()
except picamera.exc.PiCameraError:
Expand All @@ -97,12 +106,12 @@ def run_server():

remove_doreset_file()

app.run(host="0.0.0.0", port=5000, debug=False, use_reloader=False, threaded=True)
app.run(host="0.0.0.0", port=5000)
finally:
if cam:
cam.exit()
if bot:
bot.exit()

if __name__ == "__main__":
run_server()
run_server()
13 changes: 7 additions & 6 deletions coderbot/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ def __init__(self):
for filename in filenames:
if PROGRAM_PREFIX in filename:
program_name = filename[len(PROGRAM_PREFIX):-len(PROGRAM_SUFFIX)]
logging.info("adding program %s in path %s as default %r", program_name, dirname, ("default" in dirname))
with open(os.path.join(dirname, filename), "r") as f:
program_dict = json.load(f)
program_dict["default"] = "default" in dirname
program = Program.from_dict(program_dict)
self.save(program)
if self._programs.search(query.name == program_name) == []:
logging.info("adding program %s in path %s as default %r", program_name, dirname, ("default" in dirname))
with open(os.path.join(dirname, filename), "r") as f:
program_dict = json.load(f)
program_dict["default"] = "default" in dirname
program = Program.from_dict(program_dict)
self.save(program)

@classmethod
def get_instance(cls):
Expand Down
46 changes: 43 additions & 3 deletions coderbot/v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'

tags:
- Program management
responses:
Expand All @@ -184,6 +188,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
responses:
200:
description: "ok"
Expand All @@ -200,6 +207,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
requestBody:
description: Program object
required: true
Expand All @@ -225,6 +235,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
requestBody:
description: Program object
required: true
Expand All @@ -248,6 +261,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
responses:
200:
description: "ok"
Expand All @@ -264,6 +280,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
responses:
200:
description: "ok"
Expand Down Expand Up @@ -304,6 +323,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
- name: default
in: query
schema:
Expand All @@ -323,6 +345,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
requestBody:
description: Update Activity
required: true
Expand All @@ -346,6 +371,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
responses:
200:
description: "ok"
Expand Down Expand Up @@ -386,6 +414,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
tags:
- Music extensions
responses:
Expand Down Expand Up @@ -502,12 +533,13 @@ paths:
type: string
minLength: 1
maxLength: 256
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
description: text to be "spoken"
locale:
type: string
minLength: 1
maxLength: 2
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
pattern: '^[a-zA-Z]+$'
description: locale of text to be "spoken"
required:
- text
Expand Down Expand Up @@ -586,6 +618,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
tags:
- CNN Models
responses:
Expand All @@ -600,6 +635,9 @@ paths:
required: true
schema:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
tags:
- CNN Models
responses:
Expand Down Expand Up @@ -679,16 +717,17 @@ components:
properties:
name:
type: string
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
tag:
type: string
Program:
type: object
properties:
name:
type: string
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
code:
type: string
minLength: 1
Expand All @@ -709,6 +748,7 @@ components:
type: string
minLength: 1
maxLength: 128
pattern: '^[a-zA-ZA-zÀ-ú0-9-_ ]+$'
description:
type: string
minLength: 0
Expand All @@ -722,4 +762,4 @@ components:
- description
- default
- stock


5 changes: 1 addition & 4 deletions docker/stub/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# API framework
connexion==2.14.2
Flask==2.2.5
Flask-Cors==3.0.10
connexion[uvicorn,flask,swagger-ui]==3.0.5
tinydb==4.8.0
Werkzeug==2.2.3

# Misc utils
setuptools==69.2.0
Expand Down
2 changes: 1 addition & 1 deletion docker/stub/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

export PYTHONPATH=./stub:./test:./coderbot
cd /coderbot
python3 coderbot/main.py
python3 coderbot/main.py & python3 stub/wifi/main.py
5 changes: 1 addition & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# API framework
connexion==2.14.2
Flask==2.2.5
Flask-Cors==3.0.10
connexion[uvicorn,flask,swagger-ui]==3.0.5
tinydb==4.8.0
Werkzeug==2.2.3

# Misc utils
setuptools==69.2.0
Expand Down
19 changes: 19 additions & 0 deletions stub/wifi/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

def list_access_points():
return {"ssids": [{"ssid": "my_wifi"}]}

def connection_status():
return {"wifi": "true", "internet": "true"}

def connect():
return "ok"

def forget():
return "ok"

def sset_hotspot_ssid():
return "ok"

def set_hotspot_password():
return "ok"
Loading

0 comments on commit 4e0e1ec

Please sign in to comment.