Demo entry 6338634

Blutdruckgerät

   

Submitted by anonymous on Dec 15, 2016 at 15:41
Language: C++. Code size: 20.3 kB.

#include "mbed.h"
#include "C12832.h"
#include "rtos.h"
#include "BiQuad.h"
#include <complex.h>
 
 
//Button Defines
#define Button1_Pressed 1
 
//State Defines
 
//Signal Defines
#define Signal_Pump_Start  0x01
#define Signal_Pump_Stop   0x02
#define Signal_Valve_Panic 0x04
 
 
 
// State defines for Aquire-Thread
#define Signal_AQ_Start  0x01
#define Signal_AQ_Stop   0x02
 
// State defines for Analysisy-Thread
#define Signal_AN_Start 0x01
#define Signal_AN_Stop   0x02
 
 
 
// Global:  MBED-Pins & Structs
C12832      lcd(p5, p7, p6, p8, p11);
PwmOut      Pump  (p23);
PwmOut      Valve (p22);
DigitalIn dIn (p30);
AnalogIn    aIn   (p19);
AnalogIn    aInAC (p20);
Serial pc(USBTX, USBRX);
DigitalOut   led(LED1);
DigitalOut      led2(LED2);
 
 
 
void aquire(void const *args);
void display(void const *args);
void analysis(void const *args);
void statemachine(void const *args);
 
 
 
typedef struct {                             // Struckt für Sensordaten
    float    voltage;
          float           voltageAC;
    float    meanVoltage;
    float    mmHg;
          float           mmHgAC;
          float        meanmmHg;
          float        mmHgFilteredLow;
          float           mmHgACFilteredLow;
          float           VOmax;
          float           VOmin;
          float           VDC;
 }sensor_struct;
 
 
MemoryPool<sensor_struct, 10> mpool;                // Init für Message-Queque
Queue<sensor_struct, 16> queue;                     // Init für Message-Queque
/*---------------Globale Variablen (Notlösung)-------------------*/
static float ACmax1[10];
static float VDC1[10];
static float VACmax = 0.0;
static float Ps = 0.0;
static float Pd = 0.0;
static int      maxCounter  = 0;
static float MAD_koef          = 0.0;  
static float MAD = 0.0;
static sensor_struct CurrentSample = {0};                            
bool start  = 0;  
static int state = 0;
static int stateAnalysis = 0;
/*------------------------------------------------------------------*/
 
Thread threadAquire(aquire);                  // ruft Mess-Thread
Thread threadState(statemachine);             // ruft Statemachine-Thread
Thread threadDisplay(display);                // ruft Display-Thread
Thread threadAnalysis(analysis);                                 // ruft Analyse-Thread (vorher threadDisplayAnalysis(analysis);
 
