From 62905aaa550ea584f29feae2ba35fc6df6c42c8f Mon Sep 17 00:00:00 2001 From: Shai Frieder Date: Mon, 2 Sep 2019 15:09:23 +0300 Subject: add IFS system, fix reading MidTank capacity --- Software/Embedded_SW/Embedded/Modules/IFS/ifs.c | 341 +++++++++++++++++++++++- Software/Embedded_SW/Embedded/Modules/IFS/ifs.h | 50 +++- 2 files changed, 382 insertions(+), 9 deletions(-) (limited to 'Software/Embedded_SW/Embedded/Modules/IFS') diff --git a/Software/Embedded_SW/Embedded/Modules/IFS/ifs.c b/Software/Embedded_SW/Embedded/Modules/IFS/ifs.c index 347808d5e..0e18ffdad 100644 --- a/Software/Embedded_SW/Embedded/Modules/IFS/ifs.c +++ b/Software/Embedded_SW/Embedded/Modules/IFS/ifs.c @@ -1,9 +1,13 @@ -/* - * ifs.c +/** + * @file ifs.h * - * Created on: Jul 8, 2019 - * Author: shlomo + * @date Jul 8, 2019 + * @author Shlomo + * + * @date 26.8.2019 + * @author Shai */ + #include "PMR/Diagnostics/CartridgeSlot.pb-c.h" #include "PMR/Diagnostics/CartridgeTagContent.pb-c.h" #include "RfidTagContent.h" @@ -12,6 +16,20 @@ #include #include "include.h" +#include "Modules/Waste/Waste.h" +#include "Modules/IFS/ifs.h" +#include "Modules/AlarmHandling/AlarmHandling.h" +#include "CartridgeValidationRequest.pb-c.h" +#include "CartridgeValidationResponse.pb-c.h" +#include +#include "drivers/Valves/Valve.h" +#include "Modules/Waste/Waste.h" + +bool IFS_TimeOutAlarm(); +bool ColorMatch(); +bool MidTankValvesAction(bool action); + + RfidTagContent TagContent[MAX_CARTRIDGES]; NFC_Tag NFCTag[MAX_CARTRIDGES]; void TagInitialize(RfidTagContent* TagContent); @@ -29,3 +47,318 @@ void TagInitialize(RfidTagContent* TagContent) { } + + + + + + +/** + * + * @brief detect changes in cartridge INK presence + * this function detect changes in IFS presence cartridge + * when changes are happens it update the presence status + * and the correct event + * @return IFS_INK_presence_sensor or WHS_no_event event + */ + +WHS_sensor CartridgeInkCallBackFunction() +{ + uint32_t parameter = 6 ; + + WHS_sensor ret = WHS_no_event; + + if (IFS_info.IFS_Ink_precense_sensor != RdInkCartridgeSensor()) + { + REPORT_MSG(parameter," ------------ find : IFS_Ink_presence_sensor event ----------------- "); + WHS_info.event = IFS_INK_presence_sensor; + IFS_info.IFS_Ink_precense_sensor = !IFS_info.IFS_Ink_precense_sensor; + ret = IFS_INK_presence_sensor; + } + return ret; +} + +/** + * + * @brief detect TimeOut + * we have 2 way to stop filling: + * 1. time out + * 2. detect Midtank is full. + * + * 1. time out process is: + * 1. detect timeout after 5 minutes + * 2. close and open the valves + * 3. count 5 minutes again + *2. measure MidTank capacity + * if MidTank is full stop count + * + * @return IFS_INK_presence_sensor or WHS_no_event event + */ +WHS_sensor CartridgeInkTimeOutCallBackFunction() +{ + WHS_sensor ret = WHS_no_event; + double MidTank_Pressure = 0; + + MidTank_Pressure = Read_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);// Get_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color); + ReportWithPackageFilter(WasteFilter,"------------ IFS_info.Ink.time_out and Pressure -----------------", __FILE__,__LINE__,(IFS_info.Ink.time_out * SECONDS_5), RpMessage, (int)(MidTank_Pressure*1000), 0); + + if (IFS_info.Ink.time_out) IFS_info.Ink.time_out += 1; /*** counter ***/ + + +#ifdef timeout_capacity + /*** detect the MidTank capacity is full ***/ + //Read_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color); + if (MidTank_Pressure >= IFS_info.MidTank_capacity + CARTRIDGE_CAPATICY) + { + IFS_info.Ink.time_out = 0; + WHS_info.event = IFS_MidTankFull; + return IFS_MidTankFull; + } +#endif + /*** first TimeOut ***/ + if (IFS_info.Ink.time_out == CARTRIDGE_INK_TIMEOUT) + { + ReportWithPackageFilter(WasteFilter,"------------ first IFS_info.Ink.time_out -----------------", __FILE__, __LINE__, (IFS_info.Ink.time_out * SECONDS_5), RpMessage, MidTank_Pressure, 0); + //IFS_info.Ink.time_out = 0; + //return IFS_info.event = IFS_Timeout; + + REPORT_MSG(WHS_emptying," ------------ IFS_Timeout close valves----------------- "); + // try again : + // start timeout + // close and open the correct valves + + MidTankValvesAction(Cartridge_MidTank_OFF); + } + + if (IFS_info.Ink.time_out == (CARTRIDGE_INK_TIMEOUT + 1)) //need to open the valve for second chance + { + ReportWithPackageFilter(WasteFilter,"------------ IFS_Timeout open valves for second timeout -----------------", __FILE__, __LINE__, (IFS_info.Ink.time_out * SECONDS_5), RpMessage, MidTank_Pressure, 0); + MidTankValvesAction(Cartridge_MidTank_ON); + } + + if (IFS_info.Ink.time_out >= (CARTRIDGE_INK_TIMEOUTx2 + 1)) + { + REPORT_MSG(WHS_full," ------------ IFS_Timeout_Second_Time ----------------- "); + IFS_info.Ink.time_out = 0; + WHS_info.event = IFS_Timeout_Second; + return IFS_Timeout_Second; + } + return ret; +} + +/** + * + * @brief IFS Time Out Alarm + * + * @return OK + */ +bool IFS_TimeOutAlarm() +{ + bool ret = OK; + switch (IFS_info.Ink.cart_color) //todo enter the correct color from RFID + { + case 1 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_1_FILL_TIMEOUT, true); + break; + case 2 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_2_FILL_TIMEOUT, true); + break; + case 3 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_3_FILL_TIMEOUT, true); + break; + case 4 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_4_FILL_TIMEOUT, true); + break; + case 5 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_5_FILL_TIMEOUT, true); + break; + case 6 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_6_FILL_TIMEOUT, true); + break; + case 7 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_7_FILL_TIMEOUT, true); + break; + case 8 : + AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_8_FILL_TIMEOUT, true); + break; + default: //wrong color + ret = notOK; + break; + } + + return ret; +} + +/** + * + * @brief MidTank Valves Action + * the MidTank have 2 Valves: + * 1. Ink Color Valve to get INK from the INK Cartridge to Midtank + * 2. Air valve to get INK in the Midtank we have to open the Air valve so the air can go out + * + * @return action to open the Valve use: Cartridge_MidTank_ON/Atm_MidTank_ON + * to close the Valve use : Cartridge_MidTank_OFF/Atm_MidTank_OFF + */ +bool MidTankValvesAction(bool action) //Cartridge_MidTank_ON of Cartridge_MidTank_OFF +{ + bool ret = false; + if (action == Cartridge_MidTank_ON) + { + Enable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color); + } + else + { + Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color); + } + Valve_Set(IDS_Id_to_AirValve[IFS_info.Ink.cart_color], action ); //Atm_MidTank_OFF/ON + Valve_Set(IDS_Id_to_CartrideValve[IFS_info.Ink.cart_color], action ); //Atm_MidTank_OFF/ON + + ret = true; + return ret; +} + +bool ColorMatch() +{ + bool ret = false; + + ret = true; + return ret; +} + +char CartridgeRequestToken[36+1]; +int CartridgeRequestId = 0x01010101; + + +/** + * + * @brief create massege to PPC + * temporary message RFID will replace that message. + * send a request to PPC to know the MidTank number + * @return OK + */ +uint32_t ReadCartridgeData(cartridge_name cart_name) +{ + //this function triggers reading and validation of the cartridge information + // for now it sends a request to the tablet to inquire for the ink color/midtank id + MessageContainer responseContainer; + CartridgeValidationRequest ValidationReq = CARTRIDGE_VALIDATION_REQUEST__INIT; + Cartridge CartridgeData; + + strcpy(CartridgeRequestToken,"Happy days are coming to us, soon"); + memcpy(&CartridgeRequestToken[32],&CartridgeRequestId,sizeof(CartridgeRequestId)); + CartridgeRequestId++; + + cartridge__init(&CartridgeData); + CartridgeData.has_slot = true; + CartridgeData.slot = CARTRIDGE_SLOT__Ink; + CartridgeData.tag = 0; + CartridgeData.has_index = false; + + ValidationReq.has_action = true; + ValidationReq.action = CARTRIDGE_ACTION__Inserted; + + ValidationReq.cartridge = &CartridgeData; + + responseContainer = createContainer(MESSAGE_TYPE__CartridgeValidationRequest, CartridgeRequestToken, true, &ValidationReq, &cartridge_validation_request__pack, &cartridge_validation_request__get_packed_size); + //------------------------------------------------------------------------------------------- + uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer)); + size_t container_size = message_container__pack(&responseContainer, container_buffer); + my_free(responseContainer.data.data); + //SendChars((char*)container_buffer, container_size); + + return OK; +} + +/** + * + * @brief PPC answers the MidTank number and call WasteTankCBFunction() function. + * temporary message RFID will replace that message. + * + * @return OK + */ +bool CartridgeValidationResponseFunc(MessageContainer* requestContainer) +{ + bool ret = notOK; + CartridgeValidationResponse* response = cartridge_validation_response__unpack(NULL, requestContainer->data.len, requestContainer->data.data); + + Cartridge *cartridge; +// uint32_t midtankId; +// cartridge_name cart_name; + + // chack the massege: + // 1. the cartridge is valid ( autantication) + // 2. the pointer is not 0 !!! + if (response->cartridge == 0 ) + { + REPORT_MSG(WHS_filling," ------------ IFS_filling message from PPC is null!!! ----------------- "); // write to log error + return ret; + } + + if (response->isvalid == true) + { + REPORT_MSG(WHS_filling," ------------ IFS_filling INK cartridge is valid ----------------- "); // write to log error + cartridge = response->cartridge; +// midtankId = cartridge->index; // midtank 1-8 +// cart_name = cartridge->slot; //cart1(INK),cart2(WASTE1),cart3(WASTE1) + if (cartridge->slot == INK_cartridge) + { + IFS_info.Ink.cart_color = cartridge->index; // midtank 1-8 + WHS_info.event = IFS_INK_authentication_pass; + } + WasteTankCBFunction();//call cart is ok + ret = OK; + } + else + { + REPORT_MSG(WHS_filling," ------------ IFS_filling INK cartridge is not valid !!!----------------- "); // write to log error +// cartridge = response->cartridge; +// mimidtankId = cartridge->index; +// cart_name = cartridge->slot; + WHS_info.event = IFS_INK_authentication_fail; + WasteTankCBFunction();// call cart is INVALID + ret = OK; + } +return ret; +} + + +/** + * + * @brief that response is from STUB. replace the PPC + * use stub : Poweroff_progress.cs from P:\Users - Public\Shai_Frieder + * ProgressRequest progressRequest = new ProgressRequest(); + * progressRequest.Amount = 0xCC; //porgress id is 0xCC + * progressRequest.Delay = 1; //midtank number from 0-7 + * + * + * @return OK + */ +void ResponseDemo(int MidtankId) +{ + WHS_info.event = IFS_INK_authentication_pass; + //global mid tank id = MidtankId; + IFS_info.Ink.cart_color = MidtankId; + WasteTankCBFunction();//call cart is ok +} + +bool INKCartridgeAuthentication(cartridge_name cart_name) +{ + bool ret = notOK; + // TBD; + switch (cart_name) + { + case INK_cartridge: + IFS_info.Ink.autheticate = PASSED; //todo + //Pannel_Leds(CART_1, MODE_ON); + break; + + default: + //WHS_info.cartridge_1.autheticate = FAILED; //todo + // WHS_info.cartridge_2.autheticate = FAILED; //todo + //Pannel_Leds(CART_2, MODE_OFF); + //Pannel_Leds(CART_3, MODE_OFF); + break; + } + ret = OK; + return ret; +} diff --git a/Software/Embedded_SW/Embedded/Modules/IFS/ifs.h b/Software/Embedded_SW/Embedded/Modules/IFS/ifs.h index ade89372d..9e06ad69c 100644 --- a/Software/Embedded_SW/Embedded/Modules/IFS/ifs.h +++ b/Software/Embedded_SW/Embedded/Modules/IFS/ifs.h @@ -1,8 +1,11 @@ -/* - * ifs.h +/** + * @file ifs.h * - * Created on: Jul 8, 2019 - * Author: shlomo + * @date Jul 8, 2019 + * @author Shlomo + * + * @date 26.8.2019 + * @author Shai */ #ifndef MODULES_IFS_IFS_H_ @@ -10,7 +13,44 @@ -void IFS_Init(void) +//void IFS_Init(void) +#define SECONDS_5 5 /**< 5seconds */ +#define SECONDS_300 300 /**< 300 seconds = 5 minutes */ +#define SECONDS_600 600 /**< 600 seconds = 10 minutes */ +#define SECONDS_2_DLY 0 /**< dly 0 seconds */ +#define CARTRIDGE_INK_TIMEOUT (SECONDS_300 / SECONDS_5) /**< TimeOut (5 minutes) for finishing transfer the Ink from Ink cartridge to MidTank */ +#define CARTRIDGE_INK_TIMEOUTx2 (SECONDS_600 / SECONDS_5) /**< TimeOut (5 minutes) for second try finishing transfer the Ink from Ink cartridge to MidTank */ +#define CartridgeInkTimeOutCallBackTime (eOneSecond * SECONDS_5) +#define CartridgeInkCallBackTime eOneSecond +#define MidTank_Pressure_EMPTY 200 //todo get from Moti the correct number +#define CARTRIDGE_CAPATICY 1500 //todo get from Moti the correct number +#define FULL 1000 //todo get from Moti the correct number + + + +/** + * A structure to represent IFS_information + */ +struct IFS_information +{ + struct cartridge_params Ink; /**< cartridge_params */ + CarteidgPrecenseSensorStatus IFS_Ink_precense_sensor; /**< INK cartridge presence(1) or not_precense(0) */ + uint32_t Cartridge_Ink_device_Id; /**< INK device_Id number for callback */ + uint32_t Cartridge_Ink_TimeOut_device_Id; /**< INK device_Id number for TimeOut callback */ + uint32_t INK_isOK_flag ; /**< INK cartridge pass authentication (from PPC) */ + double MidTank_capacity; /**< MidTank current capacity */ +}; + +struct IFS_information IFS_info; + +WHS_sensor CartridgeInkCallBackFunction(); +WHS_sensor CartridgeInkTimeOutCallBackFunction(); +uint32_t ReadCartridgeData(cartridge_name cart_name); +bool INKCartridgeAuthentication(cartridge_name cart_name); +bool MidTankValvesAction(bool action); //Cartridge_MidTank_ON of Cartridge_MidTank_OFF +bool ColorMatch(); +bool IFS_TimeOutAlarm(); +void ResponseDemo(int MidtankId); #endif /* MODULES_IFS_IFS_H_ */ -- cgit v1.3.1