Offline
Newbie
Karma: 0
Posts: 33
|
 |
« on: April 13, 2012, 09:31:07 am » |
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
God Member
Karma: 3
Posts: 513
Have you had your Arduino fix today?
|
 |
« Reply #1 on: April 13, 2012, 09:42:18 am » |
What code? I don't see any code? I don't see anything.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #2 on: April 13, 2012, 09:49:48 am » |
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
Faraday Member
Karma: 16
Posts: 3220
20 LEDs are enough
|
 |
« Reply #3 on: April 13, 2012, 09:57:27 am » |
Learn how to implement without floating point. If you want more details you should post your code and not just snippets of it.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: April 13, 2012, 10:44:12 am » |
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.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 101
Posts: 9553
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #5 on: April 13, 2012, 12:53:15 pm » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: April 13, 2012, 12:58:16 pm » |
, 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.
|
|
|
|
West Des Moines, Iowa USA
Offline
Sr. Member
Karma: 2
Posts: 429
|
 |
« Reply #7 on: April 13, 2012, 03:28:35 pm » |
@ 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
Edison Member
Karma: 16
Posts: 1036
Arduino rocks
|
 |
« Reply #8 on: April 13, 2012, 07:37:08 pm » |
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
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #9 on: April 16, 2012, 02:06:57 am » |
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. 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.htmlhttp://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11and 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
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14110
Lua rocks!
|
 |
« Reply #10 on: April 16, 2012, 02:32:46 am » |
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
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #11 on: April 16, 2012, 02:44:26 am » |
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
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14110
Lua rocks!
|
 |
« Reply #12 on: April 16, 2012, 02:51:08 am » |
AnalogRead takes .0001 seconds: http://arduino.cc/en/Reference/AnalogReadSo you could do 10000 of them a second. ... 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
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #13 on: April 16, 2012, 03:26:38 am » |
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. 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; }
}
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; }
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
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #14 on: April 16, 2012, 04:54:13 am » |
How about a complete application? That code won't even compile.
|
|
|
|
|
Logged
|
|
|
|
|
|