Go Down

Topic: Way of programming suggestion (Read 975 times) previous topic - next topic

senioriceman

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

jremington

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.

mikb55


sterretje

#3
Dec 19, 2016, 06:24 am Last Edit: Dec 19, 2016, 06:26 am by sterretje
Regardless of what you think, the info is not enough ;)

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 :( ).

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 ;)

Let's say that an algorithm basically looks like below
Code: [Select]

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
Code: [Select]

/*
  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()
Code: [Select]

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

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


I hope this helps.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

#4
Dec 19, 2016, 07:56 am Last Edit: Dec 19, 2016, 07:58 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

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.

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up