-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(head,common): commonize tool detect
The tool detection will also be needed on the pipette. In addition to therefore needing the tool detection code to live in common/ somewhere, the pipettes also need to interact with the code in a different way - they don't care what kind of tool they are (or maybe do only as a debug), just what carrier they're on. In the same way, the head doesn't really care what carrier it reads, since it knows which pins it's looking at, just which tool it is. So we can provide separate filtering options for each and let each use of the code become simpler. Unfortunately ranges and views just don't work in clang at all because of llvm/llvm-project#44178 so even though it all compiles correctly we have to do a pretty ugly hack that removes functionality when linting. It would be nice to not have this.
- Loading branch information
Showing
17 changed files
with
458 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_subdirectory(core) | ||
if (${CMAKE_CROSSCOMPILING}) | ||
add_subdirectory(firmware) | ||
else() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
add_library(common-core STATIC tool_detection.cpp) | ||
target_include_directories(common-core PUBLIC ${CMAKE_SOURCE_DIR}/include) | ||
target_link_libraries(common-core PUBLIC can-core) | ||
|
||
set_target_properties(common-core | ||
PROPERTIES CXX_STANDARD 20 | ||
CXX_STANDARD_REQUIRED TRUE) | ||
|
||
target_compile_options(common-core | ||
PRIVATE | ||
-Wall | ||
-Werror | ||
-Weffc++ | ||
-Wreorder | ||
-Wsign-promo | ||
-Wextra-semi | ||
-Wctor-dtor-privacy | ||
-fno-rtti | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "common/core/tool_detection.hpp" | ||
|
||
#include <array> | ||
#include <span> | ||
|
||
using namespace tool_detection; | ||
|
||
constexpr static auto pipette_384_chan_z_bounds = | ||
ToolCheckBounds{.upper = 1883, .lower = 1851}; | ||
|
||
constexpr static auto pipette_96_chan_z_bounds = | ||
ToolCheckBounds{.upper = 1434, .lower = 1400}; | ||
|
||
constexpr static auto pipette_single_chan_z_bounds = | ||
ToolCheckBounds{.upper = 460, .lower = 444}; | ||
|
||
constexpr static auto pipette_multiple_chan_z_bounds = | ||
ToolCheckBounds{.upper = 945, .lower = 918}; | ||
|
||
constexpr static auto pipette_single_chan_a_bounds = | ||
ToolCheckBounds{.upper = 2389, .lower = 2362}; | ||
|
||
constexpr static auto pipette_multiple_chan_a_bounds = | ||
ToolCheckBounds{.upper = 2860, .lower = 2844}; | ||
|
||
constexpr static auto nothing_connected_z_bounds = | ||
ToolCheckBounds{.upper = 3, .lower = 1}; | ||
|
||
constexpr static auto nothing_connected_a_bounds = | ||
ToolCheckBounds{.upper = 54, .lower = 16}; | ||
|
||
// revisit these, not sure if EE has a calculation for gripper carrier bounds | ||
constexpr static auto nothing_connected_gripper_bounds = | ||
ToolCheckBounds{.upper = 54, .lower = 16}; | ||
|
||
constexpr static auto gripper_bounds = | ||
ToolCheckBounds{.upper = 999, .lower = 536}; | ||
|
||
constexpr static auto undefined_bounds = | ||
ToolCheckBounds{.upper = 0, .lower = 0}; | ||
|
||
static std::array tool_list(std::to_array<Tool>( | ||
{Tool{.tool_type = can_ids::ToolType::pipette_384_chan, | ||
.tool_carrier = Z_CARRIER, | ||
.bounds = pipette_384_chan_z_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::pipette_96_chan, | ||
.tool_carrier = Z_CARRIER, | ||
.bounds = pipette_96_chan_z_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::pipette_single_chan, | ||
.tool_carrier = Z_CARRIER, | ||
.bounds = pipette_single_chan_z_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::pipette_multi_chan, | ||
.tool_carrier = Z_CARRIER, | ||
.bounds = pipette_multiple_chan_z_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::nothing_attached, | ||
.tool_carrier = Z_CARRIER, | ||
.bounds = nothing_connected_z_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::pipette_single_chan, | ||
.tool_carrier = A_CARRIER, | ||
.bounds = pipette_single_chan_a_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::pipette_multi_chan, | ||
.tool_carrier = A_CARRIER, | ||
.bounds = pipette_multiple_chan_a_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::nothing_attached, | ||
.tool_carrier = A_CARRIER, | ||
.bounds = nothing_connected_a_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::gripper, | ||
.tool_carrier = GRIPPER_CARRIER, | ||
.bounds = gripper_bounds}, | ||
Tool{.tool_type = can_ids::ToolType::nothing_attached, | ||
.tool_carrier = GRIPPER_CARRIER, | ||
.bounds = nothing_connected_gripper_bounds}})); | ||
|
||
std::span<Tool> tool_detection::lookup_table() { return std::span(tool_list); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include <algorithm> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include "can/core/ids.hpp" | ||
#include "catch2/catch.hpp" | ||
#include "common/core/tool_detection.hpp" | ||
|
||
// Filter tests - no BDD here because these macros don't have | ||
// BDD equivalents | ||
TEMPLATE_TEST_CASE_SIG("tool list by pipette type", "", | ||
((can_ids::ToolType TT), TT), | ||
(can_ids::ToolType::pipette_96_chan), | ||
(can_ids::ToolType::pipette_384_chan), | ||
(can_ids::ToolType::pipette_single_chan), | ||
(can_ids::ToolType::pipette_multi_chan), | ||
(can_ids::ToolType::gripper), | ||
(can_ids::ToolType::nothing_attached)) { | ||
SECTION("lookup table filtering") { | ||
auto tool_only = tool_detection::lookup_table_filtered(TT); | ||
bool checked_anything = false; | ||
for (auto tool_elem : tool_only) { | ||
checked_anything = true; | ||
REQUIRE(tool_elem.tool_type == TT); | ||
} | ||
CHECK(checked_anything); | ||
} | ||
} | ||
|
||
TEMPLATE_TEST_CASE_SIG("tool list by carrier", "", | ||
((tool_detection::Carrier C), C), | ||
(tool_detection::Carrier::GRIPPER_CARRIER), | ||
(tool_detection::Carrier::A_CARRIER), | ||
(tool_detection::Carrier::Z_CARRIER)) { | ||
auto carrier_only = tool_detection::lookup_table_filtered(C); | ||
bool checked_anything = false; | ||
for (auto tool_elem : carrier_only) { | ||
checked_anything = true; | ||
REQUIRE(tool_elem.tool_carrier == C); | ||
} | ||
CHECK(checked_anything); | ||
} | ||
|
||
SCENARIO("ot3 tools list for tool detection") { | ||
GIVEN("ot3 tools lower and upper voltage bounds for tool detection") { | ||
auto tools = tool_detection::lookup_table(); | ||
WHEN("when lower bound compared with upper bound") { | ||
THEN("lower bound is always found lower or equal to upper bounds") { | ||
for (auto& element : tools) { | ||
REQUIRE(element.bounds.lower <= element.bounds.upper); | ||
} | ||
} | ||
} | ||
WHEN("you pass in an out-of-bounds read") { | ||
uint16_t reading = 0; | ||
THEN("the tool and carrier are undefined") { | ||
REQUIRE(tool_detection::tooltype_from_reading(reading) == | ||
can_ids::ToolType::undefined_tool); | ||
REQUIRE(tool_detection::carrier_from_reading(reading) == | ||
tool_detection::Carrier::UNKNOWN); | ||
} | ||
} | ||
WHEN("checking holes in the bounds set for tool mounts") { | ||
auto which_tool = GENERATE(tool_detection::Carrier::A_CARRIER, | ||
tool_detection::Carrier::Z_CARRIER); | ||
// can't modify the lookup table, gotta copy | ||
auto filtered = tool_detection::lookup_table_filtered(which_tool); | ||
auto copied = std::vector(filtered.begin(), filtered.end()); | ||
std::sort(copied.begin(), copied.end(), | ||
[](const tool_detection::Tool& a, | ||
const tool_detection::Tool& b) { | ||
return a.bounds.upper < b.bounds.upper; | ||
}); | ||
auto trailer = | ||
std::ranges::subrange(copied.begin(), --copied.end()); | ||
auto leader = std::ranges::subrange(++copied.begin(), copied.end()); | ||
for (auto trailer_it = trailer.begin(), leader_it = leader.begin(); | ||
trailer_it != trailer.end() && leader_it != leader.end(); | ||
trailer_it++, leader_it++) { | ||
INFO("Checking tool detection for carrier " | ||
<< static_cast<uint32_t>(which_tool) << ", tool type " | ||
<< static_cast<uint32_t>(trailer_it->tool_type) << " (" | ||
<< trailer_it->bounds.lower << "->" | ||
<< trailer_it->bounds.upper << "mV) -> " | ||
<< static_cast<uint32_t>(leader_it->tool_type) << " (" | ||
<< leader_it->bounds.lower << "->" | ||
<< leader_it->bounds.upper << "mV)"); | ||
// Just above the upper range of the first | ||
REQUIRE(tool_detection::tooltype_from_reading( | ||
trailer_it->bounds.upper, filtered) == | ||
can_ids::ToolType::undefined_tool); | ||
// Just below the lower range of the second | ||
REQUIRE(tool_detection::tooltype_from_reading( | ||
leader_it->bounds.lower - 1, filtered) == | ||
can_ids::ToolType::undefined_tool); | ||
// Dead-on in the middle | ||
auto mid_value = | ||
(trailer_it->bounds.upper + leader_it->bounds.lower) / 2; | ||
INFO("Bounds gap mid value " << mid_value | ||
<< " matched to a tool"); | ||
REQUIRE(tool_detection::tooltype_from_reading(mid_value, | ||
filtered) == | ||
can_ids::ToolType::undefined_tool); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
function(target_head_core TARGET) | ||
target_sources(${TARGET} PUBLIC | ||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tool_list.cpp | ||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tasks.cpp | ||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/can_task.cpp) | ||
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_FUNCTION_LIST_DIR}) | ||
endfunction() | ||
|
||
function(target_head_core_tool_list TARGET) | ||
target_sources(${TARGET} PUBLIC | ||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tool_list.cpp) | ||
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_FUNCTION_LIST_DIR}) | ||
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_FUNCTION_LIST_DIR}) | ||
target_link_libraries(${TARGET} PUBLIC common-core) | ||
endfunction() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.