RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.irobot.robots; 00002 00003 import rlpark.plugin.irobot.data.CreateAction; 00004 import rlpark.plugin.irobot.data.CreateLeds; 00005 import rlpark.plugin.irobot.internal.descriptors.IRobotObservationReceiver; 00006 import rlpark.plugin.rltoys.envio.actions.Action; 00007 import rlpark.plugin.rltoys.envio.observations.Legend; 00008 import rlpark.plugin.robot.helpers.RobotEnvironment; 00009 import rlpark.plugin.robot.helpers.Robots; 00010 import rlpark.plugin.robot.observations.ObservationReceiver; 00011 import zephyr.plugin.core.api.monitoring.abstracts.DataMonitor; 00012 import zephyr.plugin.core.api.monitoring.abstracts.MonitorContainer; 00013 import zephyr.plugin.core.api.monitoring.abstracts.Monitored; 00014 00015 abstract public class IRobotEnvironment extends RobotEnvironment implements MonitorContainer { 00016 protected final CreateAction lastSent = new CreateAction(0, 0); 00017 private final IRobotObservationReceiver connection; 00018 00019 protected IRobotEnvironment(ObservationReceiver receiver, boolean persistent) { 00020 super(receiver, persistent); 00021 connection = (IRobotObservationReceiver) receiver(); 00022 } 00023 00024 @Override 00025 public Legend legend() { 00026 return connection.legend(); 00027 } 00028 00029 public void sendMessage(byte[] bs) { 00030 connection.sendMessage(bs); 00031 } 00032 00033 public void passiveMode() { 00034 sendMessage(new byte[] { (byte) 128 }); 00035 } 00036 00037 public void safeMode() { 00038 sendMessage(new byte[] { (byte) 131 }); 00039 } 00040 00041 public void fullMode() { 00042 sendMessage(new byte[] { (byte) 132 }); 00043 } 00044 00045 public void clean() { 00046 sendMessage(new byte[] { (byte) 135 }); 00047 } 00048 00049 public void dock() { 00050 sendMessage(new byte[] { (byte) 143 }); 00051 } 00052 00053 public void registerSong(int songNumber, int[] song) { 00054 byte songLength = (byte) Math.min(song.length / 2, 16); 00055 byte[] message = new byte[3 + songLength * 2]; 00056 message[0] = (byte) 140; 00057 message[1] = (byte) songNumber; 00058 message[2] = songLength; 00059 for (int i = 0; i < songLength * 2; i++) 00060 message[3 + i] = (byte) song[i]; 00061 sendMessage(message); 00062 } 00063 00064 public void playSong(int songNumber) { 00065 sendMessage(new byte[] { (byte) 141, (byte) songNumber }); 00066 } 00067 00068 public void playSong(int[] song) { 00069 registerSong(0, song); 00070 playSong(0); 00071 } 00072 00073 @Override 00074 public void addToMonitor(DataMonitor monitor) { 00075 monitor.add("ActionWheelLeft", new Monitored() { 00076 @Override 00077 public double monitoredValue() { 00078 return lastSent.left(); 00079 } 00080 }); 00081 monitor.add("ActionWheelRight", new Monitored() { 00082 @Override 00083 public double monitoredValue() { 00084 return lastSent.right(); 00085 } 00086 }); 00087 Robots.addToMonitor(monitor, this); 00088 } 00089 00090 public void sendAction(CreateAction agentAction) { 00091 sendAction(agentAction.left(), agentAction.right()); 00092 } 00093 00094 public void sendAction(double left, double right) { 00095 lastSent.set(left, right); 00096 sendActionToRobot(left, right); 00097 } 00098 00099 abstract protected void sendActionToRobot(double left, double right); 00100 00101 protected short toActionValue(double maxAction, double value) { 00102 return (short) Math.min(maxAction, Math.max(-maxAction, value)); 00103 } 00104 00105 public void sendAction(Action a) { 00106 sendAction((CreateAction) a); 00107 } 00108 00109 public void sendLeds(CreateLeds leds) { 00110 sendLeds((byte) leds.powerColor, (byte) leds.powerIntensity, leds.play, leds.advance); 00111 } 00112 00113 abstract public void resetForCharging(); 00114 00115 abstract public void sendLeds(int powerColor, int powerIntensity, boolean play, boolean advance); 00116 }