这段代码错了吗?请帮助!! [英] wats wrong with this code? plz help!!!
问题描述
这是一个用于查找机械手inv运动学的程序.
我的怀疑不是运动学的:-).
我的程序会执行,但始终会打印无法到达的位置",即使对于已知值,它也会执行相同的操作.
在这里,我使用了弧度表示角度,因为那是c能够理解的.
请告诉我如何解决此问题,甚至告诉我实现此问题的更好方法.
谢谢.
This is a program to find robotic arm inv kinematics.
My doubt is not in kinematics :-).
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.
#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();
}
推荐答案
这是最里面的for循环的正确格式的代码:
This is the proper formatted code of your inner most for loop:
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;
}
}
}
}
如果看到,则仅在满足3个条件(即x1 = x,y1 = y,z1 = z)后修改i
.
只要它们中的任何一个都不相同,就不会修改i
,而您会进入满足i=0
的if条件
如果您进行调试,则应该发现i
可能不会从0重新分配为1.
If you see, you modify i
, only after 3 conditions are met, i.e. x1=x, y1=y, z1=z.
As long as any one of them are not same, i
is never modified and you land up in the if condition that satisfies i=0
If you debug, you should find that i
might not be getting reassigned to 1 from 0.
这篇关于这段代码错了吗?请帮助!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!