Package edu.purdue.wind.rtsj

Source Code of edu.purdue.wind.rtsj.CrackDetectorRTSJ

package edu.purdue.wind.rtsj;

import javax.realtime.RealtimeThread;
import javax.realtime.PriorityParameters;
import javax.realtime.SchedulingParameters;

import edu.purdue.wind.CrackDetector;
import edu.purdue.wind.Turbine;
import edu.purdue.wind.Log;

public class CrackDetectorRTSJ {
    private static CrackDetectorRTSJ instance;

    private CrackDetector detector;
    private Turbine turbine;
    private RealtimeThread thread;
    private int watchdogService;

    static void start(Turbine turbine) {
  instance = new CrackDetectorRTSJ(turbine);

  PriorityParameters prio = new PriorityParameters(Configuration.CRACKDETECTOR_PRIORITY);

  RealtimeThread thread = new RealtimeThread((SchedulingParameters)prio,
               null, null, null, null,
               new Runnable() {
                   public void run() {
                 instance.run();
                   }
               });
  instance.setThread(thread);
  thread.start();
    }

    /**
     * Return the singleton RTSJ crack detector.
     *
     * @return the instantiated singleton crack detector, or {@code null}
     */
    public static CrackDetectorRTSJ instance() {
  return instance;
    }

    /**
     * Construct the (singleton) RTSJ crack detector and bind it to the
     * specified turbine.  Register it with the {@code Watchdog}
     *
     * @param turbine turbine on which crack detection will run
     */
    private CrackDetectorRTSJ(Turbine turbine) {
  AudioCodecRTSJ codec = AudioCodecRTSJ.instance();

  this.turbine = turbine;
  detector = new CrackDetector(codec.sampleRate(), Configuration.MAX_SAMPLES);

  // FIXME: Real reset handler
  Watchdog.WatchdogResetHandler wrh = new Watchdog.WatchdogResetHandler() {
    public void reset() {
        Log.instance().log(Log.LogLevel.ERROR, "CrackDetectorRTSJ", "Resetting");
    }
      };


  watchdogService = Watchdog.instance().register(Configuration.CRACKDETECTOR_WATCHDOG_PERIODS, wrh);
    }

    private void setThread(RealtimeThread t) {
  this.thread = t;
    }

    RealtimeThread getThread() {
  return thread;
    }

    /**
     * Perform crack detection by waiting for the {@code AudioCodecRTSJ}
     * singleton to produce sample data to be processed, then process it
     * and repeat.
     */
    private void run() {
  AudioCodecRTSJ codec = AudioCodecRTSJ.instance();

  double[][] samples;

  Watchdog.instance().activate(watchdogService);
  for (;;) {
      // The period of this process is entirely dictated by the
      // period of the audio codec, and its releases are gated by
      // codec.retrieveSamples(), below.  Still, we want to detect
      // if it has failed.
      Watchdog.instance().checkin(watchdogService);

      samples = codec.retrieveSamples();

      if (samples.length != 2) {
    throw new IllegalStateException("AudioCodecRTSJ and CrackDetector do not agree on sample channel count");
      }

      detector.processAudio(turbine.blade(0), samples[0], codec.probingFrequency());
      detector.processAudio(turbine.blade(1), samples[1], codec.probingFrequency());
      if (detector.detect(turbine.blade(0), turbine.blade(1))) {
    // FIXME: A crack has been detected; what now?
    Log.instance().log(Log.LogLevel.WARNING, "CrackDetectorRTSJ", "Crack detected");
      }

      // FIXME: Here we should adjust the pickups to correspond to
      // the blades we want to detect next.

      codec.releaseSamples();
  }
  // Currently unreachable
  // Watchdog.instance().deactivate(watchdogService);
    }
}
TOP

Related Classes of edu.purdue.wind.rtsj.CrackDetectorRTSJ

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.