EVR.include("level/summary/graph/Margin.js");
EVR.include("level/summary/graph/Set.js");
EVR.include("level/summary/graph/Plot.js");
EVR.Level.Summary.Graph = function(cell, level)
{
   this.level = level;
   this.road = level.road;
   this.field = level.container;
   EVR.Table.call(this, cell, 1, 1);
   this.title = GRAPH_TITLE;
   this.font_size = GRAPH_FONT_SIZE;
   this.font_color = GRAPH_FONT_COLOR;
   this.colors = GRAPH_COLORS;
   this.set_measurements();
   this.element.bgColor = GRAPH_PLOT_BACKGROUND_COLOR;
   cell.style.verticalAlign = "middle";
   cell.style.paddingLeft = GRAPH_MARGIN;
   this.add_components();
   this.initialize_sets();
   this.plot = new EVR.Level.Summary.Graph.Plot(this, this.sets);
   this.insert_title();
   this.append();
}
EVR.Level.Summary.Graph.prototype = new EVR.Table;
EVR.Level.Summary.Graph.prototype.set_measurements = function()
{
   var level = this.level;
   var history = level.history;
   var measurements = [history.get_accuracies(), history.get_times()];
   for (var ii = 0; ii < measurements.length; ii++)
   {
      measurements[ii] = measurements[ii].slice(-GRAPH_HISTORY_LENGTH);
   }
   this.measurements = measurements;
}
EVR.Level.Summary.Graph.prototype.insert_title = function()
{
   var cell = this.insert_cell(this.insert_row(0));
   var text = this.build_title_text();
   var font = GRAPH_TITLE_FONT;
   var color = this.font_color;
   var size = this.font_size;
   var title = new EVR.Text(cell, text, font, color, size, this.field);
   cell.height = "1";
   cell.style.background = GRAPH_TITLE_BACKGROUND;
   cell.style.textAlign = "center";
   cell.style.letterSpacing = GRAPH_TITLE_LETTER_SPACING;
   cell.colSpan = this.get_rows()[1].cells.length;
   this.title = title;
}
EVR.Level.Summary.Graph.prototype.build_title_text = function()
{
   var count = this.measurements[0].length;
   var text = GRAPH_TITLE;
   if (count > 1)
   {
      text += " " + count;
   }
   return text;
}
EVR.Level.Summary.Graph.prototype.add_components = function()
{
   var row = this.insert_row();
   var cells = [];
   cells[0] = [this.insert_margin_cell(row)];
   cells[1] = [this.insert_margin_cell(row)];
   this.first_row = row;
   row = this.insert_row();
   cells[0].push(this.insert_margin_cell(row, true));
   cells[1].push(this.insert_margin_cell(row, true));
   var colors = this.colors;
   var left = new EVR.Level.Summary.Graph.Margin(cells[0], this, colors[0]);
   var right = new EVR.Level.Summary.Graph.Margin(cells[1], this, colors[1]);
   this.margins = [left, right];
}
EVR.Level.Summary.Graph.prototype.insert_margin_cell = function(row, bottom)
{
   var cell = this.insert_cell(row);
   cell.style.padding = "0 " + GRAPH_MARGIN_PADDING;
   cell.style.verticalAlign = bottom ? "bottom" : "top";
   cell.style.backgroundColor = GRAPH_MARGIN_BACKGROUND;
   return cell;
}
EVR.Level.Summary.Graph.prototype.initialize_sets = function()
{
   var sets = [];
   var margins = this.margins;
   var measurements = this.measurements;
   var reverse, color, set;
   for (var ii = 0, len = measurements.length; ii < len; ii++)
   {
      reverse = measurements[ii][0] instanceof EVR.Time;
      color = this.colors[ii];
      set = new EVR.Level.Summary.Graph.Set(
	 measurements[ii], margins[ii], color, reverse);
      sets.push(set);
   }
   this.sets = sets;
}
EVR.Level.Summary.Graph.prototype.insert_plot_cell = function(spacer)
{
   var row = this.first_row;
   var cell = row.insertCell(row.cells.length - 1);
   cell.rowSpan = 2;
   cell.style.verticalAlign = "bottom";
   if (spacer)
   {
      cell.style.paddingRight = GRAPH_BAR_PADDING;
   }
   return cell;
}
EVR.Level.Summary.Graph.prototype.draw = function()
{
   this.title.set_font_size();
   this.plot.draw();
   var margins = this.margins;
   for (var ii = 0; ii < margins.length; ii++)
   {
      margins[ii].draw();
   }
}
EVR.Level.Summary.Graph.prototype.toString = function()
{
   return "[object EVR.Level.Summary.Graph]";
}
EVR.Level.Summary.Graph.Set = function(measurements, margin, color, reverse)
{
   this.measurements = measurements;
   this.margin = margin;
   this.color = color;
   this.reverse = reverse || false;
   this.set_bounds();
}
EVR.Level.Summary.Graph.Set.prototype.set_bounds = function()
{
   var measurements = this.measurements;
   var min = measurements[0];
   var max = measurements[0];
   var measurement;
   for (var ii = 0, len = measurements.length; ii < len; ii++)
   {
      measurement = measurements[ii];
      if (measurement > max)
      {
	 max = measurement;
      }
      else if (measurement < min)
      {
	 min = measurement;
      }
   }
   var reverse = this.reverse;
   this.best = reverse ? min : max;
   this.worst = reverse ? max : min;
   this.range = Math.abs(this.best - this.worst);
   this.display();
}
EVR.Level.Summary.Graph.Set.prototype.display = function()
{
   var precision = GRAPH_PRECISION;
   var worst = this.worst.toString(precision);
   var best = this.best.toString(precision);
   this.margin.set_bounds(worst, best);
}
EVR.Level.Summary.Graph.Set.prototype.toString = function()
{
   return "[EVR.Level.Summary.Graph.Set]";
}
EVR.Level.Summary.Graph.Margin = function(cells, graph, color)
{
   this.cells = cells;
   this.graph = graph;
   this.color = color;
   this.initialize_text();
}
EVR.Level.Summary.Graph.Margin.prototype.initialize_text = function()
{
   var cells = this.cells;
   this.maximum = this.build_text(cells[0]);
   this.minimum = this.build_text(cells[1]);
}
EVR.Level.Summary.Graph.Margin.prototype.build_text = function(cell)
{
   var graph = this.graph;
   var font = GRAPH_MARGIN_TEXT_FONT;
   var color = GRAPH_MARGIN_TEXT_COLOR;
   var size = graph.font_size;
   var field = graph.field;
   var text = new EVR.Text(cell, null, font, color, size, field);
   text.set_color(this.color);
   return text;
}
EVR.Level.Summary.Graph.Margin.prototype.set_bounds = function(minimum, maximum)
{
   this.minimum.set(minimum);
   this.maximum.set(maximum);
}
EVR.Level.Summary.Graph.Margin.prototype.draw = function()
{
   this.maximum.set_font_size();
   this.minimum.set_font_size();
}
EVR.Level.Summary.Graph.Margin.prototype.toString = function()
{
   return "[EVR.Level.Summary.Graph.Margin]";
}
3.141.193.158
3.141.193.158
3.141.193.158
 
