CS452 F23 Lecture Notes
Lecture 11 - 19 Oct 2023

1. Train Modeling

  • In TC1, task is to route train to a particular point and stop it there
    • focusing here on the stopping part
    • One way to do this: issue stop command a suitable distance in front of the stopping point
      • need know stopping distance (or time) of train to know where to issue the stop command
      • need to know velocity of train to know when it will reach the point where the stop command should be issued
    • Sensor trigger -> stop command point -> stop point
  • In general, want to model
    • velocity of the train
    • acceleration/deceleration

2. Velocity

  • distinguish speed from velocity
    • speed - discrete train settings (0-14)
    • velocity - distance/time
  • about velocity
    • has unknown non-linear relationship to speed (e.g., doubling speed does not imply doubling velocity)
    • may differ between trains
      • two trains at same speed may have different velocity
    • may differ on different parts of the track?
    • may vary over time?
    • may depend on previous speed setting

2.1. Velocity Modeling

  • Goal: model v(speed, train) for some number of speeds and some number of trains
    • not worrying about variability in space or time (yet)
    • dependency on previous speed mostly an issue at low speeds
      • avoid issue by just avoiding very low speeds
  • Empirical approach:
    • run experiments, collect measurements, base model on measurements
  • Idea of the experiment:
    • choose two sensors a known distance ($d$d) apart
    • choose train and speed, and get train going at chosen speed
      • enough head start for train to come up to speed before reaching the first sensor
    • measure time at each sensor trigger
      • time difference (\(t\)) is time taken to travel distance \(d\), model velocity as \(d/t\)
  • But: you can only read the sensors periodically, so hard to know exactly when train triggers sensor
    • Suppose a tight continuous sensor loop, 50ms
      • At velocity of 0.5m/s = 500mm/1000ms = 0.5mm/ms => 25mm in 50ms
    • Train tiggers first sensor at time \(t_1\)
      • some delay to update sensor memory in Marklin - call this \(t_{m1}\)
      • sometime later (0-50ms later), loop reads the sensor data - call this delay \(t_{v1}\)
      • delay to send sensor data to UART, process sensor data, read RPi clock - call this delay \(t_{\pi1}\)
      • actual time we read from the clock is \(t'_1 = t_1 + t_{m1} + t_{\pi1} + t_{v1}\)
    • Train triggers second sensor at time \(t_2\), but the time we actually measure is \(t'_2 = t_2 + t_{m2} + t_{\pi2} + t_{v2}\)
    • We can assume that the Marklin and RPi delays are fixed - same on every reading, e.g., \(t_{m1} = t_{m2}\)
    • Others, especially delay due to sensor loop, are variable
    • We are actually interested in \(t = t_2 - t_1\), the difference of two absolute times
    • What we actually measure is \(t' = t'_2 - t'_1\)
      • fixed errors cancel out (yay)
      • variable errors may not:
      • so \(t' = t_2 + t_{v2} - (t_1 + t_{v1})\)
      • which means \(t' = t + t_{v2} - t_{v1}\)
    • \(t_{v1}\) and \(t_{v2}\) can range anywhere from 0 up to 50ms (the polling period)
    • So our measured time difference \(t'\) is the actual time difference (\(t\)) plus or minus 50ms
    • On each run of our experiment, we measure a new time difference
    • so, we can think the time difference as a random variable, \(T\)
      • each experiment run produces a measured time difference, \(t'\), which is a sample from \(T\)
    • What type of distribution do we expect \(T\) to have?
      • bimodal with peaks at multiples of sensor sampling period
        • e.g, if actual time is 130ms, and the sampling period is 50ms, measured times should cluster around 100ms and 150ms
  • What we actually want to estimate is velocity: \(V=d/T\)
    • \(V\) is a random variable, function of random variable \(T\)
    • We can use mean of \(V\) as velocity estimate
      • We can estimate the mean of \(V\) as \(d\) divided by mean of \(T\), but \(V\) and \(T\) are not linearly related, so this will normally underestimate the true mean of \(V\)
      • Or we can calculate mean of \(V\) directly from \(V\)’s distribution:
        • treat sample as empirical estimate of distribution of \(T\) (more accurate the more experiments you do)
        • calculate \(V\)’s distribution from \(T\)’s
      • Suppose 3 samples of \(T\): 2x50ms, 1x100ms, suppose d=10cm
        • \(1/T\) distribution is 2x(1/50), 1x(1/100)
        • \(V\) distribution is 2x(10/50), 1x(10/100), mean is 50/300 = 050/300 = 0.166 cm/ms
        • if instead we were to estimate \(V\)’s mean as \(d\) divided by the mean of \(T\), we’d get:
          • mean of \(T\) is (200/3)ms, V = 10/(200/3) = 0.15 cm/ms

Author: Ken Salem

Created: 2023-10-19 Thu 13:56