Nefer Miranda / Mbed 2 deprecated BLUETOOTH_RTC_PID

Dependencies:   mbed TextLCD

main.cpp

Committer:
nmirandal
Date:
2019-04-05
Revision:
1:8596494b7d9d
Parent:
0:d66493246074
Child:
2:ecd33165145b

File content as of revision 1:8596494b7d9d:

#include "mbed.h"
#include <stdio.h>
#include "ds3231.h"
#include "TextLCD.h"

Serial master(USBTX,USBRX);// Tx, Rx Computador
Serial GSM(PTE22,PTE23);// Tx, Rx Módulo de bluetooth
Ds3231 rtc(A4, A5);


/*
DigitalOut LedVerde(LED2);
DigitalOut LedRojo(LED1);
DigitalOut LedAzul(LED3);
*/


int vo, hb, lb;
int  P, I, D,S,AAAA=2000,MM,DD,wd=1,HH,mm,ss,mode,am_pm;
float p,i,d,s,q0,q1,q2,e,e_1,e_2,u,u_1;
char command[16], control='F';
float Kp=12.0, Ki=5.0, Kd=0.02, sp=2.0/3.3, T=0.02;
bool PID,CLK;
uint8_t vo_lb, vo_hb, j, aa;
uint16_t rtn_val;
char chain[40], character;

ds3231_time_t hora = {HH,mm,ss=45,am_pm,mode};//{horas,minutos,segundos,AM/PM,12/24H}
ds3231_calendar_t fecha= {AAAA,MM,DD,wd};//{años,mes,días,dia de semana}


AnalogIn input(PTB1); // voltage measurement
AnalogOut  u_control(PTE30);// Input voltage for the circuit
DigitalOut LedVerde(LED2);

TextLCD lcd(PTE5, PTE4, PTE3, PTE2, PTB11, PTB10); // rs, e, d4-d7

Timer k,t,rtc_t;

//########### Reads a incoming chain of characters #############################
void get_chain()
{
    j=0;
    k.start();
    while(1) {


        if(GSM.readable()) {
            character=GSM.getc();
            chain[j]=character;

            j++;
        }
        if(k.read()>.2) {
            k.stop();
            k.reset();
            break;
        }
    }
}

//############### Removes all the data in the buffer ###########################
void reset_var()
{
    for (int i=0; i<sizeof(chain); i++) {
        chain[i]='\0';
    }
}


//########### This function configures the RTC  ################################

void set_rtc()
{
    ds3231_time_t hora = {HH, mm, ss, am_pm, 1};
    ds3231_calendar_t fecha= {AAAA%100,MM,DD,wd};

    if(!rtc.set_calendar(fecha)) {

        master.printf("\r\n\r\n         Reloj Configurado el %d / %d / %d ",DD,MM,AAAA);

        if(!rtc.set_time(hora)) {
            if(hora.am_pm)master.printf("a las %d:%d:%d PM\r\n\r\n\r\n",HH,mm,ss);
            else master.printf("a las %d:%d:%d AM\r\n\r\n\r\n",HH,mm,ss);
        }
    }

}


//############## This function processes the received data depending on what
//############## it's suppoused to be for #####################################


void request()
{
    lcd.cls();
    if(chain[0]=='R') {
        control='N';



    } else if (chain[0]=='S') {
        control='F';



    } else if (chain[0]=='P') {
        sscanf (chain,"%*s %d%f%*s %d%f%*s %d%f%*s %d%f",&P,&p,&I,&i,&D,&d,&S,&s);
        master.printf ("P= %d\r\np= %f\r\nI= %d\r\ni= %f\r\nD= %d\r\nd= %f\r\nS= %d\r\ns= %f\r\n",P,p,I,i,D,d,S,s);
        Kp=P+p;
        Ki=I+i;
        Kd=D+d;
        sp=(S+s)/3.3;
        master.printf ("Kp= %f\r\nKi= %f\r\nKd= %f\r\nsp= %f\r\n",Kp,Ki,Kd,3.3*sp);



    } else if (chain[0]=='A') {
        sscanf (chain,"%*s %d%*s %d%*s %d%*s %d%*s %d",&AAAA,&MM,&DD,&HH,&mm);
        master.printf ("AAAA= %d\r\nMM= %d\r\nDD= %d\r\nHH= %d\r\nmm= %d\r\n",AAAA,MM,DD,HH,mm);
        if(HH>12) {
            HH=HH-12;
            am_pm=1;
        } else {
            if(HH==0)HH=12;
            am_pm=0;
        }

        set_rtc();

    } else if (chain[0]=='K') {
        CLK=1;

    } else if (chain[0]=='J') {
        PID=!PID;

    } else if (chain[0]=='k') {
        CLK=0;
        lcd.locate(0,0);
        lcd.printf("No device is");
        lcd.locate(0,1);
        lcd.printf("connected!");
    } else if (chain[0]=='j') {
        PID=0;
        lcd.locate(0,0);
        lcd.printf("No device is");
        lcd.locate(0,1);
        lcd.printf("connected!");
    }
}

