diff --git a/.gitignore b/.gitignore index 3f7fa41..badb2d1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ target/ out/ __pycache__/ received.py +temp.py *.iml diff --git a/cs-minibot-platform.iml b/cs-minibot-platform.iml new file mode 100644 index 0000000..013870b --- /dev/null +++ b/cs-minibot-platform.iml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/python-interface/src/received/line_follower.py b/python-interface/example-scripts/line_follower.py similarity index 60% rename from python-interface/src/received/line_follower.py rename to python-interface/example-scripts/line_follower.py index bc3ed34..e4c6747 100644 --- a/python-interface/src/received/line_follower.py +++ b/python-interface/example-scripts/line_follower.py @@ -1,5 +1,11 @@ +import os +from os import sys, path +sys.path.append(os.path.dirname('../src/received/')) +sys.path.append(os.path.join('../src/received/sensor/')) +#sys.path.append(os.path.join(os.path.dirname('../src/received/'), '..')) +from sensor import Sensor from cup_minibot_prepend import CupMiniBot -from .sensor.Sensor import Sensor + bot = CupMiniBot() left = Sensor(bot, 'left') right = Sensor(bot, 'right') @@ -13,7 +19,7 @@ while(center.read() != 1): if(left.read() == 1): bot.turn_clockwise(100) - else if(right.read() == 1): + elif(right.read() == 1): bot.turn_counter_clockwise(100) else: end = True diff --git a/python-interface/example-scripts/testcommand.pyc b/python-interface/example-scripts/testcommand.pyc new file mode 100644 index 0000000..74b5c41 Binary files /dev/null and b/python-interface/example-scripts/testcommand.pyc differ diff --git a/python-interface/src/received/cup_minibot_prepend.py b/python-interface/src/received/cup_minibot_prepend.py index 23e6223..f29a824 100644 --- a/python-interface/src/received/cup_minibot_prepend.py +++ b/python-interface/src/received/cup_minibot_prepend.py @@ -1,5 +1,8 @@ -from .sensor.Sensor import Sensor -from .sensor.GPIOSensor import GPIOSensor +import os +from os import sys, path +sys.path.append(os.path.join(os.path.dirname('sensor'), '..')) +from sensor import Sensor +from sensor import GPIOSensor class CupMiniBot: diff --git a/python-interface/src/received/cup_minibot_prepend.pyc b/python-interface/src/received/cup_minibot_prepend.pyc new file mode 100644 index 0000000..d8ad73a Binary files /dev/null and b/python-interface/src/received/cup_minibot_prepend.pyc differ diff --git a/python-interface/src/received/sensor/GPIOSensor.py b/python-interface/src/received/sensor/GPIOSensor.py index f8160e4..80ef180 100644 --- a/python-interface/src/received/sensor/GPIOSensor.py +++ b/python-interface/src/received/sensor/GPIOSensor.py @@ -2,7 +2,7 @@ # Needs to be updated to actually work, fake for now! # Abstract class representing the sensor interface -from .Sensor import Sensor +from sensor import Sensor class GPIOSensor(Sensor): def __init__(self, bot, name, pin_number): diff --git a/python-interface/src/received/sensor/GPIOSensor.pyc b/python-interface/src/received/sensor/GPIOSensor.pyc new file mode 100644 index 0000000..a1efee6 Binary files /dev/null and b/python-interface/src/received/sensor/GPIOSensor.pyc differ diff --git a/python-interface/src/received/sensor/__init__.py b/python-interface/src/received/sensor/__init__.py index e69de29..07281d4 100644 --- a/python-interface/src/received/sensor/__init__.py +++ b/python-interface/src/received/sensor/__init__.py @@ -0,0 +1 @@ +from sensor import Sensor diff --git a/python-interface/src/received/sensor/__init__.pyc b/python-interface/src/received/sensor/__init__.pyc new file mode 100644 index 0000000..78cd8f1 Binary files /dev/null and b/python-interface/src/received/sensor/__init__.pyc differ diff --git a/python-interface/src/received/sensor/sensor.pyc b/python-interface/src/received/sensor/sensor.pyc new file mode 100644 index 0000000..5a218d8 Binary files /dev/null and b/python-interface/src/received/sensor/sensor.pyc differ diff --git a/python-interface/src/tcp.pyc b/python-interface/src/tcp.pyc new file mode 100644 index 0000000..9d6354a Binary files /dev/null and b/python-interface/src/tcp.pyc differ diff --git a/script.py b/script.py deleted file mode 100644 index 98f8e7f..0000000 --- a/script.py +++ /dev/null @@ -1,168 +0,0 @@ -import socket -import sys -import time -import json - -HOST = "localhost" -PORT = 11111 - -sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - -class SimMiniBot: - """ - Abstract simulator class defining the base functions of the MiniBot. More customized MiniBots may - subclass this. - """ - def __init__(self): - server_address = (HOST, PORT) - sock.connect(server_address) - - def move_forward(self, power): - """ - Moves the bot forward at a percentage of its full power - - :param power The percentage of the bot's power to use from 0-100 - :return True if the action is supported - """ - message = "FORWARD:" + str(power) + '\n' - sock.sendall(message) - return - - def move_backward(self, power): - """ - Moves the bot backward at a percentage of its full power - - :param power The percentage of the bot's power to use from 0-100 - :return True if the action is supported - """ - message = "BACKWARD:" + str(power) + '\n' - sock.sendall(message) - return - - def turn_clockwise(self, power): - """ - Moves the bot clockwise at a percentage of its full power - - :param power The percentage of the bot's power to use from 0-100 - :return True if the action is supported - """ - message = "RIGHT:" + str(power) + '\n' - sock.sendall(message) - return - - def turn_counter_clockwise(self, power): - """ - Moves the bot counter-clockwise at a percentage of its full power - - :param power The percentage of the bot's power to use from 0-100 - :return True if the action is supported - """ - message = "LEFT:" + str(power) + '\n' - sock.sendall(message) - return - - def set_wheel_power(self, front_left, front_right, back_left, back_right): - """ - Sets the power of the bot's wheels as a percentage from -100 to 100. If a wheel - specified does not exist, the power for that wheel is ignored. - :param front_left power to deliver to the front_left wheel - :param front_right power to deliver to the front_right wheel - :param back_left power to deliver to the back_left wheel - :param back_right power to deliver to the back_right wheel - :return True if the action is supported - """ - message = "WHEELS:" + str(front_left) + ',' + str(front_right) + ',' + str(back_left) + ',' \ - + str(back_right) + '\n'; - sock.sendall(message) - return - - def wait(self, t): - """ - Waits for a duration in seconds. - :param t The duration in seconds - """ - message = "WAIT:" + str(t) + '\n' - sock.sendall(message) - time.sleep(t) - return - - def stop(self): - """ - Waits for a duration in seconds. - :param t The duration in seconds - """ - message = "STOP:0" + '\n' - sock.sendall(message) - return - - def register_sensor(self, name): - """ - Registers a new sensor. - :param name The name of the sensor - """ - message = "REGISTER:" + name + '\n' - sock.sendall(message) - return - - def kill(self): - """ - Kills TCP connection - """ - message = "KILL:-1" + '\n' - sock.sendall(message) - sock.close() - return - - -class Sensor: - def __init__(self, bot, name): - self.name = name - bot.register_sensor(name) - - def read(self): - return "Invalid: Abstract Sensor Class Reading" - - -class GPIOSensor(Sensor): - def __init__(self, bot, name, pin_number): - Sensor.__init__(self, bot, name) - self.pin_number = pin_number - - def readAll(self): - """ - Get All Sensor Data - """ - message = "GET:ALL" + '\n' - sock.sendall(message) - result = sock.recv(1024) - return result - - def read(self): - """ - Get Sensor Data - """ - message = "GET:" + self.name + '\n' - sock.sendall(message) - result = sock.recv(1024) - return result - -bot = SimMiniBot() -bot.move_forward(70) -s = GPIOSensor(bot, 'bot1', 1) -state = 0 -while(True): - data = s.readAll() - j = json.loads(data) - center = j['center']['data'] - right = j['right']['data'] - left = j['left']['data'] - - if left and right: - bot.move_forward(70) - elif right: - bot.turn_clockwise(70) - elif left: - bot.turn_counter_clockwise(70) - else: - if state == 0: - bot.move_forward(70) \ No newline at end of file diff --git a/src/main/java/minibot/BaseHTTPInterface.java b/src/main/java/minibot/BaseHTTPInterface.java index ced7ff5..a5fc04d 100644 --- a/src/main/java/minibot/BaseHTTPInterface.java +++ b/src/main/java/minibot/BaseHTTPInterface.java @@ -90,19 +90,23 @@ public static void main(String[] args) { } else { SimBotConnection sbc = new SimBotConnection(); - PhysicalObject po = new PhysicalObject("simbot", 50, simvs.getWorld(), 0.0f, 0.0f, 1f, 3.6f, true); - SimBot simbot; - simbot = new SimBot(sbc, name, po); - newBot = simbot; + + PhysicalObject po = new PhysicalObject("TESTBOT", 50, simvs.getWorld(), 0.4f, 0.0f, 1f, 1f, true); + SimBot smbot; + smbot = new SimBot(sbc, name, po); + newBot = smbot; + ArrayList pObjs = new ArrayList<>(); pObjs.add(po); simvs.processPhysicalObjects(pObjs); // Color sensor TODO put somewhere nice - ColorIntensitySensor colorSensorL = new ColorIntensitySensor((SimBotSensorCenter) simbot.getSensorCenter(),"right",simbot, 5); - ColorIntensitySensor colorSensorR = new ColorIntensitySensor((SimBotSensorCenter) simbot.getSensorCenter(),"left",simbot, -5); - ColorIntensitySensor colorSensorM = new ColorIntensitySensor((SimBotSensorCenter) simbot.getSensorCenter(),"center",simbot, 0); + + ColorIntensitySensor colorSensorL = new ColorIntensitySensor((SimBotSensorCenter) smbot.getSensorCenter(),"right",smbot, 7); + ColorIntensitySensor colorSensorR = new ColorIntensitySensor((SimBotSensorCenter) smbot.getSensorCenter(),"left",smbot, -7); + ColorIntensitySensor colorSensorM = new ColorIntensitySensor((SimBotSensorCenter) smbot.getSensorCenter(),"center",smbot, 0); + } return BaseStation.getInstance().getBotManager().addBot(newBot); diff --git a/src/main/java/simulator/simbot/SimBot.java b/src/main/java/simulator/simbot/SimBot.java index 66f6101..6ec9180 100644 --- a/src/main/java/simulator/simbot/SimBot.java +++ b/src/main/java/simulator/simbot/SimBot.java @@ -88,6 +88,7 @@ public void run() { if (!content.contains("WHEELS") && !content.contains("REGISTER") && !content.contains("GET")) { value = Double.parseDouble(content.substring(content.indexOf(':') + 1)); } + String name = content.substring(content.indexOf(':') + 1); switch (content.substring(0, content.indexOf(':'))) { case "FORWARD": //fl fr bl br @@ -118,7 +119,6 @@ public void run() { System.out.println("Exiting\n"); break; case "GET": - String name = content.substring(content.indexOf(':') + 1); if (name.equals("ALL")) { out.println(this.sensorCenter.getAllDataGson()); } else { @@ -132,11 +132,8 @@ public void run() { System.out.println("Added New sensor"); break; default: - String cmd = content.substring(content.indexOf(':') + 1); - System.out.println(cmd); - String[] wheel_cmds = cmd.split(","); - - + System.out.println(name); + String[] wheel_cmds = name.split(","); this.commandCenter.setWheelPower( Double.parseDouble(wheel_cmds[0]), Double.parseDouble(wheel_cmds[1]), @@ -144,7 +141,6 @@ public void run() { Double.parseDouble(wheel_cmds[3]) ); break; - } } } diff --git a/src/main/java/simulator/simbot/SimBotCommandCenter.java b/src/main/java/simulator/simbot/SimBotCommandCenter.java index eb7af37..95d5d27 100644 --- a/src/main/java/simulator/simbot/SimBotCommandCenter.java +++ b/src/main/java/simulator/simbot/SimBotCommandCenter.java @@ -73,21 +73,22 @@ public boolean sendKV(String key, String value) { } String prg = value; - BufferedWriter out = new BufferedWriter(new FileWriter("script.py")); - out.write(header); - out.write(prg); - out.close(); + BufferedWriter scriptOut = new BufferedWriter(new FileWriter("temp.py")); + scriptOut.write(header); + scriptOut.write(prg); + scriptOut.close(); - ProcessBuilder pb = new ProcessBuilder("python","script.py"); + ProcessBuilder pb = new ProcessBuilder("python","temp.py"); Process p = pb.start(); - BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); + BufferedReader scriptFeedbackInStream = new BufferedReader(new InputStreamReader(p.getInputStream())); + - String ret; String line; - while ((line = in.readLine()) != null) { - ret = new String(line); - System.out.println(ret); + while ((line = scriptFeedbackInStream.readLine()) != null) { + int ret = new Integer(line).intValue(); + System.out.println("" + ret); + } }catch(Exception e){ System.out.println(e);