Go Down

Topic: Speed up arduino (Read 6358 times) previous topic - next topic

hong89

Hi,  currently my program is too slow(120Hz) for me system requirement(~2kHz).
May I know how to speed up my arduino by changing the code?
I discover that "analogRead", "division","map" function, "square root" slow down my system.
Is there any more advance of coding method to speed up the arduino?




Regards,
Chao Hong

spycatcher2k

What code? I don't see any code? I don't see anything.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

hong89

code like below, when I include the as below in the system, the sampling rate of the whole system become significantly slower.

analogRead(1); 
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
V_Conversion = 1 / (1+V1)^2 ;      //any normal divide method, can change by using shift method? How?
,Amplitude = sqrt(v*v + x*x);

Udo Klein

Learn how to implement without floating point. If you want more details you should post your code and not just snippets of it.
Check out my experiments http://blog.blinkenlight.net

AWOL

Code: [Select]
V_Conversion = 1 / (1+V1)^2 ;
What does the XOR do in that expression?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart

Quote
What does the XOR do in that expression?

making it slow ;)

more serious, think the OP meant - V_Conversion = 1 / ((1+V1) * (1 + V1)) ;

this is a formula that only makes sense in the float domain as in the integer domain it will mostly return 0, 1 or a divide by zero .

So the trick should be to map the formulas in the integer domain without los of precission. Eg. instead of doing math in volts do it in millivolts.



fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);  is fast code. If you allow an error you could do 

fsrVoltage = fsrReading * 5;  // error 2-3%  can be acceptible (?)


analogRead(1);  can be replaced by fastAnalogRead(1) which is 10x faster ==> search the forum (upper right)


So please post your code then we can have a look ! 
And tell us as much as you can about the goal of the project, sensors actuators
and please post relevant datalinks...


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Quote
, think the OP meant - V_Conversion = 1 / ((1+V1) * (1 + V1)) ;

Yes, I thought that too, but wouldn't it have wasted less time if the OP had posted actual code?

Just another thought.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Morris Dovey

@ OP - there is. Have you considered doing your calculations working with (possibly improper) fractions with a common denominator of 1023? It's a lot more think-work if you aren't used to doing integer math, but it works a lot faster - and can sometimes actually provide more accurate results than working with floats...
There's always a better way!

WizenedEE

If it's a finished product (ie you're done adding features), you could put all the slow formulas in a lookup table.

hong89


Quote
What does the XOR do in that expression?


more serious, think the OP meant - V_Conversion = 1 / ((1+V1) * (1 + V1)) ;


Ya, that is the purpose of the code.

Quote
analogRead(1);  can be replaced by fastAnalogRead(1) which is 10x faster ==> search the forum (upper right)



I have found this but the sampling rate still same.
http://arduino.cc/forum/index.php/topic,74895.0.html
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11

and if I want to use fastAnalogRead(1), do I need to include any library? as it return me error say the fastAnalogRead is not declared.




Nick Gammon


May I know how to speed up my arduino by changing the code?


What code?


I discover that "analogRead", "division","map" function, "square root" slow down my system.


Yes, instructions tend to do that.


Is there any more advance of coding method to speed up the arduino?


More advanced than what?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

hong89




Is there any more advance of coding method to speed up the arduino?


More advanced than what?



the code that help to decrease proccesing time of arduino by replacing analogRead() and devision method with other method of coding.

Nick Gammon

AnalogRead takes .0001 seconds:

http://arduino.cc/en/Reference/AnalogRead

So you could do 10000 of them a second.

Quote
... currently my program is too slow(120Hz) ...


So that does not explain why you are getting 120 when you could have 10000. So if you post your code then we could see the problem. If not, well ...
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

hong89

here is my code. Thanks for the help =)

currently my code running at rate of 600Hz, but I need sampling rate up to 2kHz.
I check the sampling rate by setting high and low at one of the digital pin and monitor with scopemeter.

Code: [Select]

void Position_Count()
{
  A = digitalRead(22);
  B = digitalRead(24);
  double p=0;
  double a1 = 0;
  double b1 = 0;

  if(A==1 && a1!=A)
  {
     if(B==1)
     {
       p=p-1;
       a1=1;
       b1=1;
     }
     count=p;
   }
   
   if(A==1 && a1!=A)
   {
      if(B==0)
      {
        p=p+1;
        a1=1;
        b1=0;
      }
      count=p;
   }

   if(A==0 && a1!=A)
   {
      if(B==1)
      {
        p=p+1;
        a1=0;
        b1=1;
      }
      count=p;
   }

   if(A==0 && a1!=A)
   {
      if(B==0)
      {
        p=p-1;
        a1=0;
        b1=0;
      }       
      count=p;
   }

   if(B==1 && b1!=B)
   {
      if(A==1)
      {
        p=p+1;
        b1=1;
        a1=1;
      }
      count=p;
   }
   
   if(B==1 && b1!=B)
   {
      if(A==0)
      {
        p=p-1;
        b1=1;
        a1=0;
      }
      count=p;
   }

   if(B==0 && b1!=B)
   {
      if(A==1)
      {
        p=p-1;
        b1=0;
        a1=1;
      }
      count=p;
   }
   
   if(B==0 && b1!=B)
   {
      if(A==0)
      {
        p=p+1;
        b1=0;
        a1=0;
      }
      count=p;
   }

}



Code: [Select]

void Force()
{
   fsrReading = analogRead(fsrPin);

  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
   fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
 
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V       
    fsrResistance = 5000 - fsrVoltage;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance *= 10000;                // 10K resistor
    fsrResistance = fsrVoltage;

    fsrConductance = 1000000;           // we measure in micromhos so
    fsrConductance = fsrResistance;

    // Use the two FSR guide graphs to approximate the force
    if (fsrConductance <= 100)                 
    {
      fsrForce = fsrConductance + 20;
    }
   
/    else
    {
      fsrForce = fsrConductance ;               
      fsrForce = 50;           
    }   
 
  fsrForce = fsrForce*3;
}



Code: [Select]

void Virtual_System(double maxampl, double SollAmpl, double Kp, double Damp, int Mass )
{
  double Amplitude, P_ampl;
  double Sum;
  double dampingValue, Damping;
  double Vset;
  double acceleration, acceleration2;
 
//  Amplitude = sqrt(v*v + x*x);
 
  // C code for dampingValue, if...else
  if(Amplitude >= maxampl && Amplitude <= maxampl+0.05)
  {
    dampingValue = 0.1*Vset;
  }
  else if(Amplitude > maxampl+0.05)
  {
    dampingValue = 0.3*Vset;
  }
  else if(Amplitude > maxampl+0.1)
  {
    dampingValue = 10*Vset;
  }
  else
  {
    dampingValue = 0;
  }
 
  P_ampl = Amplitude - SollAmpl;
 
  Damping = P_ampl*Kp; 
 
  Sum = Vset*Damping; 
 
  acceleration = 0 - dampingValue - Sum - Vset*Damp - x*w ;//- ;//SatForce/Mass;
 
  double V_Conversion;
 
  if(v >= 0)
  {
   V_Conversion = 1 / ((1+V1)*(1+V1)) ;
  }
  else
  {
    V_Conversion = 1 / ((1-V1)*(1-V1)) ;
  }
 
  acceleration2 = acceleration*V_Conversion;
 
  // v = integrate of acceleration2
  v = v + acceleration2; 
 
  Potential(); 
  w = Freq * 2*PI/60 ;

   Vset = v*w - PosGain; 
 
  //x = integrate of Vset   
  x = x + Vset*Sampletime ;
 
}

PaulS

How about a complete application? That code won't even compile.

Go Up