I have a structure in C for a motor control application, the structure have the element for a PID. I created 2 variables with that structure and 2 pointers pointing to those variable. My application is on a microcontroller so I have an other register that is quite important for the PID to work, that is the encoder count. So I would like to have my pointers pointing to their respective PID variable but I would like to have the element "Encoder" pointing to the encoder register of the microcontroller. So that when I access the pointer I can get the encoder position directly.
typedef struct{
int Error;
int DeadBand;
int SetPoint;
int Encoder;
.
.
.
double Integral;
double Derivative;
double Kp;
double Ki;
double Kd;
float Position;
bool Done;
}Upid;
Upid PID1, PID2, *pPID1, *pPID2;
pPID1 = &PID1;
pPID2 = &PID2;
pPID1->Encoder = &POS1CNTL;
pPID2->Encoder = &POS2CNTL;
I didn't try the code above because I 100% it will not work like I want. Dereferencing the element Encoder and assigning the address of the encoder position register will just put the address number in the Encoder element. It is only there to show a little what I want to accomplish. But I don't know how to do that.
After I want to be able to call functions without the need to include the Encoder count in the function itself.
Now I do something like this:
void MovePosition(Upid* PID, int Acceleration, int MaxVelocity, int StartPosition, int EndPosition)
{
PID->Acceleration = Acceleration;
PID->MaxVelocity = MaxVelocity;
PID->Position = PID->StartPos = StartPosition;
PID->EndPos = EndPosition;
PID->DeceleratonStarted = false;
PID->VelocityMode = false;
PID->Step = 0;
PID->Done = false;
}
When I call this function, I always put the current encoder count (the value of the encoder count register of the microcontroller) in the StartPosition.
I would like to do something like this:
void MovePosition(Upid* PID, int Acceleration, int MaxVelocity, int EndPosition)
{
PID->Acceleration = Acceleration;
PID->MaxVelocity = MaxVelocity;
PID->Position = PID->StartPos = PID->Encoder;
PID->EndPos = EndPosition;
PID->DeceleratonStarted = false;
PID->VelocityMode = false;
PID->Step = 0;
PID->Done = false;
}
Where everything points to the PID1 or PID2 variable but the element Encoder points to the microcontroller's Encoder count register.
How would I be able to do something like this? Thank you.
Edit:
I use a microchip microcontroller with the XC16 compiler.
I use POS1CNTL like a normal variable:
I can read it like: A = POS1CNTL;
Or I can write a value to it like: POS1CNTL = 30;
POS1CNTL is defined a follow:
#define POS1CNTL POS1CNTL
extern volatile uint16_t POS1CNTL attribute((sfr));