Browse Source

Store servo angles in EEPROM

Jan 6 years ago
parent
commit
aa80e448e2

+ 56
- 0
Marlin/src/gcode/config/M281.cpp View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#include "../../inc/MarlinConfig.h"
23
+
24
+#if HAS_SERVOS
25
+
26
+#include "../gcode.h"
27
+#include "../../module/servo.h"
28
+
29
+void GcodeSuite::M281() {
30
+    if (!parser.seen('P')) return;
31
+  const int servo_index = parser.value_int();
32
+  if (WITHIN(servo_index, 0, NUM_SERVOS - 1)) {
33
+    bool angle_change = false;
34
+    if (parser.seen('L')) {
35
+      servo_angles[servo_index][0] = parser.value_int();
36
+      angle_change = true;
37
+    }
38
+    if (parser.seen('U')) {
39
+      servo_angles[servo_index][1] = parser.value_int();
40
+      angle_change = true;
41
+    }
42
+    if (!angle_change) {
43
+      SERIAL_ECHO_START();
44
+      SERIAL_ECHOPAIR(" Servo ", servo_index);
45
+      SERIAL_ECHOPAIR(" L", servo_angles[servo_index][0]);
46
+      SERIAL_ECHOLNPAIR(" U", servo_angles[servo_index][1]);
47
+    }
48
+  }
49
+  else {
50
+    SERIAL_ERROR_START();
51
+    SERIAL_ECHOPAIR("Servo ", servo_index);
52
+    SERIAL_ECHOLNPGM(" out of range");
53
+  }
54
+}
55
+
56
+#endif

+ 6
- 6
Marlin/src/gcode/config/M43.cpp View File

103
 
103
 
104
     SERIAL_PROTOCOLLNPGM("Servo probe test");
104
     SERIAL_PROTOCOLLNPGM("Servo probe test");
105
     SERIAL_PROTOCOLLNPAIR(".  using index:  ", probe_index);
105
     SERIAL_PROTOCOLLNPAIR(".  using index:  ", probe_index);
106
-    SERIAL_PROTOCOLLNPAIR(".  deploy angle: ", z_servo_angle[0]);
107
-    SERIAL_PROTOCOLLNPAIR(".  stow angle:   ", z_servo_angle[1]);
106
+    SERIAL_PROTOCOLLNPAIR(".  deploy angle: ", servo_angles[probe_index][0]);
107
+    SERIAL_PROTOCOLLNPAIR(".  stow angle:   ", servo_angles[probe_index][1]);
108
 
108
 
109
     bool probe_inverting;
109
     bool probe_inverting;
110
 
110
 
146
     uint8_t i = 0;
146
     uint8_t i = 0;
147
     bool deploy_state, stow_state;
147
     bool deploy_state, stow_state;
148
     do {
148
     do {
149
-      MOVE_SERVO(probe_index, z_servo_angle[0]); //deploy
149
+      MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]); //deploy
150
       safe_delay(500);
150
       safe_delay(500);
151
       deploy_state = READ(PROBE_TEST_PIN);
151
       deploy_state = READ(PROBE_TEST_PIN);
152
-      MOVE_SERVO(probe_index, z_servo_angle[1]); //stow
152
+      MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); //stow
153
       safe_delay(500);
153
       safe_delay(500);
154
       stow_state = READ(PROBE_TEST_PIN);
154
       stow_state = READ(PROBE_TEST_PIN);
155
     } while (++i < 4);
155
     } while (++i < 4);
170
       #endif
170
       #endif
171
     }
171
     }
172
     else {                                           // measure active signal length
172
     else {                                           // measure active signal length
173
-      MOVE_SERVO(probe_index, z_servo_angle[0]);     // deploy
173
+      MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]);     // deploy
174
       safe_delay(500);
174
       safe_delay(500);
175
       SERIAL_PROTOCOLLNPGM("please trigger probe");
175
       SERIAL_PROTOCOLLNPGM("please trigger probe");
176
       uint16_t probe_counter = 0;
176
       uint16_t probe_counter = 0;
194
           else
194
           else
195
             SERIAL_PROTOCOLLNPGM("noise detected - please re-run test"); // less than 2mS pulse
195
             SERIAL_PROTOCOLLNPGM("noise detected - please re-run test"); // less than 2mS pulse
196
 
196
 
197
-          MOVE_SERVO(probe_index, z_servo_angle[1]); //stow
197
+          MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); //stow
198
 
198
 
199
         }  // pulse detected
199
         }  // pulse detected
200
 
200
 

+ 1
- 0
Marlin/src/gcode/gcode.cpp View File

487
 
487
 
488
       #if HAS_SERVOS
