I've been working on some small projects for fun to learn more control theory things and embedded programming. Thank the esp32 and Arduino!
Either way I've been programming everything entirely in a single loop, for a bit of pseudo code:
void loop(){
data = get(sensor);
pdata = process(data);
lqr(pdata);
millis(10):
}
This above code is by no means real code, just a quick example of how my code is designed. I am by no means any kind of programming expert and only really know Arduino styled Wiring and bits of other things to complete small tasks.
What I'm unsure of, is it better to buffer/loop sensor data as fast as the micro-controller can and then afterwards only call the controller (LQR in this case) to actuate the system at my calculated discrete period like so:
void loop(} {
unsigned long currentMillis = millis()
interval = 10;
data = get(sensor);
pdata = process(data);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
lqr(pdata);
}
}
or call everything sequentially as I have been doing.
I feel as if having everything in the loop being calculated and then actuated sequentially adds extra time (if only microseconds the esp32 is 200~mhz or so) to the loop which isn't accounted for in my controller.
What is the best method/style for programming an embedded controller. Are both my methods/ideas incorrect?
process(data)
returns the average of last 8 readings .... note: please use proper indentation in your code, it will be so much easier to read, and it will make it easier to spot missing braces, like in your second example $\endgroup$