I’m implementing a PID-control algorithm to a robot I’m currently building using Arduino.
My question is more related to the logics of programming.
I have to calculate a variable, an error, int eps. This eps will be between -7 and +7.
From the robot I acquire an input in the form of a double with values between 0 and 7000.
My algorithm has to work something like this:
if(input >= 500){
if(input >= 1000){
if(input >= 1500){
..........
}
}else{
eps = 6;
}
}else{
eps = 7;
}
And so on…
In other words I have to assign a value to eps that will be determined by which interval the input is included in.
My question is what would be the most efficient, time and resource-saving way of doing this?
I’m using Arduino and their own IDE, not Eclipse.
If 7 corresponds to 7000 and -7 corresponds to 0, and the intervals between different values of epsilon are equal (500), than you can write
eps = static_cast<int>(input/500.0) - 7. Not sure about static casts in Arduino – you just have to round you value to the lower integer (floor).If you have some other algorithm of determining eps, please state it more clearly, we’ll try to produce math for this;)