/*
* The author of this software is Thomas Hubbard.
* Copyright (c) 2002 Thomas Hubbard.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, THE AUTHOR AND Scott Edwards Electronics, Inc. MAKE NO
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
*/
import javax.comm.*;
import java.io.*;
/**
SevroTest is a sample application that allows you to rotate a servo clockwise
then counter-clockwise using the Mini SSC (tm) II Serial Servo Controller from
Scott Edwards Electronics, Inc.
Installation:
Make sure you have the Java (tm) Communications API installed. You can
find the communications API at http://java.sun.com/products/javacomm/
Make sure you have Java(tm) installed and working properly. I was using Java 2
but as long as the communications API supports your version of Java you should
be okay. Compile this sample code using your Java compiler.
Next, connect the servo controller and servos to your PC according to the
installation instructions from the servo controller manufacturer.
Note: This sample is setup to exercise servo 0 at 9600 baud. If this is not what you need,
change the sample code as noted below. If you are running under Windows, make sure
the port settings are set properly in Control Panel. Now run the sample.
From the installation directory:
java ServoTest [com port]
Example: java ServoTest com1
That should move the servo clockwise then counter-clockwise. The sample is meant to be
illustrative only. It would be quite easy, using this sample, to create a Java
application that captured input from the keyboard to control the servos to make a
simple remote control. That is left as an exercise for the reader;)
@author Thomas Hubbard (jack-7@rcn.com)
*/
public class ServoTest
{
CommPortIdentifier mPortId = null;
SerialPort mPort = null;
/**
* Open the port that is attached to the servo controller.
*
* @param portName The name of the port to open e.g. COM1
*
* @return none
*
* @exception NoSuchPortException when a port requested is not present in the system.
* @exception PortInUseException when the port requested has been opened by another application.
*/
public void openPort( String portName ) throws NoSuchPortException, PortInUseException
{
System.out.println( "Opening port..." + portName );
mPortId = CommPortIdentifier.getPortIdentifier(portName);
mPort = (SerialPort)mPortId.open(portName, 30000);
}
/**
* Close the port that is attached to the servo controller.
*
* @return none
*/
public void closePort()
{
System.out.println( "Closing port..." );
mPort.close();
}
/**
* Intialize the port that is attached to the servo controller.
*
* @param baud the baud rate the servo controller is set for (see the servo controller
* installation instructions)
*
* @return none
*
* @exception UnsupportedCommOperationException if the serial port does not setting baud,
* data, stop or parity.
*/
public void initPort( int baud ) throws UnsupportedCommOperationException
{
System.out.println( "Initializing Port..." );
// Set the baud rate and other parameters.
//
mPort.setSerialPortParams( baud,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE );
// Setup the flow control
//
mPort.setFlowControlMode(javax.comm.SerialPort.FLOWCONTROL_NONE);
}
/**
* Show the proper usage.
*/
public void usage()
{
System.out.println( "Usage: java ServoTest [port]");
System.out.println( "Example: java ServoTest COM1");
}
/**
* Get an output stream that we can use to control the servo controller.
* @return OutputStream to use for communicating with the servo controller.
* @exception IOExeception if the the OutputStream can not be openned.
*/
OutputStream getOutputStream() throws IOException
{
return mPort.getOutputStream();
}
public static void main( String args[] )
{
ServoTest test = new ServoTest();
// handle command line arguments.
//
if( args.length == 0 )
{
test.usage();
System.exit(1);
}
// Open the requested port.
//
try
{
test.openPort( args[0].toUpperCase() );
}
catch (NoSuchPortException e)
{
System.out.println( "No such port " + args[0] );
test.usage();
System.exit(1);
}
catch (PortInUseException e)
{
System.out.println( "Port already in use " + e.getMessage() );
test.usage();
System.exit(1);
}
// Initialize the port. To use a slower baud rate replace the 9600 below
// with something more to your liking.
//
int baudRate = 9600;
try
{
test.initPort( baudRate );
}
catch (UnsupportedCommOperationException e)
{
System.out.println( e.getMessage() );
test.usage();
System.exit(1);
}
OutputStream os = null;
// Open the output stream for the port.
//
try
{
os = test.getOutputStream();
}
catch (IOException e)
{
test.closePort();
System.out.println( e.getMessage() );
}
// To control other servos replace the 0 below with the servo number.
//
byte servo = 0;
// Time to control the servo. First rotate clockwise then counter clockwise.
//
try
{
int i = 0;
// rotate clockwise in 1 step units.
//
for( i = 0; i < 254; i++ )
{
// send the attention signal
//
os.write((byte)255);
// select the servo to control
//
os.write((byte)servo);
// write the position
//
os.write((byte)i);
}
// rotate counter clockwise in 1 step units.
//
for( i = 254; i >= 0; i-- )
{
os.write((byte)255);
// select the servo to control
//
os.write((byte)servo);
// write the position
//
os.write((byte)i);
}
}
catch( IOException ioe )
{
System.out.println( ioe );
}
test.closePort();
System.out.println( "Done!" );
}
}