Skip to content

Commit

Permalink
feat: decrease cpu ussage
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto-abarzua committed Nov 17, 2023
1 parent 61f543c commit 30c4999
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dev = [


[tool.pdm.scripts]
start = "pdm run uvicorn src.main:app --host 0.0.0.0 --port ${BACKEND_HTTP_PORT} --reload"
start = "pdm run uvicorn src.main:app --host 0.0.0.0 --port ${BACKEND_HTTP_PORT}"
isort = "isort src"
black = "black src"
pure_lint = "flake8 src"
Expand Down
5 changes: 4 additions & 1 deletion controller/src/ribot/control/controller_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from abc import ABC, abstractmethod
from enum import Enum
from typing import Any, Optional, Union
import time

import websockets

Expand Down Expand Up @@ -55,7 +56,7 @@ def __init__(self, controller: Any, port: int) -> None:
self._connection_mutex: FIFOLock = FIFOLock()
self.stop_event = controller.stop_event

self.status_time_interval: float = 1 / 30 # 30 Hz
self.status_time_interval: float = 1 / 15

@property
def connection_mutex(self) -> FIFOLock:
Expand Down Expand Up @@ -202,6 +203,8 @@ def handle_controller_connection(self) -> None:
handler = self.controller.message_op_handlers[msg.op]
handler(msg)

self.stop_event.wait(0.05)


# -----------------
# WebsocketServer
Expand Down
2 changes: 1 addition & 1 deletion controller/src/ribot/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def start(self, wait: bool = False, websocket_server: bool = True) -> None:
start_time = time.time()
if wait:
while not self.is_ready and not self.stop_event.is_set():
time.sleep(0.1)
time.sleep(2)
if time.time() - start_time > connection_controller_timeout:
raise TimeoutError("Controller took too long to start, check arm client")

Expand Down
8 changes: 7 additions & 1 deletion controller/src/ribot/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ def setUpClass(cls) -> None:
arm_params.a5x = 64.1
arm_params.a6x = 169

cls.controller = ArmController(arm_parameters=arm_params)
controler_server_port = int(os.environ.get("CONTROLLER_SERVER_PORT", 8500))
controller_websocket_port = int(os.environ.get("CONTROLLER_WEBSOCKET_PORT", 8430))
print("controller_websocket_port", controller_websocket_port)
print("controller_server_port", controler_server_port)

cls.controller = ArmController(arm_parameters=arm_params,
server_port=controler_server_port, websocket_port=controller_websocket_port)
cls.controller.start(websocket_server=False, wait=True)

cls.controller.print_status = os.environ.get("CONTROLLER_PRINT_STATUS", "False").upper() == "TRUE"
Expand Down
2 changes: 2 additions & 0 deletions docker_services/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ services:
- PDM_OVERRIDE_VERSION=$CONTROLLER_PDM_OVERRIDE_VERSION
- PDM_INCREMENT_VERSION=$CONTROLLER_PDM_INCREMENT_VERSION
- CONTROLLER_PRINT_STATUS
- CONTROLLER_WEBSOCKET_PORT
- CONTROLLER_SERVER_PORT

15 changes: 14 additions & 1 deletion firmware/components/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ bool Controller::start() {
if (succesful_setup != ArmClientCode::SUCCESS) {
std::cout << "Failed to setup arm client, retrying in 3 seconds"
<< std::endl;
run_delay(2000);
run_delay(3000);
return false;
}
this->run_step_task();
Expand Down Expand Up @@ -712,9 +712,22 @@ void Controller::stop_step_task() {}
#else

void Controller::step_target_fun() {
task_add();

uint64_t iter = 0;
uint64_t iter_delay = 500;

while (this->stop_flag == false) {
if (iter > iter_delay) {
task_feed();
run_delay(10);
iter = 0;
}

this->step();
iter++;
}
task_end();
}

void Controller::run_step_task() {
Expand Down
3 changes: 2 additions & 1 deletion firmware/components/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "utils.h"
#include <cstdint>

#ifdef ESP_PLATFORM
#include <rom/ets_sys.h>
Expand Down Expand Up @@ -155,7 +156,7 @@ void task_feed() {}
void task_end() {}

void run_delay(uint32_t delay_ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms*5));
}

void run_delay_microseconds(uint32_t delay_us) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.8'
services:
backend:
image: "uintuser/ribot-backend"
command: pdm run uvicorn src.main:app --host 0.0.0.0 --port ${BACKEND_HTTP_PORT} --reload
command: pdm run uvicorn src.main:app --host 0.0.0.0 --port ${BACKEND_HTTP_PORT}
volumes:
- ./web_arm.toml:/app/src/config/main_arm.toml
ports:
Expand Down
6 changes: 4 additions & 2 deletions instanciator/backend/src/instance_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self) -> None:
)
self.start_instance_checker()

def isntance_checker_target_fun(self) -> None:
def instance_checker_target_fun(self) -> None:
while True:
instances = self.instances
free_instances = 0
Expand All @@ -43,7 +43,9 @@ def isntance_checker_target_fun(self) -> None:

if free_instances <= 3:
new_uuid = str(uuid.uuid4())
self.instances = instances
new_instance = self.create_instance(new_uuid)
instances = self.instances
new_instance["free"] = True
instances[new_uuid] = new_instance

Expand All @@ -52,7 +54,7 @@ def isntance_checker_target_fun(self) -> None:
time.sleep(5)

def start_instance_checker(self) -> None:
thread = threading.Thread(target=self.isntance_checker_target_fun)
thread = threading.Thread(target=self.instance_checker_target_fun)
thread.start()

def get_instances(self) -> Dict[str, Any]:
Expand Down
1 change: 1 addition & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def lint(self, **kwargs):

def test_debug(self, **_):
os.environ["CONTROLLER_PRINT_STATUS"] = "true"

self.docker_manager.dc_up(['controller.yaml', 'firmware.yaml'], env={
"ESP_CONTROLLER_SERVER_HOST": "controller", "CONTROLLER_COMMAND": "test"})
self.docker_manager.dc_down(['controller.yaml', 'firmware.yaml'])
Expand Down

0 comments on commit 30c4999

Please sign in to comment.