Simulation Examples

Now that you've read through all the simulation guides, let's bring it all together with a few quick examples. These examples will compare block diagrams with their qdex simulation equivalencies.

Example 1

This example uses two types of block connections, a gain element, and a transfer function to modify a sine wave. The result is output to a plot. In the equivalent qdex module, the simulation is started when the page is active (that is, open) and is stopped when the page is inactive. 

<sectionNoTitle name="example1Section">

  <timePlot name="sinePlot" />
  <simulation name="simpleSim">
      if active then
        <sine amplitude="30" frequency="0.5" />
          <gain value="3" />
          <transferFunction num="1 5" den="1 0 10" />
        <probe ref="example1Section.sinePlot" />

Example 2

This example uses multiple input signals, a feedback loop, and a number of math elements. The resulting waves are muxed together so that they can both be output to the same plot.

In the equivalent qdex module, the simulation is started when the user presses the button, and stops after 10 seconds. Note that to output both waves to the same plot, the series attribute in the timePlot declaration must be set to 2.

<sectionNoTitle name="example2Section">

  <timePlot name="thisPlot" series="2" />

  <button content="Start Simulation">

  <simulation name="anotherSim" duration="10">
            <sine amplitude="100" phase="4" />
                <transferFunction num="1" den="1 1" />
                <math op="abs" />
              <gain value="0.5" />
            <square amplitude="8" bias="-10" />
            <signalGenerator waveform="random" />
        <probe ref="example2Section.thisPlot" />


Example 3

This is an example of a simple system being used to animate a shape. The source is created using a sine wave multiplied with a function in a system to create decay. Two plots inside a stack are used to plot the response and animate a ball.

<include src="plotTools.xml" />

<section name="animationSection">
  <title>Bouncy Ball</title>

  <stack name="plotStack" orientation="horizontal">
    <timePlot name="responsePlot">
      <axis dim="x" min="0" max="5" />
    <xyPlot name="ballPlot">
      <axis dim="x" auto="growAndShrink" major="false" />
      <axis dim="y" auto="lockAspectRatio" major="false" />
      <series name="path" draw="lineSegments" />
      <series name="ball" draw="radialFill" />
      <!-- Transparent frame used to steady the plot as the ball moves -->
      <series name="frame">
          <foreground color="transparent" />
        <data>-1 6; 6 6; 6 -1; -1 -1</data>
  <!-- Drawing the ball -->
    plotTools.drawCircle(plotStack.ballPlot.ball, {0,5}, 0.1)

  <button content="Start">

  <simulation name="ballSimulation" duration="5" period="0.005">
        <sine amplitude="5" phase="1.57" />
          <input name="sineIn" width="1" />
          <output name="decaySine" width="1" />
            -- Creating decay
            return vector({sineIn[1]*(1/(0.75*time+1))})
        <math op="abs" /> <!-- Faking the presence of the ground, making ball "bounce" -->
        <probe ref="animationSection.plotStack.responsePlot" />
        <probe ref="animationSection.plotStack.ballPlot.path" />
        <system name="drawBall">
          <input name="ballLocation" width="1" />
            -- Translating the ball using the value of the response 
            plotStack.ballPlot.ball.Transform = matrixf.translation(time, ballLocation[1]-5, 0)