January 23, 2021

I wanted to document this chat-controlled robot I made for Babycastles' LOLCAM📸 that accepts a predefined set of commands like a character in an RPG party 〰 commands like walk, spin, bash, drill. It can also understand donut, worm, ring, wheels, and more. The signal for each command is transmitted as a 24-bit value over infrared using two Arduinos, one with an infrared LED, and the other with an infrared receiver. I built the transmitter circuit, and the receiver was built into the board that came with the mBot robot kit. The infrared library IRLib2 was used to transmit and receive the data as a 24-bit value.


fig. 1.1: the LEDs don't have much to do with this post!

I wanted to control the robot the way the infrared remote that came with the mBot controlled it, but the difference would be that since we would be getting input from the computer, it would be like having a remote with an unlimited amount of buttons. The way the remote works is each button press sends a 24-bit value to the robot over infrared. Inspired by Game Boy Advance registers and tracker commands, I started thinking that if we packed multiple parameters into the 24 bits, it would allow a custom move to be sent each time, so I wrote transmitter and receiver code to process commands that looked like this:

bit
name
description
00
time
multiply by 64 to get duration of command in ms
01
02
03
04
left
multiply by 16 to get left motor power
05
06
07
08
right
multiply by 16 to get right motor power
09
10
11
12
left sign
0 = left wheel backward, 1 = left wheel forward
13
right sign
0 = right wheel forward, 1 = right wheel backward
14
robot id
0 = send to player one, 1 = send to player two
15
flip
negate motor signs when repeating command
16
repeats
number of times to repeat command
17
18
19
delay
multiply by 128 to get time between repeats in ms
20
21
22
23
swap
swap the motor power values on repeat
fig 1.2: tightly stuffed bits

The first command I was able to send with this method that seemed interesting was one that made the mBot do a wheelie.

$ ./send_command.py 15 12 15 1 0 0 0 7 0 1
sending 0xff871fcf...


fig 1.3: sick wheels

A side effect of sending the signal this way is any button on any infrared remote will cause the robot to do something. The star command was actually reverse engineered from looking at the code a random remote button sent. For the robot's debut, it ended up with 15 preset commands (that number is in stonks 📈). I posted a highlights video on social media of how the chat controls turned out.

This idea was inspired by a remote frog tank LED project I made for Ribbit's Frog World which had a similar concept: press a button, and in a remote location where 🐸 and 🐠 live, an LED would turn on.


fig 2.1: saying hi to froggo remotely using an LED

😇 The transmitter and receiver Arduino programs are available to be copied and modified 😇