//############### This is a function which manages the incoming data ###########

void in_data()
{
    get_chain();
    master.printf("\r\n%s\r\n",chain);
    request();
    reset_var();
}









//########################################## Main starts here ##################



int main(void)
{

    GSM.baud(9600);
    GSM.format(8,Serial::None,1);
    LedVerde=1.0;

    t.start();
    rtc_t.start();

    GSM.attach(&in_data, Serial::RxIrq);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Welcome!");
    lcd.locate(0,1);
    lcd.printf("RTC & PID");
    while(1) {


        vo = input.read_u16();
        vo_hb=vo/256;       //calculo la cifra mas significativa
        vo_lb=vo-vo_hb*256;     //calculo la cifra menos significativa
        GSM.putc(vo_hb);
        GSM.putc(vo_hb);   //mas significativa primero, menos despues si no no funciona!!! y con la orden PUTC solo asi le envia binarios

        /*
                vo_lb=vo&0xFF;
                vo_hb=vo>>8;
                GSM.putc(vo_lb);
                GSM.putc(vo_hb);

        if(vo<256) {          //debo generar dos casos a APP inventor solo me recibe hex asi: 0xhhhh (4 cifras)
            GSM.putc(0);     //si el numero es hasta 255 se le ponen dos ceros adelante a la secuencia de bits
            GSM.putc(vo);     //luego la cifra menos significativa
        }
        if(vo>255) {         //pero si es mayor a 255 las cifras deben ser convertidas a un hex de dos bytes de la siguiente forma
            vo_hb=vo/256;       //calculo la cifra mas significativa
            vo_lb=vo-vo_hb*256;     //calculo la cifra menos significativa
            GSM.putc(vo_hb);
            GSM.putc(vo_hb);   //mas significativa primero, menos despues si no no funciona!!! y con la orden PUTC solo asi le envia binarios
        }

        */
        if(t.read()>T && PID) {
            if(control=='N') {
                e=sp-1.0*input;

                q0=Kp+Ki*T/2.0+Kd/T;
                q1=-Kp+Ki*T/2.0-2.0*Kd/T;
                q2=Kd/T;

                u=u_1+q0*e+q1*e_1+q2*e_2;

                u_1=u;
                e_1=e;
                e_2=e_1;


                if(u>1.0)u=1.0;
                else if(u<0.0)u=0.0;
                LedVerde=0.0;
            } else {
                u=0.0;
                LedVerde=1.0;
            }

            u_control=u;
            t.stop();
            t.reset();
            t.start();

        } else if (t.read()>T && PID) {
            u=0.0;
            LedVerde=1.0;
        }// end for  if(t.read()>T)



        if(rtc_t.read()>=1.0 && CLK) {

            rtc.get_calendar(&fecha);
            rtc.get_time(&hora);
            if(aa==99 && fecha.year==0) {
                AAAA++;
                lcd.cls();
            }
            aa=fecha.year;
            if(hora.seconds==0)lcd.cls();
            if(hora.am_pm) {
                master.printf("HORA: %d:%d:%d PM  ",hora.hours,hora.minutes,hora.seconds);
                lcd.locate(14,0);
                lcd.printf("PM");
            } else {
                master.printf("HORA: %d:%d:%d AM    ",hora.hours,hora.minutes,hora.seconds);
                lcd.locate(14,0);
                lcd.printf("AM");
            }
            lcd.locate(0,0);
            lcd.printf("HORA %d:%d:%d",hora.hours,hora.minutes,hora.seconds);
            master.printf("FECHA %d / %d / %d \r\n",fecha.date,fecha.month,AAAA);
            lcd.locate(0,1);
            lcd.printf("FECHA %d/%d/%d",fecha.date,fecha.month,AAAA);


            rtc_t.stop();
            rtc_t.reset();
            rtc_t.start();


        } else if (rtc_t.read()>=0.1 && PID) {

            lcd.locate(0,0);
            lcd.printf("Voltaje: %.2f V",3.3*input.read());
            lcd.locate(0,1);
            lcd.printf("Set_point: %.2f V",3.3*sp);
            rtc_t.stop();
            rtc_t.reset();
            rtc_t.start();
        } else if(rtc_t.read()>=1.0 && !PID && !CLK) {
            // lcd.cls();
        }//end for if(rtc_t.read()>=1.0)




    }//end for while(1)
}//end for void main()