488
       #if HAS_SERVOS
489
         case 280: M280(); break;                                  // M280: Set servo position absolute
489
         case 280: M280(); break;                                  // M280: Set servo position absolute
490
+        case 281: M281(); break;
490
       #endif
491
       #endif
491
 
492
 
492
       #if ENABLED(BABYSTEPPING)
493
       #if ENABLED(BABYSTEPPING)

+ 2
- 0
Marlin/src/gcode/gcode.h View File

172
  * M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS)
172
  * M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS)
173
  * M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS)
173
  * M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS)
174
  * M280 - Set servo position absolute: "M280 P<index> S<angle|µs>". (Requires servos)
174
  * M280 - Set servo position absolute: "M280 P<index> S<angle|µs>". (Requires servos)
175
+ * M281 - Set servo min|max position: "M281 P<index> L<min> U<max>". (Requires servos)
175
  * M290 - Babystepping (Requires BABYSTEPPING)
176
  * M290 - Babystepping (Requires BABYSTEPPING)
176
  * M300 - Play beep sound S<frequency Hz> P<duration ms>
177
  * M300 - Play beep sound S<frequency Hz> P<duration ms>
177
  * M301 - Set PID parameters P I and D. (Requires PIDTEMP)
178
  * M301 - Set PID parameters P I and D. (Requires PIDTEMP)
627
 
628
 
628
   #if HAS_SERVOS
629
   #if HAS_SERVOS
629
     static void M280();
630
     static void M280();
631
+    static void M281();
630
   #endif
632
   #endif
631
 
633
 
632
   #if ENABLED(BABYSTEPPING)
634
   #if ENABLED(BABYSTEPPING)

+ 69
- 1
Marlin/src/module/configuration_store.cpp View File

37
  */
37
  */
38
 
38
 
39
 // Change EEPROM version if the structure changes
39
 // Change EEPROM version if the structure changes
40
-#define EEPROM_VERSION "V55"
40
+#define EEPROM_VERSION "V56"
41
 #define EEPROM_OFFSET 100
41
 #define EEPROM_OFFSET 100
42
 
42
 
43
 // Check the integrity of data offsets.
43
 // Check the integrity of data offsets.
70
   #include "../feature/bedlevel/bedlevel.h"
70
   #include "../feature/bedlevel/bedlevel.h"
71
 #endif
71
 #endif
72
 
72
 
73
+#if HAS_SERVOS
74
+  #include "servo.h"
75
+#endif 
76
+
73
 #if HAS_BED_PROBE
77
 #if HAS_BED_PROBE
74
   #include "../module/probe.h"
78
   #include "../module/probe.h"
75
 #endif
79
 #endif
175
   int8_t ubl_storage_slot;                              // ubl.storage_slot
179
   int8_t ubl_storage_slot;                              // ubl.storage_slot
176
 
180
 
177
   //
181
   //
182
+  // SERVO_ANGLES
183
+  //
184
+  #if HAS_SERVOS
185
+    uint8_t servo_angles[NUM_SERVOS][2];
186
+  #endif
187
+
188
+  //
178
   // DELTA / [XYZ]_DUAL_ENDSTOPS
189
   // DELTA / [XYZ]_DUAL_ENDSTOPS
179
   //
190
   //
180
   #if ENABLED(DELTA)
191
   #if ENABLED(DELTA)
532
       EEPROM_WRITE(storage_slot);
543
       EEPROM_WRITE(storage_slot);
533
     #endif // AUTO_BED_LEVELING_UBL
544
     #endif // AUTO_BED_LEVELING_UBL
534
 
545
 
546
+    #if HAS_SERVOS
547
+      EEPROM_WRITE(servo_angles);
548
+    #endif
549
+
535
     // 11 floats for DELTA / [XYZ]_DUAL_ENDSTOPS
550
     // 11 floats for DELTA / [XYZ]_DUAL_ENDSTOPS
536
     #if ENABLED(DELTA)
551
     #if ENABLED(DELTA)
537
 
552
 
1136
       #endif // AUTO_BED_LEVELING_UBL
1151
       #endif // AUTO_BED_LEVELING_UBL
1137
 
1152
 
1138
       //
1153
       //
1154
+      // SERVO_ANGLES
1155
+      //
1156
+      #if HAS_SERVOS
1157
+        EEPROM_READ(servo_angles);
1158
+      #endif
1159
+
1160
+
1161
+      //
1139
       // DELTA Geometry or Dual Endstops offsets
1162
       // DELTA Geometry or Dual Endstops offsets
1140
       //
1163
       //
1141
 
1164
 
1774
     zprobe_zoffset = Z_PROBE_OFFSET_FROM_EXTRUDER;
