RLPark 1.0.0
Reinforcement Learning Framework in Java

SerialLinkWatchDog.java

Go to the documentation of this file.
00001 package rlpark.plugin.irobot.internal.server;
00002 
00003 import gnu.io.SerialPortEvent;
00004 
00005 import java.io.IOException;
00006 
00007 import rlpark.plugin.irobot.internal.descriptors.IRobotCreateSerialConnection;
00008 import rlpark.plugin.irobot.internal.descriptors.IRobotSerialDescriptor;
00009 import rlpark.plugin.irobot.internal.serial.SerialPortToRobot;
00010 import zephyr.plugin.core.api.signals.Listener;
00011 import zephyr.plugin.core.api.synchronization.Chrono;
00012 
00013 public class SerialLinkWatchDog implements Runnable, Listener<SerialPortToRobot> {
00014   static private final int CheckingTime = 5000; // (ms)
00015   private final IRobotCreateSerialConnection connection;
00016   private final IRobotSerialDescriptor descriptor;
00017   private Thread thread;
00018   private final Chrono lastSerialEventTime = new Chrono();
00019 
00020   public SerialLinkWatchDog(IRobotCreateSerialConnection connection, IRobotSerialDescriptor descriptor) {
00021     this.connection = connection;
00022     this.descriptor = descriptor;
00023   }
00024 
00025   @Override
00026   public void run() {
00027     while (!connection.isClosed()) {
00028       try {
00029         Thread.sleep(CheckingTime);
00030       } catch (InterruptedException e) {
00031         e.printStackTrace();
00032       }
00033       if (lastSerialEventTime.getCurrentMillis() > CheckingTime) {
00034         System.err.println("Warning: serial link is soundless. Reinitialising communication...");
00035         try {
00036           descriptor.initializeRobotCommunication(connection.stateMachine().serialPort);
00037         } catch (IOException e) {
00038           e.printStackTrace();
00039           SerialPortToRobot.fatalError("Error while reinitialising the communication with the robot");
00040         }
00041       }
00042     }
00043   }
00044 
00045   public void start() {
00046     connection.stateMachine().serialPort.register(SerialPortEvent.DATA_AVAILABLE, this);
00047     thread = new Thread(this, "Serial Link Watch Dog");
00048     thread.setDaemon(true);
00049     thread.start();
00050   }
00051 
00052   @Override
00053   public void listen(SerialPortToRobot eventInfo) {
00054     lastSerialEventTime.start();
00055   }
00056 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark