Chip efficiency

woerr
Posts: 27
Joined: Thu Oct 09, 2014 7:48 am

Chip efficiency

Post by woerr » Wed Apr 19, 2017 11:21 am

Good day

I need some advice achieving what I am trying with my program. I am using it to manage some equipment.

I am using 5 inputs to calculate RPM using external Interrupts.
Max Speed from sensors
input 1: 120 Hz
input 2: 10 Hz
input 3: 40 Hz
input 4: 40 Hz
input 5: 40 Hz

While also driving three motors using PWM. and communicating over serial at the same time.

Accuracy seems to get worse and worse as more inputs are used and motors are driven up to a point where timings can double.

Can anybody help me improving performance.

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Chip efficiency

Post by majenko » Wed Apr 19, 2017 12:15 pm

Those frequencies are peanuts compared to what you should be able to cope with.

Care to share the portions of code that pertain to the interrupts?
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

woerr
Posts: 27
Joined: Thu Oct 09, 2014 7:48 am

Re: Chip efficiency

Post by woerr » Wed Apr 19, 2017 12:32 pm

Code: Select all

void gswheelint(){
  //groundWheel.gswheelcount++;
  groundWheel.timeStamp2 = groundWheel.timeStamp1;
  groundWheel.timeStamp1 = millis();
  if(groundWheel.calibStat == 1){
    groundWheel.calibCount++;
    meetAndroid.send('n',groundWheel.calibCount , 0);
  }
}
void mot1int(){
  if (config.bin1.calibStat == 1){
  config.bin1.motcount++;
  }
  config.bin1.timeStamp2 = config.bin1.timeStamp1;
  config.bin1.timeStamp1 = millis();
}
void mot2int(){
  if (config.bin2.calibStat == 1){
  config.bin2.motcount++;
  }
  config.bin2.timeStamp2 = config.bin2.timeStamp1;
  config.bin2.timeStamp1 = millis();
}
void mot3int(){
  if (config.bin3.calibStat == 1){
  config.bin3.motcount++;
  }
  config.bin3.timeStamp2 = config.bin3.timeStamp1;
  config.bin3.timeStamp1 = millis();
}
void fanint(){
  fanconfig.Fancount++;
}

Code: Select all

attachInterrupt(0, mot3int, FALLING);   
  attachInterrupt(1, mot2int, FALLING); 
  attachInterrupt(2, mot1int, FALLING);    
  attachInterrupt(4, fanint, FALLING);
  attachInterrupt(CN_18, gswheelint,FALLING);
 

Code: Select all

uint32_t s = disableInterrupts();
if (config.bin1.timeStamp1 - config.bin1.timeStamp2 > 0){     
      if(millis() - config.bin1.timeStamp1 < 1000){
        config.bin1.pulsesPerSecond = 1000 / (config.bin1.timeStamp1 - config.bin1.timeStamp2);
      }
      else{ config.bin1.pulsesPerSecond = 0;}
      restoreInterrupts(s);
    } 

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Chip efficiency

Post by majenko » Wed Apr 19, 2017 1:07 pm

I can only see two possible things there, both linked to gswheelint.

First, change notification interrupts are not as efficient as normal interrupts. They have to do extra processing to work out exactly which pin changed, which slows them down. The more you add of them the more it has to work.

Secondly is this suspect line:

Code: Select all

    meetAndroid.send('n',groundWheel.calibCount , 0);
I don't know what `meetAndroid` is, or how it sends, and that may be taking quite some time to execute, blocking other interrupts from triggering.

You may also want to investigate interrupt priorities. The default priorities assigned to normal external interrupts and change notification interrupts may not be suitable for your needs. You may want to use `setIntPriority(...)` to tweak the settings on the different interrupts (including things like serial interrupts) after they have been initialised and configure.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

woerr
Posts: 27
Joined: Thu Oct 09, 2014 7:48 am

Re: Chip efficiency

Post by woerr » Wed Apr 19, 2017 1:31 pm

Code: Select all

void MeetAndroid::send(char c ){
	Serial.print(startFlag);
	Serial.print(c);
	Serial.print(ack);
}

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Chip efficiency

Post by majenko » Wed Apr 19, 2017 1:37 pm

Enough of those in rapid succession will cause lag in that function. There's only either a 4 or 8 byte (depending on your chip) FIFO for the UART, and it will block until there is room in the FIFO for another character. You are best off doing serial communication outside any interrupts.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

woerr
Posts: 27
Joined: Thu Oct 09, 2014 7:48 am

Re: Chip efficiency

Post by woerr » Wed Apr 19, 2017 1:44 pm

Would increasing baud rate improve performance? at the moment the baud rate is 9600 which is very low.

Also having that serial send inside that interrupt is almost never used, and when it is used there is almost nothing else happening. There have been no issues while using that send.

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Chip efficiency

Post by majenko » Wed Apr 19, 2017 1:53 pm

OK, so that is unlikely to be giving your slowdown.

Are all the extra inputs you are adding using change notification interrupts?
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

woerr
Posts: 27
Joined: Thu Oct 09, 2014 7:48 am

Re: Chip efficiency

Post by woerr » Wed Apr 19, 2017 2:00 pm

They are, But they are triggered very seldom.

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Chip efficiency

Post by majenko » Wed Apr 19, 2017 2:08 pm

It may be to do with interrupt priorities then. You may like to try pumping the external interrupts up to priority 6. The default for them is 4. However there's not much else that could be higher than 4 anyway - UART is 2, change notification is 3. Just after the attachInterrupts:

Code: Select all

setIntPriority(_EXTERENAL_0_IRQ, 6, 0);
setIntPriority(_EXTERENAL_1_IRQ, 6, 0);
setIntPriority(_EXTERENAL_2_IRQ, 6, 0);
setIntPriority(_EXTERENAL_3_IRQ, 6, 0);
setIntPriority(_EXTERENAL_4_IRQ, 6, 0);
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply