Skip to content

Bringing canopen to uc2-rest#136

Merged
beniroquai merged 9 commits into
masterfrom
mergemaster
May 26, 2026
Merged

Bringing canopen to uc2-rest#136
beniroquai merged 9 commits into
masterfrom
mergemaster

Conversation

@beniroquai
Copy link
Copy Markdown
Contributor

This pull request removes several example and library files related to the AS5311 sensor and rotary encoder implementations, as well as adds new documentation for QID-based done/busy tracking in the UC2 firmware and Python API. The changes primarily focus on cleaning up unused or deprecated code and providing comprehensive documentation for a new feature.

The most important changes are:

Documentation and Feature Explanation:

  • Added a new documentation file, DOCUMENTATION/QID_TRACKING_DOCUMENTATION.md, which explains the QID-based done/busy tracking system, its architecture, firmware and Python API usage, implementation details, and testing procedures. This includes details on pause/resume functionality and the new CAN protocol changes.

Code Cleanup and Removal (AS5311 Example and Library Files):

  • Removed the entire ESP32/AS5311-AB-LIB-QUEUE example, including AS5311-AB-LIB-QUEUE.ino, AS5311AB.cpp, and AS5311AB.h, which implemented a FreeRTOS-based rotary encoder interface using queues and interrupts. [1] [2] [3]
  • Removed the ESP32/AS5311-AB-LIB example and library files, including AS5311-AB-LIB.ino, AS5311.cpp, and AS5311.h, which provided a PWM-based position and edge counter implementation for the AS5311 sensor. [1] [2] [3]
  • Removed other related example files: ESP32/AS5311-AB/AS5311-AB.ino (rotary encoder queue example), ESP32/AS5311-Forum/AS5311-Forum.ino (forum demo implementation), and ESP32/AS5311-PWM-LIB-QUEUE/AS5311-PWM-LIB-QUEUE.ino (PWM/edge counter queue example). [1] [2] [3]

These removals help declutter the codebase and focus development on the new QID tracking feature.

Add QID-based done/busy tracking documentation and include a prebuilt CAN-slave firmware binary. Update Python client to support QID-driven completion: ledmatrix expands allowed statuses ("on","off"), motor.move_stepper uses firmware QID mode to set nResponses=1 when enabled, and serial layer (mserial) adds use_qid_done, per-qid threading Events, incoming-state handling, and waiting/timeout logic for QID notifications. Files changed: DOCUMENTATION/QID_TRACKING_DOCUMENTATION.md (new), binaries/latest/esp32_seeed_xiao_esp32s3_can_slave_motor_merged.bin (new), uc2rest/ledmatrix.py, uc2rest/motor.py, uc2rest/mserial.py.
Replace the legacy binary CAN OTA streaming protocol with a JSON-driven CANopen OTA flow that mirrors tools/ota/canopen_ota_serial.py. The new implementation sends an /ota_start JSON preamble, streams raw firmware in CHUNK_SIZE (4096 B) blocks at an ACK-paced baud (default 921600), uses CRC32 for integrity, and waits for ota_status transitions (ready -> success/error). Serial handling and parent-serial restoration were improved, and many old binary-packet helpers were removed in favor of JSON/line-parsing helpers (_wait_for_status, _drain_acks, _try_extract_json_status). Also updates binaries/latest: adds UC2_canopen_master.bin and UC2_canopen_slave_led.bin, and removes several outdated .bin files.
Add get_can_ids() to return CAN node IDs from the latest scan (useful for OTA flashing). Expand and clarify reboot_remote() docstring to document can_address semantics (0 = master, 1..127 = remote slave via SDO write), note qid is unused but kept for API compatibility, and show example response formats. Small cleanup: add an inline comment for the /can_act task and change the scan call to expect 1 response (nResponses=1).
Copilot AI review requested due to automatic review settings May 26, 2026 08:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new optional “QID-based done/busy tracking” mode in the Python serial client (to block on a firmware-emitted {"qid":X,"state":"done"} event instead of counting responses), adjusts a few API call sites to work with that mode, and removes a large set of legacy ESP32/Matlab examples and old firmware sources. It also adds a dedicated markdown document describing the QID tracking and pause/resume behavior and protocol implications.

Changes:

  • Add event-based QID completion waiting in uc2rest/mserial.py and adapt motor blocking behavior to not depend on response counts when enabled.
  • Expand LED matrix status API validation and adjust CAN scan response counting.
  • Remove legacy ESP32 example/firmware files and Matlab helpers; add DOCUMENTATION/QID_TRACKING_DOCUMENTATION.md.

