Problem 208456 · medium · Robotics · Level 02 Motion

Cutting Corners With Euler

Euler method · simulation · exact arc · discretisation error · unicycle model

A robot simulator moves a unicycle robot (it always drives in the direction it faces) whose pose (x, y, \theta) obeys

\dot x = v\cos\theta, \qquad \dot y = v\sin\theta, \qquad \dot\theta = \omega

with x to the right, y up, \theta in radians counter-clockwise from +x, forward speed v in m/s and turning rate omega in rad/s. The robot starts at (0, 0, 0) and holds v and omega fixed for T seconds.

The exact answer is easy here: the robot drives along a circle (or a straight line when omega == 0). A general simulator does not know that. It cuts the time into n equal steps of dt = T / n and takes Euler steps: each step moves the position as if the heading stayed fixed for the whole step, then updates the heading. In this order:

x = x + v * dt * cos(theta)      (with the heading at the start of the step)
y = y + v * dt * sin(theta)
theta = theta + omega * dt

Write euler_arc(v, omega, T, ns) that, for each step count n in ns (in order), runs the Euler simulation with n steps and returns a tuple (x, y, gap): the Euler end position and its straight-line distance from the exact end position. Return the list of these tuples.

Examples

Input:  v = 1.0, omega = 1.0, T = 2.0, ns = [1, 2, 4, 8]
Output: [(2.0, 0.0, 1.78748537), (1.54030231, 0.84147098, 0.85347495),
         (1.24431103, 1.15919576, 0.42220608), (1.08157492, 1.29510120, 0.21055066)]
Explanation: the exact path is a circle of radius 1 m, ending at (0.9093, 1.4161).
One step drives 2 m straight ahead and misses badly; each doubling of n roughly halves the gap.

Input:  v = 0.5, omega = 0.0, T = 10.0, ns = [1, 7]
Output: [(5.0, 0.0, 0.0), (5.0, 0.0, 0.0)]
Explanation: on a straight line the heading never changes, so Euler is exact.

Constraints

  • floats are compared with a tolerance of 1e-6

Goals

  • Simulate a turning robot with Euler steps in a stated order
  • Compare the simulation with the exact circular path it should follow
  • See the error halve when the step is halved: Euler's method is first order
Starting Python…