Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoders: only look at leftmost edge of left sensor signal #218

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

jonathanperret
Copy link
Contributor

@jonathanperret jonathanperret commented Dec 15, 2024

Problem

Hopefully fixes #40.

Theory of the issue

My current theory for why needles are sometimes wrongly selected (going to position D instead of B) is that the associated solenoid is activated too late in the process, causing the solenoid's armature to no longer be in contact with the solenoid core anymore by the time it gets powered, resulting in the selection lever not being prevented from moving the selector plate and selecting the needle to D.

In principle, this could be solved by simply increasing the "timing advance" (START_OFFSET in ayab-firmware) for activating solenoids when the carriage moves, i.e. instead of selecting for needle 0 when the carriage center is at needle 4 as we do currently, we could select for needle 0 as soon as the carriage center is over needle 3 (or even earlier).

But in the particular case of the lace carriage, this would interfere with the initial carriage detection. Because of the existence of the garter carriage, when we detect a lace carriage's magnet at the left Hall sensor, we wait for a few more needles before declaring to the desktop app that a lace carriage is present and requesting the first line's data. If an opposite polarity magnet is detected in that interval, we instead report a garter carriage. Also, once we have determined the carriage type and requested the line data, even though it normally arrives with milliseconds of the request, the firmware currently needs to wait for the next needle before effectively applying the data to the solenoids.

This adds up to a situation where we can't start selecting solenoids from the pattern until the lace carriage's center is over needle 4 (L97). If we are using a pattern that goes up to needle 0 (L100), the solenoid for that needle will be activated right after we receive the line data. But if we changed the timing advance to select for needle 0 when the carriage is over needle 3 instead, then that needle would never get selected properly, since by the time we have the pattern data we have already reached needle 4.

A round of testing by @Adrienne200 with a machine affected by #40 showed however that the alternative firmware https://github.com/jpcornil-git/ayabAsync-firmware was not exhibiting the issue.

After comparing the code of the two firmwares, and measuring their timing behavior, it appeared that they were both using the same timing advance (selecting for needle 0 when the center is over needle 4). So, something else had to be different.

Looking into the left Hall sensor logic as suggested by @jpcornil-git proved promising: it turns out ayabAsync-firmware does something different than ayab-firmware there. Where ayab-firmware currently resets the carriage position to zero as long as a magnet is detected in front of the sensor (effectively declaring that needle 0 is at the rightmost position where the magnet can be detected), ayabAsync-firmware instead places needle 0 at the position where the sensor signal reaches an extremum (minimum for the lace carriage).

Since we have observed that the area where the sensor was triggered by a magnet could reach up to three needles in width, this could create a difference of one to two needles in the timing of solenoid activation between the firmwares. This could be enough to make the difference between issue #40 manifesting or not.

Proposed solution

In this PR, we change the logic for the left Hall sensor to reset the carriage position to "zero" (actually END_LEFT_PLUS_OFFSET but that corresponds to needle 0) only when the sensor signal is seen going from a mid-level value to a "magnet present" value, while the carriage is moving left. In effect this sets the "zero" position at the leftmost edge of the magnet detection area. This should shift everything left by one or two needles, leaving more time for the solenoids to be activated.

Summary by CodeRabbit

  • New Features

    • Introduced a new method for detecting the left carriage type.
    • Added a new member variable to enhance tracking of the left carriage state.
  • Bug Fixes

    • Improved test cases for the encoder's rising edge detection, ensuring accurate sensor reading expectations.
  • Tests

    • Enhanced clarity and maintainability of the testing framework with the introduction of constants for sensor values.

The code in encA_rising used to reset the carriage position to "zero" for
every needle where the sensor was triggered.
This caused the zero position to be taken as the rightmost position where
a magnet was detected (a magnet is usually detected for two to three
needle widths).
In turn this caused solenoids to sometimes be activated too late to correctly
select needles.
By checking for the signal going from "no magnet detected" to "any
magnet detected", the "zero" position will be set only at the leftmost edge
of the magnet detection area. This should shift everything left by one or two
needles, leaving more time for the solenoids to be activated.
Copy link

coderabbitai bot commented Dec 15, 2024

Walkthrough