Reviewed changes

Copilot reviewed 70 out of 77 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
uc2rest/mserial.py Adds QID-done mode and event-based blocking for command completion.
uc2rest/motor.py Uses nResponses=1 when QID-done mode is enabled.
uc2rest/ledmatrix.py Accepts additional LED “status” values (on/off).
uc2rest/can.py Adds helper to list CAN IDs; tweaks scan response handling and docs.
MATLAB/TEST_UC2_REST.m Removed legacy Matlab test script.
MATLAB/README.md Removed legacy Matlab README.
MATLAB/OLD/sendMotorAction.m Removed legacy Matlab helper.
MATLAB/OLD/sendLEDArrayAction.m Removed legacy Matlab helper.
MATLAB/OLD/sendJSONMessage.m Removed legacy Matlab helper.
MATLAB/OLD/main.m Removed legacy Matlab example.
MATLAB/mSerial.m Removed legacy Matlab serial wrapper.
MATLAB/motor.m Removed legacy Matlab motor wrapper.
ESP32/main/wifi.ino Removed legacy ESP32 source file.
ESP32/main/read_sensor.ino Removed legacy ESP32 sensor endpoint implementation.
ESP32/main/pindefUC2Standalone.h Removed legacy standalone pin definition header (contained hardcoded credentials).
ESP32/main/pindef.h Removed legacy pin definition header (contained hardcoded credentials).
ESP32/main/pindef_WEMOS_d1_r32.h Removed legacy board pin map header.
ESP32/main/parameters_wifi.h Removed legacy WiFi/OTA/swagger embedded assets header.
ESP32/main/parameters_state.h Removed legacy state/endpoints header.
ESP32/main/parameters_slm.h Removed legacy SLM parameter header.
ESP32/main/parameters_scanner.h Removed legacy scanner parameter header.
ESP32/main/parameters_ps.h Removed legacy gamepad parameter header.
ESP32/main/parameters_PID.h Removed legacy PID parameter header.
ESP32/main/parameters_motor.h Removed legacy motor parameter header.
ESP32/main/parameters_ledarr.h Removed legacy LED array patterns header.
ESP32/main/parameters_laser.h Removed legacy laser parameter header.
ESP32/main/parameters_digital.h Removed legacy digital parameter header.
ESP32/main/parameters_dac.h Removed legacy DAC parameter header.
ESP32/main/parameters_config.h Removed legacy config/preferences keys header.
ESP32/main/parameters_analog.h Removed legacy analog/PWM parameter header.
ESP32/main/main.ino Removed legacy ESP32 “main” firmware sketch.
ESP32/main/docs.cpp Removed legacy protocol documentation source.
ESP32/main/control_wifi.ino Removed legacy WiFi/webserver/SPIFFS routing code.
ESP32/main/control_state.ino Removed legacy state controller code.
ESP32/main/control_slm.ino Removed legacy SLM controller code.
ESP32/main/control_scanner.ino Removed legacy scanner controller code.
ESP32/main/control_PS4.ino Removed legacy PS4 controller integration.
ESP32/main/control_PS3.ino Removed legacy PS3 controller integration.
ESP32/main/control_PID.ino Removed legacy PID controller code.
ESP32/main/control_motor.ino Removed legacy motor controller code.
ESP32/main/control_LEDarray.ino Removed legacy LED array controller code.
ESP32/main/control_laser.ino Removed legacy laser controller code.
ESP32/main/control_dummy.ino Removed legacy dummy controller code.
ESP32/main/control_digitalout.ino Removed legacy digital-out controller code.
ESP32/main/control_DAC.ino Removed legacy DAC controller code.
ESP32/main/control_config.ino Removed legacy config controller code.
ESP32/main/control_analog.ino Removed legacy analog controller code.
ESP32/ds1822-temperaturecontrol/ds1822-temperaturecontrol.ino Removed DS1822 temperature control example.
ESP32/digital-calliper/esp32-digital-calliper/esp32-digital-calliper.ino Removed digital caliper example.
ESP32/digital-calliper/esp32-digital-caliper-WithInterrupts/esp32-digital-caliper-WithInterrupts.ino Removed interrupt-based digital caliper example.
ESP32/AS5311/AS5311.ino Removed AS5311 sensor example.
ESP32/AS5311/AS5311_BD.h Removed AS5311 helper header.
ESP32/AS5311/AS5311_BD.cpp Removed AS5311 helper implementation.
ESP32/AS5311-PWM/AS5311-PWM.ino Removed AS5311 PWM capture example.
ESP32/AS5311-PWM-LIB/AS5311.h Removed AS5311 PWM “LIB” header.
ESP32/AS5311-PWM-LIB/AS5311.cpp Removed AS5311 PWM “LIB” implementation.
ESP32/AS5311-PWM-LIB/AS5311-PWM-LIB.ino Removed AS5311 PWM “LIB” example sketch.
ESP32/AS5311-PWM-LIB-QUEUE/AS5311.h Removed AS5311 queue-based PWM “LIB” header.
ESP32/AS5311-PWM-LIB-QUEUE/AS5311.cpp Removed AS5311 queue-based PWM “LIB” implementation.
ESP32/AS5311-PWM-LIB-QUEUE/AS5311-PWM-LIB-QUEUE.ino Removed AS5311 queue-based PWM example sketch.
ESP32/AS5311-Forum/AS5311-Forum.ino Removed AS5311 forum demo sketch.
ESP32/AS5311-AB/AS5311-AB.ino Removed rotary encoder queue example.
ESP32/AS5311-AB-LIB/AS5311.h Removed rotary encoder “LIB” header.
ESP32/AS5311-AB-LIB/AS5311.cpp Removed rotary encoder “LIB” implementation.
ESP32/AS5311-AB-LIB/AS5311-AB-LIB.ino Removed rotary encoder “LIB” example sketch.
ESP32/AS5311-AB-LIB-QUEUE/AS5311AB.h Removed rotary encoder queue-based “LIB” header.
ESP32/AS5311-AB-LIB-QUEUE/AS5311AB.cpp Removed rotary encoder queue-based “LIB” implementation.
ESP32/AS5311-AB-LIB-QUEUE/AS5311-AB-LIB-QUEUE.ino Removed rotary encoder queue-based example sketch.
DOCUMENTATION/QID_TRACKING_DOCUMENTATION.md Adds QID tracking + pause/resume documentation and testing guidance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread uc2rest/mserial.py
Comment on lines +481 to +486
# QID-done mode: wait for {"qid":X,"state":"done"} from firmware
if self.use_qid_done and nResponses > 0:
event = threading.Event()
self.qid_done_events[identifier] = event
self.qid_done_responses.pop(identifier, None)

