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
|
/*************************************
* Pump.h - Manage the hydraulic pump.
*/
#ifndef PUMP_H
#define PUMP_H
#include "Configuration.h"
#include "TaskScheduler.h"
#include "Monitor.h"
#include "Machine.h"
#include "Clock.h"
// move to common
#define PUMP_ON HIGH
#define PUMP_OFF LOW
#define MAX_PUMP_RUN_SECONDS 5
#define GOING_UP LOW
#define GOING_DOWN HIGH
#define SOLENOID_OFF LOW
static void runThePump();
static void readPumpPressure();
static void PMS (uint8_t kwIndex, uint8_t verb,char *args);
static void MUP (uint8_t kwIndex, uint8_t verb,char *args);
static void MDN (uint8_t kwIndex, uint8_t verb,char *args);
static void PS1 (uint8_t kwIndex, uint8_t verb,char *args);
static void SOL (uint8_t kwIndex, uint8_t verb,char *args);
class Pump {
private:
Task checkPressure;
Task runPump;
bool wereWeGoingDown;
bool wasTheMotorOn;
long int startedAt;
bool motorOn;
bool goingDown;
public:
int pressure;
bool pressureChanged;
void motor(bool state) {
motorOn=state;
if((motorOn==PUMP_ON) && (wasTheMotorOn==PUMP_OFF))
{
startedAt=clock.second();
}
digitalWrite(PUMP_PIN, motorOn?PUMP_ON:PUMP_OFF);
if (motorOn != wasTheMotorOn) {
monitor.update("PMS", "%d", motorOn);
}
wasTheMotorOn=motorOn;
}
bool isMotorOn() {return motorOn;}
bool areWeGoingDown() {return goingDown;}
void solenoid(bool state) {
goingDown=state;
digitalWrite(SOLENOID_PIN, goingDown?GOING_DOWN:GOING_UP);
if (goingDown != wereWeGoingDown) {
monitor.update("SOL", "%d", goingDown);
}
goingDown = wereWeGoingDown;
}
void init() {
pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, PUMP_OFF);
pinMode(SOLENOID_PIN, OUTPUT);
digitalWrite(SOLENOID_PIN, GOING_UP);
StopPump();
// register monitor commands
monitor.registerAction(_PMS_, &PMS);
monitor.registerAction(_PS1_, &PS1);
monitor.registerAction(_SOL_, &SOL);
monitor.registerAction(_MUP_, &MUP);
monitor.registerAction(_MDN_, &MDN);
checkPressure.set(100L, TASK_FOREVER, &readPumpPressure);
machine.todolist.addTask(checkPressure);
checkPressure.enable();
runPump.set(100L, TASK_FOREVER, &runThePump);
machine.todolist.addTask(runPump);
runPump.enable();
}
void Press() {
solenoid(GOING_UP);
motor(PUMP_ON);
CheckPump(); // ??
}
void Release() {
solenoid(GOING_DOWN);
motor(PUMP_ON);
CheckPump();
}
void CheckPump() {
if((motorOn) && ((clock.second()-startedAt) > MAX_PUMP_RUN_SECONDS)){
monitor.debug("Motor run timed out");
motor(PUMP_OFF);
}
// check here for the home switch.
}
void StopPump() {
motor(PUMP_OFF);
solenoid(SOLENOID_OFF);
}
// 'accessors'
bool updatePressure() {
int tol = 50; // number foo bad move to macro
int thisReading = analogRead(PS1_APIN);
if (thisReading < 224) {
pressure=0;
} else {
pressure=(thisReading-200)*10;
}
pressureChanged = abs(pressure - pressure) >= tol;
if (pressureChanged)
pressure = pressure;
return pressureChanged;
}
void setPressureChanged(bool v) {
pressureChanged = v;
}
};
Pump pump;
static void runThePump() {
pump.CheckPump();
}
static void readPumpPressure() {
if (pump.updatePressure()) {
monitor.update("PS1", "%d", pump.pressure);
pump.setPressureChanged(false);
}
}
// should also take on as a value
static void PMS (uint8_t kwIndex, uint8_t verb,char *args) {
if (verb == '!') {
pump.motor((bool) atoi(args));
} else {
monitor.update("PMS", "%d", pump.isMotorOn());
}
}
// should also take on as a value
static void SOL (uint8_t kwIndex, uint8_t verb,char *args) {
if (verb == '!') {
pump.solenoid((bool)atoi(args));
} else {
monitor.update("SOL", "%d", pump.areWeGoingDown());
}
}
static void MUP (uint8_t kwIndex, uint8_t verb,char *args) {
monitor.update("ACK","Going up");
pump.Press();
}
static void MDN (uint8_t kwIndex, uint8_t verb,char *args) {
monitor.update("ACK", "Going down");
pump.Release();
}
static void PS1 (uint8_t kwIndex, uint8_t verb,char *args) {
monitor.update("PS1", "%d", pump.pressure);
}
#endif // PUMP_H
|