瀏覽代碼

Fixes for MP_SCARA (#21113)

Co-Authored-By: svsergo <52426708+svsergo@users.noreply.github.com>
Scott Lahteine 4 年之前
父節點
當前提交
65e24f812f
No account linked to committer's email address
共有 1 個文件被更改,包括 50 次插入61 次删除
  1. 50
    61
      Marlin/src/module/scara.cpp

+ 50
- 61
Marlin/src/module/scara.cpp 查看文件

@@ -47,19 +47,22 @@ void scara_set_axis_is_at_home(const AxisEnum axis) {
47 47
 
48 48
     #if ENABLED(MORGAN_SCARA)
49 49
       // MORGAN_SCARA uses arm angles for AB home position
50
-      // SERIAL_ECHOLNPAIR("homeposition A:", homeposition.a, " B:", homeposition.b);
50
+      //DEBUG_ECHOLNPAIR("homeposition A:", homeposition.a, " B:", homeposition.b);
51 51
       inverse_kinematics(homeposition);
52 52
       forward_kinematics_SCARA(delta.a, delta.b);
53 53
       current_position[axis] = cartes[axis];
54 54
     #else
55 55
       // MP_SCARA uses a Cartesian XY home position
56
-      // SERIAL_ECHOPGM("homeposition");
57
-      // SERIAL_ECHOLNPAIR_P(SP_X_LBL, homeposition.x, SP_Y_LBL, homeposition.y);
58
-      current_position[axis] = homeposition[axis];
56
+      //DEBUG_ECHOPGM("homeposition");
57
+      //DEBUG_ECHOLNPAIR_P(SP_X_LBL, homeposition.x, SP_Y_LBL, homeposition.y);
58
+      delta.a = SCARA_OFFSET_THETA1;
59
+      delta.b = SCARA_OFFSET_THETA2;
60
+      forward_kinematics_SCARA(delta.a, delta.b);
61
+      current_position[axis] = cartes[axis];
59 62
     #endif
60 63
 
61
-    // SERIAL_ECHOPGM("Cartesian");
62
-    // SERIAL_ECHOLNPAIR_P(SP_X_LBL, current_position.x, SP_Y_LBL, current_position.y);
64
+    //DEBUG_ECHOPGM("Cartesian");
65
+    //DEBUG_ECHOLNPAIR_P(SP_X_LBL, current_position.x, SP_Y_LBL, current_position.y);
63 66
     update_software_endstops(axis);
64 67
   }
65 68
 }
@@ -75,14 +78,14 @@ void forward_kinematics_SCARA(const float &a, const float &b) {
75 78
 
76 79
   const float a_sin = sin(RADIANS(a)) * L1,
77 80
               a_cos = cos(RADIANS(a)) * L1,
78
-              b_sin = sin(RADIANS(b)) * L2,
79
-              b_cos = cos(RADIANS(b)) * L2;
81
+              b_sin = sin(RADIANS(b + TERN0(MP_SCARA, a))) * L2,
82
+              b_cos = cos(RADIANS(b + TERN0(MP_SCARA, a))) * L2;
80 83
 
81 84
   cartes.set(a_cos + b_cos + scara_offset.x,  // theta
82
-             a_sin + b_sin + scara_offset.y); // theta+phi
85
+             a_sin + b_sin + scara_offset.y); // phi
83 86
 
84 87
   /*
85
-    SERIAL_ECHOLNPAIR(
88
+    DEBUG_ECHOLNPAIR(
86 89
       "SCARA FK Angle a=", a,
87 90
       " b=", b,
88 91
       " a_sin=", a_sin,
@@ -90,74 +93,60 @@ void forward_kinematics_SCARA(const float &a, const float &b) {
90 93
       " b_sin=", b_sin,
91 94
       " b_cos=", b_cos
92 95
     );
93
-    SERIAL_ECHOLNPAIR(" cartes (X,Y) = "(cartes.x, ", ", cartes.y, ")");
96
+    DEBUG_ECHOLNPAIR(" cartes (X,Y) = "(cartes.x, ", ", cartes.y, ")");
94 97
   //*/
95 98
 }
96 99
 