/*-----------------------------------------------------------------------------*/
// Main-Trhead als STATE-Machine für Steuerung
// Quasi EIN-AUS, Displayausgabe bedienen, Buttons bedienen usw.    
int main(){
 
     
Pump  = 0;
Valve = 0;
     
    while(true)
    {
                         
        switch(dIn){
       
            case Button1_Pressed:           // Definiert als 1
 
                Thread::wait(100);          // Warte 100ms --> Taster entprellen
                if(dIn)                     // Wenn Taster 100ms High war, setzte start auf 1
                {
                    start = !start;                         // Toggelt Taster
                   
                    if(start)
                    {
                        threadDisplay.signal_set(Signal_Pump_Start);
                        threadState.signal_set(Signal_Pump_Start);
                    }
                    else
                    {
 
                        threadDisplay.signal_set(Signal_Valve_Panic);    
                        threadState.signal_set(Signal_Valve_Panic);
 
                    }
                                       
                }                
               
            break;
            }  
       
    }      
}
/*-----------------------------------------------------------------------------*/
void aquire(void const *args) {                                                                       // Mess-Thread                                                                  
                                   
    static int aquireState = Signal_AQ_Stop;
     
          float       ADC_int        = 0.0;
    float       ADC_sum        = 0.0;
    float       ADC_realTime   = 0.0;
          float                    ADC_realTimeAC = 0.0;
          float                    max                    = 0.0;
          float                    min                         = 100.0;
    int         counter        = 0;
     
 
          /*----------------------------BIQUAD FILTER LOWPASS Platinen Input--------------------------*/
     
          BiQuad bq1_low( 5.84324e-08, 1.16865e-07, 5.84324e-08, -1.94264e+00, 9.43602e-01 );
          BiQuad bq2_low( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.97527e+00, 9.76247e-01 );
 
          BiQuadChain bqc_low;
     
          bqc_low.add( &bq1_low ).add( &bq2_low );
         
          /*---------------------------BIQUAD FILTER LOWPASS Schaltungs Input-------------------------*/
 
Sponsored Content

    RANKED: The Best NBA Draft Picks of All Time RANKED: The Best NBA Draft Picks of All Time PointAfter | By Graphiq
    The Most Legendary Rushing Season for Every NFL Team The Most Legendary Rushing Season for Every NFL Team PointAfter | By Graphiq
    Nie wieder Rückenschmerzen mit diesem Training Nie wieder Rückenschmerzen mit diesem Training www.mybellance.com 

    Homesick General Eisenhower Writes Lovingly to Wife Mamie at Christmas time Homesick General Eisenhower Writes Lovingly to Wife Mamie at Christmas time Shapell
    25 Real Women on the Sexiest Body Part in a Man 25 Real Women on the Sexiest Body Part in a Man Men's Fitness
    Obese to Beast: Guy Bravely Strips Down to Show What Extreme Weight Loss Does to Skin Obese to Beast: Guy Bravely Strips Down to Show What Extreme Weight Loss Does to Skin Muscle & Fitness 

Recommended by
         
          BiQuad bq1_low2( 5.84324e-08, 1.16865e-07, 5.84324e-08, -1.94264e+00, 9.43602e-01 );
          BiQuad bq2_low2( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.97527e+00, 9.76247e-01 );
 
          BiQuadChain bqc_low2;
     
          bqc_low2.add( &bq1_low2 ).add( &bq2_low2 );
 
          /*------------------------------------------------------------------------------------------*/
         
      while(true)
      {
 
               for(counter=1;counter<=1000;counter++)          
               {      
                   
       
                ADC_int += aIn.read();               // Soll 1000 mal den Wert von AnalogIn lesen und aufsummieren
                ADC_realTime   = aIn.read();         // Echtzeitwert lesen
                                        ADC_realTimeAC = aInAC.read();
 
                                        CurrentSample.voltage         = ADC_realTime*3.3f;
                                        CurrentSample.voltageAC = (ADC_realTimeAC*3.3f)-(1.5f);     //-1.5f entfernt offset der Summierer-Schaltung
                   
                         /*_____________Koeffizient für mmHg anpassen________*/
                                        if(CurrentSample.voltage<=1.0)
                                        {
MAD_koef = 75.0f;
                                             }
                   
                                        else if(CurrentSample.voltage<=1.5)
                                        {
MAD_koef = 81.5f;
                                             }
                   
                                        else if(CurrentSample.voltage<=1.8)
                                        {
MAD_koef = 84.0f;
                                             }
 
                                        else if(CurrentSample.voltage<=2.4)
                                        {
MAD_koef = 85.5f;
                                             }                                           
 
                                        else if(CurrentSample.voltage<=3.0 || CurrentSample.voltage>=3.0)
                                        {
MAD_koef = 86.9f;
                                             }                                                                                         
                         /*__________________________________________________*/                                    
                   
                                        CurrentSample.mmHg       = (CurrentSample.voltage * MAD_koef);   
                                        CurrentSample.mmHgAC = (CurrentSample.voltageAC * MAD_koef);
                                        CurrentSample.mmHgFilteredLow  = (float)bqc_low.step ( CurrentSample.mmHg );              // Filterung anwenden für Drucktreppe
                                        //CurrentSample.mmHgACFilteredLow = (float)bqc_low2.step ( CurrentSample.mmHgAC);         // Filterung anwenden für Oszillationen
                                             
                                             
                         /*----------------------------MIN & MAX bestimmen-----------------------------*/              
                                        if(state == Signal_AQ_Start)
                                        {
                                             
                                             
                                        if(max < CurrentSample.voltageAC)                                                                   // Max und Min-Wert bestimmen
                                        {
                                                  max = CurrentSample.voltageAC;
                                                 
                                             }
                                       
                                        if(min > CurrentSample.voltageAC)
                                        {
                                                  min = CurrentSample.voltageAC;
 
                                             }   
 
                                                            CurrentSample.VOmax  = max;                                                                        
                                                            CurrentSample.VOmin =    min;
                                                            CurrentSample.VDC = CurrentSample.voltage;                       // Sollte man evtl vorher digital TP filtern
                                             
                                             
                                             }
                                              if (state == Signal_AQ_Stop)
                                             {
                                       
 
                                                            max = 0.0;                                                                           
                                                            min = 1000.0;
                                                            //CurrentSample.meanmmHg = 0.0;
                                                 
                                             }
                              /*---------------------------------------------------------------------------*/
                             
 
                  pc.printf("%d,%d,%d\r",(int)CurrentSample.mmHgAC,(int)(CurrentSample.VOmax*MAD_koef),(int)(CurrentSample.VOmin*MAD_koef));
                                             
                     Thread::wait(1);                                                                                     // 1ms Wartezeit macht 1000 Messungen Pro 2sekunden                                                                          
        }
                   
                         ADC_sum = ADC_int*3.3;                                                                 // Ausgangsspannung des Sensors (3.3 MBED Ref.)
                         CurrentSample.meanVoltage = ADC_sum / counter;                                       // Mittelwert bilden
                      CurrentSample.meanmmHg =  CurrentSample.meanVoltage  * MAD_koef;   
                         maxCounter++;
                   
                    if(maxCounter < 10)
                    {
                         ACmax1[maxCounter] = CurrentSample.VOmax;
                         VDC1[maxCounter]      = CurrentSample.VDC;
                         }
                   
                    if(maxCounter>=10)
                    {
                    stateAnalysis = Signal_AN_Start;                                                                                        // Startet Analyse für MAD
                    maxCounter = 0;
                    }
                   
               }
}
 
