Hello everyone, I come with a piece of code that got me into a "brawl" in the office:
It's a module designed to accelerate and decelerate a motor from a PLC within a certain range of RPS. The normal speed is generated in a second module that doesn't matter here. My boss created the code and then shared with me:
While...
//Logic protections...
//Acceleration iterations...
//Deceleration iterations...
//Limits:
IF output_speed > MaxSpeedAllowed THEN
output_speed := MaxSpeedAllowed;
END_IF;
IF output_speed < MinSpeedAllowed THEN
output_speed := MinSpeedAllowed;
END_IF;
endWhile;
I told him that he could improve it a lot by setting the high and low limits of speed as conditionals and then running the acceleration or declaration depending on the conditions, because this way there would be many less iterations per cycle and we would be changing the output speed only once per cycle, which is the ideal case:
While...
//Logic protections...
//Limits:
IF currentSpeed + speedFactor > MaxSpeedAllowed THEN
currentSpeed := MaxSpeedAllowed;
ELSE_IF currentSpeed - SpeedFactor < MinSpeedAllowed THEN
currentSpeed := MinSpeedAllowed;
ELSE
//Acceleration iterations...
//Deceleration iterations...
endWhile;
He got annoyed and told me to "stop asking stupid questions". I would like to have your opinion because I am getting tired of this guy and I just want to make him swallow his ego.
It's a module designed to accelerate and decelerate a motor from a PLC within a certain range of RPS. The normal speed is generated in a second module that doesn't matter here. My boss created the code and then shared with me:
While...
//Logic protections...
//Acceleration iterations...
//Deceleration iterations...
//Limits:
IF output_speed > MaxSpeedAllowed THEN
output_speed := MaxSpeedAllowed;
END_IF;
IF output_speed < MinSpeedAllowed THEN
output_speed := MinSpeedAllowed;
END_IF;
endWhile;
I told him that he could improve it a lot by setting the high and low limits of speed as conditionals and then running the acceleration or declaration depending on the conditions, because this way there would be many less iterations per cycle and we would be changing the output speed only once per cycle, which is the ideal case:
While...
//Logic protections...
//Limits:
IF currentSpeed + speedFactor > MaxSpeedAllowed THEN
currentSpeed := MaxSpeedAllowed;
ELSE_IF currentSpeed - SpeedFactor < MinSpeedAllowed THEN
currentSpeed := MinSpeedAllowed;
ELSE
//Acceleration iterations...
//Deceleration iterations...
endWhile;
He got annoyed and told me to "stop asking stupid questions". I would like to have your opinion because I am getting tired of this guy and I just want to make him swallow his ego.