diff --git a/pins.drawio b/pins.drawio index 6126078..50f6d39 100644 --- a/pins.drawio +++ b/pins.drawio @@ -1,6 +1,6 @@ - + @@ -1122,7 +1122,7 @@ - + @@ -1136,7 +1136,7 @@ - + diff --git a/servo_controller/COMMANDS.md b/servo_controller/COMMANDS.md new file mode 100644 index 0000000..6ea3176 --- /dev/null +++ b/servo_controller/COMMANDS.md @@ -0,0 +1,88 @@ +# USB Command Handler + +Implementation of command handler for this project. + + +### ACK + +Every command returns either an ACK or NACK. + +### Commands +```c +typedef enum { + COMMAND_ACK, + COMMAND_NACK, + LED_TOGGLE, + LED_SET, + SERVO_SET, + SERVO_SET_ALL, + ADC_READ_RAW, + ADC_READ_RAW_ALL, + ADC_READ, + ADC_READ_ALL, + ADC_SET_READ, + DIGITAL_IN, + DIGITAL_LEG_OUT, + POWER_MONITOR_SET_READ, + POWER_MONITOR_READ, + SETTINGS_TEST_SAVE, + SETTINGS_TEST_READ, + + MOVE_COMMAND_START, + // All the commands going to robot thread go here + TEST_COMMAND, // 16 + + MOVE_COMMAND_END + +} commands_e; +``` + +### Command Structure + +```c +#define COMMAND_PREFIX 0x69 +#define COMMAND_ID 0x00 +#define COMMAND_DATA_SIZE 160 +#define COMMAND_HEADER_SIZE 13 + +struct command_message_t { + uint8_t prefix; + uint8_t length; + uint8_t id; + uint8_t command; + int64_t tick; + uint8_t crc; + uint8_t data[COMMAND_DATA_SIZE]; +} __attribute__((packed)); +``` + +### CRC Calculation Function + +```c +uint8_t command_calculate_crc(struct command_message_t *msg) { + uint32_t sum = 0; + uint8_t crc = 0; + + uint8_t *byte_ptr = (uint8_t *)msg; + int loop_length = (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length); + + for (int i = 0; i < loop_length; i++) { + if (i == (COMMAND_HEADER_SIZE - 1)) { continue; } + sum += byte_ptr[i]; + } + + crc = 0x100 - (sum & 0xff); + + return crc; +} +``` + +### Python test script (AI generated) + +**Command Usage:** + +```bash +python3 scripts/send_cmd.py --help +``` + +This script sends/reads commands \ No newline at end of file diff --git a/servo_controller/README.md b/servo_controller/README.md index 3891cc0..5716ec7 100644 --- a/servo_controller/README.md +++ b/servo_controller/README.md @@ -1,56 +1,42 @@ -# Zephyr USB Command Handler +# Servo controller for Servo2350 board -This repository contains the implementation of a USB command handler for the Zephyr RTOS. The system allows devices to communicate via USB using custom command messages. - -### Components - -1. **command_handler.c/h**: Handles incoming commands, processes them, and performs actions based on the command type. -2. **command_message.c/h**: Manages the creation, validation, and logging of command messages, including CRC calculation. -3. **usb.c/h**: Manages USB initialization, data reception via UART, and sending responses. Includes a USB thread. -4. **usb_conf.c/h**: Configures the USB device. - -### ACK - -Every command returns either an ACK or NACK. - -### Command Structure +A zephyr app for a hexapod project. ```c -struct command_message_t { - uint8_t prefix; // 0x69 - uint8_t length; - uint8_t id; - uint8_t command; - uint8_t crc; - uint8_t data[160]; -} __attribute__((packed)); +/* + + (1) (6) + \ / + \ / + \ / + *---(F)---* + / \ + / \ + / \ +(2)---* o *---(5) + \ / + \ / + \ / + *---(B)---* + / \ + / \ + / \ + (3) (4) + +*/ ``` -### CRC Calculation Function +## Project overview +From [../pins.drawio](../pins.drawio) +![image](project_overview.png) -```c -uint8_t calculate_crc(struct command_message_t *msg) { - uint32_t sum = 0; - uint8_t crc = 0; +## Hardware +This project uses custom open source RP2350B board developed in [Servo2350](https://git.aintmina.org/aintgit/servo2350). - uint8_t *byte_ptr = (uint8_t *)msg; +Schematics can be found in [boards/arm/servo2350/docs/Servo2350.pdf](boards/arm/servo2350/docs/Servo2350.pdf). - for (int i = 0; i < (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length) - 1; i++) { - sum += byte_ptr[i]; - } +## Zephyr environment setup +See [SETUP.md](SETUP.md) - crc = 0x100 - (sum & 0xff); - - return crc; -} -``` - -### Python test script (AI generated) - -**Command Usage:** - -```bash -python3 scripts/led_blink.py -``` - -This script sends LED toggle commands to the device connected via USB. \ No newline at end of file +## Communication +See [COMMANDS.md](COMMANDS.md) \ No newline at end of file diff --git a/servo_controller/SETUP.md b/servo_controller/SETUP.md new file mode 100644 index 0000000..c19cabb --- /dev/null +++ b/servo_controller/SETUP.md @@ -0,0 +1,95 @@ +# Installation Guide + +## Prerequisites + +Install required tools: + +```bash +python -m venv venv +source venv/bin/activate +pip install west pyserial pyocd +``` + +## Project Structure + +``` +zephyr/ +├── venv/ # Python virtual environment +├── sdk/zephyr/ # Zephyr source code +├── zephyr-sdk-1.0.1/ # Zephyr SDK installation +├── project/ # Git root +| └── servo_controller # Zephyr project +└── env.sh # Environment setup script +``` + +## Setup + +### 1. Clone Zephyr SDK + +```bash +# in zephyr/ +mkdir sdk +cd sdk +git clone https://github.com/zephyrproject-rtos/zephyr.git +``` + +### 2. Install Zephyr SDK + +```bash +# in zephyr/ +wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v1.0.1/zephyr-sdk-1.0.1_linux-x86_64_gnu.tar.xz +tar xf zephyr-sdk-1.0.1_linux-x86_64_gnu.tar.xz +``` + +### 3. Initialize West + +```bash +# in zephyr/sdk/zephyr +west init -l . +west update +``` + +### 4. Export a Zephyr CMake package. + +```bash +# in zephyr/sdk/zephyr +west zephyr-export +``` + +### 5. Install Python dependencies using west packages. + +```bash +# in zephyr/sdk/zephyr +west packages pip --install +``` + +### 6. Create env.sh + +Create `zephyr/env.sh` with the following content: + +```bash +#!/bin/bash + +SCRIPT_DIR=$(dirname "$(readlink -f "$0")") + +export ZEPHYR_BASE="${SCRIPT_DIR}/sdk/zephyr" +echo "export ZEPHYR_BASE=\"${SCRIPT_DIR}/sdk/zephyr\"" + +export ZEPHYR_SDK_INSTALL_DIR="${SCRIPT_DIR}/zephyr-sdk-1.0.1" +echo "export ZEPHYR_SDK_INSTALL_DIR=\"${SCRIPT_DIR}/zephyr-sdk-1.0.1\"" + +source "${SCRIPT_DIR}/venv/bin/activate" +echo "source \"${SCRIPT_DIR}/venv/bin/activate\"" +``` + +### 7. Build the zephyr app + +```bash +cd /servo_controller + +# Source the environment +source ../../zephyr/env.sh + +# Build the app +west build -p -b servo2350/rp2350b/m33 +``` \ No newline at end of file diff --git a/servo_controller/project_overview.png b/servo_controller/project_overview.png new file mode 100644 index 0000000..f89e115 Binary files /dev/null and b/servo_controller/project_overview.png differ diff --git a/servo_controller/scripts/PYTHONS.md b/servo_controller/scripts/PYTHONS.md new file mode 100644 index 0000000..c625a2d --- /dev/null +++ b/servo_controller/scripts/PYTHONS.md @@ -0,0 +1,4 @@ +# Python codes + +> [!NOTE] +> These python test scripts are all vibe coded to save time. \ No newline at end of file