-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolunit.cpp
More file actions
executable file
·509 lines (419 loc) · 15.3 KB
/
controlunit.cpp
File metadata and controls
executable file
·509 lines (419 loc) · 15.3 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Implemented by Oleksandra Baga
#include "controlunit.h"
#include "serviceroutine.h"
#include <QCoreApplication>
#include <math.h>
ControlUnit::ControlUnit() : ingredientTanks(NULL) {
activeUserChoice = NULL;
ingredientTanks = new Ingredient();
}
void ControlUnit::linkInteractionUnit(InteractionUnit *iunit) {
qDebug() << "CONTROL_UNIT: InteractionUnit linked";
this->iunit = iunit;
}
ControlUnit::~ControlUnit() {
if (ingredientTanks)
delete ingredientTanks;
}
void ControlUnit::setDisplayBacklight() {
qDebug() << "CONTROL UNIT: setDisplayBacklight() started.";
if (brightSensor->getBrightness() < 3 && !display->getBacklight()) {
display->setBacklight(true);
qDebug() << "LCD: Backlight turned on (BRIGHTNESS SENSOR)";
}
else if (brightSensor->getBrightness() >= 3 && display->getBacklight()) {
display->setBacklight(false);
qDebug() << "LCD: Backlight turned off (BRIGHTNESS SENSOR)";
}
}
void ControlUnit::onStartInit() {
qDebug() << "CONTROL UNIT: onStartInit() started.";
qDebug() << "CONTROL UNIT: CREDIT_LIMIT = 5.0. You can change the limit in account.cpp";
checkCardReader();
activeUserChoice->setSelectedDrink(NO_DRINK);
opticalSensor->setDistanceToObject(10);
opticalSensor->getOpticalFlowSensorsMeasurements();
setDisplayBacklight();
}
bool ControlUnit::checkCardReader() {
return cardScanner->getIsCardInside();
}
bool ControlUnit::checkCard() {
return cardScanner->isValidCardInside();
}
CardHolderState ControlUnit::insertCard(Card *card) {
qDebug() << "CONTROL UNIT: insertCard() started.";
// if NO card in RFID
if (!checkCardReader()) {
// After card was inserted, the RFID Scanner provides verification
// without instructions outside
// modifies private attribute isChoiceAllowed for InteractionUnit
if (cardScanner->insertCard(card)) {
display->writeGreetingText(activeUserChoice);
return VALID_CARD_INSIDE;
}
// if card is not valid, it will be automaticaly ejected (simulation in Design)
// and the purschasing will be ended
// the next card can be inserted in RFID
else {
display->writeCardErrorText();
cardScanner->ejectCard();
return NONVALID_CARD_INSIDE;
}
}
// if there IS A CARD
else {
cardScanner->ejectCard();
delete(activeUserChoice);
activeUserChoice = iunit->initUserChoice(card);
activeUserChoice->setDefaultChoice();
display->writeDefaultText(activeUserChoice);
return NO_CARD;
}
}
bool ControlUnit::isBrewingFinished() {
return opticalSensor->getOpticalValue() && flow->getHasPreparedDrink();
}
// we need two sensors for three states: no cup, empty cup, cup with drink
CupHolderState ControlUnit::checkCupHolder() {
qDebug() << "CONTROL UNIT: checkCupHolder() started.";
// if cup detected
if (opticalSensor->getOpticalValue()) {
// and a drink was prepared
if (flow->getHasPreparedDrink()) {
qDebug() << "OPTICAL SENSOR: There is a full cup";
opticalSensor->setDistanceToObject(10);
opticalSensor->getOpticalFlowSensorsMeasurements();
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_FULL_CUP);
return FULL_CUP;
}
// only cup stays and no drink was prepared
else {
qDebug() << "OPTICAL SENSOR: There is an empty cup";
opticalSensor->setDistanceToObject(10);
opticalSensor->getOpticalFlowSensorsMeasurements();
return EMPTY_CUP;
}
}
// there is not cup and no drink
else {
qDebug() << "OPTICAL SENSOR: Cup holder is empty";
opticalSensor->setDistanceToObject(2);
opticalSensor->getOpticalFlowSensorsMeasurements();
return NO_CUP;
}
}
void ControlUnit::writeMessageLCD(LCD_Message message) {
switch (message) {
case (DEFAULT):
display->writeDefaultText(activeUserChoice);
break;
case (GREETING):
display->writeGreetingText(activeUserChoice);
break;
case (USER_CHOICE):
display->writeUserChoiceText(activeUserChoice);
break;
case (ERROR):
display->writeCardErrorText();
break;
case (WAIT_PLEASE):
display->writeWaitText();
break;
case (TAKE_YOUR_DRINK):
display->writeTakeDrinkMessage();
break;
case (PAYMENT_ERROR):
display->writeSystemErrorMessage(PREPARE_ERROR_PAYMENT);
break;
default:
break;
}
}
void ControlUnit::writeMessageLCD(LCD_Message message, PreparationStatus status) {
switch (message) {
case (SYSTEM_ERROR):
display->writeSystemErrorMessage(status);
break;
default:
break;
}
}
void ControlUnit::maintenanceRoutine() {
qDebug() << "CONTROL UNIT: maintenanceRoutine() started.";
checkIngredients();
// Set sensor and actuator states OK if UNDEFINED
if (cardScanner->getSensorState() == UNDEFINED)
cardScanner->setSensorState(OK);
if (flow->getSensorState() == UNDEFINED)
flow->setSensorState(OK);
if (opticalSensor->getSensorState() == UNDEFINED)
opticalSensor->setSensorState(OK);
if (temperatureSensor->getSensorState() == UNDEFINED)
temperatureSensor->setSensorState(OK);
if (brightSensor->getSensorState() == UNDEFINED)
brightSensor->setSensorState(OK);
if (display->getActuatorState() == UNDEFINED)
display->setActuatorState(OK);
for (int i = 0; i < AMOUNT_OF_MOTORS; i++) {
if (motor[i]->getActuatorState() == UNDEFINED)
motor[i]->setActuatorState(OK);
}
if (heater->getActuatorState() == UNDEFINED)
heater->setActuatorState(OK);
if (milkMaker->getActuatorState() == UNDEFINED)
milkMaker->setActuatorState(OK);
qDebug() << "CONTROL_UNIT: Initialisation done. State is OK.";
}
void ControlUnit::staffServiceRoutine() {
ServiceRoutine sr;
sr.refillOrNot(this->ingredientTanks);
// reset all sensors and actuators
maintenanceRoutine();
}
bool ControlUnit::checkIngredients() {
qDebug() << "CONTROL UNIT: checkIngredients() started.";
if (activeUserChoice->getSelectedDrink() == HOTWATER) {
qDebug() << "CONTROL UNIT: Water is OK!";
return true; // it is assumed that we always have water
}
if (!checkSugarAmount()) {
return false;
}
if (!checkMilkAmount()) {
return false;
}
if (activeUserChoice->getSelectedDrink() == CACAO) {
if (!checkCacaoIngredient()) {
return false;
}
}
else if (activeUserChoice->getSelectedDrink() != HOTWATER) {
if (!checkCoffeeIngredient()) {
return false;
}
}
qDebug() << "CONTROL UNIT: All Ingredients are OK!";
return true;
}
bool ControlUnit::checkCacaoIngredient() {
if (ingredientTanks->getCacaoIngredient() < activeUserChoice->getSpecificRecipeComponent() + MINIMAl_THRESHOLD) {
qDebug() << "CONTROL UNIT: Cacao Powder is not enough!";
return false;
}
return true;
}
bool ControlUnit::checkCoffeeIngredient() {
if (ingredientTanks->getCoffeeIngredient() < activeUserChoice->getSpecificRecipeComponent() + MINIMAl_THRESHOLD) {
qDebug() << "CONTROL UNIT: Coffee Powder is not enough!";
return false;
}
return true;
}
bool ControlUnit::checkSugarAmount() {
if (ingredientTanks->getSugarIngredient() < activeUserChoice->getSugarAmount() + MINIMAl_THRESHOLD) {
qDebug() << "CONTROL UNIT: Sugar is not enough!";
return false;
}
return true;
}
bool ControlUnit::checkMilkAmount() {
if (ingredientTanks->getMilkIngredient() < activeUserChoice->getMilkAmount() + MINIMAl_THRESHOLD) {
qDebug() << "CONTROL UNIT: Milk is not enough!";
return false;
}
return true;
}
PreparationStatus ControlUnit::checkStartConditions() {
// get Optical Sensor Measurements
if (opticalSensor->getOpticalFlowSensorsMeasurements() == NO_CUP) {
qDebug() << "OPTICAL SENSOR: Place your cup first!";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_NO_CUP);
return PREPARE_ERROR_NO_CUP;
}
else if (opticalSensor->getOpticalFlowSensorsMeasurements() == FULL_CUP) {
qDebug() << "OPTICAL SENSOR: Take your cup with prepared drink before order a new one! :)";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_FULL_CUP);
return PREPARE_ERROR_FULL_CUP;
}
else if (opticalSensor->getOpticalFlowSensorsMeasurements() == EMPTY_CUP) {
// PASSED
qDebug() << "OPTICAL SENSOR: Cup is OK, liquid can flow!";
}
//==============================================================
// check if drink is preselected
if (activeUserChoice->getSelectedDrink() == NO_DRINK) {
qDebug() << "CONTROL UNIT: Select drink first!";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_NO_DRINK);
return PREPARE_ERROR_NO_DRINK;
}
// check if card is still in the card holder
if (cardScanner->isValidCardInside() != VALID_CARD_INSIDE) {
qDebug() << "RFID SCANNER: Card missed, please return your card back!";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_NO_CARD);
return PREPARE_ERROR_NO_CARD;
}
if (!checkIngredients()) {
qDebug() << "CONTROL UNIT: Error: Not enough ingredients...";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_NO_INGREDIENT);
return PREPARE_ERROR_NO_INGREDIENT;
}
// if it is possible to get here, the payment process could start
if (activeUserChoice->payDrink()) {
qDebug() << "CONTROL UNIT: YOU HAVE PAID! WE CAN START PREPARE A DRINK!";
writeMessageLCD(WAIT_PLEASE);
return PREPARE_IS_ALLOWED;
}
else {
qDebug() << "CONTROL UNIT: Payment error!";
qDebug() << "CONTROL UNIT: Take your card!";
writeMessageLCD(SYSTEM_ERROR, PREPARE_ERROR_PAYMENT);
return PREPARE_ERROR_PAYMENT;
}
}
PreparationStatus ControlUnit::prepareSelectedDrink() {
writeMessageLCD(WAIT_PLEASE);
if (!checkIngredients()) {
qDebug() << "CONTROL UNIT: Not enough ingredients. Aborted...";
abortPreparation();
return PREPARE_ERROR_NO_INGREDIENT;
}
//=============================================================
// heat water to the recipe temperature
heater->setWorkTemperature(activeUserChoice->getRecipeTemperature());
heater->heatWater();
// Waiting for water heating
while (heater->getIsHeating()) {
QCoreApplication::instance()->processEvents();
}
// put the heater back in a idle state
heater->setWorkTemperature(heater->getIdleTemperature());
if (!temperatureSensor->compareTemperature()) {
qDebug() << "CONTROL UNIT: Water heater does not heat a water. Aborted...";
abortPreparation();
return PREPARE_ERROR_HEATER;
}
//=============================================================
// Motor Simulation for Drink Mixing
if (activeUserChoice->getSelectedDrink() != HOTWATER) {
MotorType optionalMotor = MOTOR_TYPES_NUMBER;
motor[MOTOR_SUGAR]->rotate(activeUserChoice->getSugarAmount());
ingredientTanks->setSugarIngredient(ingredientTanks->getSugarIngredient() - activeUserChoice->getSugarAmount());
motor[MOTOR_MILK]->rotate(activeUserChoice->getMilkAmount());
ingredientTanks->setMilkIngredient(ingredientTanks->getMilkIngredient() - activeUserChoice->getMilkAmount());
if (activeUserChoice->getSelectedDrink() != CACAO) {
motor[MOTOR_COFFEE]->rotate(activeUserChoice->getSpecificRecipeComponent());
ingredientTanks->setCoffeeIngredient(ingredientTanks->getCoffeeIngredient() - activeUserChoice->getSpecificRecipeComponent());
optionalMotor = MOTOR_COFFEE;
}
else {
motor[MOTOR_CACAO]->rotate(activeUserChoice->getSpecificRecipeComponent());
ingredientTanks->setCacaoIngredient(ingredientTanks->getCacaoIngredient() - activeUserChoice->getSpecificRecipeComponent());
optionalMotor = MOTOR_CACAO;
}
// Waiting for rotation
while (motor[MOTOR_SUGAR]->getIsRotating() ||
motor[MOTOR_MILK]->getIsRotating() ||
motor[optionalMotor]->getIsRotating()) {
QCoreApplication::instance()->processEvents();
}
}
//=============================================================
// Make Milk Foam if required
// it is assumed milk maker can not have an error state and a drink will be prepared anyway
if (activeUserChoice->getSelectedDrink() == CAPPUCCINO || activeUserChoice->getSelectedDrink() == LATTEMACCHIOTO) {
milkMaker->makeMilkFoam(activeUserChoice->getSelectedDrink());
}
//=============================================================
// Pour a drink into a cup
flow->setRecipeAmountOfLiquid(activeUserChoice);
if (flow->mainFlowmeterRoutine()) {
qDebug() << "CONTROL UNIT: Flowmeter has done his task successfuly";
}
else {
qDebug() << "CONTROL UNIT: Flowmeter had a problem during his task"; return PREPARE_ERROR_FLOW;
}
//=============================================================
writeMessageLCD(TAKE_YOUR_DRINK);
return PREPARE_DONE;
}
void ControlUnit::abortPreparation() {
activeUserChoice->setDefaultChoice();
writeMessageLCD(USER_CHOICE);
}
void ControlUnit::connectRFID(RFID_Scanner *sensor) {
cardScanner = sensor;
}
void ControlUnit::connectOptical(OpticalSensor *sensor)
{
opticalSensor = sensor;
}
void ControlUnit::connectTemperatur(TemperaturSensor *sensor) {
temperatureSensor = sensor;
}
void ControlUnit::connectBrightnessSensor(BrightnessSensor *sensor) {
brightSensor = sensor;
}
void ControlUnit::connectFlow(Flowmeter *sensor) {
flow = sensor;
}
void ControlUnit::connectLCD(LCD_Display *actuator) {
display = actuator;
}
void ControlUnit::connectMotor(DC_Motor *actuator, int numOfActuators) {
for(int i = 0; i < numOfActuators; i++) {
motor[i] = &actuator[i];
motor[i]->setMotorType(static_cast<MotorType>(i));
}
}
void ControlUnit::connectHeater(Waterheater *actuator) {
heater = actuator;
}
void ControlUnit::connectMilkMaker(Milkmaker *actuator) {
milkMaker = actuator;
}
void ControlUnit::blockCupHolder() {
qDebug() << "CONTROL UNIT: Cup Holder is blocked, the cup will be realeased after drink preparing the drink";
}
void ControlUnit::unblockCupHolder() {
qDebug() << "CONTROL UNIT: Cup Holder is unblocked";
}
Ingredient *ControlUnit::getIngredients() const {
return this->ingredientTanks;
}
RFID_Scanner *ControlUnit::getCardScanner() const
{
return cardScanner;
}
Flowmeter *ControlUnit::getFlowmeter() const
{
return flow;
}
OpticalSensor *ControlUnit::getOpticalSensor() const
{
return opticalSensor;
}
TemperaturSensor *ControlUnit::getTemperatureSensor() const
{
return temperatureSensor;
}
BrightnessSensor *ControlUnit::getBrightSensor() const
{
return brightSensor;
}
LCD_Display *ControlUnit::getDisplay() const
{
return display;
}
DC_Motor *ControlUnit::getMotor(MotorType mt) const
{
return motor[mt];
}
Waterheater *ControlUnit::getHeater() const
{
return heater;
}
Milkmaker *ControlUnit::getMilkMaker() const
{
return milkMaker;
}