1

I am building a little maze solver project using Webots. I am writing my controller in C programming language. I am using Windows OS.

Can someone let me know if there is a way to pause the simulation when a certain event happens?

Something like :

if(event){
    pause(10 seconds);
    continue();
}

This is part of the code I wrote (where I want to introduce the pause). while(wb_robot_step(TIME_STEP) != -1) is in a infinite while loop.

int main(int argc, char **argv) {

  //… initialization code for Webots API, not important

  // feedback loop: step simulation until an exit event is received
  while (wb_robot_step(TIME_STEP) != -1) {

    // read sensors outputs
    for (i = 0; i < 8 ; i++){
      ps_values[i] = wb_distance_sensor_get_value(ps[i]);
    }

    //program
    goForward();
    if (forwardWall()==1) {
      //pause needed here 
      turnLeft(1);
    }
    else if(rightWall()==0){
      Uturn();
    }   
 } 
  // cleanup the Webots API
  wb_robot_cleanup();
  return 0; //EXIT_SUCCESS
}

EDIT : The solution was using the Sleep() function. It did not work at first because I was not importing the <windows.h> library.

ROBlackSnail
  • 501
  • 1
  • 5
  • 20
  • What operating system are you using? In Linux [sleep()](https://linux.die.net/man/3/sleep) can be used. In Windows [Sleep()](https://stackoverflow.com/questions/3379139/sleep-function-in-windows-using-c) can be used. I am not sure what `continue();` is for in your code example. – ryyker Jan 08 '20 at 12:50
  • Note that Linux sleep is in seconds, Windows sleep is in milliseconds. – cup Jan 08 '20 at 12:52
  • @ryyker I am using Windows. I tried using Sleep(1000), but I got a warning, and it doesn't pause at all. Do I need to import this function from a library ? – ROBlackSnail Jan 08 '20 at 13:05
  • @ryyker `warning: implicit declaration of function 'Sleep'; did you mean '_sleep'? [-Wimplicit-function-declaration]` – ROBlackSnail Jan 08 '20 at 13:06
  • What was the warning? Did you look at the link I left? It includes several examples, including header file to use. By the way, when asking a question like this it would just be good to include the environment information from the start, i.e. OS, Development tools, etc. Its also generally best to include a [mcve]. Without seeing your code, there is no way to see if there is some other problem, such as compile time warnings, or algorithm issues. – ryyker Jan 08 '20 at 13:13
  • That warning is telling you that the function you are calling has not been not been declared. Header files (such as #include are where function declarations are made. Libraries (or source code) are where they are defined. In this case Sleep() will be in a library. What environment are you using to compile your code? (see simple example below.) – ryyker Jan 08 '20 at 13:33
  • In case you are interested, there is an installer for the _[Code::Blocks IDE that bundles GCC](http://www.codeblocks.org/downloads/26#windows)_ here. – ryyker Jan 08 '20 at 13:37
  • 1
    @ryyker I did not notice the links you left (I'm new). I also updated my post to include the code I'm working with. – ROBlackSnail Jan 08 '20 at 13:39
  • @ryyker I tried using the Sleep() function and included the required library. I got no more error, but it still doesn't work. I put a pause of 10 seconds at the beginning of the program, but as soon as I click start, my robot starts moving. Might be in conflict with Webots ? – ROBlackSnail Jan 08 '20 at 13:45
  • It depends on where it is called. What calls are being made in `main()` (you do not include that part of your code in the post.), and in what sequence? (Try putting the `Sleep(10000);` function as the very first function call in the `main()` function, before any `webot` calls are made. That will test whether it is working ) – ryyker Jan 08 '20 at 13:54
  • @ryyker It works now. I was making a stupid mistake. Thank you for your time ! I updated my now working code. – ROBlackSnail Jan 08 '20 at 14:19
  • I am glad to know you were able find the issue. However replacing your problem code with working code is not a good idea as it removes the reason for the post in the first place to future readers :). It would be better to add an edit section at the bottom of your post with a brief description of what you have done, eg: `EDIT: -Here is what was done to fix the problem: (add code and/or description below)` (You can add your fix in an edit, but it is not necessary.) – ryyker Jan 08 '20 at 14:28

1 Answers1

4

I am using Windows. I tried using Sleep(1000), but I got a warning...

This small example will compile and run using GCC:

#include <windows.h> //Sleep()
#include <stdio.h> //printf()

int main(void) 
{
    Sleep(10000); // 10 seconds
    printf("process continuing...\n");

    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87