Inertial sensor programming questions

This won’t work, as the oreintation will most likely not ever be properly zero, and this technically isn’t PID or even a Bang Bang algorithm. (Bang bang would be going backwards if overshooting, going forwards if undershooting within a certain tolerance). I see that you’ve also blindly copied the code above and I highly discourage you from doing so without understanding it.

I have these resources for you to properly understand what the two algorithms I mentioned above:

https://wiki.purduesigbots.com/software/control-algorithms/bang-bang

https://wiki.purduesigbots.com/software/control-algorithms/pid-controller

(I know there is a tolerance in the see saw itself for balancing so it may work given some overshoot, but the function being named PID is still deceptive as it is not a PID).

9 Likes

Just skimming through this thread, I can say there are a ton of quality resources provided. Enough to build a robust PID controller that you can use for practically any application, even over multiple seasons.

BUT, with that being said, its important to take a look at them and understand how things really work. Looking at PID code for the hundredth time will not make more sense than the first time unless you break it down and understand how each step works and why we need it. What the the benefits of each term? What are the drawbacks? How do they compliment each other? Why do we use certain combinations?

Without an understanding of these kind of questions, your PID code will just be a magical covered box, even you the programmer can’t look inside. Furthermore, if you understand the theory, you can write your own custom controller with ease.

So, @46b_toxic, your first task is to go through the resources provided and tell us your understanding of P, I and D individually. Just something simple like what they stand for, how they are calculated, and why they are used.

This

and this,

Are good starting points.

Hint: if you are paying close attention, the first link I provided actually links the second link I provided, but I still included the second link because it is very important.

tl;dr read and understand resources they are REALLY good!!

5 Likes

thanks .And by the way I was able to create a pid loop paired with a while loop ,it’s just that I don’t know how to make work for balancing

does anyone know what is wrong with this pid ,and if it is a pid

void myPID(int setpoint)

{ <this bracket is highlighted in red and I dont know why

while ((1)

{

error = setpoint - sensor value;

integral = integral + error;

if (error = 0 or passes setpoint)

integral = 0;

if (error is outside useful range)

integral = 0;

derivative = error - prevError;

prevError = error;

power = error=kP + integral=kI + derivative=kD;

wait 15 mSec;

};

};

I copy and pasted this into vexcode v5 and the bracket you said was red isn’t. However, the bracket just below the while is red. The code said that it was expecting a “)” to complete the parentheses in the while((1). I fixed that and as soon as that happened, everything in the while loop went red. It fixed the bracket being red problem though.

1 Like

couple things I noticed:

these are not going to work, since passes is not an operator and useful range isn’t a variable.

To fix the variable errors, you just need to add double to the variables.

Assigning the variables properly should look like this:

double sensorvalue = 0;
double kP = 0.0;
double kI = 0.0;
double kD = 0.0;

void myPID(int setpoint)

{

while (1)

{

double error = setpoint - sensorvalue;

double integral = integral + error;

if (error == 0 || error > setpoint)

double integral = 0;

if (error > range)

double integral = 0;

double prevError = error;

double derivative = error - prevError;

double power = error=kP + integral=kI + derivative=kD;

wait(15,msec);

};

}; 

This simply removes the errors you previously had. There are warning that your integral variable is not being used once, so you might want to go back over it to make sure everything is right. From there, you would need to code the motors to actually move, and then you would just need to test your PID.

3 Likes

double integral = integral + error;

double error = setpoint - sensorvalue;

double sensorvalue = 0;

double kP = 0.0;

double kI = 0.0;

double kD = 0.0;

void myPID(int setpoint)

{

while (1)

{

double error = setpoint - sensorvalue;

double integral = integral + error;

if (error == 0 || error > setpoint)

double integral = 0;

if (error > orange)

double integral = 0;

double prevError = error;

double derivative = error - prevError;

double power = error=kP + integralkI + derivativekD;

Lrw.spin(directionType::fwd,inertial9.pitch(),velocityUnits::pct);

Lfw.spin(directionType::fwd,inertial9.pitch(),velocityUnits::pct);

Rrw.spin(directionType::fwd,inertial9.pitch(),velocityUnits::pct);

Rfw.spin(directionType::fwd,inertial9.pitch (),velocityUnits::pct);

Lfw.stop(brakeType::hold);

Lrw.stop(brakeType::hold);

Rrw.stop(brakeType::hold);

Rfw.stop(brakeType::hold);

wait(15,msec);

};

};

do you know what is wrong i’ve played with it it and there errors still. Is it because I haven’t actually plugged in and set up my sensor on the robot?

Maybe you should read the errors, and Google might help you with this too.

4 Likes