1
2
3
4
5
6
7
8
9
10
11 package nl.tudelft.simulation.traffic.vehicle;
12
13 import java.rmi.RemoteException;
14
15 import nl.tudelft.simulation.dsol.formalisms.dess.DifferentialEquation;
16 import nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface;
17 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
18 import nl.tudelft.simulation.jstats.ode.integrators.NumericalIntegrator;
19 import nl.tudelft.simulation.logger.Logger;
20
21 /***
22 * <br>
23 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
24 * University of Technology </a>, the Netherlands. <br>
25 * See for project information <a href="http://www.simulation.tudelft.nl">
26 * www.simulation.tudelft.nl </a> <br>
27 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
28 * Public License (GPL) </a>, no warranty <br>
29 *
30 * @version Jul 4, 2004 <br>
31 * @author <a
32 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
33 * Verbraeck </a>
34 */
35 public class VehiclePositioner extends DifferentialEquation
36 {
37 /*** the vehicle */
38 protected VehiclePhysicalInterface vehiclePhysical;
39
40 /***
41 * @param vehiclePhysical
42 * @param simulator
43 * @throws RemoteException
44 */
45 public VehiclePositioner(final VehiclePhysicalInterface vehiclePhysical,
46 final DESSSimulatorInterface simulator) throws RemoteException
47 {
48 this(vehiclePhysical, simulator, simulator.getTimeStep(),
49 NumericalIntegrator.DEFAULT_INTEGRATOR);
50 }
51
52 /***
53 * @param vehiclePhysical
54 * @param simulator
55 * @param timeStep
56 */
57 public VehiclePositioner(final VehiclePhysicalInterface vehiclePhysical,
58 final DESSSimulatorInterface simulator, final double timeStep)
59 {
60 this(vehiclePhysical, simulator, timeStep,
61 NumericalIntegrator.DEFAULT_INTEGRATOR);
62 }
63
64 /***
65 * @param vehiclePhysical
66 * @param simulator
67 * @param timeStep
68 * @param numericalMethod
69 */
70 public VehiclePositioner(final VehiclePhysicalInterface vehiclePhysical,
71 final DESSSimulatorInterface simulator, final double timeStep,
72 final short numericalMethod)
73 {
74 super(simulator, timeStep, numericalMethod);
75 this.vehiclePhysical = vehiclePhysical;
76 this.initialize(0.0, new double[]{0.0, 0.0});
77 }
78
79 /***
80 * @param vehiclePhysical
81 * @param simulator
82 * @param timeStep
83 * @param numericalIntegrator
84 */
85 public VehiclePositioner(final VehiclePhysical vehiclePhysical,
86 final DESSSimulatorInterface simulator, final double timeStep,
87 final NumericalIntegrator numericalIntegrator)
88 {
89 super(simulator, timeStep, numericalIntegrator);
90 this.vehiclePhysical = vehiclePhysical;
91 this.initialize(0.0, new double[]{0.0, 0.0});
92 }
93
94 /***
95 * sets the speed and progression (e.g. when entering a new track, the
96 * progression will be reset).
97 *
98 * @param speed
99 * @param progression
100 *
101 */
102 public void setValue(final double speed, final double progression)
103 {
104 try
105 {
106 super.initialize(this.simulator.getSimulatorTime(), new double[]{
107 speed, progression});
108 } catch (RemoteException exception)
109 {
110 Logger.warning(this, "setValue", exception);
111 }
112 }
113
114 /***
115 * @see nl.tudelft.simulation.jstats.ode.DifferentialEquationInterface#dy(double,
116 * double[])
117 */
118 public double[] dy(double x, double[] y)
119 {
120
121 double[] dy = new double[2];
122 dy[0] = this.vehiclePhysical.getCurrentAcceleration(y[0], y[1]);
123 dy[1] = y[0];
124 return dy;
125 }
126
127 /***
128 * @see nl.tudelft.simulation.jstats.ode.DifferentialEquation#integrateY(double,
129 * double, double[])
130 */
131 protected double[] integrateY(double x, double initialX, double[] initialY)
132 {
133 double[] y = super.integrateY(x, initialX, initialY);
134 y[0] = Math.max(0.0, y[0]);
135
136 y = this.vehiclePhysical.setSpeedAndProgression(y[0], y[1]);
137 return y;
138 }
139
140 /***
141 * Stop the integration; do not make calculations anymore.
142 */
143 public void stopIntegration()
144 {
145 try
146 {
147 super.simulator.removeListener(this,
148 SimulatorInterface.TIME_CHANGED_EVENT);
149 } catch (RemoteException e)
150 {
151 Logger.warning(this, "stopIntegration", e);
152 }
153 }
154 }