|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON General Java How to Control A Robot Over the Internet
How to Control A Robot Over the Internet
Jun. 1, 2000 12:00 AM
So you want to build a robot that walks around and bumps into things. But that's not enough for you (this is the year 2000, after all); you also want to control your robot over the Internet. What's more, you want to use Java to control it. We'll start by showing you the basics of controlling an electric motor over the Web. Specifically, we'll describe how to control radio control (RC) servo motors over the Web. Servos are electric motors popular for use in many applications, including robotics. The components of the system (see Figure 1 for an overview) include a front-end HTML form for entering commands to control the robot, and a Java servlet that accepts these commands and uses a free open-source Java software kit from FerretTronics to send them through a serial port to your robot. On the other end of the serial port receiving these commands will be a FerretTronics chip, the FT639 Servo Controller. The FT639 converts the serial data into electric pulses that a servo can understand. The FT639 sends the signal to the appropriate servo, which responds by moving to the position specified. We'll assume that your robot is attached to your serial port and that the computer you're using is running a Web server that supports Java servlets.
Software
HTML Form
The form has standard HTML controls for setting items, as follows:
After you press the send button on the HTML form, all the information is posted over the Internet to the Java servlet (see Listing 1). The servlet is installed on a Web server and the Web server is physically attached to your robot through a serial port. We're keeping this servlet simple, with limited error-checking, and avoiding performance issues. The servlet works, but isn't robust or efficient. A few things will need to be set up on your Web server to get this servlet working. First, you'll need to get Java's serial port package (JavaComm). It's a standard Java extension package (javax.comm.*). The Windows and Solaris reference implementations are available on Sun's Web site (www.sun.com). Implementations for other operating systems are available, but you might have to dig around if you're using an operating system other than Windows or Solaris. Three things you need to remember:
If these steps aren't followed and JavaComm isn't set up correctly, it won't be able to find any ports on your system and the servlet won't work. (See the documentation that comes with JavaComm and your servlet engine for more details.) The final thing you need to get is the FTJDK it's available at www.ferrettronics.com. You'll need to put the ftjdk.jar file in the servlet engine's classpath. If you're not using Windows or if your robot isn't attached to COM2, you'll need to adjust the servlet code accordingly so that it accesses the correct serial port on your system, and then recompile the servlet like this: if ( portId.getName().equals( "COM2" ) ) { .... }
Let's take a look at the servlet code, starting with the doPost method because that's the method that gets invoked when a remote HTML client presses its send button and posts its form data to the servlet. The first part of the code deals with finding and opening the serial port. It iterates through all the communication ports it finds on the system and stops when it finds the one specified. You may need to change this code to point to the port your robot is attached to (COM1, /dev/term/a, and so on). Next, the servlet code gets the form data, puts it into convenient variables, prints their values back to the HTML client and then calls the sendTo639 method. This method uses the FTJDK, to write the appropriate bytes to the serial port to control the servo. Let's take a closer look at the sendTo639 method. After it formats the parameters for the FTJDK. it then creates an Ft639 object from the FTJDK, connects it to a SerialPort object from JavaComm and finally sends the appropriate commands to configure the FT639 chip or to move a servo attached to one of the FT639's pins. An Ft639 object from the FTJDK knows how to translate these requests into bytestreams that a real FT639 chip can understand. The Ft639 object sends these bytes to the connected serial port when one of its methods is called. That's all there is to it. The bytes will flow out the serial port and eventually reach an FT639 chip. The FT639 translates the bytes to electric signals that a servo can understand and sends these signals out on one of its pins. The servo will then move to whatever position the remote HTML client requested.
The FTJDK
FTJDK Design
The downside of this arrangement is that the user of the FTJDK needs to re-create the physical hardware connections in the software so that the objects know where they should forward commands. For a large array of FT649s and FT639s this may be somewhat burdensome, but we feel the initial work is well worth the effort as it removes the need to include the routing information in the command every time you want to move a different servo. To redress the shortcomings of this design, a future version of the FTJDK will come with a GUI allowing you to create your circuit visually and then generate the code that glues the devices together. Figure 3 is a diagram of the entire system. On the left is the software that we've described above. On the right is the hardware we'll describe now.
The Hardware
The robot in this article consists of a single RC servo and isn't very powerful or interesting, but it could become so if you treat it as a building block. Our example can be extended to create just about anything. The servo is controlled via the FerretTronics FT639, which is in turn controlled by the Java servlet described above. The FT639 has eight pins one for the ground connection, one for the positive power supply (+5 volts) and one that is connected to the transmit line of the RS-232 connector from the host computer (the serial line). The remaining five pins are used to control up to five servos. The connection to the serial port is the most complicated aspect of the hardware. It requires two resistors that act to reduce the voltage from the serial port. Additionally, a single diode is placed in line with the serial line to prevent a negative voltage from the host computer entering the circuit. You also need to connect the ground from the serial port to the ground used by the FT639. Finally, the RS-232 protocol on the host machine must be set to 2400 baud, 8 data bits, 1 stop bit and no parity. Don't worry about setting the RS-232 protocol if you're using the FTJDK it takes care of setting the correct protocol before it sends any data over the serial port. RC hobby servos have three wires terminating at a single connector. One wire is the ground, another is the +5 volt supply and the third is for the control signal. The control signal line is connected directly to the FT639. The ground and V++ wires are connected to the same power supply as the FT639.
Conclusion
Resources
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
BREAKING JAVA NEWS |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||