001package jmri.jmrix.rps.swing.debugger;
002
003import java.awt.FlowLayout;
004import javax.swing.BoxLayout;
005import javax.swing.JLabel;
006import javax.swing.JPanel;
007import javax.swing.JTextField;
008import javax.vecmath.Point3d;
009import jmri.jmrix.rps.Distributor;
010import jmri.jmrix.rps.Engine;
011import jmri.jmrix.rps.Measurement;
012import jmri.jmrix.rps.MeasurementListener;
013import jmri.jmrix.rps.Reading;
014import jmri.jmrix.rps.ReadingListener;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 * Pane for manual operation and debugging of the RPS system.
020 * <p>
021 * All index numbers here are 1-based, so they are the same as the RPS hardware
022 * number.
023 *
024 * @author Bob Jacobsen Copyright (C) 2008
025 */
026public class DebuggerTimePane extends JPanel
027        implements ReadingListener, MeasurementListener {
028
029    public DebuggerTimePane() {
030        super();
031
032        NUMSENSORS = Engine.instance().getMaxReceiverNumber();
033
034        times = new JTextField[NUMSENSORS + 1];
035        residuals = new JLabel[NUMSENSORS + 1];
036        times[0] = null;
037        residuals[0] = null;
038        for (int i = 1; i <= NUMSENSORS; i++) {
039            times[i] = new JTextField(10);
040            times[i].setText("");
041            residuals[i] = new JLabel("          ");
042        }
043    }
044
045    public void dispose() {
046        // separate from data source
047        Distributor.instance().removeReadingListener(this);
048        Distributor.instance().removeMeasurementListener(this);
049    }
050
051    java.text.NumberFormat nf;
052
053    int NUMSENSORS;
054
055    JTextField[] times;
056    JLabel[] residuals;
057
058    public void initComponents() {
059        nf = java.text.NumberFormat.getInstance();
060        nf.setMinimumFractionDigits(1);
061        nf.setMaximumFractionDigits(1);
062        nf.setGroupingUsed(false);
063
064        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
065
066        JPanel p1;
067        JPanel p3 = new JPanel();
068        p3.setLayout(new java.awt.GridLayout(NUMSENSORS, 2));
069
070        for (int i = 1; i <= NUMSENSORS; i++) {
071            p1 = new JPanel();
072            p1.setLayout(new FlowLayout());
073            p1.add(new JLabel("r" + i + ":"));
074            p1.add(times[i]);
075            p3.add(p1);
076            p1 = new JPanel();
077            p1.add(new JLabel("r-t: "));
078            p1.add(residuals[i]);
079            p3.add(p1);
080        }
081        add(p3);
082    }
083
084    void setResidual(int i, Measurement m) {
085        if (times[i].getText().equals("")) {
086            residuals[i].setText(""); // just blank out
087            return;
088        }
089        try {
090            if (Engine.instance().getReceiver(i) == null) {
091                residuals[i].setText(""); // just blank out
092                return;
093            }
094            Point3d p = Engine.instance().getReceiverPosition(i);
095            Point3d x = new Point3d((float) m.getX(), (float) m.getY(), (float) m.getZ());
096
097            double rt = p.distance(x) / Engine.instance().getVSound();
098            int res = (int) (rt - m.getReading().getValue(i)) - Engine.instance().getOffset();
099            residuals[i].setText("" + res);
100            log.debug(" residual {} from {} vs {}", res, p, x);
101        } catch (Exception e) {
102            residuals[i].setText(""); // just blank out
103        }
104    }
105
106    Measurement lastPoint = null;
107
108    @Override
109    public void notify(Reading r) {
110        // Display this set of time values
111        for (int i = 1; i <= Math.min(r.getNValues(), times.length - 1); i++) {
112            times[i].setText(nf.format(r.getValue(i)));
113        }
114    }
115
116    @Override
117    public void notify(Measurement m) {
118        try {
119            for (int i = 1; i <= NUMSENSORS; i++) {
120                setResidual(i, m);
121            }
122        } catch (Exception e) {
123            log.error("Error setting residual: ", e);
124        }
125    }
126
127    private final static Logger log = LoggerFactory.getLogger(DebuggerTimePane.class);
128
129}