The changes modify the carriage detection mechanism in the Encoders class. A new private method detectCarriageLeft() is introduced to centralize the logic for determining the carriage type based on hall sensor readings. The method reads the left hall sensor and uses predefined filter values to detect the carriage. A new member variable m_previousDetectedCarriageLeft is added to track the previously detected carriage type, enhancing the detection process within the encA_rising() method.

Changes

File Change Summary
src/ayab/encoders.cpp - Added detectCarriageLeft() method
- Modified encA_rising() to use new detection method
- Introduced tracking of previous carriage detection
src/ayab/encoders.h - Added m_previousDetectedCarriageLeft member variable
- Added detectCarriageLeft() method declaration
test/test_encoders.cpp - Added MID_SENSOR_VALUE constant
- Updated test cases with refined sensor reading expectations

Assessment against linked issues

Objective Addressed Explanation
Prevent extra needle selection in lace carriage [#40] Changes appear to improve carriage detection, but direct impact on lace knitting issue is unclear
Ensure accurate carriage position recognition Refactoring detection logic might help, but full verification requires testing

Note: The changes improve code modularity and detection logic, but a direct resolution of the lace carriage issue cannot be conclusively determined from this code review alone. Further testing would be required to validate the fix.

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e32811 and 1b51c24.

📒 Files selected for processing (3)
  • src/ayab/encoders.cpp (3 hunks)
  • src/ayab/encoders.h (1 hunks)
  • test/test_encoders.cpp (4 hunks)
🔇 Additional comments (8)
src/ayab/encoders.h (1)

183-183: LGTM: Clean interface changes for improved carriage detection

The new member variable and method declaration support the PR objective of improving left sensor timing logic. The volatile qualifier is correctly used as the variable is modified in interrupt context.

Also applies to: 191-191

src/ayab/encoders.cpp (3)

127-135: LGTM: Clean implementation of carriage detection

The method cleanly encapsulates the logic for determining carriage type based on hall sensor readings, making the code more maintainable.


77-77: LGTM: Proper initialization of new state

Correctly initializes the previous carriage state during setup.


158-166: Verify the timing improvement for needle selection

The new logic only triggers on transitions from mid-level to magnet-present state, which should address the timing issues mentioned in PR #40. However, we should verify this improvement.

test/test_encoders.cpp (4)

33-34: LGTM: Good constant definition for sensor values

The MID_SENSOR_VALUE constant improves test readability and maintainability.


39-44: LGTM: Proper test setup for sensor initialization

Correctly sets up the initial state with mid-level sensor reading.


79-116: LGTM: Comprehensive test coverage for rising edge detection

The test case thoroughly verifies the new carriage detection logic, including:

  • Initial state verification
  • Proper position tracking
  • Multiple edge transitions

214-227: LGTM: Improved test readability

Good use of HIGH/LOW constants instead of boolean literals improves code clarity.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@jpcornil-git jpcornil-git left a comment

Choose a reason for hiding this comment

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

encoders.cpp L171 (after changes), remove Direction_t::Right == m_direction
Before L203 (after changes), insert m_carriage = detected_carriage; and remove L210-211, L230-232
Do the same for the right sensor ? (same problem)

@jonathanperret
Copy link
Contributor Author

Thanks for the review!

encoders.cpp L171 (after changes), remove Direction_t::Right == m_direction
Before L203 (after changes), insert m_carriage = detected_carriage; and remove L210-211, L230-232

Good ideas, but unrelated to the issue, and I'm trying to minimize changes in this patch, since this code is so fragile currently and I'm not satisfied with the current test suite. My plan is to clean all this up once we get a full automated test suite running.

Do the same for the right sensor ? (same problem)

Yes, this is the plan, but since the right sensor is essentially not supported currently, I thought it would be simpler to only touch the left sensor code for now and get feedback on whether this fixes issue #40.

I'm working on the right sensor code anyway in #205, I'll integrate this logic change if it works.

@dl1com
Copy link
Contributor

dl1com commented Jan 1, 2025

@jonathanperret / @Adrienne200 indicated that this solves #40.
Merging...

@dl1com dl1com merged commit 1c469a5 into AllYarnsAreBeautiful:main Jan 1, 2025
2 checks passed
@jonathanperret jonathanperret deleted the detect-earlier branch January 1, 2025 16:50
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.

Extra needles selected when knitting lace
3 participants