|
@@ -27,22 +27,88 @@
|
27
|
27
|
|
28
|
28
|
#if HAS_SERVOS
|
29
|
29
|
|
|
30
|
+uint8_t ServoCount; //=0
|
|
31
|
+
|
30
|
32
|
#include "HAL_Servo_Stm32f1.h"
|
31
|
33
|
|
32
|
|
-int8_t libServo::attach(const int pin) {
|
33
|
|
- if (this->servoIndex >= MAX_SERVOS) return -1;
|
34
|
|
- return Servo::attach(pin);
|
|
34
|
+//#include "Servo.h"
|
|
35
|
+
|
|
36
|
+#include <boards.h>
|
|
37
|
+#include <io.h>
|
|
38
|
+#include <pwm.h>
|
|
39
|
+#include <wirish_math.h>
|
|
40
|
+
|
|
41
|
+/**
|
|
42
|
+ * 20 millisecond period config. For a 1-based prescaler,
|
|
43
|
+ *
|
|
44
|
+ * (prescaler * overflow / CYC_MSEC) msec = 1 timer cycle = 20 msec
|
|
45
|
+ * => prescaler * overflow = 20 * CYC_MSEC
|
|
46
|
+ *
|
|
47
|
+ * This uses the smallest prescaler that allows an overflow < 2^16.
|
|
48
|
+ */
|
|
49
|
+#define MAX_OVERFLOW ((1 << 16) - 1)
|
|
50
|
+#define CYC_MSEC (1000 * CYCLES_PER_MICROSECOND)
|
|
51
|
+#define TAU_MSEC 20
|
|
52
|
+#define TAU_USEC (TAU_MSEC * 1000)
|
|
53
|
+#define TAU_CYC (TAU_MSEC * CYC_MSEC)
|
|
54
|
+#define SERVO_PRESCALER (TAU_CYC / MAX_OVERFLOW + 1)
|
|
55
|
+#define SERVO_OVERFLOW ((uint16)round((double)TAU_CYC / SERVO_PRESCALER))
|
|
56
|
+
|
|
57
|
+// Unit conversions
|
|
58
|
+#define US_TO_COMPARE(us) ((uint16)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW))
|
|
59
|
+#define COMPARE_TO_US(c) ((uint32)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC))
|
|
60
|
+#define ANGLE_TO_US(a) ((uint16)(map((a), this->minAngle, this->maxAngle, \
|
|
61
|
+ SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW)))
|
|
62
|
+#define US_TO_ANGLE(us) ((int16)(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, \
|
|
63
|
+ this->minAngle, this->maxAngle)))
|
|
64
|
+
|
|
65
|
+libServo::libServo() {
|
|
66
|
+ this->servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO;
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+bool libServo::attach(const int32_t pin, const int32_t minAngle, const int32_t maxAngle) {
|
|
70
|
+ if (this->servoIndex >= MAX_SERVOS) return false;
|
|
71
|
+
|
|
72
|
+ this->pin = pin;
|
|
73
|
+ this->minAngle = minAngle;
|
|
74
|
+ this->maxAngle = maxAngle;
|
|
75
|
+
|
|
76
|
+ timer_dev *tdev = PIN_MAP[this->pin].timer_device;
|
|
77
|
+ uint8 tchan = PIN_MAP[this->pin].timer_channel;
|
|
78
|
+
|
|
79
|
+ pinMode(this->pin, PWM);
|
|
80
|
+ pwmWrite(this->pin, 0);
|
|
81
|
+
|
|
82
|
+ timer_pause(tdev);
|
|
83
|
+ timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based
|
|
84
|
+ timer_set_reload(tdev, SERVO_OVERFLOW);
|
|
85
|
+ timer_generate_update(tdev);
|
|
86
|
+ timer_resume(tdev);
|
|
87
|
+
|
|
88
|
+ return true;
|
35
|
89
|
}
|
36
|
90
|
|
37
|
|
-int8_t libServo::attach(const int pin, const int min, const int max) {
|
38
|
|
- return Servo::attach(pin, min, max);
|
|
91
|
+bool libServo::detach() {
|
|
92
|
+ if (!this->attached()) return false;
|
|
93
|
+ pwmWrite(this->pin, 0);
|
|
94
|
+ return true;
|
39
|
95
|
}
|
40
|
96
|
|
41
|
|
-void libServo::move(const int value) {
|
|
97
|
+int32_t libServo::read() const {
|
|
98
|
+ if (this->attached()) {
|
|
99
|
+ timer_dev *tdev = PIN_MAP[this->pin].timer_device;
|
|
100
|
+ uint8 tchan = PIN_MAP[this->pin].timer_channel;
|
|
101
|
+ return US_TO_ANGLE(COMPARE_TO_US(timer_get_compare(tdev, tchan)));
|
|
102
|
+ }
|
|
103
|
+ return 0;
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+void libServo::move(const int32_t value) {
|
42
|
107
|
constexpr uint16_t servo_delay[] = SERVO_DELAY;
|
43
|
108
|
static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
|
44
|
|
- if (this->attach(0) >= 0) {
|
45
|
|
- this->write(value);
|
|
109
|
+
|
|
110
|
+ if (this->attached()) {
|
|
111
|
+ pwmWrite(this->pin, US_TO_COMPARE(ANGLE_TO_US(constrain(value, this->minAngle, this->maxAngle))));
|
46
|
112
|
safe_delay(servo_delay[this->servoIndex]);
|
47
|
113
|
#if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
|
48
|
114
|
this->detach();
|