我正在尝试编辑其他人在 CAPL 中编写的脚本。原始脚本按预期工作,但我编辑的版本可以编译,但有一些运行时问题。当我按下“u”键时,整个程序崩溃了,就好像它陷入了一个循环,它无法中断。对于我编辑的部分,代码的简化版本如下所示:
(原始代码每次按下“u”时 CMD 递减 5,每次按下“U”时 CMD 递增 5。我希望我的新脚本在我定义的限制之间不断上升和下降 CMD,在我定义的步骤和无限期的时间间隔,直到按下“U”,这会将 CMD 重置为 0)
variables
{
//Added for my script
int rampFlag = 0;
int tmrFlag = 0;
long rampRate = 50; //ms
long rampStep = 1;
long LimUpper = 100; //max 350
long LimLower = -100; //min -350
msTimer tmrRamp;
//Other existing variables from working script...
}
on start
{
//No changes for my script
}
//Added for my script:
on timer tmrRamp
{
//Do nothing
}
on key 'u'
{
//Working script (commented out) This just decrements CMD every time 'u' is pressed:
/*CMD = CMD - 5;
if(CMD < -350)
{
CMD = -350;
}*/
//Added for my script:
CMD = 0;
//ramp up and down the command CMD until key 'U' is pressed
rampFlag = 1;
while(rampFlag == 1)
{
//ramp up
while(CMD < LimUpper)
{
CMD = CMD + rampStep;
setTimer(tmrRamp,rampRate); //start timer
while(isTimerActive(tmrRamp) == 1){} //wait for timer to expire
//delay_ms(rampRate);
}
//ramp down
while(CMD > LimLower)
{
CMD = CMD - rampStep;
setTimer(tmrRamp,rampRate); //start timer
while(isTimerActive(tmrRamp) == 1){} //wait for timer to expire
}
}
}
on key 'U'
{
//Working script (commented out) This just increments CMD every time 'U' is pressed:
/*CMD = CMD + 5;
if(CMD >= 350)
{
CMD = 350;
}*/
//Added for my script:
//Cease ramping up and down the torque request when key 'U' is pressed
rampFlag = 0;
cancelTimer(tmrRamp);
CMD = 0;
}
我尝试在“on key 'u'”中注释掉外部while循环,以防在“on key 'u'”语句正在运行时无法执行“on key 'U'”事件语句......我是假设任何事件都可以覆盖任何其他事件,即使它仍在执行?无论如何,当我按下“u”时,运行它仍然会导致一切冻结,所以我认为使用我的计时器功能也存在问题。
我想要的只是类似于 delay_ms() 的函数,但 CAPL 似乎无法识别这一点,所以我不得不尝试使用 setTimer 和 isTimerActive 函数。
有什么建议么?