1797
     zprobe_zoffset = Z_PROBE_OFFSET_FROM_EXTRUDER;
1775
   #endif
1798
   #endif
1776
 
1799
 
1800
+  //
1801
+  // Servo Angles
1802
+  //
1803
+
1804
+  #if HAS_SERVOS
1805
+    #if ENABLED(SWITCHING_EXTRUDER)
1806
+      #if EXTRUDERS > 3
1807
+        #define REQ_ANGLES 4
1808
+      #else
1809
+        #define REQ_ANGLES 2
1810
+      #endif
1811
+      const uint8_t extruder_angles[2] = SWITCHING_EXTRUDER_SERVO_ANGLES;
1812
+      static_assert(COUNT(extruder_angles) == REQ_ANGLES, "SWITCHING_EXTRUDER_SERVO_ANGLES needs " STRINGIFY(REQ_ANGLES) " angles.");
1813
+      servo_angles[SWITCHING_EXTRUDER_SERVO_NR][0] = extruder_angles[0];
1814
+      servo_angles[SWITCHING_EXTRUDER_SERVO_NR][1] = extruder_angles[1];
1815
+    #endif
1816
+
1817
+    #if ENABLED(SWITCHING_NOZZLE)
1818
+      const uint8_t nozzel_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
1819
+      servo_angles[SWITCHING_NOZZLE_SERVO_NR][0] = nozzel_angles[0];
1820
+      servo_angles[SWITCHING_NOZZLE_SERVO_NR][1] = nozzel_angles[1];
1821
+    #endif
1822
+
1823
+    #if defined(Z_SERVO_ANGLES) && defined(Z_PROBE_SERVO_NR)
1824
+      const uint8_t z_probe_angles[2] = Z_SERVO_ANGLES;
1825
+      servo_angles[Z_PROBE_SERVO_NR][0] = z_probe_angles[0];
1826
+      servo_angles[Z_PROBE_SERVO_NR][1] = z_probe_angles[1];
1827
+    #endif
1828
+
1829
+  #endif
1830
+
1777
   #if ENABLED(DELTA)
1831
   #if ENABLED(DELTA)
1778
     const float adj[ABC] = DELTA_ENDSTOP_ADJ,
1832
     const float adj[ABC] = DELTA_ENDSTOP_ADJ,
1779
                 dta[ABC] = DELTA_TOWER_ANGLE_TRIM;
1833
                 dta[ABC] = DELTA_TOWER_ANGLE_TRIM;
2252
 
2306
 
2253
     #endif // HAS_LEVELING
2307
     #endif // HAS_LEVELING
2254
 
2308
 
2309
+    #if HAS_SERVOS
2310
+      if (!forReplay) {
2311
+        CONFIG_ECHO_START;
2312
+        SERIAL_ECHOLNPGM_P(port, "Servo Angles:");
2313
+      }
2314
+      for (uint8_t i = 0; i < NUM_SERVOS; i++) {
2315
+        CONFIG_ECHO_START;
2316
+        SERIAL_ECHOPAIR_P(port, "  M281 P", i);
2317
+        SERIAL_ECHOPAIR_P(port, " L",servo_angles[i][0]);
2318
+        SERIAL_ECHOPAIR_P(port, " U",servo_angles[i][1]);
2319
+        SERIAL_EOL_P(port);
2320
+      }
2321
+    #endif
2322
+
2255
     #if ENABLED(DELTA)
2323
     #if ENABLED(DELTA)
2256
 
2324
 
2257
       if (!forReplay) {
2325
       if (!forReplay) {

+ 1
- 2
Marlin/src/module/probe.cpp View File

54
 
54
 
55
 #if HAS_Z_SERVO_PROBE
55
 #if HAS_Z_SERVO_PROBE
56
   #include "../module/servo.h"
56
   #include "../module/servo.h"
57
-  const int z_servo_angle[2] = Z_SERVO_ANGLES;
58
 #endif
57
 #endif
59
 
58
 
60
 #if ENABLED(Z_PROBE_SLED)
59
 #if ENABLED(Z_PROBE_SLED)
435
 
434
 
436
       #elif HAS_Z_SERVO_PROBE && DISABLED(BLTOUCH)
435
       #elif HAS_Z_SERVO_PROBE && DISABLED(BLTOUCH)
437
 
436
 
438
-        MOVE_SERVO(Z_PROBE_SERVO_NR, z_servo_angle[deploy ? 0 : 1]);
437
+        MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][(deploy ? 0 : 1)]);
439
 
438
 
440
       #elif ENABLED(Z_PROBE_ALLEN_KEY)
