Page 89 - The Definitive Guide to Building Java Robots
P. 89

Preston_5564C03.fm  Page 70  Wednesday, October 5, 2005  7:21 AM



                 70     CHAPTER 3  ■  MOTION



                                    // get instance of SingleSerialPort
                                    JSerialPort sPort = (JSerialPort)SingleSerialPort.getInstance(1);
                                    // create instance of SpeedDiffDrive
                                    SpeedDiffDrive diffDrive = new SpeedDiffDrive(sPort);
                                    // set speed to 5
                                    diffDrive.setSpeed(5);
                                    // move forward 2 seconds
                                    diffDrive.forward(2000);
                                    // close port
                                    sPort.close();
                                } catch (Exception e) {
                                    // print stack trace and exit
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }

                            // for interface passthroughs
                            public void forward(int ms) throws Exception{
                                super.forward(ms);
                            }
                            public void reverse(int ms) throws Exception{
                                super.reverse(ms);
                            }
                            public void pivotRight(int ms) throws Exception{
                                super.pivotRight(ms);
                            }
                            public void pivotLeft(int ms) throws Exception{
                                super.pivotLeft(ms);
                            }
                        }


                        Section Summary
                        The three classes in this section will get you through most wheeled motion using a serial servo
                        controller and/or electronic speed control. The classes I discussed were
                            • BasicDiffDrive.java: The basic differential drive control
                            • TimedDiffDrive.java: The extended version of BasicDiffDrive that allows motion to
                              occur at specific time intervals
   84   85   86   87   88   89   90   91   92   93   94