Skip to content

Commit 5bdd148

Browse files
committed
Improve rewind logic.
+ No longer "instantly" detect close rewind if the sensor is open -- use timeouts only for simplicity. + "Cooldown" mode detects some sorts of "serious blockages" and turns off everything for about a minute. + Removed a TODO comment about the periodic timer being set too far in the future: That will not happen because pin changes don't happen frequently.
1 parent ace7304 commit 5bdd148

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

compile.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22

33
src=main.c
4-
avr-gcc -o "${src/.c/.elf}" -Wall -g -mmcu=attiny84 "$src"
4+
avr-gcc -o "${src/.c/.elf}" -Wall -O3 -mmcu=attiny84 "$src"

main.c

+36-47
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ enum action {
1313
ACT_OUTER,
1414

1515
// "close" or "open" is sw position when rewind began
16-
ACT_REWIND_CLOSE_INIT, // rewind just started, sensor OPEN
17-
ACT_REWIND_CLOSE, // rewind under way (or just started but never was
18-
// OPEN), sensor MIDDLE
19-
ACT_REWIND_OPEN, // rewind under way, sensor MIDDLE
16+
ACT_REWIND_CLOSE,
17+
ACT_REWIND_OPEN,
18+
19+
ACT_COOLDOWN,
2020
};
2121

2222
enum pins {
@@ -100,11 +100,6 @@ static bool outer_done_p() {
100100
(!state.sw_outer_closed && state.sens_open);
101101
}
102102

103-
// if currently closing outer door, determine if we must rewind
104-
static bool close_rewind_p() {
105-
return state.sw_outer_closed && state.sens_open;
106-
}
107-
108103
static void ensure_flshgnd() {
109104
TCCR0B = (1<<CS02);
110105
TCCR0A = (1<<COM0A0);
@@ -161,8 +156,6 @@ static void action_outer() {
161156
static void auto_action_motion() {
162157
if (state.inner_done) {
163158
if (outer_done_p()) {
164-
// TODO: this will the periodic timer to be set too far
165-
// ahead sometimes?
166159
action_idle();
167160
} else {
168161
action_outer();
@@ -172,16 +165,9 @@ static void auto_action_motion() {
172165
}
173166
}
174167

175-
static void action_rewind_close_init() {
176-
ensure_flshgnd();
177-
state.action = ACT_REWIND_CLOSE_INIT;
178-
pin_outer_up();
179-
OCR1B = TCNT1 + 0x0800;
180-
}
181-
182168
static void action_rewind_close() {
183169
ensure_flshgnd();
184-
state.action = ACT_REWIND_CLOSE_INIT;
170+
state.action = ACT_REWIND_CLOSE;
185171
// should already be set, but for consistency...
186172
pin_outer_up();
187173
OCR1B = TCNT1 + 0x0800;
@@ -194,6 +180,14 @@ static void action_rewind_open() {
194180
OCR1B = TCNT1 + 0x0800;
195181
}
196182

183+
static void action_cooldown() {
184+
ensure_flshgnd();
185+
state.action = ACT_COOLDOWN;
186+
pin_idle();
187+
// about a minute
188+
OCR1B = TCNT1 + 0x2000;
189+
}
190+
197191
///////////////////////////////
198192
// act and act-timer
199193

@@ -210,14 +204,6 @@ static void act() {
210204
case ACT_OUTER:
211205
if (outer_done_p()) {
212206
auto_action_motion();
213-
// TODO: make close_rewind_p not shitty
214-
} else if (false && close_rewind_p()) {
215-
action_rewind_close_init();
216-
}
217-
break;
218-
case ACT_REWIND_CLOSE_INIT:
219-
if (!state.sens_open) {
220-
action_rewind_close();
221207
}
222208
break;
223209
case ACT_REWIND_CLOSE:
@@ -226,23 +212,26 @@ static void act() {
226212
auto_action_motion();
227213
} else if (state.sens_open) {
228214
// got stuck on the way down during rewind, so it came
229-
// back up the other side. For now, just lower
230-
// immediately -- but if there's a serious blockage,
231-
// this could cause a continuous loop, maybe damaging
232-
// the motor? TODO: some delay
233-
action_outer();
215+
// back up the other (normal spooling) side. Assume a
216+
// serious blockage.
217+
action_cooldown();
234218
}
235219
break;
236220
case ACT_REWIND_OPEN:
237-
if (state.sens_closed) {
238-
// rewind finished
221+
if (state.sens_closed || state.sens_open) {
222+
// rewind finished OR the door got caught while
223+
// rewinding (never reached the bottom) and is now open
224+
// but spooled the wrong direction, which will be
225+
// corrected when the door is next opened.
226+
227+
// This could cause a loop of activating the motor in
228+
// case of a serious blockage.
239229
auto_action_motion();
240-
} else if (state.sens_open) {
241-
// we rewound all the way? means it got stuck lowering
242-
// while rewinding.
243-
action_rewind_close_init();
244230
}
245231
break;
232+
case ACT_COOLDOWN:
233+
// do nothing, we're waiting for the timer.
234+
break;
246235
}
247236
}
248237

@@ -252,30 +241,30 @@ static void act_timer() {
252241
case ACT_IDLE:
253242
// Inner door periodic
254243
state.inner_done = false;
255-
act();
244+
auto_action_motion();
256245
break;
257246
case ACT_INNER:
258247
// Stop moving inner door
259248
state.inner_done = true;
260-
act();
249+
auto_action_motion();
261250
break;
262251
case ACT_OUTER:
263252
// Start rewind
264253
if (state.sw_outer_closed) {
265-
if (state.sens_open) {
266-
action_rewind_close_init();
267-
} else {
268-
action_rewind_close();
269-
}
254+
action_rewind_close();
270255
} else {
271256
action_rewind_open();
272257
}
273258
break;
274259

275260
case ACT_REWIND_OPEN:
276-
case ACT_REWIND_CLOSE_INIT:
277261
case ACT_REWIND_CLOSE:
278-
// rewind took too long. TODO
262+
// rewind took too long
263+
action_cooldown();
264+
break;
265+
case ACT_COOLDOWN:
266+
// cooldown complete
267+
auto_action_motion();
279268
break;
280269
}
281270
}

0 commit comments

Comments
 (0)