Browse Source

ESP32 servo support (#14109)

felixstorm 5 years ago
parent
commit
74f44783ac

+ 2
- 0
Marlin/src/HAL/HAL_ESP32/HAL.h View File

@@ -75,6 +75,8 @@ extern portMUX_TYPE spinlock;
75 75
 
76 76
 typedef int16_t pin_t;
77 77
 
78
+#define HAL_SERVO_LIB Servo
79
+
78 80
 // --------------------------------------------------------------------------
79 81
 // Public Variables
80 82
 // --------------------------------------------------------------------------

+ 69
- 0
Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.cpp View File

@@ -0,0 +1,69 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2019 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
+#ifdef ARDUINO_ARCH_ESP32
23
+
24
+#include "../../inc/MarlinConfig.h"
25
+
26
+#if HAS_SERVOS
27
+
28
+#include "HAL_Servo_ESP32.h"
29
+
30
+int Servo::channel_next_free = 0;
31
+
32
+Servo::Servo() {
33
+  this->channel = channel_next_free++;
34
+}
35
+
36
+int8_t Servo::attach(const int pin) {
37
+  if (this->channel >= CHANNEL_MAX_NUM) return -1;
38
+  if (pin > 0) this->pin = pin;
39
+
40
+  ledcSetup(this->channel, 50, 16); // channel X, 50 Hz, 16-bit depth
41
+  ledcAttachPin(this->pin, this->channel);
42
+  return true;
43
+}
44
+
45
+void Servo::detach() { ledcDetachPin(this->pin) }
46
+
47
+int Servo::read() { return this->degrees; }
48
+
49
+void Servo::write(int degrees) {
50
+  this->degrees = constrain(degrees, MIN_ANGLE, MAX_ANGLE);
51
+  int us = map(this->degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
52
+  int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE);
53
+  ledcWrite(channel, duty);
54
+}
55
+
56
+void Servo::move(const int value) {
57
+  constexpr uint16_t servo_delay[] = SERVO_DELAY;
58
+  static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
59
+  if (this->attach(0) >= 0) {
60
+    this->write(value);
61
+    safe_delay(servo_delay[this->channel]);
62
+    #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
63
+      this->detach();
64
+    #endif
65
+  }
66
+}
67
+#endif // HAS_SERVOS
68
+
69
+#endif // ARDUINO_ARCH_ESP32

+ 49
- 0
Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.h View File

@@ -0,0 +1,49 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2019 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
+#pragma once
23
+
24
+#include <Arduino.h>
25
+
26
+class Servo {
27
+  static const int MIN_ANGLE =   0,
28
+                   MAX_ANGLE = 180,
29
+                   MIN_PULSE_WIDTH =  544,  // Shortest pulse sent to a servo
30
+                   MAX_PULSE_WIDTH = 2400,  // Longest pulse sent to a servo
31
+                   TAU_MSEC = 20,
32
+                   TAU_USEC = (TAU_MSEC * 1000),
33
+                   MAX_COMPARE = ((1 << 16) - 1), // 65535
34
+                   CHANNEL_MAX_NUM = 16;
35
+
36
+public:
37
+  Servo();
38
+  int8_t attach(const int pin);   // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail)
39
+  void detach();
40
+  void write(int degrees);        // set angle
41
+  void move(const int degrees);   // attach the servo, then move to value
42
+  int read();                     // returns current pulse width as an angle between 0 and 180 degrees
43
+
44
+private:
45
+  static int channel_next_free;
46
+  int channel;
47
+  int pin;
48
+  int degrees;
49
+};

+ 1
- 1
Marlin/src/HAL/shared/servo.cpp View File

@@ -53,7 +53,7 @@
53 53
 
54 54
 #include "../../inc/MarlinConfig.h"
55 55
 
56
-#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F1) || defined(STM32F1xx) || defined(STM32F4) || defined(STM32F4xx) || defined(STM32F7xx))
56
+#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F1) || defined(STM32F1xx) || defined(STM32F4) || defined(STM32F4xx) || defined(STM32F7xx) || defined(ARDUINO_ARCH_ESP32))
57 57
 
58 58
 #include "servo.h"
59 59
 #include "servo_private.h"

+ 2
- 0
Marlin/src/HAL/shared/servo.h View File

@@ -80,6 +80,8 @@
80 80
   #include "../HAL_STM32F4/HAL_Servo_STM32F4.h"
81 81
 #elif defined(ARDUINO_ARCH_STM32)
82 82
   #include "../HAL_STM32/HAL_Servo_STM32.h"
83
+#elif defined(ARDUINO_ARCH_ESP32)
84
+  #include "../HAL_ESP32/HAL_Servo_ESP32.h"
83 85
 #else
84 86
   #include <stdint.h>
85 87
 

Loading…
Cancel
Save