Skip to content

Commit

Permalink
40 finish tutorial (#41)
Browse files Browse the repository at this point in the history
* feat: finish tutorial

* chore: checkpoint

* feat: base for demo backend

* chore: update instanciator docker compose

* fix: update nginx container network mode

* fix: editable controller issues

* feat: add editable and non editable mode for ribot-controller backenc

* fix: wrong raise in manage.py

* chore: update nginx conf

* feat: add demo

* chore: remote router from instanciator docker compose

* chore: fix outdted free port function

* fix: ch docker-compose to docker compose

* fix: ch docker-compose to docker compose

* fix: another  bad docker compose

* fix: cleanup ArmSimulation.jsx

* fix: websocket port

* fix: websocket port missing /

* chore: update arm simulation

* chore: update arm simulation

* chore: update instance generator

* chore: update broken instanciator docker compose file

* feat: decrease cpu ussage

* fix: linting issues

* feat: update tutorial, improve actions play button

* chore: fix linting

* chore: update docker compose for  instanciator

* fix: instanciator healthcheck method

* fix: instanciator healthcheck method

* chore: update api url for base url

* chore: isntanciator error on free instances

* chore: udpate simulation css and settigns for instanciator

* chore: update instance generator

* feat: add gripper to unity simulation

* feat: update instance scripto to properly support healthcheck

* fix: update healthcheck endpoint to be get instead of post

* fix: update instanciator

* chore: isntanciator error on free instances

* add prints to healthcech

* chore: start refactor of instanciator

* chore: isntanciator error on free instances

* chore: isntanciator error on free instances

* feat: finish refactor of instanciator and update location of arm in simulation

* chore: update simulation

* chore: update simulation

* chore: update simulation

* feat: add token access to instanciator

* feat: add token access to instanciator

* feat: complete instanciator
  • Loading branch information
alberto-abarzua authored Nov 19, 2023
1 parent 3fbeb4e commit 64e7a33
Show file tree
Hide file tree
Showing 80 changed files with 2,926 additions and 438 deletions.
26 changes: 18 additions & 8 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
# ESP32
export ESP_WIFI_SSID=ssid
export ESP_WIFI_PASSWORD=password
export ESP_WIFI_SSID=PALVI
export ESP_WIFI_PASSWORD=Palvi.1400
export ESP_CONTROLLER_SERVER_HOST=backend
export ESP_CONTROLLER_SERVER_PORT=8500
export ESP_FLASH_PORT=

# CONTROLLER DEFAULT SETTINGS
CONTROLLER_WEBSOCKET_PORT=8600
CONTROLLER_SERVER_PORT=8500

CONTROLLER_PRINT_STATUS=false

# ---PDM CONTROLLER PUBLISH SETTINGS
CONTROLLER_PDM_PUBLISH_USERNAME=__token__
CONTROLLER_PDM_PUBLISH_PASSWORD=token_here
CONTROLLER_PDM_PUBLISH_PASSWORD=tokenhere!
CONTROLLER_PDM_OVERRIDE_VERSION=None
CONTROLLER_PDM_INCREMENT_VERSION=false

# BACKEND
BACKEND_HTTP_PORT=8000


# BACKEND
BACKEND_HTTP_PORT=5000
BACKEND_UPDATE_RIBOT_CONTROLLER=false

# FRONT
VITE_BACKEND_URL=http://localhost:8000
VITE_BACKEND_URL=http://localhost:5000
VITE_ARM_SIMULATION_WEBSOCKET_PORT=8600
VITE_ARM_SIMULATION_WEBSOCKET_HOST=localhost
VITE_ARM_SIMULATION_URL=http://localhost:8080



# ARM_SIMULATION
ARM_SIMULATION_PORT=8080

# Docker compose debug buid
export COMPOSE_DOCKER_CLI_BUILD=1
export DOCKER_BUILDKIT=1



2 changes: 1 addition & 1 deletion .github/workflows/backend-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set Env File
run: printenv | grep -v "^\(PWD\|SHLVL\|HOME\)" > .env
- name: Build backend container
run: ./manage.py build --container backend
run: ./manage.py build --container backend --no-editable

- name: Lint Backend
run: ./manage.py lint --container backend
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

Needs to be updated!
`
:
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.pdm-python
controller
25 changes: 16 additions & 9 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
FROM python:3.11-slim-buster

ARG EDITABLE_PACKAGES
ENV EDITABLE_PACKAGES=${EDITABLE_PACKAGES}

WORKDIR /app

# hadolint ignore=DL3008
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential && \
build-essential coreutils && \
rm -rf /var/lib/apt/lists/*

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

ENV PYTHONPATH=/app/src


RUN pip install --no-cache-dir pdm==2.10
RUN pip install --no-cache-dir pdm==2.10.3

COPY ./pyproject.toml ./pyproject.toml
COPY ./pdm.lock ./pdm.lock


COPY . .

RUN pdm install
RUN pdm remove ribot-controller || true && \
pdm remove ribot-controller --dev || true

RUN chmod +x ./entrypoint.sh
RUN if [ "$EDITABLE_PACKAGES" = "true" ]; then \
echo "Installing editable packages"; \
pdm add -e ./controller --dev; \
else \
echo "Installing non-editable packages"; \
pdm add ribot-controller; \
fi

EXPOSE 8000

ENTRYPOINT ["/app/entrypoint.sh"]
RUN pdm install

CMD ["pdm","run","start"]

13 changes: 0 additions & 13 deletions backend/entrypoint.sh

This file was deleted.

16 changes: 7 additions & 9 deletions backend/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dependencies = [
"fastapi>=0.101.1",
"uvicorn>=0.23.2",
"numpy>=1.20",
"ribot-controller>=0.3.4",
]
requires-python = ">=3.11"

Expand All @@ -14,11 +13,12 @@ dev = [
"flake8>=6.0.0",
"flake8-annotations>=3.0.1",
"mypy>=1.4.1",
"-e file:///${PROJECT_ROOT}/controller#egg=ribot-controller",
]


[tool.pdm.scripts]
start = "pdm run uvicorn src.main:app --host 0.0.0.0"
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
9 changes: 9 additions & 0 deletions backend/src/routers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ def config_from_file(
def list_configs() -> Dict[Any, Any]:
CONFIG_PATH = Path(__file__).parent.parent / "config"
return {"configs": [str(f) for f in CONFIG_PATH.iterdir() if f.is_file()]}


@router.get("/websocket_info/")
def websocket_info(
controller: ArmController = controller_dependency,
) -> Dict[Any, Any]:
return {
"websocket_port": controller.websocket_port,
}
2 changes: 1 addition & 1 deletion controller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --no-cache-dir pdm==2.10.1
RUN pip install --no-cache-dir pdm==2.10.3

ENV PYTHONPATH "${PYTHONPATH}:/app/src"

Expand Down
8 changes: 5 additions & 3 deletions controller/src/ribot/control/controller_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 +202,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 All @@ -221,8 +223,8 @@ def __init__(self, controller: Any, port: int) -> None:
async def handler(self, websocket: Any, _: str) -> None:
async for message in websocket:
if message.strip() == "get_angles":
angles = self.controller.current_angles
response = Message(MessageOp.STATUS, 0, angles)
angles_tool = list(self.controller.current_angles) + [self.controller.tool_value]
response = Message(MessageOp.STATUS, 0, angles_tool)
await websocket.send(response.encode())

def _start(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion controller/src/ribot/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(

self.controller_server: ControllerServer = ControllerServer(self, server_port)
self.websocket_server: Optional[WebsocketServer] = WebsocketServer(self, websocket_port)
self.websocket_port = websocket_port
self.file_reload_thread: Optional[threading.Thread] = None

self.message_op_handlers: Dict[MessageOp, Callable[[Message], None]] = {
Expand Down Expand Up @@ -124,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
9 changes: 8 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,14 @@ 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
6 changes: 3 additions & 3 deletions docker_services/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ services:
build:
context: "../backend"
dockerfile: "Dockerfile"
args:
- EDITABLE_PACKAGES=$BACKEND_EDITABLE_PACKAGES
image: "uintuser/ribot-backend"
volumes:
- ../backend/src:/app/src
- ../backend/pyproject.toml:/app/pyproject.toml
- ../backend/src/config/main_arm.toml:/app/src/config/main_arm.toml
- ../backend/pdm.lock:/app/pdm.lock
- ../controller:/app/controller
ports:
Expand All @@ -17,8 +20,5 @@ services:
environment:
- BACKEND_HTTP_PORT
- CONTROLLER_WEBSOCKET_PORT
- BACKEND_UPDATE_RIBOT_CONTROLLER
- CONTROLLER_SERVER_PORT

volumes:
pdm_cache:
3 changes: 3 additions & 0 deletions docker_services/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +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

1 change: 1 addition & 0 deletions docker_services/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ services:
- VITE_ARM_SIMULATION_WEBSOCKET_PORT
- VITE_ARM_SIMULATION_WEBSOCKET_HOST
- VITE_ARM_SIMULATION_URL
- VITE_INSTANCIATOR_URL
Binary file modified firmware/app
Binary file not shown.
16 changes: 15 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 @@ -566,6 +566,7 @@ void Controller::message_handler_config(Message *message) {

EndStop *end_stop = new DummyEndStop(
0, new_movement_driver->get_current_angle_ptr());

this->joints[joint_idx]->set_movement_driver(new_movement_driver);
this->joints[joint_idx]->register_end_stop(end_stop);
new_movement_driver->hardware_setup();
Expand Down Expand Up @@ -711,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
34 changes: 1 addition & 33 deletions firmware/components/movement/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,39 +107,7 @@ uint32_t MovementDriver::steps_to_take(uint64_t current_time) {
this->angle_to_steps(this->current_angle));
return std::min(steps, steps_to_target);
}
// bool MovementDriver::step() {
// uint64_t current_time = get_current_time_microseconds();
// uint32_t steps_to_take = this->steps_to_take(current_time);

// if (steps_to_take > 0) {

// int8_t step_dir = this->target_angle > this->current_angle ? 1 : -1;

// for (uint16_t i = 0; i < steps_to_take; i++) {
// this->current_steps += step_dir;

// this->hardware_step(step_dir > 0 ? 1 : 0);

// if (!this->homed && this->end_stop != nullptr) {
// this->current_angle =
// this->steps_to_angle(this->current_steps); if
// (this->end_stop->hardware_read_state()) {
// this->set_home();
// this->last_step_time = get_current_time_microseconds();
// return false;
// }
// run_delay_microseconds(10);
// } else {
// run_delay_microseconds(30);
// }
// }

// this->last_step_time = get_current_time_microseconds();
// this->current_angle = this->steps_to_angle(this->current_steps);
// return true;
// }
// return false;
// }

bool MovementDriver::step() {
uint64_t current_time = get_current_time_microseconds();
uint32_t steps_to_take = this->steps_to_take(current_time);
Expand Down
Loading

1 comment on commit 64e7a33

@vercel
Copy link

@vercel vercel bot commented on 64e7a33 Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.