001    /*
002     *  AccelHandler.java
003     *      Part of the Spirograph problem set.
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's Computers
006     * and Cognition Group. For more information, see 
007     * <a href="http://www.cognition.olin.edu/projects/cs101/">the
008     * CS101 homepage</a> or email <las@olin.edu>.
009     *
010     * Copyright (C) 1998 Massachusetts Institute of Technology.
011     * Copyright (C) 2002-2003 Franklin W. Olin College of Engineering.
012     * Please do not redistribute without obtaining permission.
013     */
014    
015    package spirograph;
016    
017    /**
018     * This class contains a Thread that continuously prompts the
019     * user's class for an acceleration and stores the result in a {@link Coord}.
020     * Other objects (such as {@link DotPanel}) can monitor the <code>Coord</code>
021     * for updates.
022     *
023     * <p>Copyright © 1998 Massachusetts Institute of Technology.<br />
024     * Copyright © 2002-2003 Franklin W. Olin College of Engineering.</p>
025     *
026     * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu
027     * @author Henry Wong, henryw@mit.edu
028     * @version $Id: AccelHandler.java,v 1.7 2004/02/09 20:55:03 gus Exp $
029     * @see Accelerator
030     * @see DotPanel
031     * @see Coord
032     */
033    public class AccelHandler implements Runnable {
034      /** Position control mode */  
035      public static final int POSMODE = 1;
036      /** Velocity control mdde */
037      public static final int VELMODE = 2;
038      /** Acceleration control mode */
039      public static final int ACCELMODE = 3;
040        
041      private Accelerator Accel;    
042      private Coord myCoord;
043      private Coord otherCoord;
044        
045      /**
046       * Creates a new AccelHandler.  Does not start a new Thread.
047       *
048       * @param Accel Provides behavior based on student code
049       * @param myCoord Our position for this Dimension
050       * @param otherCoord Our position in the other Dimension
051       */
052      public AccelHandler(Accelerator Accel, Coord myCoord, Coord otherCoord) {
053        this.Accel = Accel;
054        this.myCoord = myCoord;
055        this.otherCoord = otherCoord;
056      }
057        
058      /**
059       * Changes the current Accelerator.
060       *
061       * @param Accel the new Accelerator
062       */
063      public synchronized void setAccel (Accelerator Accel) {
064        // This method is synchronized with part of the while(true)
065        // loop to keep people from switching the Accelerator while
066        // it is being queried.
067    
068        System.out.println ("New Accelerator set.");
069        this.Accel = Accel;
070      }
071    
072      /**
073       * Prompt the user's class for an accleration and pass the info to
074       * a Coord.
075       */    
076      public void run() {
077          while (true) {
078              synchronized (this) {
079                  double tmp = Accel.act( myCoord.getPos(), 
080                                    myCoord.getVel(), 
081                                    otherCoord.getPos(), 
082                                    otherCoord.getVel(), 
083                                    myCoord.getMaxPos());
084                  double sign = tmp/Math.abs(tmp);
085                  tmp = (Math.abs(tmp) <= myCoord.getMaxPos()) ? 
086                        tmp : myCoord.getMaxPos()*sign;
087                  myCoord.setValue(tmp);
088              }
089              try {
090                  Thread.sleep((int)(100*Spirograph.TIMESTEP));
091                  
092                  //Thread.yield();
093              } catch (Exception e) {
094                  System.out.println ("Oops.");
095              }
096          }
097      }
098    }
099    
100    /*
101     * $Log: AccelHandler.java,v $
102     * Revision 1.7  2004/02/09 20:55:03  gus
103     * javadoc fixes
104     *
105     * Revision 1.6  2003/01/13 17:52:54  gus
106     * Do some Bounds checking in the act method.
107     *
108     * Revision 1.5  2003/01/10 22:19:18  gus
109     * Complete and update the javadocs
110     *
111     * Revision 1.4  2003/01/09 20:45:23  gus
112     * Reindented the run method
113     *
114     * Revision 1.3  2003/01/09 20:36:25  gus
115     * Added Javadoc and comments, updated Copyright
116     *
117     */