/*-----------------------------------------------------------------------------*/
 
void display(void const *args)                  // Display Ausgabe-Thread
{  
     osEvent evt;
     
while(true)
    {
       
                    evt = Thread::signal_wait(0,100);
               
        if (evt.value.signals & Signal_Pump_Start)
        {  
            lcd.cls();
            lcd.locate(0,1);
            lcd.printf("Pumpe Start");
            Thread::wait(1000);
                 
        }
         if (evt.value.signals & Signal_Valve_Panic)
        {  
            lcd.cls();
            lcd.locate(0,1);
            lcd.printf("Pumpe Stop + Ventil auf");
            Thread::wait(1000);
        }
        if (evt.value.signals & Signal_Pump_Stop)
        {  
            lcd.cls();
            lcd.locate(0,1);
            lcd.printf("Pumpe Stop");
            Thread::wait(1000);
        }
                             
                    lcd.locate(0,10);
                    lcd.printf("mmHg:%f",CurrentSample.mmHg);
                    lcd.locate(0,20);
                    lcd.printf("%f, %f, %f",MAD,Ps,Pd);
                   
        Thread::wait(50);    
 
         
 
    }
 
}
/*-----------------------------------------------------------------------------*/
 
 
 
void statemachine(void const *args) {           // Statemachine
 
    int State =  0;
          osEvent evt ;
     
          while(true)
    {
                   
                    evt = Thread::signal_wait(0,10);
                    State |= evt.value.signals;
               
 
               
       
        if (evt.value.signals & Signal_Valve_Panic)
        {
            State =  Signal_Pump_Stop + Signal_Valve_Panic;
                              Pump  = 0;
            Valve = 0;
        }
        else if (evt.value.signals & Signal_Pump_Stop)
        {
            State &= ~Signal_Pump_Start;
                              Pump = 0;
        }
        else if (State & Signal_Pump_Start)
        {  
           
            Pump  = 1;
            Valve = 1;
 
            if(CurrentSample.mmHg  >= 160)
            {  
             
                                   Pump = 0;
                                   Thread::wait(800);  
                                   
                                   for(int u = 0; u < 10; u++)
                                   {
                                       
                                       
                                     evt = Thread::signal_wait(0,0);
                                        State |= evt.value.signals;
                                       
                                          if (evt.value.signals & Signal_Valve_Panic)
                                             {
                                                       State =  Signal_Pump_Stop + Signal_Valve_Panic;
                                                       Pump  = 0;
                                                       Valve = 0;
                                                       break;
                                             }
                                             else if (evt.value.signals & Signal_Pump_Stop)
                                             {
                                                       State &= ~Signal_Pump_Start;
                                                       Pump = 0;
                                                       break;
                                             }
                                       
                                        state = Signal_AQ_Start;
 
                                        Thread::wait(3500);
 
                                        state = Signal_AQ_Stop;
                                             
                                        Valve = 0;
                                     Thread::wait(150);
                                     Valve = 1;
                                        Thread::wait(1000);
                                   }
                                                 
                                        State &= ~Signal_Pump_Start;
                                        Valve = 0;
                                        start  = 0;  
                                   
            }
                   
        }
                   
                    Thread::wait(100);
       
    }
 
}
 
 
/*------------------------------------------------------------------------------*/
 
 
void analysis(void const *args){                                        // Berechnungs-Thread        
   
int i;
int maximum   = 0; 
float VDCfind = 0.0;
float Ks[7] = {0.64,0.58,0.57,0.52,0.45,0.29,0.5};
               float Kd[7] = {0.5,0.5,0.6,0.78,0.85,0.82,0.75};
 
     while(true)
          {
     
               /*---------------------Ermittelung MAD----------------*/
               
          if(stateAnalysis == Signal_AN_Start)
          {    
               led = 1;
               Thread::wait(300);
               
               
               for(i=0; i<10;i++)
               {
                   
                    if(ACmax1[i]>ACmax1[maximum])
                    {
                         maximum = i;
                         VACmax = ACmax1[maximum];
                         VDCfind= VDC1[maximum];
                         MAD = (VACmax + VDCfind) * MAD_koef;
                    }
                   
                    }
                   
           
               /*------------------Systolischer Blutdruck-----------*/
               if(MAD <= 70)
               {
                    Ps = Ks[1]*MAD;
               }
               
               else if(MAD <= 110)
               {
                    Ps = Ks[2]*MAD;
               }
               
               else if(MAD <= 120)
               {
                    Ps = Ks[3]*MAD;
               }             
               
               else if(MAD <= 135)
               {
                    Ps = Ks[4]*MAD;
               }
 
               else if(MAD <= 150)
               {
                    Ps = Ks[5]*MAD;
               }             
 
               else if(MAD <= 200 || MAD >=200 )
               {
                    Ps = Ks[6]*MAD;
               }             
               
               /*------------------Diastolischer Blutdruck-----------*/
               
               if(MAD <=50)
               {
                    Pd = Kd[1]*MAD;
               }
               
               else if(MAD <=60)
               {
                    Pd = Kd[3]*MAD;
               }
               
               else if(MAD <=120)
               {
                    Pd = Kd[4]*MAD;
               }             
               
               else if(MAD <=140)
               {
                    Pd = Kd[5]*MAD;
               }             
 
               else if(MAD <=180 || MAD >= 180)
               {
                    Pd = Kd[6]*MAD;
               }   
               
               stateAnalysis = Signal_AN_Stop;
         
               Thread::wait(300);
               led=0;
                   
               }
                     
          }    
}
     
 
/*-----------------------------------------------------------------------------*/
 
 

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).