Pages: [1]   Go Down
Author Topic: Implementing Cordic Algorithm in Arduino  (Read 386 times)
0 Members and 1 Guest are viewing this topic.
Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can any one please tell me the code to implement Cordic algorithm for Arduino Uno. I'm having the 'C' language program for Cordic Algorithm.
Logged

Oregon, USA
Offline Offline
Edison Member
*
Karma: 36
Posts: 1566
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google "cordic algorithm c language" for examples.
Logged

"It seems to run on some form of electricity"

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 119
Posts: 4587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can any one please tell me the code to implement Cordic algorithm for Arduino Uno. I'm having the 'C' language program for Cordic Algorithm.

You're in luck! The Arduino Uno uses the C++ language.
Logged

No, I don't answer questions sent in private messages...

Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what should we use in place of printf command for Arduino?
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 34
Posts: 1474
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://playground.arduino.cc/Main/Printf#.UymwFU7D_bU

Added:
Also check out Streaming:
http://playground.arduino.cc/Main/StreamingOutput#.Uymzt07D_bU

« Last Edit: March 19, 2014, 10:03:55 am by mrburnette » Logged

Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,

I had a code for implementing CORDIC algorithm. While I go through it there were lot of errors showing up. Can anyone please help me with this.

* cordic arduino.docx (17.35 KB - downloaded 6 times.)
Logged

Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,

I had a code for implementing CORDIC algorithm. While I go through it there were lot of errors showing up. Can anyone please help me with this. Thanks in advance.



I'm attaching the code below.


Code:
#define M_PI 3.1415926536897932384626
#define K1 0.6072529350088812561694

int main(int argc, char **argv)
{
    int i;
    int bits = 32; // number of bits
    int mul = (1<<(bits-2));
    
    
    int n = bits; // number of elements.
    int c;

    println("//Cordic in %d bit signed fixed point math\n", bits);
    println("//Function is valid for arguments in range -pi/2 -- pi/2\n");
    println("//for values pi/2--pi: value = half_pi-(theta-half_pi) and similarly for values -pi---pi/2\n");
    println("//\n");
    println("// 1.0 = %d\n", mul);
    println("// 1/k = 0.6072529350088812561694\n");
    println("// pi = 3.1415926536897932384626\n");

    println("//Constants\n");
    println("#define cordic_1K 0x%08X\n", (int)(mul*K1));
    println("#define half_pi 0x%08X\n", (int)(mul*(M_PI/2)));
    println("#define MUL %f\n", (double)mul);
    println("#define CORDIC_NTAB %d\n", n);
    
    println("int cordic_ctab [] = {");
    for(i=0;i<n;i++)
    {
        c = (atan(pow(2, -i)) * mul);
        println("0x%08X, ", c);
    }
    println("};\n\n");


    //Print the cordic function
    println("void cordic(int theta, int *s, int *c, int n)\n{\n  int k, d, tx, ty, tz;\n");
    println("  int x=cordic_1K,y=0,z=theta;\n  n = (n>CORDIC_NTAB) ? CORDIC_NTAB : n;\n");
    println("  for (k=0; k<n; ++k)\n  {\n    d = z>>%d;\n", (bits-1));
    println("    //get sign. for other architectures, you might want to use the more portable version\n");
    println("    //d = z>=0 ? 0 : -1;\n    tx = x - (((y>>k) ^ d) - d);\n    ty = y + (((x>>k) ^ d) - d);\n");
    println("    tz = z - ((cordic_ctab[k] ^ d) - d);\n    x = tx; y = ty; z = tz;\n  }  \n *c = x; *s = y;\n}\n");
    
}

cordic-32bit.h


Code:
//Cordic in 32 bit signed fixed point math
//Function is valid for arguments in range -pi/2 -- pi/2
//for values pi/2--pi: value = half_pi-(theta-half_pi) and similarly for values -pi---pi/2
//
// 1.0 = 1073741824
// 1/k = 0.6072529350088812561694
// pi = 3.1415926536897932384626
//Constants
#define cordic_1K 0x26DD3B6A
#define half_pi 0x6487ED51
#define MUL 1073741824.000000
#define CORDIC_NTAB 32
int cordic_ctab [] = {0x3243F6A8, 0x1DAC6705, 0x0FADBAFC, 0x07F56EA6, 0x03FEAB76, 0x01FFD55B,
0x00FFFAAA, 0x007FFF55, 0x003FFFEA, 0x001FFFFD, 0x000FFFFF, 0x0007FFFF, 0x0003FFFF,
0x0001FFFF, 0x0000FFFF, 0x00007FFF, 0x00003FFF, 0x00001FFF, 0x00000FFF, 0x000007FF,
0x000003FF, 0x000001FF, 0x000000FF, 0x0000007F, 0x0000003F, 0x0000001F, 0x0000000F,
0x00000008, 0x00000004, 0x00000002, 0x00000001, 0x00000000, };

void cordic(int theta, int *s, int *c, int n)
{
  int k, d, tx, ty, tz;
  int x=cordic_1K,y=0,z=theta;
  n = (n>CORDIC_NTAB) ? CORDIC_NTAB : n;
  for (k=0; k<n; ++k)
  {
    d = z>>31;
    //get sign. for other architectures, you might want to use the more portable version
    //d = z>=0 ? 0 : -1;
    tx = x - (((y>>k) ^ d) - d);
    ty = y + (((x>>k) ^ d) - d);
    tz = z - ((cordic_ctab[k] ^ d) - d);
    x = tx; y = ty; z = tz;
  }  
 *c = x; *s = y;
}



Moderator edit: [code] [/code] tags added.
« Last Edit: April 07, 2014, 09:55:40 pm by Coding Badly » Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1171
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What does this have to do with arduino?

Arduino hides the main() function - how do expect to send it arguments? Instead arduino uses setup() and loop().
Logged

Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm new to Arduino. I dont know the commands properly. I have downloaded this code through internet.
« Last Edit: April 07, 2014, 10:01:46 pm by nithesh26 » Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2047
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you need to go and understand  setup() and loop() before you will get very far.

All of those println() function calls in your alleged C program  will need to be replaced by Serial.println() to work on the arduino.
Logged

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

You'll also need to understand the difference between 16 and 32 bit integers.
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.

Illinois, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino hides main ( ). So what should we use in the place of main ( ) ? And also if we use the setup function,should we assign any pin number to that?   
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46222
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And also if we use the setup function,should we assign any pin number to that?   
Yes. You should use the number of the pin that setup() is attached to.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1171
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

If you want something done once, put it in setup().
If you want it done over and over, put it in loop().

Logged

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


check this thread for CORDIC - http://forum.arduino.cc/index.php?topic=75126.msg568212#msg568212 -

gr. Rob
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: