@@ -10,47 +10,13 @@ title: Simulated Time
10
10
In the simulated environment, time advances only at the pace that the simulator
11
11
is run and in cooperation with the controller code (such as the code controlling
12
12
your robot). As a result, using ` time.time ` to know how long your robot has been
13
- running for or ` time.sleep ` to wait for some duration may be unreliable.
13
+ running for or ` time.sleep ` to wait for some duration will have unpredictable
14
+ results, especially when run on a computer other than the one you are developing
15
+ on (such as during the competition).
14
16
15
- ## Approaches
16
-
17
- ### Existing projects: the ` Robot ` class
18
-
19
- <!-- We should drop this after SR2020 and move `ManualTimestepRobot` to being the default. -->
20
-
21
- For existing projects the default ` Robot ` class will automatically advance the
22
- simulator all the time. This can make programming your robot slightly easier as
23
- you don't need to worry about the progress of time and (just like a real robot)
24
- you can assume that time passing will just happen.
25
-
26
- However this has a drawback -- because the simulator advances time in small
27
- chunks (rather than the smooth progression we're used to in reality) it can mean
28
- that your robots actions sometimes run for slightly more or less time than you
29
- expect. While the time difference will be small (a few tens of milliseconds), it
30
- is likely to impact attempts at more precise movement more than larger actions
31
- due to their shorter time.
32
-
33
- This may mean for example that turning by a small angle to line up with a token
34
- will sometimes work and sometimes point the robot in not quite the right
35
- direction.
36
-
37
- If your robot code is impacted by these unpredictability issues we recommend
38
- that you change over to using the ` ManualTimestepRobot ` class instead. For
39
- guidance on doing this, see the [ upgrade guide] ( #upgrading ) below.
40
-
41
- This class is maintained for compatibility with earlier releases of the
42
- simulator, though its use is discouraged (especially for new projects).
43
-
44
- ### New projects: the ` ManualTimestepRobot ` class {#manual-timestep-robot}
45
-
46
- For new projects, or existing projects that want to be sure of getting precise
47
- robot behaviour, the recommended approach is to use the ` ManualTimestepRobot `
48
- class.
49
-
50
- This approach relies upon your code advancing the simulation explicitly by
51
- calling its ` sleep ` method (documented below) in order for the simulation to
52
- actually run. This should not be an issue for most robot code however as you
53
- will likely be doing this anyway in order to wait for things to happen.
17
+ Instead your code should explicitly signal to the simulator when it is ready for
18
+ time to advance and should query for the current time from the simulator. This
19
+ can be done by using the methods detailed below.
54
20
55
21
<div class =" info " >
56
22
If you find that the simulator freezes then this indicates that your code is
@@ -97,71 +63,3 @@ now = R.time()
97
63
98
64
duration = now - then
99
65
```
100
-
101
- ## Changing from ` Robot ` to ` ManualTimestepRobot ` {#upgrading}
102
-
103
- The changes needed to move from using ` Robot ` to ` ManualTimestepRobot ` are
104
- fairly small since both classes have the same interface. This means that
105
- anywhere in the docs a <code >Robot</code > is used, you can also use a
106
- <code >ManualTimestepRobot</code >.
107
-
108
- 1 . Anywhere that your code mentions ` Robot ` , change it to ` ManualTimestepRobot ` ,
109
- for example:
110
-
111
- ``` python
112
- from sr.robot import Robot # change this
113
-
114
- R = Robot() # change this
115
-
116
- R.motors[0 ].m1.power = 20 # this stays the same
117
- ```
118
-
119
- should be changed to:
120
-
121
- ``` python
122
- from sr.robot import ManualTimestepRobot
123
-
124
- R = ManualTimestepRobot()
125
-
126
- R.motors[0 ].m1.power = 20
127
- ```
128
-
129
- 2 . Remove any usages of either `time.time` or `time.sleep` and replace them with
130
- the equivalent robot methods (documented above). For example:
131
-
132
- ``` python
133
- import time # remove this everywhere
134
-
135
- start = time.time()
136
- time.sleep(1.2 )
137
- print (" I slept for {} seconds" .format(time.time() - start))
138
- ```
139
-
140
- should be changed to:
141
-
142
- ``` python
143
- start = R.time()
144
- R.sleep(1.2 )
145
- print (" I slept for {} seconds" .format(R.time() - start))
146
- ```
147
-
148
- 3 . Check for any places where you have code which expect that `R.time()` will
149
- increase on its own and ensure that they sleep for at least a very small
150
- amount of time on each iteration. For example:
151
-
152
- ``` python
153
- end = R.time() + 5
154
- while R.time() < end:
155
- if has_touched_something(R):
156
- break
157
- ```
158
-
159
- should be changed to:
160
-
161
- ``` python
162
- end = R.time() + 5
163
- while R.time() < end:
164
- R.sleep(0.01 ) # note addition of this line
165
- if has_touched_something(R):
166
- break
167
- ```
0 commit comments