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

1. ## 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();
}```

2. 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. Your bug is probably due to floating point representation. Floating point values are not exact, they are essentially a binary form of scientific notation.

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.

4. Originally Posted by sana.iitkgp
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. Welcome to the forum, Treehugger!

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

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

7. @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. sorry i tried but couldnt edit...

9. Originally Posted by sana.iitkgp
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. 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. Originally Posted by sana.iitkgp
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. 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. 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. any way of optimizing the above code for faster answer?