Pages: [1] 2 3 4   Go Down
Author Topic: Speed up arduino  (Read 5232 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Scotland
Offline Offline
Edison Member
*
Karma: 24
Posts: 1243
Have you had your Arduino fix today?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What code? I don't see any code? I don't see anything.
Logged

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Learn how to implement without floating point. If you want more details you should post your code and not just snippets of it.
Logged

Check out my experiments http://blog.blinkenlight.net

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
V_Conversion = 1 / (1+V1)^2 ;
What does the XOR do in that expression?
Logged

"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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does the XOR do in that expression?
making it slow smiley-wink

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...


Logged

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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...
Logged

There's always a better way!

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
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 ;
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3 4   Go Up
Jump to: