Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Some problems with structure and array . . . (http://www.go4expert.com/forums/structure-array-t6872/)

boschow 17Oct2007 01:15

Some problems with structure and array . . .
 
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.

DaWei 17Oct2007 07:18

Re: Some problems with structure and array . . .
 
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.

shabbir 17Oct2007 09:03

Re: Some problems with structure and array . . .
 
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.

boschow 17Oct2007 22:12

Re: Some problems with structure and array . . .
 
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.

boschow 18Oct2007 04:48

Re: Some problems with structure and array . . .
 
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.


All times are GMT +5.5. The time now is 03:16.