RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.irobot.internal.descriptors; 00002 00003 import gnu.io.SerialPort; 00004 00005 import java.io.IOException; 00006 00007 import rlpark.plugin.irobot.data.IRobotLabels; 00008 import rlpark.plugin.irobot.internal.serial.SerialPortToRobot; 00009 import rlpark.plugin.irobot.internal.serial.SerialPortToRobot.SerialPortInfo; 00010 import rlpark.plugin.rltoys.utils.Utils; 00011 import rlpark.plugin.robot.internal.disco.drops.Drop; 00012 import zephyr.plugin.core.api.signals.Listener; 00013 00014 public class CreateSerialDescriptor implements IRobotSerialDescriptor { 00015 class StartCommunicationRunnable implements Runnable { 00016 00017 private final SerialPortToRobot serialPort; 00018 00019 StartCommunicationRunnable(SerialPortToRobot serialPort) { 00020 this.serialPort = serialPort; 00021 } 00022 00023 @Override 00024 public void run() { 00025 try { 00026 Thread.sleep(2500); 00027 System.out.println("Setting communication"); 00028 initializeRobotCommunication(serialPort); 00029 } catch (IOException e) { 00030 e.printStackTrace(); 00031 } catch (InterruptedException e) { 00032 e.printStackTrace(); 00033 } 00034 } 00035 00036 } 00037 00038 Thread startCommunicationThread = null; 00039 00040 @Override 00041 public boolean initializeRobotCommunication(SerialPortToRobot serialPort) throws IOException { 00042 serialPort.sendAndWait(new byte[] { (byte) 128 }); 00043 serialPort.sendAndWait(new byte[] { (byte) 148, 1, 6 }); 00044 return true; 00045 } 00046 00047 @Override 00048 public SerialLinkStateMachine createStateMachine(final SerialPortToRobot serialPort) { 00049 HeaderNode headerNode = new HeaderNode(19, 53); 00050 headerNode.onMisalignedPackets.connect(new Listener<Integer>() { 00051 @Override 00052 public void listen(Integer misalignedPacket) { 00053 if (misalignedPacket % 200 == 0 && (startCommunicationThread == null || !startCommunicationThread.isAlive())) { 00054 startCommunicationThread = new Thread(new StartCommunicationRunnable(serialPort), "ReinitializeCommunication"); 00055 startCommunicationThread.start(); 00056 } 00057 } 00058 }); 00059 DataNode dataNode = new DataNode(IRobotLabels.CreateSensorsPacketSize); 00060 ChecksumNode checksumNode = new ChecksumNode(Utils.asList(dataNode, headerNode)); 00061 return new SerialLinkStateMachine((SerialPortToCreate) serialPort, checksumNode, headerNode, dataNode); 00062 } 00063 00064 @Override 00065 public Drop createSensorDrop() { 00066 return DropDescriptors.newCreateSensorDrop(); 00067 } 00068 00069 @Override 00070 public byte[] messageOnNoClient() { 00071 return new byte[] { (byte) 145, 0, 0, 0, 0 }; 00072 } 00073 00074 @Override 00075 public SerialPortInfo portInfo() { 00076 return new SerialPortInfo(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, 00077 SerialPort.FLOWCONTROL_NONE); 00078 } 00079 }