Skip to content

Commit

Permalink
Plot average working now
Browse files Browse the repository at this point in the history
  • Loading branch information
avardy committed Jul 10, 2018
1 parent f3825d4 commit d9a4b6d
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 22 deletions.
45 changes: 29 additions & 16 deletions js/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Analyzer {
redraw: false
});
} else if (config == "#CONSTRUCT") {
// PLACEHOLDER
this.chart = Highcharts.chart('chartDiv', {
chart: { animation: false },
title: { text: 'Avg. Puck-Tau Abs. Diff.' },
Expand Down Expand Up @@ -89,31 +88,39 @@ class Analyzer {
}

let timeSecs = timestamp/1000;
//console.log(this.seriesIndex);
if (forceRedraw || timeSecs - this.lastRedrawTimeSecs >= 1) {
// Add point and redraw
this.chart.series[this.seriesIndex].addPoint([timeSecs, value], true);
this.lastRedrawTimeSecs = timeSecs;
} else {
// Add point, but defer the redraw.
this.chart.series[this.seriesIndex].addPoint([timeSecs, value], false);
}
let drawNow = forceRedraw || timeSecs - this.lastRedrawTimeSecs >= 1;

// Compute the average value (over this and past series)
if (!this.averageMap.get(timeSecs)) {
this.averageMap.set(timeSecs, [value]);
// Compute the average value (over this and past series). We will
// use the time as the key into 'averageMap' but it may differ slightly
// on subsequent runs. So we truncate it to three digits here.
let timeKey = timeSecs.toFixed(3);
if (!this.averageMap.get(timeKey)) {
this.averageMap.set(timeKey, [value]);

} else {
let array = this.averageMap.get(timeSecs);
let array = this.averageMap.get(timeKey);
array.push(value);
let sum = array.reduce((a, b) => a + b, 0);
let average = sum / array.length;

if (this.seriesIndex > 0) {
this.chart.series[this.seriesIndex+1].addPoint([timeSecs, average], true);
this.chart.series[this.seriesIndex+1].addPoint([timeSecs,
average],
drawNow);
}
}

if (drawNow) {
// Add point and redraw
this.chart.series[this.seriesIndex].addPoint([timeSecs, value],
true);
this.lastRedrawTimeSecs = timeSecs;
} else {
// Add point, but defer the redraw.
this.chart.series[this.seriesIndex].addPoint([timeSecs, value],
false);
}

}

reset() {
Expand All @@ -131,8 +138,14 @@ class Analyzer {

this.chart.addSeries({
name: "Average",
data: []
data: [],
lineWidth: 10,
marker: {
enabled: false
}
});

this.lastRedrawTimeSecs = 0;
}

// Return the percentage completion as defined in "Cache consensus:
Expand Down
14 changes: 14 additions & 0 deletions js/distToSegment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// From Grumdig's Javascript answer to this question:
// https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

function sqr(x) { return x * x }
function dist2(v, w) { return sqr(v.x - w.x) + sqr(v.y - w.y) }
function distToSegmentSquared(p, v, w) {
var l2 = dist2(v, w);
if (l2 == 0) return dist2(p, v);
var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
t = Math.max(0, Math.min(1, t));
return dist2(p, { x: v.x + t * (w.x - v.x),
y: v.y + t * (w.y - v.y) });
}
function distToSegment(p, v, w) { return Math.sqrt(distToSegmentSquared(p, v, w)); }
6 changes: 3 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function reset(seedValue) {
//myGlobals.screenshotCaptureSteps = [10, 100, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000];
*/
myGlobals.screenshotCaptureSteps = [];
myGlobals.maxStep = 20000000;
myGlobals.maxStep = 2000;
}

simState.robots = [];
Expand Down Expand Up @@ -407,9 +407,9 @@ function manageRobotPopulation() {
//robot.controller = new TestController();
//robot.controller = new SimpleAvoidController();
//robot.controller = new AdvancedClusterController();
//robot.controller = new BlocklyController();
robot.controller = new BlocklyController();
//robot.controller = new OrbitController();
robot.controller = new OrbitalConstructionController();
//robot.controller = new OrbitalConstructionController();
simState.robots.push(robot);
}
if (simState.robots.length > myGlobals.nRobots) {
Expand Down
6 changes: 4 additions & 2 deletions js/vendor/blockly_compressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion waggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@
<block type="robot_hold_speed">
<field name="holdTime">0</field>
</block>
<!--
<block type="robot_hold_speed_slot"></block>
-->
<block type="robot_activate_gripper"></block>
<block type="robot_deactivate_gripper"></block>
<!--
<block type="robot_set_speed_slots"></block>
-->
<block type="robot_deactivate_flash"></block>
<block type="robot_emit_pheromone">
<field name="quantity">10</field>
</block>
<!--
<block type="robot_emit_pheromone_slot"></block>
-->
<block type="robot_set_text">
<field name="text">Hi!</field>
</block>
Expand All @@ -101,16 +107,20 @@
<field name="variableName">variableA</field>
<field name="newValue">0</field>
</block>
<!--
<block type="robot_set_variable_slot">
<field name="variableName">variableA</field>
</block>
-->
<block type="robot_change_variable">
<field name="variableName">variableA</field>
<field name="deltaValue">0</field>
</block>
<!--
<block type="robot_change_variable_slot">
<field name="variableName">variableA</field>
</block>
-->
<block type="robot_get_variable">
<field name="variableName">variableA</field>
</block>
Expand Down Expand Up @@ -262,6 +272,7 @@ <h2>Waggle 1.0: Visual Programming for Swarm Robotics</h2>
<table cellpadding="2" style="width: 100%">
<tr>
<td><button type="button" id="resetButton">Reset</button></td>
<td><button type="button" id="reseedAndResetButton">Reseed and Reset</button></td>
<td><input type="checkbox" id="allowMovementCheckbox">Allow Movement</input></td>

<td><input type="checkbox" id="showSensorsCheckbox">Show Sensors</input></td>
Expand All @@ -285,7 +296,7 @@ <h2>Waggle 1.0: Visual Programming for Swarm Robotics</h2>

<tr>
<td id='nRedPucksLabel'>Red Pucks</td>
<td><input id='nRedPucksSlider' type='range' min='0' value='1' max='150' step='1'/></td>
<td><input id='nRedPucksSlider' type='range' min='0' value='1' max='250' step='1'/></td>
<td id='nRedPucksText'></td></tr>

<tr>
Expand Down
Loading

0 comments on commit d9a4b6d

Please sign in to comment.