瀏覽代碼

Add Servo::move() to servo.cpp

move(pin, angel) - Sequence of attach(pin), write(angel),
                   if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.

As we have jitter on the servos during moves of the steppers, and detaching them improves this behaviour,
the usual sequence to handle a servo movement is:
attach(pin)
write(angel)
delay(until the servo finished the move)
detache()

Here a function to handle the complete sequence.
AnHardt 10 年之前
父節點
當前提交
2ddb2a2be9
共有 2 個檔案被更改,包括 21 行新增1 行删除
  1. 16
    1
      Marlin/servo.cpp
  2. 5
    0
      Marlin/servo.h

+ 16
- 1
Marlin/servo.cpp 查看文件

@@ -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 查看文件

@@ -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…
取消
儲存