Way of programming suggestion

Hi,

Up to these days i always worked with rasppberry pi like platforms where i have threads so multitasking was not a problem. But in avr or arm i need suggestion.

My main loop is busy enough with a few algorithms. Every 5ms program goes to the algorithm loop to calculate an output based on the readings from the sensors. Then pid controller uses this output and makes another calculation. As a result i will have 4 different variables which will be indicating the high times of pwm. And this process takes 4ms in every 5 ms.

Should i use timer interrupts to drive 4 motors with this 4 calculated variables perfectly or should i use a polling loop where it waits untill all motors are completing their high time(if completed while waiting for othermotors high time it will be turned low in this loop) and all turned low before leaving this loop.

Can you suggest me a approach so everything will be working without disturbing each other

Please read the “How to use this forum” post, which offers advice on how to get informed help.

There is not enough information in your post for anyone to even guess what your problem(s) might be.

Is this for a quadcopter?

Regardless of what you think, the info is not enough :wink:

If you drive motors using hardware PWM (do you?), you only need to update those motors based on the result of your calculation/algorithm. No need to update them every time that you go through loop. I’m however not familiar with the use of a PID controller which might impose different requirements (and that type of stuff was my weak point at university :frowning: ).

You are aware of the Demonstration code for several things at the same time thread?

The trick will be to break your algorithms in smaller chunks and therefore each chunk will be executed faster. I guess you have loops in your algorithms; no idea because you did not provide the information :wink:

Let’s say that an algorithm basically looks like below

void algorithm()
{
  // a lengthy  calculation, 1000 iterations each taking 5 microseconds
  for(int cnt=0;cnt<1000;cnt++)
  {
    delayMicroseconds(5);
  }
}

You can break it into pieces like below

/*
  an algorithm
  returns:
    true if calculation completed, else false
*/
bool algorithm()
{
  // remember the counter; initial value 0
  static int cnt  = 0
  // do a shorter part of the algorithm  
  delayMicroseconds(5);
  // next call will execute next step 
  cnt++;
  if(cnt==1000)
  {
    // reset the counter
    cnt = 0;
    // indicate that algorithm is completed
    return true;
  }

  return false;
}

And in loop()

void loop()
{
  if(algorithm() == true)
  {
    // do something with result of algorithm
    ...
    ...
  }

  // do other stuff
  ...
  ...
}

I hope this helps.

senioriceman:
this algorithm takes about 4ms.

That seems to be blocking the Arduino for a very long time. Is it not possible to break the algorithm into smaller pieces with each piece implemented on each iteration of loop(). That would allow loop() to repeat much more frequently.

What Arduino are you using?

Really, without seeing your program is it almost impossible to suggest anything.

Floating point maths is very slow on an Arduino. Use integer maths if you can. Even with integer maths division is very slow.

…R

senioriceman:
After checking the hardware pwm i think what i need is this. After algorithm calculates i will need to update harware pwm values to creat new pwm that i wanted. In this way the algorithm can be long enough but the pwms will be generated by hardware and changed once the algortihm updated the values.

This is meaningless without being able to see the code you are talking about.

...R

senioriceman:
When we use hardware pwm, after setting necessary registers and compare registers, it is working internally right. A portion of mcu is design to do it like a internall thread without interfering the main loop right.

Yes. Once set to a given frequency and pulse width, it will keep on running with these settings till the code changes them.

It's just an autonomous block of hardware in the microcontroller.