Browse Source

Merge Anti-jitter for all servo moves (PR#2427)

Richard Wackerbarth 9 years ago
parent
commit
63715aba4f
4 changed files with 39 additions and 40 deletions
  1. 11
    7
      Marlin/Configuration.h
  2. 7
    32
      Marlin/Marlin_main.cpp
  3. 16
    1
      Marlin/servo.cpp
  4. 5
    0
      Marlin/servo.h

+ 11
- 7
Marlin/Configuration.h View File

@@ -504,13 +504,6 @@ const bool Z_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the logic
504 504
   //#define Z_PROBE_SLED // turn on if you have a z-probe mounted on a sled like those designed by Charles Bell
505 505
   //#define SLED_DOCKING_OFFSET 5 // the extra distance the X axis must travel to pickup the sled. 0 should be fine but you can push it further if you'd like.
506 506
 
507
-  //If defined, the Probe servo will be turned on only during movement and then turned off to avoid jerk
508
-  //The value is the delay to turn the servo off after powered on - depends on the servo speed; 300ms is good value, but you can try lower it.
509
-  // You MUST HAVE the SERVO_ENDSTOPS defined to use here a value higher than zero otherwise your code will not compile.
510
-
511
-//  #define PROBE_SERVO_DEACTIVATION_DELAY 300
512
-
513
-
514 507
 //If you have enabled the Bed Auto Leveling and are using the same Z Probe for Z Homing,
515 508
 //it is highly recommended you let this Z_SAFE_HOMING enabled!!!
516 509
 
@@ -774,6 +767,17 @@ const bool Z_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the logic
774 767
 //
775 768
 //#define NUM_SERVOS 3 // Servo index starts with 0 for M280 command
776 769
 
770
+// If DEACTIVATE_SERVOS_AFTER_MOVE is defined, the servos will be turned on only during movement and then turned off to avoid jitter
771
+// SERVO_DEACTIVATION_DELAY is the delay to turn the servo off after powered on - depends on the servo speed; 300ms is good value, but you can try lower it.
772
+// If your servo does not reach the requested position, enlarge the time.
773
+// You MUST HAVE the SERVO_ENDSTOPS defined to use here a value higher than zero otherwise your code will not compile.
774
+//
775
+//#define DEACTIVATE_SERVOS_AFTER_MOVE
776
+
777
+#ifdef DEACTIVATE_SERVOS_AFTER_MOVE
778
+  #define SERVO_DEACTIVATION_DELAY 300
779
+#endif
780
+
777 781
 // Servo Endstops
778 782
 //
779 783
 // This allows for servo actuated endstops, primary usage is for the Z Axis to eliminate calibration or bed height changes.

+ 7
- 32
Marlin/Marlin_main.cpp View File

@@ -36,7 +36,7 @@
36 36
   #endif
37 37
 #endif // ENABLE_AUTO_BED_LEVELING
38 38
 
39
-#define SERVO_LEVELING (defined(ENABLE_AUTO_BED_LEVELING) && PROBE_SERVO_DEACTIVATION_DELAY > 0)
39
+#define SERVO_LEVELING (defined(ENABLE_AUTO_BED_LEVELING) && defined(DEACTIVATE_SERVOS_AFTER_MOVE))
40 40
 
41 41
 #ifdef MESH_BED_LEVELING
42 42
   #include "mesh_bed_leveling.h"
@@ -570,13 +570,9 @@ void servo_init() {
570 570
   #ifdef SERVO_ENDSTOPS
571 571
     for (int i = 0; i < 3; i++)
572 572
       if (servo_endstops[i] >= 0)
573
-        servo[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
573
+        servo[servo_endstops[i]].move(0, servo_endstop_angles[i * 2 + 1]);
574 574
   #endif
575 575
 
576
-  #if SERVO_LEVELING
577
-    delay(PROBE_SERVO_DEACTIVATION_DELAY);
578
-    servo[servo_endstops[Z_AXIS]].detach();
579
-  #endif
580 576
 }
581 577
 
582 578
 /**
@@ -1315,14 +1311,7 @@ static void setup_for_endstop_move() {
1315 1311
       // Engage Z Servo endstop if enabled
1316 1312
       if (servo_endstops[Z_AXIS] >= 0) {
1317 1313
         Servo *srv = &servo[servo_endstops[Z_AXIS]];
1318
-        #if SERVO_LEVELING
1319
-          srv->attach(0);
1320
-        #endif
1321
-        srv->write(servo_endstop_angles[Z_AXIS * 2]);
1322
-        #if SERVO_LEVELING
1323
-          delay(PROBE_SERVO_DEACTIVATION_DELAY);
1324
-          srv->detach();
1325
-        #endif
1314
+        srv->move(0, servo_endstop_angles[Z_AXIS * 2]);
1326 1315
       }
1327 1316
 
1328 1317
     #elif defined(Z_PROBE_ALLEN_KEY)
@@ -1424,14 +1413,7 @@ static void setup_for_endstop_move() {
1424 1413
 
1425 1414
         // Change the Z servo angle
1426 1415
         Servo *srv = &servo[servo_endstops[Z_AXIS]];
1427
-        #if SERVO_LEVELING
1428
-          srv->attach(0);
1429
-        #endif
1430
-        srv->write(servo_endstop_angles[Z_AXIS * 2 + 1]);
1431
-        #if SERVO_LEVELING
1432
-          delay(PROBE_SERVO_DEACTIVATION_DELAY);
1433
-          srv->detach();
1434
-        #endif
1416
+        srv->move(0, servo_endstop_angles[Z_AXIS * 2 + 1]);
1435 1417
       }
1436 1418
 
1437 1419
     #elif defined(Z_PROBE_ALLEN_KEY)
@@ -1683,7 +1665,7 @@ static void homeaxis(AxisEnum axis) {
1683 1665
       if (axis != Z_AXIS) {
1684 1666
         // Engage Servo endstop if enabled
1685 1667
         if (servo_endstops[axis] > -1)
1686
-          servo[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
1668
+          servo[servo_endstops[axis]].move(0, servo_endstop_angles[axis * 2]);
1687 1669
       }
1688 1670
     #endif
1689 1671
 
@@ -1786,7 +1768,7 @@ static void homeaxis(AxisEnum axis) {
1786 1768
       {
1787 1769
         // Retract Servo endstop if enabled
1788 1770
         if (servo_endstops[axis] > -1)
1789
-          servo[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
1771
+          servo[servo_endstops[axis]].move(0, servo_endstop_angles[axis * 2 + 1]);
1790 1772
       }
1791 1773
     #endif
1792 1774
 
@@ -4354,14 +4336,7 @@ inline void gcode_M226() {
4354 4336
       servo_position = code_value_short();
4355 4337
       if (servo_index >= 0 && servo_index < NUM_SERVOS) {
4356 4338
         Servo *srv = &servo[servo_index];
4357
-        #if SERVO_LEVELING
4358
-          srv->attach(0);
4359
-        #endif
4360
-        srv->write(servo_position);
4361
-        #if SERVO_LEVELING
4362
-          delay(PROBE_SERVO_DEACTIVATION_DELAY);
4363
-          srv->detach();
4364
-        #endif
4339
+        srv->move(0, servo_position);
4365 4340
       }
4366 4341
       else {
4367 4342
         SERIAL_ECHO_START;

+ 16
- 1
Marlin/servo.cpp View File

@@ -35,12 +35,14 @@
35 35
 
36 36
  write()     - Sets the servo angle in degrees.  (invalid angle that is valid as pulse in microseconds is treated as microseconds)
37 37
  writeMicroseconds() - Sets the servo pulse width in microseconds
38
+ move(pin, angel) - Sequence of attach(pin), write(angel),
39
+                    if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
38 40
  read()      - Gets the last written servo pulse width as an angle between 0 and 180.
39 41
  readMicroseconds()   - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
40 42
  attached()  - Returns true if there is a servo attached.
41 43
  detach()    - Stops an attached servos from pulsing its i/o pin.
42 44
 
43
-*/
45
+ */
44 46
 #include "Configuration.h" 
45 47
 
46 48
 #ifdef NUM_SERVOS
@@ -301,4 +303,17 @@ int Servo::readMicroseconds() {
301 303
 
302 304
 bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; }
303 305
 
306
+uint8_t Servo::move(int pin, int value) {
307
+  uint8_t ret;
308
+  ret = this->attach(pin);
309
+  if (ret) {
310
+    this->write(value);
311
+    #ifdef DEACTIVATE_SERVOS_AFTER_MOVE && (SERVO_DEACTIVATION_DELAY > 0)
312
+      delay(SERVO_DEACTIVATION_DELAY);
313
+      this->detach();
314
+    #endif
315
+  }
316
+  return ret;
317
+}
318
+
304 319
 #endif

+ 5
- 0
Marlin/servo.h View File

@@ -40,6 +40,8 @@
40 40
    readMicroseconds()   - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
41 41
    attached()  - Returns true if there is a servo attached.
42 42
    detach()    - Stops an attached servos from pulsing its i/o pin.
43
+   move(pin, angel) - Sequence of attach(pin), write(angel),
44
+                      if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
43 45
  */
44 46
 
45 47
 #ifndef servo_h
@@ -120,6 +122,9 @@ class Servo {
120 122
     void detach();
121 123
     void write(int value);             // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
122 124
     void writeMicroseconds(int value); // Write pulse width in microseconds
125
+    uint8_t move(int pin, int value);  // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure.
126
+                                       // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds.
127
+                                       // if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
123 128
     int read();                        // returns current pulse width as an angle between 0 and 180 degrees
124 129
     int readMicroseconds();            // returns current pulse width in microseconds for this servo (was read_us() in first release)
125 130
     bool attached();                   // return true if this servo is attached, otherwise false

Loading…
Cancel
Save