Skip to content

Commit

Permalink
view controllers in MATLAB,Python,R, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Wooding committed Feb 20, 2024
1 parent 09ea3bc commit 41e12b6
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 0 deletions.
51 changes: 51 additions & 0 deletions misc/.ipynb_checkpoints/View_Controller-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e320638d-6141-4bca-802f-b5403e563309",
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import pandas as pd\n",
"\n",
"# Open the HDF5 file\n",
"with h5py.File('controller.h5', 'r') as f:\n",
" # Assuming you have a dataset named 'dataset' in the HDF5 file\n",
" if 'dataset' in f:\n",
" # Access the dataset\n",
" dataset = f['dataset']\n",
" \n",
" # Convert the dataset to a pandas DataFrame and transpose it\n",
" df = pd.DataFrame(dataset[:]).T\n",
" \n",
" # Display the DataFrame\n",
" display(df)\n",
" else:\n",
" print(\"No 'dataset' found in the HDF5 file.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added misc/.view_controller.py.swp
Binary file not shown.
51 changes: 51 additions & 0 deletions misc/View_Controller.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e320638d-6141-4bca-802f-b5403e563309",
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import pandas as pd\n",
"\n",
"# Open the HDF5 file\n",
"with h5py.File('controller.h5', 'r') as f:\n",
" # Assuming you have a dataset named 'dataset' in the HDF5 file\n",
" if 'dataset' in f:\n",
" # Access the dataset\n",
" dataset = f['dataset']\n",
" \n",
" # Convert the dataset to a pandas DataFrame and transpose it\n",
" df = pd.DataFrame(dataset[:]).T\n",
" \n",
" # Display the DataFrame\n",
" display(df)\n",
" else:\n",
" print(\"No 'dataset' found in the HDF5 file.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
142 changes: 142 additions & 0 deletions misc/plots.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
clear
close all
clc

%% read in files
ss = h5read('ss.h5','/dataset');
target = h5read('ts.h5','/dataset');
is = h5read('is.h5','/dataset');
controller = h5read('controller.h5','/dataset');

%% figure 1 - state space with target/avoid regions
boxSize = 1; % Eta_x

%plot the state space with target set and avoid set
figure
plot(ss(:,1),ss(:,2),'.',"Color", "blue")
hold on
x = target(:,1);
y = target(:,2);
% Plotting boxes around each point
for i = 1:length(x)
rectangle('Position', [x(i)-boxSize/2, y(i)-boxSize/2, boxSize, boxSize], 'FaceColor', 'g');
end

hold on
x = avoid(:,1);
y = avoid(:,2);
% Plotting boxes around each point
for i = 1:length(x)
rectangle('Position', [x(i)-boxSize/2, y(i)-boxSize/2, boxSize, boxSize], 'FaceColor', 'r');
end

%% Figure 2 - Plot lower bound satisfaction probabilities
C = [controller(:,1:2), controller(:,5)];
X = C(:,1) ; Y = C(:,2) ; Z = C(:,3);
dt = delaunayTriangulation(X,Y);
Tri = dt.ConnectivityList;

figure
trisurf(Tri, X, Y, Z, 'FaceColor', 'interp');
colormap('cool');
colorbar;
hold on;

% Plotting boxes around each point (target)
for i = 1:length(target)
x = target(i, 1);
y = target(i, 2);
z = 1;
patch([x-boxSize x+boxSize x+boxSize x-boxSize],[y-boxSize y-boxSize y+boxSize y+boxSize],[z z z z],'g');
end

% Plotting boxes around each point (avoid)
for i = 1:length(avoid)
x = avoid(i, 1);
y = avoid(i, 2);
z = 1;
patch([x-boxSize x+boxSize x+boxSize x-boxSize],[y-boxSize y-boxSize y+boxSize y+boxSize],[z z z z],'r');
end

%% Figure 3 - Plot uppoer bound satisfaction probabilities
C = [controller(:,1:2), controller(:,6)];
X = C(:,1) ; Y = C(:,2) ; Z = C(:,3);
dt = delaunayTriangulation(X,Y);
Tri = dt.ConnectivityList;

figure
trisurf(Tri, X, Y, Z, 'FaceColor', 'interp');
colormap('cool');
colorbar;
hold on;

% Plotting boxes around each point (target)
for i = 1:length(target)
x = target(i, 1);
y = target(i, 2);
z = 1;
patch([x-boxSize x+boxSize x+boxSize x-boxSize],[y-boxSize y-boxSize y+boxSize y+boxSize],[z z z z],'g');
end

% Plotting boxes around each point (avoid)
for i = 1:length(avoid)
x = avoid(i, 1);
y = avoid(i, 2);
z = 1;
patch([x-boxSize x+boxSize x+boxSize x-boxSize],[y-boxSize y-boxSize y+boxSize y+boxSize],[z z z z],'r');
end
%% Figure 4 - Run Trace of Controller
init_x = [-6, -6];
init_u = [0, 0];
trace = [init_x, init_u];
while ~((trace(end, 1) >= 5 && trace(end, 1) <= 8) && (trace(end, 2) >= 5 && trace(end, 2) <= 8))
%for t= 1:5
x = trace(end, 1:2);
u = getInput(controller, x);
trace = [trace; dynamics(x, u), u];
end

%plot the state space with target set and avoid set
figure
plot(ss(:,1),ss(:,2),'.',"Color", "blue")
hold on
x = target(:,1);
y = target(:,2);
% Plotting boxes around each point (target)
for i = 1:length(x)
rectangle('Position', [x(i)-boxSize/2, y(i)-boxSize/2, boxSize, boxSize], 'FaceColor', 'g');
end

hold on
x = avoid(:,1);
y = avoid(:,2);
% Plotting boxes around each point (avoid)
for i = 1:length(x)
rectangle('Position', [x(i)-boxSize/2, y(i)-boxSize/2, boxSize, boxSize], 'FaceColor', 'r');
end

hold on
% Plot arrows connecting each row
for i = 1:size(trace, 1)-1
x = trace(i, 1);
y = trace(i, 2);
dx = trace(i+1, 1) - x;
dy = trace(i+1, 2) - y;
quiver(x, y, dx, dy, 0,'LineWidth', 4);
end

%% functions

function u = getInput(controller,x)
% Calculate the Euclidean distances between each row of the array and the given value
distances = sqrt(sum((controller(:,1:2) - x).^2, 2));
% Find the index of the row with the minimum distance
[~, closestRow] = min(distances);
u = controller(closestRow, 3:4);
end

function xx = dynamics(x,u)
sigma = [1.3333,1.3333];
xx(1) = x(1) + 2 * u(1) * cos(u(2)) + normrnd(0,sigma(1));
xx(2) = x(2) + 2 * u(1) * sin(u(2)) + normrnd(0,sigma(2));
end
28 changes: 28 additions & 0 deletions misc/view_controller.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Install and load the rhdf5 package if you haven't already
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")

BiocManager::install("rhdf5")

library(rhdf5)

#set working directory to the location of the file
setwd("../HDF5_file_location/../")

#check you are in the location you think you are
getwd()

# Open the HDF5 file
h5file <- H5Fopen("controller.h5")

# List the datasets in the HDF5 file
h5ls(h5file)

# Read a dataset from the HDF5 file (replace "dataset_name" with the actual dataset name)
data <- h5read(h5file, "dataset")

#print dataset
print(data)

# Close the HDF5 file
H5Fclose(h5file)

0 comments on commit 41e12b6

Please sign in to comment.