tricodex commited on
Commit ·
cfed446
1
Parent(s): 2fca4ce
git
Browse files
learning_machines_robobo/test_actions.py
DELETED
|
@@ -1,123 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import matplotlib.pyplot as plt
|
| 3 |
-
import csv
|
| 4 |
-
from data_files import FIGRURES_DIR
|
| 5 |
-
from robobo_interface import (
|
| 6 |
-
IRobobo,
|
| 7 |
-
SimulationRobobo,
|
| 8 |
-
HardwareRobobo,
|
| 9 |
-
)
|
| 10 |
-
from datetime import datetime
|
| 11 |
-
current_datetime = datetime.now().strftime("%Y%m%d-%H%M%S")
|
| 12 |
-
|
| 13 |
-
def plot_sensor_data(irs, title):
|
| 14 |
-
plt.figure()
|
| 15 |
-
for sensor_index in range(len(irs[0])):
|
| 16 |
-
sensor_values = [data[sensor_index] for data in irs]
|
| 17 |
-
plt.plot(sensor_values, label=f'Sensor {sensor_index + 1}')
|
| 18 |
-
plt.xlabel('Steps')
|
| 19 |
-
plt.ylabel('IR Sensor Values')
|
| 20 |
-
plt.title(title)
|
| 21 |
-
plt.legend()
|
| 22 |
-
plt.savefig(str(FIGRURES_DIR / f"{title}_sensor_plot{current_datetime}.png"))
|
| 23 |
-
|
| 24 |
-
def save_to_csv(sensor_readings, filename):
|
| 25 |
-
with open(filename, 'w', newline='') as csvfile:
|
| 26 |
-
writer = csv.writer(csvfile)
|
| 27 |
-
writer.writerow(['BackL', 'BackR', 'FrontL', 'FrontR', 'FrontC', 'FrontRR', 'BackC', 'FrontLL'])
|
| 28 |
-
for data in sensor_readings:
|
| 29 |
-
writer.writerow(data)
|
| 30 |
-
|
| 31 |
-
def task_example_1(rob: IRobobo, steps: int = 100):
|
| 32 |
-
"""Task: Go straight until an object is near, then turn right without touching it."""
|
| 33 |
-
sensor_readings = []
|
| 34 |
-
rob.play_simulation()
|
| 35 |
-
try:
|
| 36 |
-
for step in range(steps):
|
| 37 |
-
irs = rob.read_irs()
|
| 38 |
-
sensor_readings.append(irs)
|
| 39 |
-
if irs[4] > 40 or irs[5] > 40 or irs[7] > 40: # All front sensors
|
| 40 |
-
rob.move(0, 0, 0) # Stop
|
| 41 |
-
rob.sleep(0.1)
|
| 42 |
-
rob.move(50, -50, 500) # Turn right
|
| 43 |
-
rob.sleep(0.1)
|
| 44 |
-
rob.move(50, 50, 1000)
|
| 45 |
-
rob.sleep(10)
|
| 46 |
-
print(f"Steps run: {step + 1}")
|
| 47 |
-
break
|
| 48 |
-
else:
|
| 49 |
-
rob.move(50, 50, 100) # Go straight
|
| 50 |
-
rob.sleep(0.1)
|
| 51 |
-
finally:
|
| 52 |
-
rob.stop_simulation()
|
| 53 |
-
|
| 54 |
-
plot_sensor_data(sensor_readings, "taske_1")
|
| 55 |
-
save_to_csv(sensor_readings, str(FIGRURES_DIR / f"1_{current_datetime}.csv"))
|
| 56 |
-
|
| 57 |
-
def task_example_2(rob: IRobobo, steps: int = 100):
|
| 58 |
-
"""Task: Go straight until it touches the wall, then go backward."""
|
| 59 |
-
sensor_readings = []
|
| 60 |
-
turns = 0
|
| 61 |
-
obstacle_sensor_threshold = 10
|
| 62 |
-
wall_sensor_threshold = 50
|
| 63 |
-
rob.play_simulation()
|
| 64 |
-
try:
|
| 65 |
-
for step in range(steps):
|
| 66 |
-
irs = rob.read_irs()
|
| 67 |
-
sensor_readings.append(irs)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if irs[4] > 10 or irs[5] > 10 or irs[7] > 10:
|
| 71 |
-
rob.move(-50, -50, 150) # Move backward
|
| 72 |
-
rob.sleep(0.15)
|
| 73 |
-
# rob.move(0, 0, 0) # Stop
|
| 74 |
-
if irs[5] > 10:
|
| 75 |
-
rob.move(50, -50, 850) # turn right 90 degrees
|
| 76 |
-
elif irs[7] > 10:
|
| 77 |
-
rob.move(-50, 50, 850)
|
| 78 |
-
else:
|
| 79 |
-
rob.move(50, -50, 850)
|
| 80 |
-
# rob.sleep(0.85)
|
| 81 |
-
print("Obstacle Dodge")
|
| 82 |
-
else:
|
| 83 |
-
rob.move(75, 75, 250) # Go straight
|
| 84 |
-
# rob.sleep(0.250)
|
| 85 |
-
|
| 86 |
-
# print(irs[4])
|
| 87 |
-
# 'BackL', 'BackR', 'FrontL', 'FrontR', 'FrontC', 'FrontRR'5 'BackC', 'FrontLL'7
|
| 88 |
-
|
| 89 |
-
if irs[4] > 40 :
|
| 90 |
-
# if irs[4] > 60:
|
| 91 |
-
# rob.move(-50, -50, 1000)
|
| 92 |
-
rob.move(-50, -50, 1000) # Move backward
|
| 93 |
-
# rob.sleep(1)
|
| 94 |
-
rob.move(0, 0, 0) # Stop
|
| 95 |
-
rob.move(50, -50, 600) # turn right 90 degrees
|
| 96 |
-
# rob.sleep(0.6)
|
| 97 |
-
print("Wall Dodge")
|
| 98 |
-
turns += 1 # Turn
|
| 99 |
-
print(f"Steps run: {step + 1} for turn: {turns}")
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
else:
|
| 103 |
-
rob.move(75, 75, 250) # Go straight
|
| 104 |
-
rob.sleep(0.25)
|
| 105 |
-
|
| 106 |
-
if turns == 4:
|
| 107 |
-
print("Done")
|
| 108 |
-
break
|
| 109 |
-
finally:
|
| 110 |
-
rob.stop_simulation()
|
| 111 |
-
|
| 112 |
-
plot_sensor_data(sensor_readings, "task_2")
|
| 113 |
-
save_to_csv(sensor_readings, str(FIGRURES_DIR / f"2_o{obstacle_sensor_threshold}_w{wall_sensor_threshold}_{current_datetime}.csv"))
|
| 114 |
-
|
| 115 |
-
def run_all_actions(rob: IRobobo):
|
| 116 |
-
if isinstance(rob, SimulationRobobo):
|
| 117 |
-
rob.play_simulation()
|
| 118 |
-
# print("task 1")
|
| 119 |
-
# task_example_1(rob, steps=1000)
|
| 120 |
-
print("task 2")
|
| 121 |
-
task_example_2(rob, steps=300)
|
| 122 |
-
if isinstance(rob, SimulationRobobo):
|
| 123 |
-
rob.stop_simulation()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|