-2

Hi I have a file with following format where am trying to calculate the position of aircraft from radar (approaching airport) every 10 msecs.

Position_X

Position_Y

Heading

Speed

t1

t2 w1

t3 w2

t4

Where w1, w2 = turn rate

in this case {t1, t2, t3, t4} = 200secs ~ 200000 msecs

and evaluating position after every 10 msecs

This is how am processing:

    // defined in Plane.h

    QElapsedTimer t;
    QTimer timer;

   // Plane.cpp

    Plane::Plane : timer()  {

     QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(computePosition()));
     timer.start(10);

        t.start();
    }

    void Plane::computePosition()
    {
     if (!t.hasExpired(t1)
    {
    //do
    }

    if (t.hasExpired(t2) || t.hasExpired(t3))

    {
    //do
    }

    if (t.hasExpired(t3) || t.hasExpired(t4))

    {
    // do
    }

    if (t.hasExpired(t5))
    {

    // do 

    }
    if(t.hasExpired(t5 + 100))

     timer.stop();

     qDebug() << QDateTime::currentMSecsSinceEpoch()<< t.elapsed()
             << Position_X  << Position_Y;
    }

am not able to match system time with elapsed time or condition time. timer interval rate is 10 ms but in debug I see it varies from 15-40 secs. And also the approach time is 200 ms but with using elapsed timer to evaluate position pushes the plane matrix way out of the airport.

How do I make sure my program is running at time intervals t1, t2, t3, t4 & t5 and position is evaluated correctly.

Appreciate any ideas or help. Thanks!

Calculation of positions is not a problem.I want to do the calculations at time t1..t5 in conjugation with QTimer (interval is 10 ms).

  • 1
    Whatever you're saying doesn't mean much. What does it mean "I need to do calculations exactly at those time intervals". Those time intervals are relative to **what**? You know it, we don't. You're talking of `Position_X`, `Position_Y` - is this considered "current" position at time = 0, and the intervals always extend into the future from that reference? What is the output of your computation for every time step - just current position, or positions at times `t1`...`t4`? You really need to clarify, otherwise the question is useless. – Kuba hasn't forgotten Monica Jun 18 '18 at 18:06
  • 1
    What is `t1` relative to? What time is `Position` taken at? What time is `t2` relative to? Why are there only two turn rates but four times? You'd need to draw some time plot that demonstrates the meaning of all those variables. And please, completely forget timers, Qt, and any such things - they are completely unnecessary and out of scope of the discussion. First you have to figure out what you compute. You will want those computations done as quickly as possible. As for updates - you say these variables are in a file. Does something update that file with new radar data? Is this homework? – Kuba hasn't forgotten Monica Jun 18 '18 at 18:09
  • 1
    By thinking of timers and signals and whatnot you've completely lost track of what you're trying to do. The problem you're solving is something a high school student should solve. So make sure you present the problem in this way first: so that anyone not knowing anything about your radars and planes, but knowing high school physics, can know what to do. It's impossible to talk any further about it before you recast the problem so that it's clear to everyone. It's a simple problem so it is supposed to have a simple, clear description. No code should be written before we all understand it first! – Kuba hasn't forgotten Monica Jun 18 '18 at 18:19
  • 1
    To put it simply: You're asking to "optimize" a problem that you haven't even described yet. "What is the fastest way home? We really need to be home soon!" "Whoa, whoa, where are we, and where's home?" – Kuba hasn't forgotten Monica Jun 18 '18 at 18:22
  • Let me try to describe the process clearly. At t = t1 = 0sec ; t1 – user3412649 Jun 18 '18 at 19:09
  • You tried? Oh? And your code is where? Edit the question to show your attempts. The code should compile and be a short single file. Minimize. – Kuba hasn't forgotten Monica Jun 19 '18 at 13:00
  • Provide a complete list of all the output you’re looking for. It looks like you want 5 positions as the output, one at each of the times t1...t5. Is that correct? If so, then the calculation involves no timers or anything like that. You can know all the answers ahead of time. Think of a magic function that takes the Plane structure as inputs and outputs a list of 5 positions. This can be done in plain C++ on a system that has no notion of passage of time. It’s just a calculation. Why do you want timers? You haven’t explained that yet. Think `std::array calculate(const Plane &)`. – Kuba hasn't forgotten Monica Jun 19 '18 at 13:02

2 Answers2

1

First of all, you don't need to use timers to do calculations: you can precalculate everything in advance, the calculations use a time variable that doesn't need to be coupled to any timer. It's just a variable you increment as you execute steps of a numerical integration. You have to tell us why you think that the program has to run at some intervals - because thus far it's simply false. It's hardly even a Qt problem, it's plain old numerical methods. The calculations are simple; for every time step:

  1. Start at t=0.
  2. Update heading according to current turn rate.
  3. Convert radial speed to cartesian speed.
  4. Integrate speed into position.
  5. Select next turn rate if current t is past given t_i.
  6. Increment t by 10ms.
  7. Repeat from #2 unless current time t is past end time.

That's it. You're free to choose whatever integration rule you want. t is just a variable. It doesn’t have anything to do with passage of time on the computer that computes the results.

The rest depends on what is the meaning of the times t1-t4, and what are the outputs you're looking for - whether you want time time history from starting position until some time t4+0.1s, or just 4 outputs - one for each time t1-t4, etc.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • what i mean to say is i need to do calculations exactly at those time intervals (t1.....t5) every 10 ms that is why am using QELaspedTimer above. however looking at how to process optimally. May be my approach is totally wrong. About the time variable.. can you please elaborate a bit. Thanks! – user3412649 Jun 18 '18 at 17:38
  • 1
    @user3412649 Suppose I tell you that you have a stopped car at time t=0s, it then accelerates at 10m/s^2, and I ask for what is the position of the car at t=10s. You don't need to wait 10 seconds to give me the answer. You can calculate the answer right now. First you integrate acceleration to velocity, `v(t) = v0 + a(t) * t`, then you integrate velocity into position `x(t) = x0 + v(t) * t`. Then: `x(t) = x0 + v0*t + a(t)*t*t`; since `a(t)` is constant, we get `x(t) = x0 + v0*t + a*t*t`. At 10 seconds, assuming `x0=0, v0=0, x(10s) = 0 + 0*10s + 10m/s^2*(10s)^2 = 1000m`. You know the future NOW – Kuba hasn't forgotten Monica Jun 18 '18 at 18:14
0

QTimer most likely won't be able to maintain strict 10 ms ticks. QElapsedTimer can be more accurate, but it's meant more to measure performance of functions, methods, etc.

If you want to simulate an interval of X ms, just set up the QTimer to fire at a particular interval. Then, you can maintain a counter that increments each time the timeout signal is emitted. From there, you can calculate your "simulated time", which would be T = X * count. There's no need for a QElapsedTimer at all.

QTimer isn't "hard real-time". If you absolutely must have hard real-time, you'll have to look elsewhere.

Jacob Robbins
  • 1,860
  • 14
  • 16
  • If the timer is used to invoke updates and I track passage of real time, then yes: QElapsedTimer would be necessary precisely because a timer event doesn’t imply much about when it had happened. Upon the timer timeout, you have to catch up the integration to the current time. And you know that from the elapsed timer. The timeout becomes merely a prompt to do a chunk of computational work. But in any case it’s not clear that any of this is needed: the math doesn’t have to be done delayed. It can all be computed ahead of time. – Kuba hasn't forgotten Monica Jun 19 '18 at 18:36