-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBlindsServo.cpp
195 lines (153 loc) · 4.4 KB
/
BlindsServo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "BlindsServo.h"
BlindsServo::BlindsServo(int id, int servoPin, int minValue, int maxValue, int maxDegree, boolean reversed, boolean debug) {
init(id, servoPin, minValue, maxValue, maxDegree, reversed, debug);
}
BlindsServo::BlindsServo() {
}
BlindsServo::~BlindsServo() {
servo.detach();
}
void BlindsServo::init(int id, int servoPin, int minPulseValue, int maxPulseValue, int maxDegree, boolean reversed, boolean debug) {
this->id = id;
this->servoPin = servoPin;
this->servoMinPulse = minPulseValue;
this->servoMaxPulse = maxPulseValue;
this->servoMaxDegree = maxDegree;
this->isDebug = debug;
this->isReversed = reversed;
attached = false;
}
void BlindsServo::attach() {
servo.attach(servoPin, servoMinPulse, servoMaxPulse);
attached = true;
}
void BlindsServo::detach() {
servo.detach();
attached = false;
}
void BlindsServo::goToAngle(int angle) {
debugPrint("Go to angle: " + String(angle));
// Ensure limits
if(angle > servoMaxDegree || angle < 0) {
return;
}
if (isMoving()) {
setStop(); // Stop
}
target = angle;
}
void BlindsServo::setStop() {
// Cancel current move
debugPrint("Stop moving!");
target = currentPosition;
statusChangedCallback(id);
}
void BlindsServo::setOpen() {
goToAngle(servoMaxDegree);
}
void BlindsServo::setClose() {
goToAngle(0);
}
void BlindsServo::goToPosition(int position) {
if(position >= 0 && position <= 100) {
float angle = (float) ((float) position / (float) 100) * (float) 270;
int res = (int)round(angle);
goToAngle(res);
}
}
void BlindsServo::loop() {
// Do the main loop
if (currentPosition != target && attached == false) {
attach();
}
// Before move, we take the prev position
previousPosition = currentPosition;
blindsStatus currentStatus = getStatus();
if(currentPosition < target){
servo.writeMicroseconds(angleToServo(currentPosition++));
if (previousStatus != currentStatus) {
statusChangedCallback(id);
}
positionChangedCallback(id);
}
else if(currentPosition > target){
servo.writeMicroseconds(angleToServo(currentPosition--));
if (previousStatus != currentStatus) {
statusChangedCallback(id);
}
positionChangedCallback(id);
}
// Notify that we have reached the target
if (currentPosition == target && previousPosition != currentPosition) {
detach();
statusChangedCallback(id);
positionChangedCallback(id);
}
previousStatus = currentStatus;
}
boolean BlindsServo::isOpening() {
return target > currentPosition;
}
boolean BlindsServo::isClosing() {
return target < currentPosition;
}
int BlindsServo::currentAngleInPercent() {
return (int)round(((float) currentPosition / (float) servoMaxDegree) * 100);
}
int BlindsServo::getId() {
return id;
}
int BlindsServo::getAngle() {
return currentPosition;
}
boolean BlindsServo::isClosed() {
return currentPosition == 0;
}
boolean BlindsServo::isMoving() {
return currentPosition != target;
}
BlindsServo::blindsStatus BlindsServo::getStatus() {
if (currentPosition == 0) {
return BlindsServo::CLOSED;
} else if(target < currentPosition) {
return BlindsServo::CLOSING;
} else if(target > currentPosition) {
return BlindsServo::OPENING;
}
return BlindsServo::OPEN;
}
void BlindsServo::setDebug(bool debug) {
isDebug = debug;
}
void BlindsServo::debugPrint(String text) {
if (isDebug) {
String value = "(" + String(id) + ") " + text;
debugPrintCallback(value);
}
}
void BlindsServo::setDebugPrintCallback(DEBUG_PRINT_CALLBACK_SIGNATURE) {
if (isDebug) {
this->debugPrintCallback = debugPrintCallback;
}
}
void BlindsServo::setStatusChangedCallback(STATUS_CHANGED_CALLBACK_SIGNATURE) {
this->statusChangedCallback = statusChangedCallback;
}
void BlindsServo::setPositionChangedCallback(POSITION_CHANGED_CALLBACK_SIGNATURE) {
this->positionChangedCallback = positionChangedCallback;
}
// Privates
int BlindsServo::angleToServo(int angle){
// Convert from min - max to 0 - 270
// formula: (degree * (max - min / servoMax)) + offset
float result = (angle * (float)((float)(servoMaxPulse - servoMinPulse) / (float)servoMaxDegree)) + servoMinPulse;
int res = (int)round(result);
if(isReversed) { // Servo reversing support
res = (servoMaxPulse + servoMinPulse) - res;
}
// Ensure result is valid
if (res < servoMinPulse || res > servoMaxPulse) {
return servoMinPulse;
}
return res;
}