Created Wed, 19 Apr 2017 10:21:08 +0000 by woerr
Wed, 19 Apr 2017 10:21:08 +0000
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.
Wed, 19 Apr 2017 11:15:19 +0000
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?
Wed, 19 Apr 2017 11:32:20 +0000
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++;
}
attachInterrupt(0, mot3int, FALLING);
attachInterrupt(1, mot2int, FALLING);
attachInterrupt(2, mot1int, FALLING);
attachInterrupt(4, fanint, FALLING);
attachInterrupt(CN_18, gswheelint,FALLING);
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);
}
Wed, 19 Apr 2017 12:07:29 +0000
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:
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.
Wed, 19 Apr 2017 12:31:23 +0000
void MeetAndroid::send(char c ){
Serial.print(startFlag);
Serial.print(c);
Serial.print(ack);
}
Wed, 19 Apr 2017 12:37:19 +0000
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.
Wed, 19 Apr 2017 12:44:30 +0000
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.
Wed, 19 Apr 2017 12:53:15 +0000
OK, so that is unlikely to be giving your slowdown.
Are all the extra inputs you are adding using change notification interrupts?
Wed, 19 Apr 2017 13:00:33 +0000
They are, But they are triggered very seldom.
Wed, 19 Apr 2017 13:08:23 +0000
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:
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);
Wed, 19 Apr 2017 13:37:32 +0000
You do seem to be spending a lot of time with interrupts disabled. That's not going to help much. You need to keep your critical sections (bits wrapped with disabled interrupts) as short as possible so that interrupts have a chance to trigger.