Compare commits

..

1 Commits

Author SHA1 Message Date
Jacob
bbc7582a7a create working branch 2019-06-03 15:56:40 +02:00
7 changed files with 36 additions and 227 deletions

View File

@ -1,3 +1,3 @@
{
"python.pythonPath": "/usr/local/bin/python3"
"python.pythonPath": "/usr/bin/python3.6"
}

View File

@ -1,30 +0,0 @@
class Flower {
constructor(type) {
this.type = type;
this.maturity = 0;
this.maxMaturity = 0;
this.grown = false;
this.ordered = false;
}
grow() {
if(!this.isReady()) {
this.maturity++;
if(this.grown == false && this.maturity >= this.maxMaturity) {
this.grown = true;
}
}
}
isOrdered() {
return this.ordered;
}
isReady() {
return this.grown;
}
setMaxMaturity(maxMaturity) {
this.maxMaturity = maxMaturity;
}
}

View File

@ -9,9 +9,6 @@ class Forklift {
this.direction = createVector(0, 0);
this.end = false;
this.currentSection = 0;
this.cargo = null;
this.queue = new PriorityQueue();
this.busy = false;
}
draw() {
@ -27,13 +24,10 @@ class Forklift {
}
nextTarget() {
if (this.targetId < this.path.length - 1) {
this.targetId += 1;
if (this.targetId < this.path.length) {
this.currentTarget = this.path[this.targetId];
} else {
// Final destination reached
magazineState[this.currentTarget].push(this.cargo);
//this.removeCargo();
this.end = true;
this.targetId = 0;
}
@ -62,80 +56,7 @@ class Forklift {
}
}
findPath(targetSection) {
let data = {
graph: magazineToGraph(),
start_node: forklift.currentSection,
dest_node: int(targetSection),
};
httpPost(
serverUrl + '/shortestPath',
data,
response => {
console.log('Find path request data: ', data);
let path = response.split('').map(Number);
this.currentTarget = path[0];
this.setPath(path);
going = true;
},
error => {
console.log(error);
},
);
}
completeOrder(order) {
this.waitUntilJobIsDone()
.then(() => {
if (this.currentSection != order.from) {
this.findPath(order.from);
}
})
.then(() => this.waitUntilJobIsDone())
.then(() => {
this.setCargo(order.what);
magazineState[order.from] = magazineState[order.from].filter(flower => {
return flower != order.what;
});
this.findPath(order.to);
})
.then(() => this.waitUntilJobIsDone())
.then(() => {
magazineState[order.to].push(order.what);
order.what.setMaxMaturity(5 + 2 * order.to);
console.log(order.message);
this.busy = false;
});
}
waitUntilJobIsDone() {
return new Promise(resolve => {
let interval = setInterval(() => {
if (!going) {
clearInterval(interval);
resolve();
}
}, 50);
});
}
completeOrders() {
while (!this.queue.isEmpty()) {
let order = queue.dequeue();
this.completeOrder(order);
}
}
setCargo(cargo) {
this.cargo = cargo;
}
removeCargo() {
this.cargo = null;
}
sub(target, pos) {
console.log(target, pos);
return createVector(target.x - pos.x, target.y - pos.y);
}

View File

@ -1,25 +0,0 @@
class Order {
constructor(from, what, to, message){
this.from = from;
this.what = what;
this.to = to;
this.message = message;
}
getFrom() {
return this.from;
}
getWhat() {
return this.what;
}
getTo() {
return this.to;
}
getMessage() {
console.log(this.message);
}
}

View File

@ -1,37 +0,0 @@
class PriorityQueue {
constructor() {
this.collection = [];
}
enqueue(element) {
if(this.isEmpty()) {
this.collection.push(element);
}else {
let added = false;
for(var i = 0; i < this.collection.length; i++) {
if(element[1] < this.collection[i][1]) {
this.collection.splice(i,0,element);
added = true;
break;
}
}
if(!added) {
this.collection.push(element);
}
}
}
dequeue() {
let value = this.collection.shift();
return value[1];
}
size() {
return this.collection.length;
}
isEmpty() {
return (this.collection.length == 0);
}
}

View File

@ -4,7 +4,6 @@ let roads;
let packageClaim;
let going = false;
let forklift;
let magazineState = { 0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
// This runs once at start
function setup() {
@ -14,17 +13,13 @@ function setup() {
createMagazineLayout();
select('#button').mousePressed(addOrder());
select('#button').mousePressed(getIrisType);
sepalWidth = select('#sepalWidth');
sepalLength = select('#sepalLength');
petalWidth = select('#petalWidth');
petalLength = select('#petalLength');
// Create a forklift instance
forklift = new Forklift(sections[0].x, sections[0].y);
setInterval(ageFlowers, 5000);
select('#completeOrdersButton').mousePressed(forklift.completeOrders());
}
// This runs every frame
@ -70,7 +65,7 @@ function drawMagazine() {
}
}
function addOrder() {
function getIrisType() {
let sw = select('#sepalWidth').value();
let sl = select('#sepalLength').value();
let pw = select('#petalWidth').value();
@ -81,15 +76,33 @@ function addOrder() {
petalWidth: pw,
petalLength: pl,
};
console.log(data);
httpPost(serverUrl + '/classify', data, response => {
magazineState[0].push(new Flower(response));
forklift.completeOrder(
new Order(0, magazineState[0][0], magazineState[0][0].type, 'msg'),
);
deliver(response);
});
}
function deliver(targetSection) {
let data = {
graph: magazineToGraph(),
start_node: forklift.currentSection,
dest_node: int(targetSection),
};
console.log(data);
httpPost(
serverUrl + '/shortestPath',
data,
response => {
path = response.split('').map(Number);
forklift.currentTarget = path[0];
forklift.setPath(path);
going = true;
},
error => {
console.log(error);
},
);
}
function createMagazineLayout() {
unit = width / 6;
@ -134,32 +147,3 @@ function magazineToGraph() {
}
return graph;
}
function ageFlowers() {
for (let key of Object.keys(magazineState)) {
for (let flower of magazineState[key]) {
flower.grow();
if (flower.isReady() && !flower.isOrdered()) {
let order;
let priority;
if (int(key) == 0) {
order = new Order(0, flower, int(flower), 'Delivering new flower');
priority = 1;
} else if (int(key) > 3) {
order = new Order(int(key), flower, 0, 'Fully grown flower ready');
priority = 3;
} else {
order = new Order(
int(key),
flower,
int(key) + 3,
'Moving grown flower seedling',
);
priority = 2;
}
forklift.queue.enqueue([order, priority]);
flower.ordered = true;
}
}
}
}

View File

@ -4,10 +4,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/addons/p5.dom.min.js"></script>
<script src="{% static 'magazine/forklift.js' %}"></script>
<script src="{% static 'magazine/flower.js' %}"></script>
<script src="{% static 'magazine/sketch.js' %}"></script>
<script src="{% static 'magazine/priorityQueue.js' %}"></script>
<script src="{% static 'magazine/order.js' %}"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
@ -44,16 +41,15 @@
<div class="container">
<div class="package">
<h3 style="margin-top: 0;">Package description</h1>
<label >Sepal Width</label>
<input type="number" id="sepalWidth" value="1">
<label >Sepal Length</label>
<input type="number" id="sepalLength" value="1">
<label >Petal Width</label>
<input type="number" id="petalWidth" value="1">
<label >Petal Length</label>
<input type="number" id="petalLength" value="1">
<label for="width">Sepal Width</label>
<input type="number" id="sepalWidth">
<label for="topWidth">Sepal Length</label>
<input type="number" id="sepalLength">
<label for="botWidth">Petal Width</label>
<input type="number" id="petalWidth">
<label for="height">Petal Length</label>
<input type="number" id="petalLength">
<button id="button">Send Package</button>
<button id="completeOrdersButton">Complete Queued Orders</button>
</div>
<div id="canvas" style="margin: 10px;"></div>
<div class="legend">