How to set the timeStep for execution of specific code snippet?

142 Views Asked by At

I need to execute certain code after certain duration/after certain timestep.

For an example, In ROS, we implement Service API and client can call the service request whenever needed.

So When a client call a service API, first block of code should be executed without any delay and then after certain duration/timeStep second block of code should be executed (like LED blinking) and the cycle repeat until client sends stop request.

Now my question is that how can I achieve this cycle?

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());
    });
}

For an example, as soon as the client call the service first block of code should be executed and after specified time step second block of code should be executed, then again after specified time step first block should be executed and cycle repeat.

Any hint or suggestion?

Thanks in advance

2

There are 2 best solutions below

0
On

You want to clearly define all of your variables first, then you can do two if statements to determine if the sword_check and/or shield_check dummy variables should be equal to 1. The following code should get you started.

#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!")
2
On

Yes, you can write an if statement like that. The error comes from your typo as mentioned in the comments.