Comment thread uc2rest/mserial.py
Comment on lines +374 to +381
# Check for QID done/state notification from firmware
if self.use_qid_done and "state" in json_response:
state_val = json_response["state"]
resp_qid = json_response.get("qid", -1)
if state_val in ("done", "error", "timeout", "paused") and resp_qid in self.qid_done_events:
self.qid_done_responses[resp_qid] = json_response.copy()
self.qid_done_events[resp_qid].set()

Comment thread uc2rest/mserial.py
Comment on lines +496 to +503
else:
# Timeout
self.qid_done_events.pop(identifier, None)
self.qid_done_responses.pop(identifier, None)
if self.DEBUG:
self._logger.debug("QID done timeout for qid: %s", identifier)
return "qid_done timeout: " + str(identifier)

Comment thread uc2rest/ledmatrix.py
Comment on lines 307 to 313
def send_LEDMatrix_status(self, status="idle"):
"""
Set the status of the LED matrix to "idle" or "busy".
JSON: "action":"status", "status": <string>
"""
if status not in ["error", "idle", "warn", "success", "busy", "rainbow"]:
if status not in ["error", "idle", "warn", "success", "busy", "rainbow", "on", "off"]:
status = "idle"
Comment on lines +136 to +149
### Pause/Resume from Python

```python
# Start a long movement
uc2.serial.use_qid_done = True
# (in a thread or non-blocking)
uc2.motor.move_stepper(steps=(100000, 0, 0), speed=(5000, 0, 0))

# Pause mid-motion
uc2.serial.post_json("/qid_pause", {"qid": last_qid})

# Resume later
uc2.serial.post_json("/qid_resume", {"qid": last_qid})
```
Comment thread uc2rest/mserial.py
Comment on lines +374 to +381
# Check for QID done/state notification from firmware
if self.use_qid_done and "state" in json_response:
state_val = json_response["state"]
resp_qid = json_response.get("qid", -1)
if state_val in ("done", "error", "timeout", "paused") and resp_qid in self.qid_done_events:
self.qid_done_responses[resp_qid] = json_response.copy()
self.qid_done_events[resp_qid].set()

@beniroquai beniroquai merged commit 5d47002 into master May 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants