【发布时间】:2017-06-30 22:44:18
【问题描述】:
我正在尝试编辑别人在 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 函数。
有什么建议吗?
【问题讨论】: