Wednesday, May 4, 2011

How to Control a Robot Over the Internet



Listing 1

/**
 * @(#)Ft639Servlet.java
 */

import com.ferrettronics.device.Ft639;
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This is a sample servlet that takes input from
 * a form, parses and processes it, and sends
 * commands to a serial port to control
 * an FT639 and attached servos.
 */
public class Ft639Servlet extends HttpServlet
{

  ////////// Data

  SerialPort serialPort = null;

  ////////// Methods

  public void init( ServletConfig config )
  throws ServletException
  {
    super.init( config );
  }

  /**
   * Send data to port in response to the POSTed
   * form.  Write a "confirmation" to the client.
   */
  public void doPost( HttpServletRequest req,
                      HttpServletResponse res )
  throws ServletException, IOException
  {
    // Set the "content type" header of response.
    res.setContentType( "text/html" );
    // Get response¹s PrintWriter to return text
    // to the client.
    PrintWriter toClient = res.getWriter();
    // Find the serial port that the FT639 is
    // attached to. In this example, it is
    // attached to 'COM2'. We enumerate through
    // all the ports, and stop when we find it.
    Enumeration portList =
      CommPortIdentifier.getPortIdentifiers();
    CommPortIdentifier portId = null;
    while ( portList.hasMoreElements() )
    {
      portId =
      (CommPortIdentifier)portList.nextElement();
      if ( portId.getPortType() ==
           CommPortIdentifier.PORT_SERIAL )
      {
        // Windows
        if ( portId.getName().equals( "COM2" ) )
        {
          try {
            serialPort =
              (SerialPort)portId.open(
                "Sample639", // App Name
                2000 );      // Timeout
            break;
          } catch ( PortInUseException piue ) {
            toClient.println(
              "Exception:<br>" + piue + "<p>" );
            piue.printStackTrace();
            System.exit( -1 );
          }
        } // end if
      } // end if
    } // end while
    // Get pin.
    String strPin = null;
    String [] pinArray =
      req.getParameterValues( "pin" );
    if ( pinArray != null ) strPin = pinArray[0];
    // Get pos.
    String strPos = null;
    String [] posArray =
      req.getParameterValues( "pos" );
    if ( posArray != null ) strPos = posArray[0];

    // Pulse Length and Header Length support
    // not included in this code.

    toClient.println( "<html>" );
    toClient.println( "<title>Got it!</title>" );
    toClient.println( "Pin=" + strPin + "<p>" );
    toClient.println( "Pos=" + strPos + "<p>" );
    toClient.println( "Serial Port =" +
                      serialPort + "<p>" );
    try {
      sendTo639( strPin, strPos );
    } catch ( Exception excp ) {
      toClient.println(
        "Exception:<br>" + excp + "<p>" );
    }
    toClient.println( "</html>" );
    // Close the writer; the response is done.
    toClient.close();
    if ( serialPort != null ) serialPort.close();
  }

  /**
   * Send data to 639.
   *
   * @param sPin  Specifies the 639 pin that the
   *              servo is attached to.
   * @param sPos  Specifies the position to move
   *              the servo to.
   */
  public void sendTo639( String sPin,
                         String sPos )
  throws Exception
  {
    if ( serialPort == null )
    {
      throw new Exception(
        "Serial port is null!!" );
    }
    int pin = Integer.parseInt( sPin );
    int pos = Integer.parseInt( sPos );

    // Some error checking on pin and pos values
    // could go here!!

    // Create a 639 and
    // connect it to the serial port.
    Ft639 ft639 = new Ft639();
    ft639.connectTo( serialPort );
    // Move the servo
    ft639.setServoPosition( pin, pos );
  }

}

// EOF

No comments:

Post a Comment