#pragma config(Motor, port1, RightsideB, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port2, RightsideF, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port9, LefttsideF, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port10, LeftsideB, tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
float xdiff, ydiff, firstpart, secondpart, firstpart2, secondpart2, total, lengthformula;
float getdistance(int x1, int x2,int y1, int y2){
return x1;
return x2;
return y1;
return y2;
xdiff = x2 - x1;
ydiff = y2 - y1;
firstpart = (xdiff);
return firstpart;
secondpart = (ydiff);
firstpart2 = pow(xdiff,2);
secondpart2 = pow(ydiff, 2);
total = firstpart2 + secondpart2;
lengthformula = sqrt(total);
return lengthformula;
}
task main()
{
getdistance(0, 4, 0, 1);
while (true){
motor[RightsideB]= vexRT[Ch2];
motor[RightsideF] = vexRT[Ch2];
motor[LeftsideB]= vexRT[Ch3];
motor[LefttsideF]= vexRT[Ch3];
}
}
这个程序运行没有错误,但是当我运行它并打开调试器时,变量不起作用。我已经用smallbasic编程了,我知道它的工作原理。我将其转换为robot-c
答案 0 :(得分:1)
删除return
中的所有getdistance
语句,但最后除了return lengthformula
。 return
导致函数停止运行,并立即返回您指定的值,因此函数的其余部分永远不会运行,并且永远不会计算距离。
在main()
中,您需要将结果分配给变量:
task main() {
float distance = getdistance(0, 4, 0, 1);
...
}
然后,您可以使用distance
来满足您的需求。