Some problems with structure and array . . .

Discussion in 'C++' started by boschow, Oct 17, 2007.

Thread Status:
Not open for further replies.
  1. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,
    i am having some problem with this part of the program written in C++ and i don't know what is wrong if you could please check it out . . .

    Code:
    #include <iostream>
    
    using namespace std;
    typedef struct {
        float pid_output;
        float pid_out_last;
        float error;
        float error_act;
        float error_last;
        float temp_p;
        float temp_i;
    }pid_para_global;
    
    typedef struct {
        float setpoint[4];
        float actual[4];
        float p_par[4];
        float i_par[4];
        float dead_band[4];
    }pid_para;
        
    float PID(pid_para_global *pid_glob, pid_para *pid_para)
    {  
        if ((pid_para->actual[0] > (pid_para->dead_band[0] + pid_para->setpoint[0]))
        || (pid_para->actual[0] < (pid_para->setpoint[0] - pid_para->dead_band[0])))
        {
           pid_glob->error_act = pid_para->setpoint - pid_para->actual;
           pid_glob->error = pid_glob->error_act - pid_glob->error_last;
        } else { pid_glob->error = 0; }
        // error_last = error;
        pid_glob->temp_p = pid_para->p_par[0] * pid_glob->error;
        pid_glob->temp_i = pid_para->p_par[0] * (pid_glob->error / pid_para->i_par[0]);
        pid_glob->pid_out_last = pid_glob->temp_p + pid_glob->temp_i;
        pid_glob->pid_output = pid_glob->pid_output + pid_glob->pid_out_last;
        if (pid_glob->pid_output < 0) pid_glob->pid_output = 0;
        if (pid_glob->pid_output > 100) pid_glob->pid_output = 100;
        return pid_glob->pid_output;
    }
    
    int main()
    {
        float rezult;
        pid_para_global *pid_global_par;
        pid_para *pid_par;
        cout << "P parameter value? " << endl;
        cin >> pid_par->p_par[0];
        cout << "I parameter value? " << endl;
        cin >> pid_par->i_par[0];
        cout << "Value of deadband " << endl;
        cin >> pid_par->dead_band[0];
        cout << "Setpoint value ?" << endl;
        cin >> pid_par->setpoint[0];
        do {    
        cout << "Actual value ?" << endl;
        cin >> pid_par->actual[0];
        rezult = PID(pid_global_par, pid_par);
        cout << "Calculation rezult is " << rezult << endl << endl;
        }while (pid_par->actual[0] != 0);
    }
    
    After i insert the last value the program closes . . . and i don't know why . . . Without making structures and arrays it was working perfectly now it doesn't. Could you please check out what is wrong ?

    Thanks for your time and help,
    Best regards,
    BoSCHoW.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,345
    Likes Received:
    382
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice