RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.irobot.internal.descriptors; 00002 00003 00004 import java.io.IOException; 00005 00006 import rlpark.plugin.irobot.internal.serial.SerialPortToRobot; 00007 import rlpark.plugin.rltoys.envio.observations.Legend; 00008 import rlpark.plugin.robot.internal.disco.datagroup.DropScalarGroup; 00009 import rlpark.plugin.robot.internal.disco.drops.Drop; 00010 import rlpark.plugin.robot.internal.sync.LiteByteBuffer; 00011 import rlpark.plugin.robot.internal.sync.Syncs; 00012 import rlpark.plugin.robot.observations.ObservationVersatile; 00013 import zephyr.plugin.core.api.signals.Listener; 00014 import zephyr.plugin.core.api.signals.Signal; 00015 import zephyr.plugin.core.api.synchronization.Chrono; 00016 00017 public class IRobotCreateSerialConnection implements IRobotObservationReceiver { 00018 public Signal<IRobotCreateSerialConnection> onClosed = new Signal<IRobotCreateSerialConnection>(); 00019 private final Drop sensorDrop; 00020 protected final DropScalarGroup sensors; 00021 protected SerialPortToRobot serialPort; 00022 protected final String fileName; 00023 private SerialLinkStateMachine stateMachine; 00024 private final IRobotSerialDescriptor serialDescriptor; 00025 private final Listener<byte[]> sensorDataListener = new Listener<byte[]>() { 00026 @Override 00027 public void listen(byte[] sensorData) { 00028 receiveData(sensorData); 00029 } 00030 }; 00031 private final LiteByteBuffer byteBuffer; 00032 private Chrono timeSinceReset = null; 00033 00034 public IRobotCreateSerialConnection(String fileName, IRobotSerialDescriptor serialDescriptor) { 00035 this.fileName = fileName; 00036 sensorDrop = serialDescriptor.createSensorDrop(); 00037 sensors = new DropScalarGroup(sensorDrop); 00038 this.serialDescriptor = serialDescriptor; 00039 byteBuffer = new LiteByteBuffer(sensorDrop.dataSize()); 00040 } 00041 00042 synchronized protected void receiveData(byte[] sensorData) { 00043 byteBuffer.clear(); 00044 byteBuffer.put(sensorData); 00045 notifyAll(); 00046 } 00047 00048 @Override 00049 public void sendMessage(byte[] bytes) { 00050 if (!canWriteOnSerialPort()) 00051 return; 00052 try { 00053 serialPort.send(bytes); 00054 } catch (IOException e) { 00055 e.printStackTrace(); 00056 close(); 00057 SerialPortToRobot.fatalError("error while sending message"); 00058 } 00059 checkResetCommand(bytes); 00060 } 00061 00062 protected void checkResetCommand(byte[] bytes) { 00063 if (bytes[0] == 7) 00064 if (timeSinceReset == null) 00065 timeSinceReset = new Chrono(); 00066 else 00067 timeSinceReset.start(); 00068 } 00069 00070 protected boolean canWriteOnSerialPort() { 00071 return timeSinceReset == null || timeSinceReset.getCurrentChrono() > 3.0; 00072 } 00073 00074 @Override 00075 synchronized public void close() { 00076 serialPort.close(); 00077 notifyAll(); 00078 onClosed.fire(this); 00079 } 00080 00081 @Override 00082 public Legend legend() { 00083 return sensors.legend(); 00084 } 00085 00086 @Override 00087 public void initialize() { 00088 serialPort = SerialPortToRobot.openPort(fileName, serialDescriptor.portInfo()); 00089 if (serialPort == null) 00090 return; 00091 ((SerialPortToCreate) serialPort).onClosed.connect(new Listener<SerialPortToRobot>() { 00092 @Override 00093 public void listen(SerialPortToRobot eventInfo) { 00094 close(); 00095 } 00096 }); 00097 serialPort.wakeupRobot(); 00098 try { 00099 serialDescriptor.initializeRobotCommunication(serialPort); 00100 } catch (IOException e) { 00101 e.printStackTrace(); 00102 serialPort.close(); 00103 return; 00104 } 00105 stateMachine = serialDescriptor.createStateMachine(serialPort); 00106 stateMachine.onDataPacket.connect(sensorDataListener); 00107 } 00108 00109 synchronized public byte[] waitForRawData() { 00110 try { 00111 wait(); 00112 } catch (InterruptedException e) { 00113 e.printStackTrace(); 00114 return null; 00115 } 00116 return byteBuffer.array(); 00117 } 00118 00119 @Override 00120 public synchronized ObservationVersatile waitForData() { 00121 waitForRawData(); 00122 return Syncs.createObservation(System.currentTimeMillis(), byteBuffer, sensors); 00123 } 00124 00125 @Override 00126 public boolean isClosed() { 00127 return serialPort == null || serialPort.isClosed(); 00128 } 00129 00130 public SerialLinkStateMachine stateMachine() { 00131 return stateMachine; 00132 } 00133 00134 public IRobotSerialDescriptor descriptor() { 00135 return serialDescriptor; 00136 } 00137 00138 @Override 00139 public int packetSize() { 00140 return sensorDrop.dataSize(); 00141 } 00142 }