-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPW.cpp
More file actions
executable file
·222 lines (184 loc) · 5.66 KB
/
Copy pathPW.cpp
File metadata and controls
executable file
·222 lines (184 loc) · 5.66 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*****************************************
* PW
* (c) Lawrie Abbott 2020
*****************************************/
#include "PW.h"
#include "utility.h"
PW::PW() {}
void PW::Init(byte a, byte b, byte c, byte d, int e, bool o )
{
RelayA = a;
RelayB = b;
SwitchUp = c;
SwitchDn = d;
CurrentSens = e;
side = o;
}
void PW::WindWindow(bool up)
{
if (!inhibit_drive) {
serial_print_val("Winding", up, side);
Winding = true;
// Set relays to drive in required direction
digitalWrite(RelayA, up);
digitalWrite(RelayB, !up);
delay(changeoverDelay);
}
}
void PW::WindowStop()
{
if (!inhibit_stop) {
//serial_print("STOP", side);
// reset winding, button state flags
Winding = false;
abort_wind = false;
timeout_counting = false;
// set both relays to same state (and de-energised)
digitalWrite(RelayA, AT_REST);
digitalWrite(RelayB, AT_REST);
delay(changeoverDelay);
} else {
serial_print("STOP inhibited", side);
}
}
void PW::Up()
{
// WINDOW UP START
if (!digitalRead(SwitchUp) && !Winding) { //SW activated and motor not running
// reset 'end of travel' flag for down direction
// bail out if already at end of travel up
end_of_travel_down = false;
if (end_of_travel_up) {
return;
}
// first read of switch
if (!buttonPressedUp) {
buttonPressedUp = true; // set button pressed flag
initTime = millis(); // set start debounce time
}
// Wait debounce delay
if ((millis()- initTime) > debounceDelay) {
buttonPressedUp = false; // set button pressed flag
WindWindow(1); // Wind window up
}
}
// WINDOW UP STOP
else if (!digitalRead(SwitchUp) && Winding ) {
// first read of switch
if (!buttonPressedUp) {
buttonPressedUp = true; // set button pressed flag
initTime = millis(); // set start debounce time
}
if (((millis()- initTime) > debounceDelay) ) {
buttonPressedUp = false; // set button pressed flag
WindowStop();
}
}
}
void PW::Down()
{
// WINDOW DOWN START
if (!digitalRead(SwitchDn) && !Winding) { //SW activated and motor not running
// reset 'end of travel' flag for up direction
// bail out if already at end of travel down
end_of_travel_up = false;
if (end_of_travel_down) {
return;
}
// first read of switch
if (!buttonPressedDn) {
buttonPressedDn = true; // set button pressed flag
initTime = millis(); // set start debounce time
}
// Wait debounce delay
if ((millis()- initTime) > debounceDelay) {
buttonPressedDn = false;
WindWindow(0); // Wind window down
}
}
// WINDOW DOWN STOP
else if (!digitalRead(SwitchDn) && Winding) {
// first read of switch
if (!buttonPressedDn) {
buttonPressedDn = true; // set button pressed flag
initTime = millis(); // set start debounce time
}
if (((millis()- initTime) > debounceDelay) ) {
buttonPressedDn = false; // set button pressed flag
WindowStop();
}
}
}
void PW::Timeout()
{
// TIMEOUT STOP
// serial_print_val("timeout", (millis()-initTime), side);
if (Winding && !timeout_counting) {
initTimeTimeout = millis();
timeout_counting = true;
}
if (timeout_counting && Winding && (millis()-initTimeTimeout >= timeout)) {
serial_print_val("TIMEOUT", (millis()-initTimeTimeout), side);
inhibit_drive = true;
inhibit_stop = false;
abort_wind = true;
}
}
void PW::Continuous()
{
// check if a a SWITCH is CONTINUOUSLY HELD
newTime = millis();
if (Winding && (newTime >= oldTime + update_interval)) {
// if switch is activated disable stop function
if (!(digitalRead(SwitchUp)) || !(digitalRead(SwitchDn))) {
inhibit_stop = true;
abort_wind = false;
}
//when switch is off allow to abort winding
if (((digitalRead(SwitchUp)) && (digitalRead(SwitchDn))) && inhibit_stop){
serial_print("Switch OFF ", side);
inhibit_stop = false;
abort_wind = true;
inhibit_drive = false;
}
}
if (newTime >= oldTime + update_interval) {
oldTime = newTime;
}
// disable drive after 'button held' timeout until button is released
if ((digitalRead(SwitchUp)) && (digitalRead(SwitchDn))) {
//serial_print("Drive Inhibit OFF ", side);
inhibit_drive = false;
}
}
void PW::Sensor()
{
// Measure current SENSOR regularly and STOP if over setpoint
new_sens_time = millis();
if (new_sens_time > old_sens_time + sensor_interval) {
old_sens_time = new_sens_time;
// Formula being used
// Amps = (((analogRead(Ax)/maxAnalog)*maxmVDC)-ACSoffset)/mvperAmp
float reada = analogRead(CurrentSens);
// Get maximum reading for Vcc
Amax = analogRead(AMAX);
//Serial.println(Amax);
//Amax = 1020; // for test
mAmps = (((((reada/(Amax-fudge)) * 5000.0) - 2500.0) / mVperAmp) * 1000); // converted to mA
// for debugging
//serial_print_uval(" ", mAmps, side);
if (abs(mAmps) >= maxAmps*1000) {
serial_print_uval("Overcurrent: Amps = ", mAmps/1000.0, side);
inhibit_stop = false;
abort_wind = true;
if (mAmps > 0) { // down
end_of_travel_down = true;
end_of_travel_up = false;
} else { // up
end_of_travel_down = false;
end_of_travel_up = true;
}
WindowStop();
}
}
}