|
6 | 6 | * This file contains functions that are called form file GCodes2.cpp to execute various G and M codes. |
7 | 7 | */ |
8 | 8 |
|
| 9 | +#include <Movement/StraightProbeSettings.h> |
9 | 10 | #include "GCodes.h" |
10 | 11 |
|
11 | 12 | #include "GCodeBuffer/GCodeBuffer.h" |
@@ -676,6 +677,105 @@ GCodeResult GCodes::DoDriveMapping(GCodeBuffer& gb, const StringRef& reply) |
676 | 677 |
|
677 | 678 | return GCodeResult::ok; |
678 | 679 | } |
| 680 | +// Handle G38.[2-5] |
| 681 | +GCodeResult GCodes::StraightProbe(GCodeBuffer& gb, const StringRef& reply) |
| 682 | +{ |
| 683 | + const int8_t fraction = gb.GetCommandFraction(); |
| 684 | + if (fraction < 2 || fraction > 5) { |
| 685 | + return GCodeResult::warningNotSupported; |
| 686 | + } |
| 687 | + /* |
| 688 | + * It is an error if: |
| 689 | + * # the current point is the same as the programmed point. |
| 690 | + * # no axis word is used |
| 691 | + * # the feed rate is zero |
| 692 | + * # the probe is already in the target state |
| 693 | + */ |
| 694 | + |
| 695 | + StraightProbeSettings& sps = reprap.GetMove().GetStraightProbeSettings(); |
| 696 | + sps.Reset(); |
| 697 | + |
| 698 | + |
| 699 | + switch (fraction) |
| 700 | + { |
| 701 | + case 2: |
| 702 | + sps.SetStraightProbeType(StraightProbeType::towardsWorkpieceErrorOnFailure); |
| 703 | + break; |
| 704 | + |
| 705 | + case 3: |
| 706 | + sps.SetStraightProbeType(StraightProbeType::towardsWorkpiece); |
| 707 | + break; |
| 708 | + |
| 709 | + case 4: |
| 710 | + sps.SetStraightProbeType(StraightProbeType::awayFromWorkpieceErrorOnFailure); |
| 711 | + break; |
| 712 | + |
| 713 | + case 5: |
| 714 | + sps.SetStraightProbeType(StraightProbeType::awayFromWorkpiece); |
| 715 | + break; |
| 716 | + } |
| 717 | + |
| 718 | + // Get the target coordinates and check if we would move at all |
| 719 | + float target[MaxAxes]; |
| 720 | + ToolOffsetTransform(currentUserPosition, target); |
| 721 | + bool seen = false; |
| 722 | + bool doesMove = false; |
| 723 | + for (size_t axis = 0; axis < MaxAxes; axis++) |
| 724 | + { |
| 725 | + if (gb.Seen(axisLetters[axis])) |
| 726 | + { |
| 727 | + seen = true; |
| 728 | + const float axisTarget = gb.GetFValue(); |
| 729 | + if (axisTarget != target[axis]) |
| 730 | + { |
| 731 | + doesMove = true; |
| 732 | + } |
| 733 | + target[axis] = axisTarget; |
| 734 | + sps.AddMovingAxis(axis); |
| 735 | + } |
| 736 | + } |
| 737 | + |
| 738 | + // No axis letters seen |
| 739 | + if (!seen) |
| 740 | + { |
| 741 | + // Signal error for G38.2 and G38.4 |
| 742 | + if (sps.SignalError()) |
| 743 | + { |
| 744 | + reply.copy("No axis specified."); |
| 745 | + return GCodeResult::error; |
| 746 | + } |
| 747 | + return GCodeResult::ok; |
| 748 | + } |
| 749 | + |
| 750 | + // At least one axis seen but it would not result in movement |
| 751 | + else if (!doesMove) |
| 752 | + { |
| 753 | + // Signal error for G38.2 and G38.4 |
| 754 | + if (sps.SignalError()) |
| 755 | + { |
| 756 | + reply.copy("Target equals current position."); |
| 757 | + return GCodeResult::error; |
| 758 | + } |
| 759 | + return GCodeResult::ok; |
| 760 | + } |
| 761 | + sps.SetTarget(target); |
| 762 | + |
| 763 | + // See whether we are using a user-defined Z probe or just current one |
| 764 | + size_t probeToUse = platform.GetEndstops().GetCurrentZProbeNumber(); |
| 765 | + if (gb.Seen('P')) |
| 766 | + { |
| 767 | + probeToUse = gb.GetUIValue(); |
| 768 | + if (platform.GetEndstops().GetZProbe(probeToUse) == nullptr) |
| 769 | + { |
| 770 | + reply.copy("Invalid probe number"); |
| 771 | + return GCodeResult::error; |
| 772 | + } |
| 773 | + } |
| 774 | + sps.SetZProbeToUse(probeToUse); |
| 775 | + |
| 776 | + gb.SetState(GCodeState::straightProbe0); |
| 777 | + return GCodeResult::ok; |
| 778 | +} |
679 | 779 |
|
680 | 780 | // Deal with a M585 |
681 | 781 | GCodeResult GCodes::ProbeTool(GCodeBuffer& gb, const StringRef& reply) |
|
0 commit comments