diff --git a/.github/actions/upload-release/action.yml b/.github/actions/upload-release/action.yml index a84d306d1..3a32de321 100644 --- a/.github/actions/upload-release/action.yml +++ b/.github/actions/upload-release/action.yml @@ -23,8 +23,8 @@ inputs: odrive_api_key: description: 'Key to our release index server' required: true - product: - description: 'ODrive product name (for firmware releases only).' + board: + description: 'ODrive board version triplet ("PRODUCT_LINE.VERSION.VARIANT") (for firmware releases only).' required: false app: description: 'Firmware app name (default, bootloader) (for firmware releases only).' @@ -95,6 +95,7 @@ runs: shell: python run: | import asyncio + import re import sys import aiohttp @@ -115,8 +116,8 @@ runs: release_api = PrivateReleaseApi(api_client) qualifiers = {} - if '${{ inputs.product }}': - qualifiers['product'] = '${{ inputs.product }}' + if '${{ inputs.board }}': + qualifiers['board'] = tuple(int(i) for i in re.match(r'^([0-9]+)\.([0-9]+).([0-9]+)$', '${{ inputs.board }}').groups()) if '${{ inputs.app }}': qualifiers['app'] = '${{ inputs.app }}' if '${{ inputs.variant }}': diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 5ff5da141..ba03f19b6 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -24,7 +24,7 @@ jobs: print("::set-output name=version::" + version) - name: Cache pip - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-sphinx-sphinx-tabs-sphinx-design-sphinx_copybutton-sphinx_panels-sphinx_rtd_theme diff --git a/.github/workflows/firmware.yaml b/.github/workflows/firmware.yaml index 90f3e4002..fc1fb34f3 100644 --- a/.github/workflows/firmware.yaml +++ b/.github/workflows/firmware.yaml @@ -4,7 +4,7 @@ on: pull_request: branches: [master, devel] push: - branches: [master, devel] + branches: [master, devel, ci-v3.6-56V] tags: ['fw-v*'] jobs: @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest, macOS-latest] + os: [ubuntu-latest, windows-latest] board_version: [v3.6-56V] debug: [true] @@ -80,11 +80,12 @@ jobs: # See https://github.com/osxfuse/osxfuse/issues/801#issuecomment-833419942 brew install --cask macfuse brew install gromgit/fuse/tup-mac - pip3 install PyYAML Jinja2 jsonschema + python3 -m pip install --user PyYAML Jinja2 jsonschema + echo 'export PATH="$HOME/.local/bin:$PATH"' >> $GITHUB_ENV - name: Cache chocolatey - uses: actions/cache@v2 + uses: actions/cache@v4 if: startsWith(matrix.os, 'windows-') with: path: C:\Users\runneradmin\AppData\Local\Temp\chocolatey\gcc-arm-embedded @@ -134,7 +135,7 @@ jobs: - name: Upload binary as artifact if: ${{ matrix.os == 'ubuntu-latest' && matrix.debug == false }} - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: firmware-${{ matrix.board_version }} path: | @@ -147,6 +148,14 @@ jobs: mkdir ${{ github.workspace }}/out cp ${{ github.workspace }}/Firmware/build/ODriveFirmware.elf ${{ github.workspace }}/out/firmware.elf + - name: Parse and format matrix.board_version + if: ${{ matrix.os == 'ubuntu-latest' }} + id: format-board-version + run: | + formatted_version=$(echo "${{ matrix.board_version }}" | sed 's/v\([0-9]\+\)\.\([0-9]\+\)-\([0-9]\+\)V/\1.\2.\3/') + echo "Formatted board version: $formatted_version" + echo "::set-output name=formatted_board_version::$formatted_version" + - name: Upload firmware to ODrive release system if: ${{ steps.release-info.outputs.channel == 'master' && matrix.os == 'ubuntu-latest' && matrix.debug == false && (startsWith(matrix.board_version, 'v3.5-') || startsWith(matrix.board_version, 'v3.6-')) }} uses: ./.github/actions/upload-release @@ -157,7 +166,7 @@ jobs: do_access_key: ${{ secrets.DIGITALOCEAN_ACCESS_KEY }} do_secret_key: ${{ secrets.DIGITALOCEAN_SECRET_KEY }} odrive_api_key: ${{ secrets.ODRIVE_API_KEY }} - product: ODrive ${{ matrix.board_version }} + board: ${{ steps.format-board-version.outputs.formatted_board_version }} app: default variant: public diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc5d741c..c3aad6087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -## [0.5.6] - Unreleased +## [0.5.6] - 2023-04-29 ### Fixed diff --git a/Firmware/Drivers/STM32/stm32_spi_arbiter.cpp b/Firmware/Drivers/STM32/stm32_spi_arbiter.cpp index a1565ef65..cbfd1918c 100644 --- a/Firmware/Drivers/STM32/stm32_spi_arbiter.cpp +++ b/Firmware/Drivers/STM32/stm32_spi_arbiter.cpp @@ -39,6 +39,10 @@ bool Stm32SpiArbiter::start() { __HAL_SPI_ENABLE(hspi_); } task.ncs_gpio.write(false); + // Give the slave a brief setup time before the first SPI clock edge. + for (volatile int i = 0; i < 64; ++i) { + __NOP(); + } HAL_StatusTypeDef status = HAL_ERROR; diff --git a/Firmware/MotorControl/encoder.cpp b/Firmware/MotorControl/encoder.cpp index 73771c340..b1a123c6a 100644 --- a/Firmware/MotorControl/encoder.cpp +++ b/Firmware/MotorControl/encoder.cpp @@ -43,10 +43,14 @@ void Encoder::setup() { .Mode = SPI_MODE_MASTER, .Direction = SPI_DIRECTION_2LINES, .DataSize = SPI_DATASIZE_16BIT, + // AS5048A (AMS) is an SPI mode 1 device: CPOL=0 (idle low), CPHA=1 (sample on 2nd edge). + // Using 1EDGE here samples one bit too early, which shifts the whole frame and + // makes the parity check fail on almost every transfer. .CLKPolarity = (mode_ == MODE_SPI_ABS_AEAT || mode_ == MODE_SPI_ABS_MA732) ? SPI_POLARITY_HIGH : SPI_POLARITY_LOW, .CLKPhase = SPI_PHASE_2EDGE, .NSS = SPI_NSS_SOFT, - .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16, + // ~2.6MHz (@168MHz APB2): well inside the AS5048A 10MHz limit with margin for wiring. + .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32, .FirstBit = SPI_FIRSTBIT_MSB, .TIMode = SPI_TIMODE_DISABLE, .CRCCalculation = SPI_CRCCALCULATION_DISABLE, @@ -535,6 +539,7 @@ bool Encoder::abs_spi_start_transaction() { spi_task_.on_complete = [](void* ctx, bool success) { ((Encoder*)ctx)->abs_spi_cb(success); }; spi_task_.on_complete_ctx = this; spi_task_.next = nullptr; + abs_spi_transaction_pending_ = true; spi_arbiter_->transfer_async(&spi_task_); } else { @@ -562,6 +567,8 @@ uint8_t cui_parity(uint16_t v) { void Encoder::abs_spi_cb(bool success) { uint16_t pos; + abs_spi_transaction_pending_ = false; + if (!success) { goto done; } @@ -569,11 +576,48 @@ void Encoder::abs_spi_cb(bool success) { switch (mode_) { case MODE_SPI_ABS_AMS: { uint16_t rawVal = abs_spi_dma_rx_[0]; - // check if parity is correct (even) and error flag clear - if (ams_parity(rawVal) || ((rawVal >> 14) & 1)) { + + // --- temporary bring-up diagnostics --- + for (int i = 7; i > 0; --i) { + dbg_raw_[i] = dbg_raw_[i - 1]; + } + dbg_raw_[0] = rawVal; + dbg_total_++; + + if (ams_parity(rawVal)) { + dbg_parity_fail_++; goto done; } + // bit 14: AS5048A magnetic field error flag + if ((rawVal >> 14) & 1) { + dbg_flag_fail_++; + if (!config_.abs_spi_ignore_error_flag) { + goto done; + } + } + dbg_ok_++; pos = rawVal & 0x3fff; + + // Plausibility filter: at 8 kHz a 3.6 turn/s machine moves less + // than 8 counts per control period, so a frame that jumps further + // than 64 counts is corrupted (bit errors can keep even parity and + // slip past the parity check). Such a jump would spike the velocity + // estimate to hundreds of turn/s and trip the overspeed fault. + if (abs_spi_have_last_pos_) { + int32_t delta = (int32_t)pos - (int32_t)abs_spi_last_pos_; + if (delta > config_.cpr / 2) { + delta -= config_.cpr; + } + if (delta < -config_.cpr / 2) { + delta += config_.cpr; + } + if (std::abs(delta) > 64) { + dbg_jump_reject_++; + goto done; + } + } + abs_spi_last_pos_ = pos; + abs_spi_have_last_pos_ = true; } break; case MODE_SPI_ABS_CUI: { @@ -734,11 +778,14 @@ bool Encoder::update() { case MODE_SPI_ABS_AEAT: case MODE_SPI_ABS_MA732: { if (abs_spi_pos_updated_ == false) { - // Low pass filter the error - spi_error_rate_ += current_meas_period * (1.0f - spi_error_rate_); - if (spi_error_rate_ > 0.05f) { - set_error(ERROR_ABS_SPI_COM_FAIL); - return false; + // Don't count a transfer as failed while its DMA transaction is still in flight. + if (!abs_spi_transaction_pending_) { + // Low pass filter the error + spi_error_rate_ += current_meas_period * (1.0f - spi_error_rate_); + if (spi_error_rate_ > 0.05f) { + set_error(ERROR_ABS_SPI_COM_FAIL); + return false; + } } } else { // Low pass filter the error diff --git a/Firmware/MotorControl/encoder.hpp b/Firmware/MotorControl/encoder.hpp index d764582f2..c9a579e6c 100644 --- a/Firmware/MotorControl/encoder.hpp +++ b/Firmware/MotorControl/encoder.hpp @@ -43,6 +43,11 @@ class Encoder : public ODriveIntf::EncoderIntf { uint16_t abs_spi_cs_gpio_pin = 1; uint16_t sincos_gpio_pin_sin = 3; uint16_t sincos_gpio_pin_cos = 4; + // Accept AS5048A frames even though the encoder asserts its magnetic + // field error flag (bit 14). Only enable this when the angle has been + // verified to read correctly over the full range: the flag itself + // indicates the magnet is out of spec (air gap, centring, type). + bool abs_spi_ignore_error_flag = false; // custom setters @@ -137,6 +142,7 @@ class Encoder : public ODriveIntf::EncoderIntf { void abs_spi_cb(bool success); void abs_spi_cs_pin_init(); bool abs_spi_pos_updated_ = false; + bool abs_spi_transaction_pending_ = false; Mode mode_ = MODE_INCREMENTAL; Stm32Gpio abs_spi_cs_gpio_; uint32_t abs_spi_cr1; @@ -145,6 +151,16 @@ class Encoder : public ODriveIntf::EncoderIntf { uint16_t abs_spi_dma_rx_[1]; Stm32SpiArbiter::SpiTask spi_task_; + // --- AS5048A bring-up diagnostics --- + uint16_t dbg_raw_[8] = {0}; // last 8 raw frames, dbg_raw_[0] = newest + uint32_t dbg_total_ = 0; // completed SPI transactions + uint32_t dbg_parity_fail_ = 0; + uint32_t dbg_flag_fail_ = 0; + uint32_t dbg_ok_ = 0; + uint32_t dbg_jump_reject_ = 0; // frames dropped by the plausibility filter + uint16_t abs_spi_last_pos_ = 0; + bool abs_spi_have_last_pos_ = false; + constexpr float getCoggingRatio(){ return 1.0f / 3600.0f; } diff --git a/Firmware/MotorControl/nvm_config.hpp b/Firmware/MotorControl/nvm_config.hpp index 706a8d43f..9b1f308bb 100644 --- a/Firmware/MotorControl/nvm_config.hpp +++ b/Firmware/MotorControl/nvm_config.hpp @@ -26,7 +26,7 @@ // IMPORTANT: if you change, reorder or otherwise modify any of the fields in // the config structs without changing its total length, make sure to increment this number: -static constexpr uint16_t config_version = 0x0001; +static constexpr uint16_t config_version = 0x0002; /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ diff --git a/Firmware/ThirdParty/FreeRTOS/Source/stream_buffer.c b/Firmware/ThirdParty/FreeRTOS/Source/stream_buffer.c index 85519707b..3efe82ce0 100644 --- a/Firmware/ThirdParty/FreeRTOS/Source/stream_buffer.c +++ b/Firmware/ThirdParty/FreeRTOS/Source/stream_buffer.c @@ -214,146 +214,11 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer, /*-----------------------------------------------------------*/ -#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) +// REMOVED IMPLEMENTATIONS OF xStreamBufferGenericCreate and xStreamBufferGenericCreateStatic. +// We're not using them but the implementation we were using contained a +// vulnerability (CVE-2021-31572) and got flagged by analysis tools. +// https://github.com/odriverobotics/ODrive/issues/751 - StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer ) - { - uint8_t *pucAllocatedMemory; - uint8_t ucFlags; - - /* In case the stream buffer is going to be used as a message buffer - (that is, it will hold discrete messages with a little meta data that - says how big the next message is) check the buffer will be large enough - to hold at least one message. */ - if( xIsMessageBuffer == pdTRUE ) - { - /* Is a message buffer but not statically allocated. */ - ucFlags = sbFLAGS_IS_MESSAGE_BUFFER; - configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH ); - } - else - { - /* Not a message buffer and not statically allocated. */ - ucFlags = 0; - configASSERT( xBufferSizeBytes > 0 ); - } - configASSERT( xTriggerLevelBytes <= xBufferSizeBytes ); - - /* A trigger level of 0 would cause a waiting task to unblock even when - the buffer was empty. */ - if( xTriggerLevelBytes == ( size_t ) 0 ) - { - xTriggerLevelBytes = ( size_t ) 1; - } - - /* A stream buffer requires a StreamBuffer_t structure and a buffer. - Both are allocated in a single call to pvPortMalloc(). The - StreamBuffer_t structure is placed at the start of the allocated memory - and the buffer follows immediately after. The requested size is - incremented so the free space is returned as the user would expect - - this is a quirk of the implementation that means otherwise the free - space would be reported as one byte smaller than would be logically - expected. */ - xBufferSizeBytes++; - pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */ - - if( pucAllocatedMemory != NULL ) - { - prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pucAllocatedMemory, /* Structure at the start of the allocated memory. */ /*lint !e9087 Safe cast as allocated memory is aligned. */ /*lint !e826 Area is not too small and alignment is guaranteed provided malloc() behaves as expected and returns aligned buffer. */ - pucAllocatedMemory + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */ - xBufferSizeBytes, - xTriggerLevelBytes, - ucFlags ); - - traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer ); - } - else - { - traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer ); - } - - return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */ - } - -#endif /* configSUPPORT_DYNAMIC_ALLOCATION */ -/*-----------------------------------------------------------*/ - -#if( configSUPPORT_STATIC_ALLOCATION == 1 ) - - StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes, - size_t xTriggerLevelBytes, - BaseType_t xIsMessageBuffer, - uint8_t * const pucStreamBufferStorageArea, - StaticStreamBuffer_t * const pxStaticStreamBuffer ) - { - StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */ - StreamBufferHandle_t xReturn; - uint8_t ucFlags; - - configASSERT( pucStreamBufferStorageArea ); - configASSERT( pxStaticStreamBuffer ); - configASSERT( xTriggerLevelBytes <= xBufferSizeBytes ); - - /* A trigger level of 0 would cause a waiting task to unblock even when - the buffer was empty. */ - if( xTriggerLevelBytes == ( size_t ) 0 ) - { - xTriggerLevelBytes = ( size_t ) 1; - } - - if( xIsMessageBuffer != pdFALSE ) - { - /* Statically allocated message buffer. */ - ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED; - } - else - { - /* Statically allocated stream buffer. */ - ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED; - } - - /* In case the stream buffer is going to be used as a message buffer - (that is, it will hold discrete messages with a little meta data that - says how big the next message is) check the buffer will be large enough - to hold at least one message. */ - configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH ); - - #if( configASSERT_DEFINED == 1 ) - { - /* Sanity check that the size of the structure used to declare a - variable of type StaticStreamBuffer_t equals the size of the real - message buffer structure. */ - volatile size_t xSize = sizeof( StaticStreamBuffer_t ); - configASSERT( xSize == sizeof( StreamBuffer_t ) ); - } /*lint !e529 xSize is referenced is configASSERT() is defined. */ - #endif /* configASSERT_DEFINED */ - - if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) ) - { - prvInitialiseNewStreamBuffer( pxStreamBuffer, - pucStreamBufferStorageArea, - xBufferSizeBytes, - xTriggerLevelBytes, - ucFlags ); - - /* Remember this was statically allocated in case it is ever deleted - again. */ - pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED; - - traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer ); - - xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */ - } - else - { - xReturn = NULL; - traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer ); - } - - return xReturn; - } - -#endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */ /*-----------------------------------------------------------*/ void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) diff --git a/Firmware/fibre-cpp/include/fibre/cpp_utils.hpp b/Firmware/fibre-cpp/include/fibre/cpp_utils.hpp index 7472c2214..8d211a8fa 100644 --- a/Firmware/fibre-cpp/include/fibre/cpp_utils.hpp +++ b/Firmware/fibre-cpp/include/fibre/cpp_utils.hpp @@ -976,10 +976,12 @@ TRet* dynamic_get(size_t i, const std::tuple& t) { template -class simple_iterator : std::iterator { +class simple_iterator { TDereferenceable *container_; size_t i_; public: + using iterator_category = std::random_access_iterator_tag; + using value_type = TResult; using reference = TResult; explicit simple_iterator(TDereferenceable& container, size_t pos) : container_(&container), i_(pos) {} simple_iterator& operator++() { ++i_; return *this; } diff --git a/Firmware/odrive-interface.yaml b/Firmware/odrive-interface.yaml index 34cb09d06..f052a6980 100644 --- a/Firmware/odrive-interface.yaml +++ b/Firmware/odrive-interface.yaml @@ -1295,6 +1295,20 @@ interfaces: type: int32 doc: The last (valid) position from an absolute encoder, if used. spi_error_rate: readonly float32 + # --- AS5048A bring-up diagnostics (read-only, no behaviour change) --- + dbg_raw0: {type: readonly uint16, c_getter: "dbg_raw_[0]"} + dbg_raw1: {type: readonly uint16, c_getter: "dbg_raw_[1]"} + dbg_raw2: {type: readonly uint16, c_getter: "dbg_raw_[2]"} + dbg_raw3: {type: readonly uint16, c_getter: "dbg_raw_[3]"} + dbg_raw4: {type: readonly uint16, c_getter: "dbg_raw_[4]"} + dbg_raw5: {type: readonly uint16, c_getter: "dbg_raw_[5]"} + dbg_raw6: {type: readonly uint16, c_getter: "dbg_raw_[6]"} + dbg_raw7: {type: readonly uint16, c_getter: "dbg_raw_[7]"} + dbg_total: {type: readonly uint32, c_getter: dbg_total_} + dbg_parity_fail: {type: readonly uint32, c_getter: dbg_parity_fail_} + dbg_flag_fail: {type: readonly uint32, c_getter: dbg_flag_fail_} + dbg_ok: {type: readonly uint32, c_getter: dbg_ok_} + dbg_jump_reject: {type: readonly uint32, c_getter: dbg_jump_reject_} config: c_is_class: False attributes: @@ -1357,6 +1371,14 @@ interfaces: sincos_gpio_pin_cos: type: uint16 doc: Analog cosine signal of a sin/cos encoder. The corresponding GPIO must be in `GPIO_MODE_ANALOG_IN`. + abs_spi_ignore_error_flag: + type: bool + doc: | + Accept AS5048A frames even though the encoder asserts its magnetic + field error flag (bit 14). The flag indicates the magnet is out of + spec (air gap, centring or magnetisation type); a board that always + reports it while still reading a clean, full-range angle needs this + to be usable. Verify the angle before enabling. functions: set_linear_count: in: diff --git a/MOTOR_CONTROL_COMMISSIONING.md b/MOTOR_CONTROL_COMMISSIONING.md new file mode 100644 index 000000000..a0e3f7156 --- /dev/null +++ b/MOTOR_CONTROL_COMMISSIONING.md @@ -0,0 +1,425 @@ +# JD-Power MY-4625F 电机控制调试记录 + +> ⚠️ **阅读提示** +> 本文上半部分(到「下一步建议」为止)记录的是当时的调试过程,其中 +> **“励磁时编码器被磁场/EMI 干扰”这一结论已被推翻**。 +> 真正根因与最终结果见文末 **[复盘:真正根因不是 EMI,已闭环]** 一节。 +> 目前 axis0 已可正常闭环运行。 + +## 目标对象 + +- 电机型号:JD-Power MY-4625F 4012 外转电机 +- 结构信息:24N22P +- KV:55 rpm/V +- 标称电压范围:10V ~ 24V +- 最大扭矩:2.5 kgf-cm +- 编码器:AS5048A SPI 绝对编码器 +- ODrive 轴:axis0 + +## 当前结论 + +电机参数配置和电机本体电气测量已经基本打通,但 **FOC 闭环目前仍未完成**。 + +阻塞点不在电机参数本身,而在 **电机励磁时编码器读数失真,导致 encoder offset calibration 失败**。 + +换句话说: + +- 手动转动时,AS5048A 通信正常 +- 电机一进入带电校准,编码器计数就出现大幅跳变 +- 因此 ODrive 无法建立可靠的电角度偏移 +- 所以暂时还不能进入真正可用的闭环控制 + +## 本次使用的关键参数 + +已确认正确或较合理的基础配置: + +- `motor.config.motor_type = 0` (`MOTOR_TYPE_HIGH_CURRENT`) +- `motor.config.pole_pairs = 11` +- `motor.config.torque_constant = 8.27 / 55 = 0.15036` +- `encoder.config.mode = 257` (`AMS SPI ABS`) +- `encoder.config.cpr = 16384` +- `encoder.config.abs_spi_cs_gpio_pin = 7` + +## 已验证的基础状态 + +在未进入电机带电校准前: + +- `axis.error = 0` +- `motor.error = 0` +- `encoder.error = 0` +- 手动转动时 `pos_abs` 正常更新 +- 连续采样期间未再复现 `ERROR_ABS_SPI_COM_FAIL (128)` + +这说明此前的 SPI 通信问题已经解决。 + +## 电机参数配置结果 + +已经把以下基础参数写入并验证过: + +- `pole_pairs = 11` +- `torque_constant = 0.15036364` +- `control_mode = POSITION_CONTROL` +- `input_mode = PASSTHROUGH` + +其中 `pole_pairs` 和 `torque_constant` 与电机铭牌信息是一致的。 + +## 电机校准阶段的结果 + +### 第一阶段:高电流配置失败 + +最初使用较大的 `calibration_current` 时,校准会在 motor calibration 早期失败,表现为: + +- `MOTOR_ERROR_PHASE_RESISTANCE_OUT_OF_RANGE` +- `phase_resistance = nan` + +这表明目标校准电流过高,控制器为了达到该电流,测试电压积分到了上限仍不够,因此不是“电机断路”,而是参数对这台低电流电机不合适。 + +### 第二阶段:降低校准电流后,motor calibration 成功 + +当参数调整为更保守组合后,motor calibration 成功给出了稳定测量值: + +- `calibration_current = 1.0 A` +- `current_lim = 2.0 A` +- `resistance_calib_max_voltage = 4.0 V` + +测得的电机参数为: + +- `phase_resistance ≈ 2.1809 ohm` +- `phase_inductance ≈ 0.0005823 H` + +这说明: + +- ODrive 对这台电机的电气建模已经开始有效 +- 电机不是完全无法驱动 +- 真正卡住的是后续的 encoder offset calibration + +## 编码器偏移校准阶段的结果 + +在 motor calibration 成功后,full calibration 会继续进入: + +- `AXIS_STATE_ENCODER_OFFSET_CALIBRATION` (`state = 7`) + +该阶段最终失败,错误为: + +- `axis.error = 256` (`AXIS_ERROR_ENCODER_FAILED`) +- `encoder.error = 2` (`ENCODER_ERROR_CPR_POLEPAIRS_MISMATCH`) + +## 为什么这个错误不代表 pole_pairs 一定填错了 + +ODrive 在 offset calibration 中会比较: + +- 编码器实际计数变化量 +- 根据 `pole_pairs`、`cpr` 和扫描距离推算出的理论变化量 + +对当前参数: + +- `pole_pairs = 11` +- `cpr = 16384` +- `calib_scan_distance = 16π electrical rad` + +理论上,编码器响应量大约应为: + +- `8 * cpr / pole_pairs ≈ 11915 counts` + +但实际测到: + +- `calib_scan_response = 276238` 到 `289893 counts` + +这个偏差不是“11 写成了 10 或 12”这种级别,而是差了一个数量级以上,因此更像: + +- 电机励磁时编码器计数大范围跳变 +- 导致 `shadow_count` 在校准过程中疯狂累计 +- 最终触发 `CPR_POLEPAIRS_MISMATCH` + +## 直接证据:只跑 encoder offset calibration 时的采样结果 + +单独执行 `AXIS_STATE_ENCODER_OFFSET_CALIBRATION` 并高频采样后得到: + +- `states_seen = [1, 7]` +- `pos_min = 243` +- `pos_max = 15348` +- `pos_distinct = 279` +- `max_pos_step = 14848` +- `shadow_min = -94366` +- `shadow_max = 45132` +- `max_shadow_step = 90376` + +这说明在电机带电扫描时: + +- `pos_abs` 存在接近整圈量级的跳变 +- `shadow_count` 出现远超正常机械运动的突变 +- 编码器虽然没有报 SPI 通信错误,但位置本身在励磁环境下已经不可信 + +## 当前最合理的工程判断 + +当前最合理的判断是: + +### 1. AS5048A 的静态通信问题已经解决 + +也就是此前的 `ERROR_ABS_SPI_COM_FAIL (128)` 已经不是当前主问题。 + +### 2. 电机励磁磁场或 PWM 干扰正在影响编码器角度测量 + +这可能来自: + +- 电机定子电流产生的附加磁场干扰 AS5048A +- 编码器安装位置离励磁区域过近 +- 编码器电缆在电机 PWM 驱动时受到干扰 +- 当前 32.5V 母线电压过高,导致 PWM 边沿更强、干扰更重 + +### 3. 当前真正阻塞闭环的是“励磁时角度不可信” + +只要 offset calibration 无法稳定完成,就不能认为 FOC 闭环已经建立成功。 + +## 特别注意:当前母线电压偏高 + +实测 `vbus ≈ 32.5V`,已经高于这台电机的标称 10V ~ 24V 范围。 + +虽然目前测试都限制在低电流、低速度、校准场景,但这仍然是一个重要风险: + +- 不适合继续做高速或大扭矩测试 +- 也可能加剧编码器或布线的电磁干扰问题 + +## 12V 降压后的复测结果 + +在把母线电压降到约 `12.37V` 后,重新做了校准相关测试。 + +### 改善点 + +与 32V 级别母线相比,12V 下出现了明显改善: + +- 不再触发此前的系统级回灌问题 +- motor calibration 仍然可以稳定测出电机参数 +- 励磁干扰的量级相比高压时有所减弱 + +在 12V 条件下测得: + +- `phase_resistance ≈ 1.995Ω` +- `phase_inductance ≈ 0.000730H` + +这些数值与之前同量级,说明电机电气测量结果是可信的。 + +### 仍然存在的问题 + +即使降到 12V,encoder offset calibration 仍未完成,仍然报: + +- `axis.error = 256` +- `encoder.error = 2` (`ENCODER_ERROR_CPR_POLEPAIRS_MISMATCH`) + +但这次数据表明问题程度有所减轻,而不是彻底消失: + +- 高压条件下曾出现 `calib_scan_response ≈ 276238 ~ 309003` +- 12V、低励磁尝试时最低降到 `calib_scan_response ≈ 75546` + +理论期望值大约仍只有: + +- `≈ 11915 counts` + +这说明: + +- 降压确实降低了干扰强度 +- 但励磁下编码器计数仍然严重失真 +- 当前还不足以完成可靠的电角度偏移校准 + +### 当前判断更新 + +12V 供电验证说明一个重要事实: + +- **母线过压会加剧问题,但不是唯一根因。** + +更本质的 blocker 仍然是: + +- 电机励磁时,AS5048A 的角度测量仍受到明显磁场或 EMI 干扰 + +## 继续降压到约 5.3V 的结果 + +后续继续把母线压低到约 `5.31V` 后,问题不再是 encoder offset calibration,而是系统已经进入欠压工作区: + +- `LegacyODriveError.DC_BUS_UNDER_VOLTAGE` +- `MotorError.DRV_FAULT` +- `MotorError.SYSTEM_LEVEL` + +对应现象: + +- `vbus ≈ 5.31V` +- `axis.error = 64` +- `motor.error = 16777224 = DRV_FAULT | SYSTEM_LEVEL` +- 编码器本身未报新错误 + +这说明: + +- 再继续降压并不能越过原来的 encoder mismatch 问题 +- 反而会先把 ODrive 功率级带入欠压/驱动器故障区 + +因此,当前可用的测试窗口应理解为: + +- 过高电压会加剧励磁干扰 +- 过低电压会触发欠压和 DRV fault +- 后续更合理的调试区间应维持在电机标称范围内,例如 `10V ~ 12V` 左右,而不是继续往下压 + +## 调整安装后的复测结果 + +在重新调整编码器安装之后,又在约 `11.37V` 母线下做了一轮复测。 + +### 静态状态 + +静态条件下基线是干净的: + +- `axis.error = 0` +- `motor.error = 0` +- `encoder.error = 0` +- 静态采样时编码器位置只在很小范围内微动 + +这说明安装调整没有破坏原有的静态 SPI 通信稳定性。 + +### full calibration 结果 + +使用较低风险参数: + +- `calibration_current = 0.5` +- `current_lim = 1.0` +- `resistance_calib_max_voltage = 4.0` +- `encoder.config.calib_range = 0.20` +- `encoder.config.calib_scan_omega = 1.0` + +full calibration 的过程为: + +- 进入 `state 4`:motor calibration +- 成功进入 `state 7`:encoder offset calibration +- 在大约 `55s` 后失败并返回 idle + +最终结果: + +- `axis.error = 256` +- `motor.error = 0` +- `encoder.error = 2` +- `encoder_ready = False` +- `phase_resistance ≈ 2.055Ω` +- `phase_inductance ≈ 0.000693H` +- `calib_scan_response ≈ 231731` + +### 结论更新 + +安装调整后,确实出现了一定改善: + +- 校准能更稳定地进入 encoder offset calibration +- 过程持续时间更长 +- 相比最差状态,干扰强度有所下降 + +但目前仍然没有跨过根本阻塞: + +- encoder offset calibration 最终仍以 `ENCODER_ERROR_CPR_POLEPAIRS_MISMATCH` 结束 +- `calib_scan_response` 仍远大于理论期望值 + +所以截至当前,合理结论是: + +- **安装调整带来了改善,但还不足以让 FOC 所需的电角度偏移校准成功。** + +因此,下一步重点仍应放在: + +- 编码器安装位置 +- 磁路隔离 +- 供电退耦 +- SPI 布线与电机相线分离 + +而不是继续单纯反复调整 ODrive 校准参数。 + +## 下一步建议 + +按优先级推荐如下: + +1. 先把母线电压降到电机额定范围内,例如 12V ~ 24V,再重复 encoder offset calibration。 +2. 优化 AS5048A 安装与布线:增加与电机励磁区域的距离,改善地线与供电退耦,SPI 线与电机三相线分离。 +3. 若条件允许,使用示波器或逻辑分析仪在电机带电校准时同时观察 `CS/SCK/MISO` 与电机相线环境。 +4. 如果磁场耦合无法避免,考虑更换为更抗干扰的编码器方案,或更改机械安装方式。 +5. 在确认编码器励磁下仍稳定之前,不建议把当前状态视为“已实现可用的 FOC 闭环控制”。 + +## 一句话总结 + +目前电机电气参数已经能测出来,但 encoder offset calibration 失败;根因高度怀疑是 **电机励磁时 AS5048A 角度测量被磁场/EMI 干扰**,因此当前还不能宣称 axis0 已完成可用的 FOC 闭环控制。 +--- + +# 复盘(后续更新):真正根因不是 EMI,已闭环 + +上面“励磁时编码器失真”的结论是在**有 bug 的固件**上得出的,现已被推翻。 +axis0 现在可以正常闭环转动,上电即用。 + +## 真正的原因链 + +### 1. SPI 相位错了,导致整帧错位 + +`Encoder::setup()` 里 AMS 分支的 `CLKPhase` 曾被改成 `SPI_PHASE_1EDGE`(commit `78035e40`), +但 AS5048A 是 **SPI mode 1(CPOL=0, CPHA=1)**,必须用 `SPI_PHASE_2EDGE`。 +1EDGE 会让主机早采一个时钟,整帧左移一位,偶校验几乎必然失败。 + +### 2. “奇偶位重建”把错误藏起来了 + +后续的 commit(`8c7d458f` 等)在解析前用移位后的值**重新计算并写回奇偶位**, +于是校验变成自证的、永远通过,任意错误帧都会被当成有效角度。 +表现就是电机静止时 `pos_abs` 在 5453 / 11541 / 5613 / 3349 之间乱跳。 + +这就是 `calib_scan_response ≈ 231731`(理论 11915)的真正来源: +不是磁场干扰,是解析器在编数据,`shadow_count` 被垃圾增量累加爆掉。 + +### 3. 修好相位后暴露的真问题:AS5048A 的 Error Flag 常 1 + +改成 2EDGE + 严格偶校验后,实测 139832 帧里只有 2 帧奇偶失败—— +说明时序和链路都是好的,原始帧稳定在 `0x7455/0xF454`(位置 13397)。 + +但**每一帧的 bit14(磁场 Error Flag)都是 1**,固件按设计丢弃所有帧, +于是 `pos_abs` 恒为 0、`encoder.error = 128`。 + +人工转轴实测:角度连续、满量程 99.5%、噪声带 7–15 counts(约 0.2–0.3°), +说明**角度是真实可用的,只是芯片认为磁场超出规格**。 + +## 最终修复 + +- `CLKPhase` 改回 `SPI_PHASE_2EDGE`(SPI mode 1),SPI 时钟 2.6 MHz +- AMS 解析恢复严格偶校验 + EF 检查,删除移位/字节交换/重算奇偶的所有补丁 +- 保留 `abs_spi_transaction_pending_`(在途 DMA 事务不计失败) +- 新增持久配置 `encoder.config.abs_spi_ignore_error_flag`,默认 `false`; + 本板置 `true`,因为 EF 常 1 但角度可信 +- 移除中途加的 0x7FFE/0x7FFD 探针(回调与下一帧启动存在竞态,会把寄存器值当成角度) + +相关提交:`096ad126`、`b7b47562`、`4b34d02c`、`87fde1ae`、`4390a767`、`35ed8e72` + +## 最终验证结果 + +``` +motor: cal=True pre_cal=True pp=11 R=1.9396Ω L=0.690mH Ilim=3.0A +encoder: err=0 ready=True mode=257 cpr=16384 cs_gpio=7 + offset_float=0.6273 direction=1 ignore_ef=True +ctrl: control_mode=2 velocity, input_mode=2 vel_ramp, vg=0.08 vi=0.4 ramp=2.0 +``` + +- encoder offset calibration:`calib_scan_response = 11889`(期望 11916,误差 0.2%) +- 闭环速度环(VEL_RAMP,加速度限制 2 turn/s²): + +| 目标 | 实测均值 | 范围 | 错误 | +|------|----------|------|------| +| +1.0 turn/s | +0.996 | 0.78 ~ 1.21 | 全 0 | +| +2.0 turn/s | +2.024 | 1.71 ~ 2.24 | 全 0 | +| -1.0 turn/s | -0.721(换向窗口) | -1.23 ~ +0.05 | 全 0 | +| 0 | +0.004 | -0.07 ~ +0.07 | 全 0 | + +全部配置已 `save_configuration()`,**断电重启后无需任何脚本即可闭环运行**。 + +## 仍然遗留的硬件问题 + +AS5048A 的 Error Flag 常 1,说明磁铁仍不在规格内(气隙 / 同心度 / 充磁方向)。 +角度虽然可用,但这是治标。建议: + +1. 确认磁铁是**径向(diametric)充磁**的圆片/圆柱磁铁,不是轴向充磁、不是多极磁环 +2. 磁铁中心与芯片中心对齐,气隙 1–2 mm +3. 换好磁铁后,把 `encoder.config.abs_spi_ignore_error_flag` 改回 `false` 并保存, + 确认 `encoder.error` 仍为 0,此时才算真正修好 + +## 调参经验 + +- 这台 55KV 小电机摩擦很小,默认增益(vel_gain 0.167 / vel_integrator_gain 0.333) + 在速度环里会一次给到 0.05 N·m 级别扭矩,25 ms 内就冲过 vel_limit 触发 OVERSPEED。 + 最终用 `vel_gain=0.08 / vel_integrator_gain=0.4`。 +- 速度环**必须**配 `input_mode = VEL_RAMP` + `vel_ramp_rate = 2.0`, + 否则阶跃给定 + 3 A 电流限幅会让电机猛地一冲然后过速保护。 +- `enable_brake_resistor` 只在**上电时**生效:运行中再置 true 会报 `BRAKE_RESISTOR_DISARMED`, + 必须保存配置后重启。刹车电阻实际阻值也要和 `config.brake_resistance` 一致。 diff --git a/README.md b/README.md index 97b77abf4..17132726c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ +## Important Note + +The firmware in this repository is compatible with the ODrive v3.x (NRND) and is no longer under active development. + +Firmware for the new generation of ODrives ([ODrive Pro](https://odriverobotics.com/shop/odrive-pro), [S1](https://odriverobotics.com/shop/odrive-s1), [Micro](https://odriverobotics.com/shop/odrive-micro), etc.) is currently being actively maintained and developed, however its source code is currently not publicly available. Access may be available under NDA, please [reach out to us](mailto:info@odriverobotics.com) for inquiries. + +## Overview + ![ODrive Logo](https://static1.squarespace.com/static/58aff26de4fcb53b5efd2f02/t/59bf2a7959cc6872bd68be7e/1505700483663/Odrive+logo+plus+text+black.png?format=1000w) This project is all about accurately driving brushless motors, for cheap. The aim is to make it possible to use inexpensive brushless motors in high performance robotics projects, like [this](https://www.youtube.com/watch?v=WT4E5nb3KtY). diff --git a/docs/Makefile b/docs/Makefile index d4bb2cbb9..2e7f1c67c 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -18,3 +18,4 @@ help: # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 7490c08d6..e6c3e2f22 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -79,5 +79,5 @@ 'https://docs.odriverobotics.com/docsInject.js' ] -version = "0.5.4" +version = "0.5.5" release = version