439
       #elif ENABLED(Z_PROBE_ALLEN_KEY)
441
 
440
 

+ 0
- 1
Marlin/src/module/probe.h View File

53
 #endif
53
 #endif
54
 
54
 
55
 #if HAS_Z_SERVO_PROBE
55
 #if HAS_Z_SERVO_PROBE
56
-  extern const int z_servo_angle[2];
57
   void servo_probe_init();
56
   void servo_probe_init();
58
 #endif
57
 #endif
59
 
58
 

+ 1
- 0
Marlin/src/module/servo.cpp View File

31
 #include "servo.h"
31
 #include "servo.h"
32
 
32
 
33
 HAL_SERVO_LIB servo[NUM_SERVOS];
33
 HAL_SERVO_LIB servo[NUM_SERVOS];
34
+uint8_t servo_angles[NUM_SERVOS][2];
34
 
35
 
35
 void servo_init() {
36
 void servo_init() {
36
   #if NUM_SERVOS >= 1 && HAS_SERVO_0
37
   #if NUM_SERVOS >= 1 && HAS_SERVO_0

+ 3
- 2
Marlin/src/module/servo.h View File

30
 #include "../HAL/shared/servo.h"
30
 #include "../HAL/shared/servo.h"
31
 
31
 
32
 extern HAL_SERVO_LIB servo[NUM_SERVOS];
32
 extern HAL_SERVO_LIB servo[NUM_SERVOS];
33
+extern uint8_t servo_angles[NUM_SERVOS][2];
33
 extern void servo_init();
34
 extern void servo_init();
34
 
35
 
35
 #define MOVE_SERVO(I, P) servo[I].move(P)
36
 #define MOVE_SERVO(I, P) servo[I].move(P)
37
 #include "../inc/MarlinConfig.h"
38
 #include "../inc/MarlinConfig.h"
38
 
39
 
39
 #if HAS_Z_SERVO_PROBE
40
 #if HAS_Z_SERVO_PROBE
40
-  #define DEPLOY_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, z_servo_angle[0])
41
-  #define STOW_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, z_servo_angle[1])
41
+  #define DEPLOY_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][0])
42
+  #define STOW_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][1])
42
 #endif
43
 #endif
43
 
44
 
44
 #endif // _SERVO_H_
45
 #endif // _SERVO_H_

+ 2
- 7
Marlin/src/module/tool_change.cpp View File

60
 #if DO_SWITCH_EXTRUDER
60
 #if DO_SWITCH_EXTRUDER
61
 
61
 
62
   #if EXTRUDERS > 3
62
   #if EXTRUDERS > 3
63
-    #define REQ_ANGLES 4
64
     #define _SERVO_NR (e < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
63
     #define _SERVO_NR (e < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
65
   #else
64
   #else
66
-    #define REQ_ANGLES 2
67
     #define _SERVO_NR SWITCHING_EXTRUDER_SERVO_NR
65
     #define _SERVO_NR SWITCHING_EXTRUDER_SERVO_NR
68
   #endif
66
   #endif
69
 
67
 
70
   void move_extruder_servo(const uint8_t e) {
68
   void move_extruder_servo(const uint8_t e) {
71
-    constexpr int16_t angles[] = SWITCHING_EXTRUDER_SERVO_ANGLES;
72
-    static_assert(COUNT(angles) == REQ_ANGLES, "SWITCHING_EXTRUDER_SERVO_ANGLES needs " STRINGIFY(REQ_ANGLES) " angles.");
73
     planner.synchronize();
69
     planner.synchronize();
74
     #if EXTRUDERS & 1
70
     #if EXTRUDERS & 1
75
       if (e < EXTRUDERS - 1)
71
       if (e < EXTRUDERS - 1)
76
     #endif
72
     #endif
77
     {
73
     {
78
-      MOVE_SERVO(_SERVO_NR, angles[e]);
74
+      MOVE_SERVO(_SERVO_NR, servo_angles[_SERVO_NR][e]);
79
       safe_delay(500);
75
       safe_delay(500);
80
     }
76
     }
81
   }
77
   }
85
 #if ENABLED(SWITCHING_NOZZLE)
81
 #if ENABLED(SWITCHING_NOZZLE)
86
 
82
 
87
   void move_nozzle_servo(const uint8_t e) {
83
   void move_nozzle_servo(const uint8_t e) {
88
-    const int16_t angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
89
     planner.synchronize();
84
     planner.synchronize();
90
-    MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, angles[e]);
85
+    MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_EXTRUDER_SERVO_NR][e]);
91
     safe_delay(500);
86
     safe_delay(500);
92
   }
87
   }
93
 
88
 

Loading…
Cancel
Save