RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.irobot.robots; 00002 00003 import rlpark.plugin.irobot.data.IRobotLabels; 00004 import rlpark.plugin.irobot.data.RoombaLeds; 00005 import rlpark.plugin.irobot.internal.descriptors.DropDescriptors; 00006 import rlpark.plugin.irobot.internal.descriptors.RoombaSerialDescriptor; 00007 import rlpark.plugin.irobot.internal.irobot.IRobotDiscoConnection; 00008 import rlpark.plugin.irobot.internal.roomba.RoombaSerialConnection; 00009 import rlpark.plugin.irobot.internal.server.IRobotDiscoServer; 00010 import rlpark.plugin.rltoys.envio.observations.Legend; 00011 import rlpark.plugin.robot.observations.ObservationReceiver; 00012 00013 public class RoombaRobot extends IRobotEnvironment { 00014 static public final double MaxAction = 200; 00015 00016 public RoombaRobot(String serialPortPath) { 00017 this(new RoombaSerialConnection(serialPortPath)); 00018 } 00019 00020 public RoombaRobot(String localhost, int port) { 00021 this(new IRobotDiscoConnection(localhost, port, DropDescriptors.newRoombaSensorDrop())); 00022 } 00023 00024 public RoombaRobot() { 00025 this("localhost", IRobotLabels.DiscoDefaultPort); 00026 } 00027 00028 private RoombaRobot(ObservationReceiver receiver) { 00029 super(receiver, true); 00030 } 00031 00032 public void sendLeds(RoombaLeds leds) { 00033 sendLeds(leds.dirt, (byte) leds.cleanColor, (byte) leds.intensity); 00034 } 00035 00036 public void sendLeds(boolean dirt, byte cleanColor, byte intensity) { 00037 sendMessage(new byte[] { (byte) 139, (byte) (dirt ? 1 : 0), cleanColor, intensity }); 00038 } 00039 00040 @Override 00041 public void sendLeds(int powerColor, int powerIntensity, boolean play, boolean advance) { 00042 sendLeds(new RoombaLeds(true, advance, play, powerColor, powerIntensity)); 00043 } 00044 00045 @Override 00046 protected void sendActionToRobot(double left, double right) { 00047 short shortLeft = toActionValue(MaxAction, left); 00048 short shortRight = toActionValue(MaxAction, right); 00049 sendMessage(new byte[] { (byte) 146, (byte) (shortRight >> 8), (byte) shortRight, (byte) (shortLeft >> 8), 00050 (byte) shortLeft }); 00051 } 00052 00053 public void filterLastReceivedObs() { 00054 if (lastReceivedObsBuffer == null) 00055 return; 00056 Legend legend = legend(); 00057 final double[] lastObs = lastReceivedObsBuffer.doubleValues(); 00058 double icRight = lastObs[legend.indexOf(IRobotLabels.ICRight)]; 00059 lastObs[legend.indexOf(IRobotLabels.ICOmni)] = icRight; 00060 if (icRight == 88 || icRight == 89) 00061 lastObs[legend.indexOf(IRobotLabels.WallVirtual)] = 1.0; 00062 } 00063 00064 @Override 00065 public void resetForCharging() { 00066 System.out.println("Resetted"); 00067 } 00068 00069 public static void main(String[] args) { 00070 IRobotDiscoServer.runServer("/dev/cu.FireFly-155A-SPP", new RoombaSerialDescriptor()); 00071 } 00072 }