我正在尝试编辑CAPL中其他人编写的脚本。原始脚本按预期工作,但我编辑的版本编译,但有一些运行时问题。当我按下键“你好”时整个程序崩溃了,就好像它陷入了循环一样,它无法破解。对于我编辑过的部分,下面显示了代码的简化版本:
(每次按下原始代码将CMD减少5次,按下,每按一次CMD增加5次,按下“U”键。我希望我的新脚本能够连续渐变在我定义的限制之间,在我定义的步骤和时间间隔内无限期地上下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;
}
我尝试在&#34;关键字&#39; u&#39;中评论外部while循环。 &#34;万一&#34;关键&#39; U&#39; &#34; &#34;无法执行事件语句。关键词&#39; u&#39; &#34;语句正在运行....我假设任何事件都可以覆盖任何其他事件,即使它仍在执行?无论如何,当我按下“你好”时,这仍然会导致一切都冻结,所以我认为使用我的计时器功能也存在问题。
我想要的只是与delay_ms()类似的功能,但是CAPL似乎没有意识到这一点,因此我不得不尝试使用setTimer和isTimerActive函数。
有什么建议吗?
答案 0 :(得分:1)
我希望我的新脚本能够在我定义的限制之间,在我定义的步长和时间间隔内不断地上下移动CMD,直到按下“U”,这会将CMD重置为0
为了避免嵌套while循环,我建议使用“on timer”事件来解决这个问题。在其中,每次执行都会重新启动计时器,直到它被取消。
variables
{
int rampDirection = 1;
long rampRate = 50; //ms
long rampStep = 1;
long LimUpper = 100; //max 350
long LimLower = -100; //min -350
msTimer tmrRamp;
}
on start
{
}
on timer tmrRamp
{
// currently ramping up
if(rampDirection == 1) {
if (CMD < LimUpper)
{
CMD = CMD + rampStep
}
// max is reached -> start ramping down
else
{
rampDirection = 0;
CMD = CMD - rampStep;
}
}
// currently ramping down
else
{
if (CMD > LimLower)
{
CMD = CDM - rampStep;
}
//min is reached start ramping up
else
{
rampDirerction = 1;
CMD = CDM + rampStep;
}
}
// timer restarts itslef until canceled
setTimer(tmrRamp,rampRate);
}
on key 'u'
{
CMD = 0;
rampDirection = 1; // start with ramping up
setTimer(tmrRamp,rampRate);
}
on key 'U'
{
cancelTimer(tmrRamp);
CMD = 0;
}