【问题标题】:How to set the timeStep for execution of specific code snippet? [closed]如何设置执行特定代码段的 timeStep? [关闭]
【发布时间】:2019-08-21 16:12:44
【问题描述】:

我需要在特定持续时间/特定时间步之后执行特定代码。

举个例子, 在ROS中,我们实现了Service API,客户端可以在需要的时候调用服务请求。

因此,当客户端调用服务 API 时,应立即执行第一个代码块,然后在一定的持续时间/timeStep 之后执行第二个代码块(如 LED 闪烁)并重复循环,直到客户端发送停止请求.

现在我的问题是如何实现这个循环?

uint m_tickCount{};
uint duration {500}; // timeStep /** tick */

void LedFlash(const Animation& animation, std::unordered_map<int, int> ledsHandle)
{
    //first block
    std::for_each(leds.begin(), leds.end(), [&](int value) {
        simSetShapeColor(ledsHandle[value], nullptr, sim_colorcomponent_ambient_diffuse,
                         colorValue.data());
    });
    //second block
    std::for_each(leds.begin(), leds.end(), [&](int value) {
        simSetShapeColor(ledsHandle[value], nullptr, sim_colorcomponent_ambient_diffuse,
                         colorValue.data());
    });
}

例如,一旦客户端调用服务第一个代码块应该被执行,并且在指定的时间步之后应该执行第二个代码块,然后在指定的时间步之后再次执行第一个代码块并循环重复。

有什么提示或建议吗?

提前致谢

【问题讨论】:

  • 变量sword_charge 似乎不存在。
  • 当 Sword_charge >= 0.90 时,您希望变量 Sword_check 为 =1 吗?
  • 打错了,你写了word_check,没有定义sword_charge,这与if无关
  • Kelsey,是的,我需要 Sword_check 为 1,而且我需要知道我的代码有什么问题。我刚刚看到一个文档并尝试在 python 中运行相同的代码,但它现在显示错误。
  • 什么是sword_charge 你在哪里定义的?

标签: c++ duration timestep


【解决方案1】:

是的,您可以编写这样的 if 语句。错误来自您在 cmets 中提到的拼写错误。

【讨论】:

  • 您能否再解释一下!如何克服它
  • 该错误表明没有定义sword_charge。定义变量,您的代码应该可以工作。错字已在问题中被删除。另外,您可以阅读@Kelsey 的回答。
【解决方案2】:

您想首先明确定义所有变量,然后您可以执行两个 if 语句来确定 Sword_check 和/或 shield_check 虚拟变量是否应该等于 1。下面的代码应该可以帮助您入门。

#Define sword_charge and shield_energy 
sword_charge = 0.90
shield_energy = 5

#Set sword_check and shield_check as 0
sword_check = 0
shield_check = 0

if sword_charge >= 0.90:
    sword_check = True

if shield_energy >= 100: 
    shield_check = True

if sword_check and shield_check:
    print("The dragon crumples in a heap. You rescue the gorgeous princess!") 

else:
    print("Your attack has no effect, the dragon fries you to a crisp!")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多