Fijense unicamente en la parte del codigo que trata de la comunicacion, el resto del codigo trata de representar el modelo de un paciente anestesiado durante una intervencion quirurgica.
CODIGO DE ARDUINO
#include <stdlib.h>
#include <stdio.h>
double m=1.0; //la escala
int WT=2; //peso del paciente en kg
byte HT=2; //altura del paciente en cm
byte Age= 2; //edad del paciente
byte Gender=2; //Género del paciente, 1 si es mujer, 0 si es hombre
byte t= 2; // periodo de muestreo en segundoscahr
char MARCA='254';
float tiempo;
//variables del modelo
float bisref=0.0;
float Kp=0.0;
float Ki=0.0;
float alfa=0.0;
float beta=0.0;
float bis0=0.0;
float bismin=0.0;
float ec50=0.0;
float g=0.0;
float LBM=0.0;
float V1=0.0;
float V2=0.0;
float V3=0.0;
float CL1=0.0;
float CL2=0.0;
float CL3=0.0;
float k12=0.0;
float k13=0.0;
float k21=0.0;
float k31=0.0;
float k10=0.0;
float ke0=0.0;
float a1=0.0;
float a2=0.0;
float a3=0.0;
float a4=0.0;
float a5=0.0;
float a6=0.0;
float a7=0.0;
float a8=0.0;
float Q11=0.0;
float Q12=0.0;
float Q13=0.0;
float Q14=0.0;
float Q21=0.0;
float Q22=0.0;
float Q23=0.0;
float Q24=0.0;
float Q31=0.0;
float Q32=0.0;
float Q33=0.0;
float Q34=0.0;
float Q41=0.0;
float Q42=0.0;
float Q43=0.0;
float Q44=0.0;
void setup() {
Serial.begin(9600);
Serial.flush();
}
void serialEvent(){
WT= Serial.read();
}
void loop(){
Serial.flush();
while (MARCA=='254'){
MARCA=Serial.read();}
MARCA='254';
/* Serial.flush();
while (WT==2){
WT= Serial.read();
}
Serial.print('y');*/
/*
while (HT==2){
HT= Serial.read();
}
Serial.print('y');
while (Age==2){
Age= Serial.read();
}
Serial.print('y');
while (Gender==2){
Gender = Serial.read();
}
Serial.print('y');
while (t==2){
t = Serial.read();
}
Serial.print('y');
while (MARCA==254){
MARCA=Serial.read();
}
Serial.print('y');
MARCA=254;
*/
// comvertir los parametros a variables de tipo double
//WT=float(WT);
// HT=float(HT);
// Age=float(Age);
//Gender=float(Gender);
// t=float(t);
float T=t*m/60.0; //pasar el periodo de muestreo a minutos
if (Gender==1.0){
LBM = 1.1 * WT - 128.0*pow((WT/HT),2.0);
}
else if (Gender==0.0){
LBM = (m*1.07) * WT - 148.0 * m * pow(WT/HT,2.0);
}
else{
// Serial.print("Gender debe ser 0 ó 1. Por defecto se toma Gender=1 (MALE)");
Gender = 1.0;
LBM =(1.1*m) * WT - 128.0 * m * pow(WT/HT,2.0);
}
// Parámetros del controlador PI
bisref = 50.0*m;
Kp = 0.25*m;
Ki = 0.1*m;
alfa = Kp + Ki* T/(2.0*m);
beta = -Kp + Ki*T/(2.0*m);
//condiciones iniciales del paciente
bis0 = 95*m;
bismin = 8.9*m;
ec50 = 4.94*m;
g = 2.69;
V1 = 4.27; //volumen del compartimento uno
V2 = 18.9 - 0.391*(Age-53.0); //volumen del compartimento dos
V3 = 238.0*m; //volumen del compartimento tres
CL1 = 1.89+0.0456*(WT-77.0) - 0.0681*(LBM-59.0)+0.0264*(HT-177.0);
CL2 = 1.29-0.024*(Age-53.0);
CL3 = 0.836;
k12 = CL2*m / V1;
k13 = CL3*m / V1;
k21 = CL1*m / V2;
k31 = CL1*m / V3;
k10 = CL1*m / V1;
ke0 = 0.456*m;
float parametros []={V1,WT,HT,Gender,Age,t,CL3,k12,k13,k21,k31,k10,ke0,alfa,beta,Kp,Ki,bisref};
Serial.print('y');
for (int n=0; n<=17; n++){
Serial.print (parametros[n],4);
delay(5);
Serial.print('y');
while (MARCA=='255'){
MARCA=Serial.read();}
MARCA=255;
}
a1=-(k10+k12+k13);
a2=k21*V2/V1;
a3=k31*V3/V1;
a4=k12*V1/V2;
a5=-k21;
a6=k13*V1/V3;
a7=-k31;
a8=ke0;
float bisini[] = {bis0};
float bolo = 1.45*m;
float Tbolo = 1.0; // tiempo del bolo en min
float vel_mgkgh = bolo/(Tbolo/60.0);
float vel_bolo = (bolo*WT*Tbolo*m)/(1000.0*V1);
float tfinal = 40.0; // tiempo de simulacion
float kfin = round(tfinal*m/T);
float x1[] = {0.0,0.0,0.0,0.0};
float x2[] = {0.0,0.0,0.0,0.0};
float x3[] = {0.0,0.0,0.0,0.0};
float x4[] = {0.0,0.0,0.0,0.0};
float vel[] = {vel_bolo};
float bisdiscreto[] = {bis0*m};
for (int i=1; i<=kfin-1; i++){
if (i <= (Tbolo*m/T)-1){
vel[i] =vel_bolo;
}
else{
float ebis = bisref - bisdiscreto[i-1];
float ebisant = ebis;
vel[i] = vel[i-1] - (alfa*ebis/m + beta*ebisant/m);
if (vel[i] < 0.0){
vel[i] = 0.0;
}
if (vel[i] > m*0.03){
vel[i] = m*0.03;
}
}
// METODO RUNGE KUTTA CLASICO 4 ORDEN
// CALCULO DE LOS COEFICIENTES DE LA CONCENTRACION EN EL COMPARTIMENTO 1 (X1):
Q11= a1*x1[i-1]/m + a2*x2[i-1]/m + a3*x3[i-1]/m +vel[i];
Q21= a1*(x1[i-1] + T*Q11/(pow(2.0*m,2.0)))/m + a2*(x2[i-1]+ T*Q11/(pow(2.0*m,2.0)))/m + a3*(x3[i-1] + T*Q11/(pow(2.0*m,2.0)))/m +vel[i];
Q31= a1*(x1[i-1] + T*Q21/(pow(2.0*m,2.0)))/m+ a2*(x2[i-1] + T*Q21/(pow(2.0*m,2.0)))/m + a3*(x3[i-1]+ T*Q21/(pow(2.0*m,2.0)))/m+vel[i];
Q41= a1*(x1[i-1] + T*Q31/pow(m,2.0))/m + a2*(x2[i-1] + T*Q31/pow(m,2.0))/m + a3*(x3[i-1]+ T*Q31/pow(m,2.0))/m +vel[i];
// CALCULO DE LOS COEFICIENTES DE LA CONCENTRACION EN EL COMPARTIMENTO 2 (X2):
Q12= a4*x1[i-1]/m+a5*x2[1,i-1]/m;
Q22= a4*(x1[i-1]+T*Q12/(pow(2.0*m,2.0)))/m +a5*(x2[i-1]+T*Q12/(pow(2.0*m,2.0)))/m;
Q32= a4*(x1[i-1]+T*Q22/(pow(2.0*m,2.0)))/m+a5*(x2[i-1]+(T*Q22/(pow(2.0*m,2.0))))/m;
Q42= a4* (x1[i-1]+T*Q32/pow(m,2.0))/m+a5*(x2[i-1]+T*Q32/pow(m,2.0))/m;
// CALCULO DE LOS COEFICIENTES DE LA CONCENTRACION EN EL COMPARTIMENTO 3 X3
Q13= a7*x3[i-1]/m + a6*x1[i-1]/m;
Q23= a7*(x3[i-1] + (T*Q13/(pow(2.0*m,2.0))))/m + a6*(x1[i-1]+(T*Q13/(pow(2.0*m,2.0))))/m;
Q33= a7*(x3[i-1] + (T*Q23/(pow(2.0*m,2.0))))/m + a6*(x1[i-1]+(T*Q23/(pow(2.0*m,2.0))))/m;
Q43= a7*(x3[i-1]+ T*Q33/pow(m,2.0))/m + a6*(x1[i-1]+T*Q33/pow(m,2.0))/m;
// CALCULO DE LOS COEFICIENTES DE LA CONCENTRACION EN EL COMPARTIMENTO 4 X4
Q14= a8*x1[i-1]/m-a8*x4[i-1]/m;
Q24= a8*(x1[i-1]+(T*Q14/(pow(2.0*m,2.0))))/m-a8*(x4[i-1]+(T*Q14/(pow(2.0*m,2.0))))/m;
Q34= a8*(x1[i-1]+(T*Q24/(pow(2.0*m,2.0))))/m-a8*(x4[i-1]+(T*Q24/(pow(2.0*m,2.0))))/m;
Q44= a8*(x1[i-1]+ T*Q34/pow(m,2.0))/m-a8*(x4[i-1]+(T*Q34/pow(m,2.0)))/m;
// CALCULO DE LAS CONCENTRACIONES DE LOS COMPARTIMENTOS DEL PACIENTE
x1[1,i]= x1[i-1]+(T/(6.0*m))*(Q11 + 2.0*Q21 + 2.0*Q31 + Q41);
x2[1,i]= x2[i-1]+(T/(6.0*m))*(Q12 + 2.0*Q22 + 2.0*Q32 + Q42);
x3[1,i]= x3[i-1]+(T/(6.0*m))*(Q13 + 2.0*Q23 + 2.0*Q33 + Q43);
x4[1,i]= x4[i-1]+(T/(6.0*m))*(Q14 + 2.0*Q24 + 2.0*Q34 + Q44);
// bis discreto
bisdiscreto[i] = bis0 - (bis0-bismin)*(pow(x4[i],g))/(pow(x4[i],g)+pow(ec50/1000.0,g));
tiempo=T*i;
Serial.print('y');
Serial.print (vel[i],5);
while (MARCA==255){
MARCA=Serial.read();}
MARCA=255;
Serial.print (bisdiscreto[i],5);
while (MARCA==255){
MARCA=Serial.read();}
MARCA=255;
Serial.print (tiempo,5);
while (MARCA==255){
MARCA=Serial.read();}
}
}