I am creating a program to control a car engine. One of the tasks is to read the readings from the crankshaft rotation sensor. There is a gear on the crankshaft, there are 35 teeth and one gap. Due to the fact that the port is bouncing, I read the value 10 times, and between readings I stop for 250 microseconds (exactly micro). Everything works, the readings are very accurate.
Further, I get the position with an accuracy of a degree, that is, in the interval between the impulses of the “full” and “empty” teeth, some time passes. Based on the average tooth travel time, since it is only 5 degrees, I calculate 1/5 of the time and add +1 degree. This is how I achieve degree accuracy. Everything works here, everything is fine.
Further, I need to give impulses to the nozzles and candles when a certain degree is reached, with an accuracy of 1.
If I do this in the same stream, then reading the readings from the sensor starts to slow down and the readings become inaccurate. If I create two separate streams, then since the priority between the main stream and the one I created is uneven, the readings start to slow down even more. If I set the priority to normal, I still see braking. If I set the priority higher than normal, then the readings from the sensor start to read well, but the execution of the main one stops. That is, after setting the priority, only the thread with the sensor reading is executed.
I need a main thread with normal priority. A very high priority sensor read flow and a flow to track the degree and generate pulses to the spark plugs and injectors.
What am I doing wrong? What is the point of thread priorities if they just disable the execution of other threads?