aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/IDS/IDS_init.c
blob: b035ff97b5182dba943f3a3ab9e9a6b7e5b8a4d7 (plain)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; 
/************************************************************************************************************************
 **************************************************************************************************************************/

#include <DataDef.h>
#include "include.h"

#include "ids.h"
#include "PMR/common/MessageContainer.pb-c.h"
#include "PMR/Hardware/HardwareDispenser.pb-c.h"
#include "PMR/Hardware/HardwarePidControl.pb-c.h"
#include "PMR/Printing/JobSpool.pb-c.h"
#include "PMR/Printing/JobSpoolType.pb-c.h"

#include "drivers/Motors/Motor.h"
#include "drivers/ADC_Sampling/adc.h"

#define MAX_CONTROL_SAMPLES 10

HardwareDispenser DispensersCfg[ MAX_SYSTEM_DISPENSERS];

TimerMotors_t DispenserIdToMotorId[MAX_SYSTEM_DISPENSERS] = {HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_2,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_3,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_4,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_5,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_6,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_7,HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_8};

float DispenserPressure[MAX_SYSTEM_DISPENSERS] = {0};

float GetDispenserPressure(int DispenserId)
{
    return DispenserPressure[DispenserId];
}
float Vlow,VHigh,Bits_0Pascal,Bits_1MPascal,a,b;
void Calculateinit (void)
{
    Vlow = 0.004 * 130; // 4mA*130ohm  = 0.52 V(0 Pascal)
    VHigh = 0.020 *130; // 20mA*130ohm  = 2.6V (1M Pascal)

    Bits_0Pascal = Vlow * 4096 / 3; // Vlow * 4096bit / 3Vref = 709.973328 bits
    Bits_1MPascal = VHigh * 4096 / 3; // VHigh * 4096bit / 3Vref = 3549.86646 bits

    a = (1 - 0) / (Bits_1MPascal - Bits_0Pascal);// Mpascal/Bits = 0.000352125

    b = 0 - (a * Bits_0Pascal ); // y = ax + b (x= Bits_0Pascal, y = 0)

}
float CalculateDispenserPressure (int DispenserId)
{
    unsigned short data = 0;
    float temp = 0.0;

/*
    data = ADC_GetReading(DispenserIdToPressureSensorId[DispenserId]);
    temp = 4096 - data;
    temp *= 3;
    temp *= 1000; //move to mv
    temp /= 4096;
    DispenserPressure[DispenserId] = 3000 - temp;
    */
    data = ADC_GetReading(DispenserIdToPressureSensorId[DispenserId]);

    if(data<Bits_0Pascal)
        data = Bits_0Pascal;//40mA 0Bar

    temp = a * data + b;

    DispenserPressure[DispenserId] = temp; // [Mpascal] ( 1 Mpascal = 10 Bar )

    //unmark one of them to convert from Mpascal to :
    //-----------------------------------
    //DispenserPressure[DispenserId] /= 1000000; //Pascal
    //DispenserPressure[DispenserId] *=10;        //Bar;
    //DispenserPressure[DispenserId] *=10000000;        //uBar;
    //DispenserPressure[DispenserId] *=145.0377;  //PSI;

    DispenserPressure[DispenserId] *= 10; //MPascal -> Bar

    return(DispenserPressure[DispenserId]);
}


uint32_t DispenserConfigMessage(HardwareDispenser * request)
{
    uint32_t status = PASSED;
    int Dispenser_i;

    Dispenser_i = request->hardwaredispensertype;
    if (Dispenser_i< MAX_SYSTEM_DISPENSERS)
    {
        memcpy (&DispensersCfg[Dispenser_i],request,sizeof(HardwareDispenser));
        return status;
    }
    else return Dispenser_i;

}