Hi, I am creating a pressure controller with PIC micro that is controlling the heating coil and a pocketbeagle that is running a PID loop. The PIC micro takes commands from pocketbeagle over UART.
PIC responds to 0 to 9, with 9 being heating coil switched off and 1 being full on.The PID loop outputs numbers up to 10000000, depending upon what is the difference between the measured pressure and the desired pressure, and I am really struggling to convert this PID output (0 to 10 million ) to a range of 0 to 9 and not killing the PID functionality - i.e. keeping it PID and not making it like a normal code. I managed to have a few range of loop, and each range is a PID loop on its own, but that is not an ideal solution. Below is the code I tried, int_diff is the (int) PID output. Can anyone pelase help ? I have a feeling I might be over complicating things but don't know how.
Just to explain, in below code, each range sends 0 to 9 numbers to PIC micro, so the problem arises when the difference between set pressure and measured pressure is low and it just need to go up by a little, but the output (int_diff) switches range and suddenly it goes to 0 (heating coil full on) and then on next measurement (after about a second) it goes to 9 (heating coil off), while it should be moving from 8 to 9. This kind of movement (0 to 9) causes overshoot, and the reverse is also true, causing very slow rise in temperature or undershoot.
I hope I am making sense. Thanks
if(0<int_diff && int_diff<99) { value = diff/10; //(gives 1 to 9) closer =1; } #if 1 else if(99<int_diff && int_diff<999) { value = diff/100; closer =1; } else if (999<int_diff && int_diff<9999) { value = diff/1000; closer =1; } else if (9999<int_diff && int_diff<99999) { value = diff/10000; closer =1; } else if (99999<int_diff && int_diff<999999) { value = diff/100000; closer =1; } else if (999999<int_diff && int_diff<9999999) { value = diff/1000000; closer =1; //value = 0; } if(closer==1) { if(value>=0) { value= 9-(int)value; } else if(value<0) { value=9; } } else { value = 0; }