1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Some problems with structure and array . . .

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

  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 and i don't know what is wrong if you could please check it out . . .
    Code:
    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. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    See another of today's posts regarding programs closing in some manner that seems premature to you. It is not strange. It is appropriate behavior. The deficiency is not in the program, but in your inappropriate thought.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    I sense that you have not allocated some memory for which you have a crash in the program and for us to check the program we probably need the structure as well.
     
  4. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Well the whole code is here, so please check it out. Further more i will search on the forum and will try to find the answer for this error.

    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);
    }
    
    Thanks and best regards,
    BoSCHoW.
     
  5. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    I solved the problem but a new question is running through my mind lets say if i want to have lets say 5 PID regulators all i have to do is add a few arrays to the variables in the struct . . . and this should look something like this ?

    Code:
    
    typedef  struct {
            int pid_output[4];
            int pid_error[4];
            .
            .
            .
    }pid_var; // this are PIDs variables that change during the // calculation
    
    typedef  struct {
             int p_par[4];
             int i_par[4];
             .
             .
             .
    } pid_para; // this are the parameters set by the user
    
    It is possible to do something like this for easier programing to make a struct that contains lets say 5 arrays
    of all the variables parameters and internal variables used for calculation ... the code should look something like this :

    Code:
    typedef struct {
             .
             .
             .
    } pid_para;
    
    typedef struct {
            .
            .
            .
    }pid_var;
    
    typedef struct {
          pid_para param;
          pid_var vary;
    }reg;
    
    typedef struct {
           reg pidReg[4];
    }regWith;
    
    So in the function would be possible to use just regWith.pidReg[0].param *pid_param and the next array
    regWith.pidReg[0].vary *pid_vary. So if i would use 5 pids i would call the same function with the same structures the array number would varray . . . see the code for more explanations....

    Code:
    float PID(regWith.pidReg[0].param *pid_param, regWith.pidReg[0].vary *pid_vary)
    {
    .
    .
    .
    }
    
    .
    .
    .
    
    
    float PID(regWith.pidReg[4].param *pid_param, regWith.pidReg[4].vary *pid_vary)
    {
    .
    .
    .
    }
    
    .
    .
    .
    
    It is possible to do something like this, is any better way to do something like this ?

    Thanks for Your help,
    Best Regards,
    BoSChoW.
     

Share This Page