100
+/**
101
+ * SCARA Inverse Kinematics. Results in 'delta'.
102
+ *
103
+ * See https://reprap.org/forum/read.php?185,283327
104
+ *
105
+ * Maths and first version by QHARLEY.
106
+ * Integrated into Marlin and slightly restructured by Joachim Cerny.
107
+ */
97 108
 void inverse_kinematics(const xyz_pos_t &raw) {
109
+  float C2, S2, SK1, SK2, THETA, PSI;
98 110
 
99
-  #if ENABLED(MORGAN_SCARA)
100
-    /**
101
-     * Morgan SCARA Inverse Kinematics. Results in 'delta'.
102
-     *
103
-     * See https://reprap.org/forum/read.php?185,283327
104
-     *
105
-     * Maths and first version by QHARLEY.
106
-     * Integrated into Marlin and slightly restructured by Joachim Cerny.
107
-     */
108
-    float C2, S2, SK1, SK2, THETA, PSI;
109
-
110
-    // Translate SCARA to standard XY with scaling factor
111
-    const xy_pos_t spos = raw - scara_offset;
112
-
113
-    const float H2 = HYPOT2(spos.x, spos.y);
114
-    if (L1 == L2)
115
-      C2 = H2 / L1_2_2 - 1;
116
-    else
117
-      C2 = (H2 - (L1_2 + L2_2)) / (2.0f * L1 * L2);
118
-
119
-    S2 = SQRT(1.0f - sq(C2));
111
+  // Translate SCARA to standard XY with scaling factor
112
+  const xy_pos_t spos = raw - scara_offset;
120 113
 
121
-    // Unrotated Arm1 plus rotated Arm2 gives the distance from Center to End
122
-    SK1 = L1 + L2 * C2;
114
+  const float H2 = HYPOT2(spos.x, spos.y);
115
+  if (L1 == L2)
116
+    C2 = H2 / L1_2_2 - 1;
117
+  else
118
+    C2 = (H2 - (L1_2 + L2_2)) / (2.0f * L1 * L2);
123 119
 
124
-    // Rotated Arm2 gives the distance from Arm1 to Arm2
125
-    SK2 = L2 * S2;
120
+  LIMIT(C2, -1, 1);
126 121
 
127
-    // Angle of Arm1 is the difference between Center-to-End angle and the Center-to-Elbow
128
-    THETA = ATAN2(SK1, SK2) - ATAN2(spos.x, spos.y);
122
+  S2 = SQRT(1.0f - sq(C2));
129 123
 
130
-    // Angle of Arm2
131
-    PSI = ATAN2(S2, C2);
124
+  // Unrotated Arm1 plus rotated Arm2 gives the distance from Center to End
125
+  SK1 = L1 + L2 * C2;
132 126
 
133
-    delta.set(DEGREES(THETA), DEGREES(THETA + PSI), raw.z);
127
+  // Rotated Arm2 gives the distance from Arm1 to Arm2
128
+  SK2 = L2 * S2;
134 129
 
135
-    /*
136
-      DEBUG_POS("SCARA IK", raw);
137
-      DEBUG_POS("SCARA IK", delta);
138
-      SERIAL_ECHOLNPAIR("  SCARA (x,y) ", sx, ",", sy, " C2=", C2, " S2=", S2, " Theta=", THETA, " Phi=", PHI);
139
-    //*/
130
+  // Angle of Arm1 is the difference between Center-to-End angle and the Center-to-Elbow
131
+  THETA = ATAN2(SK1, SK2) - ATAN2(spos.x, spos.y);
140 132
 
141
-  #else // MP_SCARA
133
+  // Angle of Arm2
134
+  PSI = ATAN2(S2, C2);
142 135
 
143
-    const float x = raw.x, y = raw.y, c = HYPOT(x, y),
144
-                THETA3 = ATAN2(y, x),
145
-                THETA1 = THETA3 + ACOS((sq(c) + sq(L1) - sq(L2)) / (2.0f * c * L1)),
146
-                THETA2 = THETA3 - ACOS((sq(c) + sq(L2) - sq(L1)) / (2.0f * c * L2));
136
+  delta.set(DEGREES(THETA), DEGREES(PSI + TERN0(MORGAN_SCARA, THETA)), raw.z);
147 137
 
148
-    delta.set(DEGREES(THETA1), DEGREES(THETA2), raw.z);
149
-
150
-    /*
151
-      DEBUG_POS("SCARA IK", raw);
152
-      DEBUG_POS("SCARA IK", delta);
153
-      SERIAL_ECHOLNPAIR("  SCARA (x,y) ", x, ",", y," Theta1=", THETA1, " Theta2=", THETA2);
154
-    //*/
155
-
156
-  #endif // MP_SCARA
138
+  /*
139
+    DEBUG_POS("SCARA IK", raw);
140
+    DEBUG_POS("SCARA IK", delta);
141
+    DEBUG_ECHOLNPAIR("  SCARA (x,y) ", sx, ",", sy, " C2=", C2, " S2=", S2, " Theta=", THETA, " Psi=", PSI);
142
+  //*/
157 143
 }
158 144
 
159 145
 void scara_report_positions() {
160
-  SERIAL_ECHOLNPAIR("SCARA Theta:", planner.get_axis_position_degrees(A_AXIS), "  Psi+Theta:", planner.get_axis_position_degrees(B_AXIS));
146
+  SERIAL_ECHOLNPAIR(
147
+    "SCARA Theta:", planner.get_axis_position_degrees(A_AXIS),
148
+    "  Psi" TERN_(MORGAN_SCARA, "+Theta") ":", planner.get_axis_position_degrees(B_AXIS)
149
+  );
161 150
   SERIAL_EOL();
162 151
 }
163 152
 

Loading…
取消
儲存