Thread: wats wrong with tis prog? plz help!!!

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    8

    Unhappy wats wrong with tis prog? plz help!!!

    This is a program to find robotic arm inv kinematics. my doubt is not in kine:-). My program executes but always prints "position unacheivable", even for a known value, it does the same. here I have used radians for angles since thats what c understands. Please tell me how to come across this or even tell me better method to implement this. Thank you.

    insert
    Code:
        #include<stdio.h>
        #include<math.h>
        #include<conio.h>
        void main()
        {
            float a, b, c, d;
            int x = 0, y = 0, z = 0;
             int x1 = 0, y1 = 0, z1 = 0;
             int i = 0;
             printf("Enter X value: \n");
             scanf("%d", &x);
             printf("Enter Y value: \n");
             scanf("%d", &y);
             printf("Enter Z value: \n");
             scanf("%d", &z);
             for (a = -1.74 ; a <= 1.74 ; a = a + 0.02)
             {
                  for (b = 0.0; b <= 3.14; b = b + 0.02)
                  {
                       for (c = -1.74; c <= 1.74; c = c + 0.02)
                       {
                            for (d = -0.784; d <= 0.784; d = d + 0.02)
                            {
                                 x1 = (int)((11*cos(d+c+b+a)+11*cos(d+c+b-a)+12*cos(c+b+a)+12*cos(c+b-a)+9*cos(b+a)+9*cos(b-a))/2);
                                 if(x1 == x)
                                 {
                                         y1 = (int)((11*sin(d+c+b+a)-11*sin(d+c+b-a)+12*sin(c+b+a)-12*sin(c+b-a)+9*sin(b+a)-9*sin(b-a))/2);
                                         if(y1 == y)
                                         {
                                                 z1 =  (int)(11*sin(d+c+b) + 12*sin(c+b) + 9*sin(b));
                                                 if(z1 == z)
                                                 {
                                                       i = 1;
                                                       goto status;
                                                  } 
                                         }
                                  }
                           }
                     }
                }
           }
        status:
        if(i == 0)
        printf("*****Positon unacheivable*****");
        else
        printf(" The joint angles for the desired positon are %d \t %d \t %d \t %d \n", a, b, c, d);
        getch();
        }
    Last edited by treehugger; 08-27-2012 at 11:12 PM.

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    It is working fine.

    # cc cpb7.c -lm
    cpb7.c: In function âmainâ:
    cpb7.c:5: warning: return type of âmainâ is not âintâ
    # ./a.out
    Enter X value:
    0
    Enter Y value:
    -1
    Enter Z value:
    29
    The joint angles for the desired positon are 1073741824 -1074014454 1073741824 1072273817

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your bug is probably due to floating point representation. Floating point values are not exact, they are essentially a binary form of scientific notation.
    Reading: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Furthermore, you're only written PI to 2dp, so you're introducing quite a bit of error there yourself as well.

    Edit: Oh and as someone else shows, you're using the wrong format specific to printf for floats.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Quote Originally Posted by sana.iitkgp View Post
    It is working fine.

    # cc cpb7.c -lm
    cpb7.c: In function âmainâ:
    cpb7.c:5: warning: return type of âmainâ is not âintâ
    # ./a.out
    Enter X value:
    0
    Enter Y value:
    -1
    Enter Z value:
    29
    The joint angles for the desired positon are 1073741824 -1074014454 1073741824 1072273817
    You are getting such a large values for a, b, c, d?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Treehugger!

    FORMAT your code, if you want much help on the forums.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    I didn't change anything, I executed same program and the output is what i have given last time.

    One morething, your a,b,c,d are float values but you are printing them as int.
    If you change in your last printf(), then the output is


    Enter X value:
    0
    Enter Y value:
    -1
    Enter Z value:
    29
    The joint angles for the desired positon are -1.740000 0.800000 0.939999 0.036000
    Last edited by sana.iitkgp; 08-27-2012 at 11:06 PM.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    @imalc - i have written pi only to 2 dp coz tis isnt a practical prog, its only for training/testing and furthermore, at-least it should give some output right. instead of "position unacheivable" every time?

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    sorry i tried but couldnt edit...

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Quote Originally Posted by sana.iitkgp View Post
    I didn't change anything, I executed same program and the output is what i have given last time.

    One morething, your a,b,c,d are float values but you are printing them as int.
    If you change in your last printf(), then the output is


    Enter X value:
    0
    Enter Y value:
    -1
    Enter Z value:
    29
    The joint angles for the desired positon are -1.740000 0.800000 0.939999 0.036000
    thanks sana. i changed printf statement back to %f, and i should say that was a blunder. but still i couldnt get. I dont know how you got it.. and could suggest me a better way of doing this if you may???

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    I can explain what I did. But the math part, frankly i don't have any idea.

    First i printed values of x1,y1,z1 for every a,b,c,d, then i took one triple i.e.(x1,y1,z1) and given that input. It worked.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Quote Originally Posted by sana.iitkgp View Post
    I can explain what I did. But the math part, frankly i don't have any idea.

    First i printed values of x1,y1,z1 for every a,b,c,d, then i took one triple i.e.(x1,y1,z1) and given that input. It worked.
    couldnt understand.. a little more briefly plz

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Are you still getting "Positon unacheivable" ?

    For every a,b,c,d in their range we have x1,y1,z1 values. I printed all them using below code. Then for original program i have given input as one such triple.
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
        void main()
        {
            float a, b, c, d;
            int x = 0, y = 0, z = 0;
             int x1 = 0, y1 = 0, z1 = 0;
             int i = 0;
             printf("Enter X value: \n");
             scanf("%d", &x);
             printf("Enter Y value: \n");
             scanf("%d", &y);
             printf("Enter Z value: \n");
             scanf("%d", &z);
             for (a = -1.74 ; a <= 1.74 ; a = a + 0.02)
             {
                  for (b = 0.0; b <= 3.14; b = b + 0.02)
                  {
                       for (c = -1.74; c <= 1.74; c = c + 0.02)
                       {
                            for (d = -0.784; d <= 0.784; d = d + 0.02)
                            {
                                  x1 = (int)((11*cos(d+c+b+a)+11*cos(d+c+b-a)+12*cos(c+b+a)+12*cos(c+b-a)+9*cos(b+a)+9*cos(b-a))/2);
                                  y1 = (int)((11*sin(d+c+b+a)-11*sin(d+c+b-a)+12*sin(c+b+a)-12*sin(c+b-a)+9*sin(b+a)-9*sin(b-a))/2);
              z1 =  (int)(11*sin(d+c+b) + 12*sin(c+b) + 9*sin(b));
             printf("x1 = %d  y1 = %d  z1 = %d  \n",x1,y1,z1);
                                               
                                         }
                                  }
                           }
                     }
                }
           }
        status:
        if(i == 0)
        printf("*****Positon unacheivable*****");
        else
        printf(" The joint angles for the desired positon are %f \t %f \t %f \t %f \n", a, b, c, d);
        getch();
        }

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    actually tis would print all possible values but not the specific for user input of x, y, z.... i have edited the program above and now it works fine but takes lot of time to execute. try running if you may.
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    int main()
    {
        float a, b, c, d;
        int a1, b1, c1, d1;
        int x = 0, y = 0, z = 0;
        int x1 = 0, y1 = 0, z1 = 0;
        int i = 0, j = 0;
        printf("Enter X value: \n");
        scanf("%d", &x);
        printf("Enter Y value: \n");
        scanf("%d", &y);
        printf("Enter Z value: \n");
        scanf("%d", &z);
        for (a1 = -100 ; a1 <= 100 ; a1++)
        {
                printf("%d \t", j++);
                for (b1 = 0; b1 <= 180; b1++)
            {
                for (c1 = -100; c1 <= 100; c1++)
                {
                                  for (d1 = -45; d1 <= 45; d1++)
                          {
                           a = a1 * 0.0174532925;
                           b = b1 * 0.0174532925;
                           c = c1 * 0.0174532925;
                           d = d1 * 0.0174532925;
                                   x1 = (11*cos(d+c+b+a)+11*cos(d+c+b-a)+12*cos(c+b+a)+12*cos(c+b-a)+9*cos(b+a)+9*cos(b-a))/2;
                                   if(x1 == x)
                                   {
                                               y1 =(11*sin(d+c+b+a)-11*sin(d+c+b-a)+12*sin(c+b+a)-12*sin(c+b-a)+9*sin(b+a)-9*sin(b-a))/2;
                                               if(y1 == y)
                                               {
                                                     z1 = 11*sin(d+c+b) + 12*sin(c+b) + 9*sin(b);
                                                     if(z1 == z)
                                                     {
                                  i = 1;
                                  goto status;
                                                     }
                                                }
                                      }
                    }
                }
            }
        }
        status:
        if(i == 0)
        printf("*****Positon unacheivable*****");
        else
            printf(" The joint angles for the desired positon are \n %d \t %d \t %d \t %d \n", a1, b1, c1, d1);
            getch();

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    any way of optimizing the above code for faster answer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wats wrong with this code
    By juice in forum C Programming
    Replies: 3
    Last Post: 12-20-2011, 12:00 PM
  2. Wats wrong!!
    By juice in forum C++ Programming
    Replies: 12
    Last Post: 10-02-2011, 03:11 PM
  3. Wats wrong!!
    By juice in forum C Programming
    Replies: 6
    Last Post: 09-26-2011, 04:54 AM
  4. Can any1 tell me wats wrong in this c program..
    By Hrisav in forum C Programming
    Replies: 2
    Last Post: 07-09-2009, 05:03 AM
  5. wats wrong with this code?
    By Marrah_janine in forum C++ Programming
    Replies: 20
    Last Post: 12-10-2006, 08:01 PM

Tags for this Thread