<?php
$GLOBALS["CACHE_PATH"] = "flash/cache/";
$GLOBALS["FILE_EXTENSION"] = ".gif";
$GLOBALS["RELATIVE_IMAGE_DIRECTORY_PATH"] = "img/";
remove_cached_strip();
function remove_cached_strip()
{
   go_to_cache_directory();
   $path = find_image_file();
   unlink($path);
}
function go_to_cache_directory()
{
   go_to_image_directory();
   chdir($GLOBALS["CACHE_PATH"]);
}
function go_to_image_directory()
{
   $path = $GLOBALS["RELATIVE_IMAGE_DIRECTORY_PATH"];
   while (!is_dir($path))
   {
      chdir("..");
   }
   chdir($path);
}
function find_image_file()
{
   $index = $_GET["index"];
   foreach (scandir(".") as $file)
   {
      if (fnmatch($index . "_*", $file))
      {
         return $file;
      }
   }
}
EVR.Level.Road.Path.Line = function(container, style)
{
   EVR.Graphic.call(this, container, RATIO_HEIGHT, null, ALIGN_LEFT);
   this.style = style;
   this.margin = LINE_MARGIN;
   this.checkers = [];
   this.build();
}
EVR.Level.Road.Path.Line.prototype = new EVR.Graphic;
EVR.Level.Road.Path.Line.prototype.build = function()
{
   this.set_proportions(LINE_WIDTH, 1);
   this.set_color(LINE_COLOR);
   this.set_opacity(LINE_OPACITY);
   if (this.style == LINE_FINISH)
   {
      this.add_checkers();
   }
}
EVR.Level.Road.Path.Line.prototype.add_checkers = function()
{
   var count = LINE_CHECKER_COUNT;
   var width = .5;
   var height = 1.0 / count;
   var alignment;
   for (var ii = 0; ii < count; ii++)
   {
      alignment = ii % 2 ? ALIGN_TOP_RIGHT : ALIGN_TOP_LEFT;
      var checker = new EVR.Graphic(this, null, null, alignment);
      checker.set_color(LINE_CHECKER_COLOR);
      checker.set_proportions(width, height);
      checker.place(null, height * ii);
      checker.append();
      this.checkers.push(checker);
   }
}
EVR.Level.Road.Path.Line.prototype.draw = function()
{
   EVR.Graphic.prototype.draw.call(this);
   if (this.style != LINE_FINISH)
   {
      this.set_border();
   }
   for (var ii = 0; ii < this.checkers.length; ii++)
   {
      this.checkers[ii].draw();
   }
}
EVR.Level.Road.Path.Line.prototype.set_border = function()
{
   var width = this.container.get_dimensions()[0] * this.get_margin();
   var color = this.container.level.background;
   if (!!window.ActiveXObject)
   {
      this.set_dimensions(this.get_dimensions()[0] + width);
   }
   this.css.borderRight = width + "px solid " + color;
}
EVR.Level.Road.Path.Line.prototype.get_margin = function()
{
   var dimensions = this.container.get_dimensions();
   return dimensions[1] / dimensions[0] * this.margin;
}
EVR.Level.Road.Path.Line.prototype.toString = function()
{
   return "[object EVR.Level.Road.Path.Line]";
}
EVR.include("level/road/path/Line.js");
EVR.include("level/road/path/trail/Trail.js");
EVR.Level.Road.Path = function(container, level)
{
   EVR.Animation.call(this, PATH_INITIAL_RATE, true, PATH_TIMING_BUFFER);
   this.container = container;
   this.level = level;
   this.clusters = level.clusters;
   this.racer = container.racer;
   this.sprinting = false;
   this.offset = 0;
   this.speed = PATH_INITIAL_SPEED;
   this.visible = 0;
   this.color = null;
   this.variance = [0, 0];
   this.meter_unlocked = false;
   this.trail = new EVR.Level.Road.Path.Trail(level);
   this.populate();
   this.last_ms = +new Date;
}
EVR.Level.Road.Path.prototype = new EVR.Animation;
EVR.Level.Road.Path.prototype.populate = function()
{
   this.items = [new EVR.Level.Road.Path.Line(this.container)];
   for (var ii = 0; ii < this.clusters.length; ii++)
   {
      this.clusters[ii].initiate(this.container, this.level);
   }
   this.items = this.items.concat(this.clusters);
   this.items.push(new EVR.Level.Road.Path.Line(this.container, LINE_FINISH));
   this.set_origin();
   this.arrange();
}
EVR.Level.Road.Path.prototype.set_origin = function()
{
   var racer = this.racer;
   var offset = racer.get_coordinates(true)[0] + racer.get_dimensions(true)[0];
   this.origin = offset + this.items[0].get_margin();
}
EVR.Level.Road.Path.prototype.arrange = function()
{
   var x = this.origin - this.offset;
   var container = this.container;
   var border = container.get_dimensions()[0];
   var racer = this.racer.get_coordinates()[0];
   var cluster, left, width, right, ratio, inhabited = null;
   for (var ii = this.visible, len = this.items.length; ii < len; ii++)
   {
      cluster = this.items[ii];
      if (!cluster.attached)
      {
	 cluster.append();
      }
      cluster.place(x);
      left = cluster.get_coordinates()[0];
      width = cluster.get_dimensions()[0];
      right = left + width;
      if (inhabited == null)
      {
	 inhabited = this.locate_racer(racer, left, right, ii, len);
	 if (inhabited != null)
	 {
	    this.trail.add(this.speed, this.racer.lane);
	 }
      }
      ratio = width / border;
      x += ratio;
      if (right < 0)
      {
	 cluster.remove();
	 this.visible++;
	 this.offset -= ratio;
      }
      else if (left > border)
      {
	 break;
      }
   }
   return inhabited;
}
EVR.Level.Road.Path.prototype.locate_racer = function(x, left, right, ii, len)
{
   if (ii == 0 && x < left)
   {
      return -1;
   }
   else if (this.intersect(x, left, right) || ii == len - 1)
   {
      return ii;
   }
   return null;
}
EVR.Level.Road.Path.prototype.intersect = function(x, left, right)
{
   return x >= left && x < right;
}
EVR.Level.Road.Path.prototype.sequence = function()
{
   var ii = this.arrange();
   var item = this.items[ii];
   if (item instanceof EVR.Level.Road.Path.Line)
   {
      if (item.style == LINE_FINISH)
      {
	 this.stop();
	 this.update_cheers();
	 this.level.finish();
	 return;
      }
   }
   else if (item != null)
   {
      this.cluster = item;
      this.update_color();
      this.update_variance();
      this.update_streak();
      this.update_sprint_state();
      this.update_flame();
      this.update_flash();
      this.set_instruments(ii);
      this.set_speed();
//       this.update_scoreboard(ii);
      this.update_cheers();
   }
   this.step();
   this.update_ghost(ii);
   this.unlock_meter(ii);
}
EVR.Level.Road.Path.prototype.unlock_meter = function(index)
{
   if (index >= this.items.length - PATH_METER_UNLOCK_OFFSET)
   {
      if (!this.meter_unlocked)
      {
	 this.meter_unlocked = true;
	 this.set_meter_color();
      }
   }
}
EVR.Level.Road.Path.prototype.update_color = function()
{
   var cluster = this.cluster;
   var racer = this.racer;
   var x = racer.get_coordinates()[0];
   var gap_x = cluster.get_gap_x();
   if (x >= gap_x)
   {
      this.color = null;
   }
   else
   {
      this.color = racer.lane;
      if (!this.level.practice)
      {
	 this.racer.add_color(this.color, cluster.passage);
      }
   }
}
EVR.Level.Road.Path.prototype.update_variance = function()
{
   if (this.color != null)
   {
      this.variance[0]++;
      this.variance[1] += !this.is_in_passage();
   }
}
EVR.Level.Road.Path.prototype.is_in_passage = function()
{
   return this.color == this.cluster.passage;
}
EVR.Level.Road.Path.prototype.update_streak = function()
{
   var streak = this.level.streak;
   if (this.color != null)
   {
      if (this.is_in_passage())
      {
	 streak.increase();
      }
      else
      {
	 streak.reset();
      }
   }
}
EVR.Level.Road.Path.prototype.set_instruments = function(ii)
{
   if (ii > 0)
   {
      this.level.map.advance_player(ii - 1);
      this.level.register.advance(ii - 1);
   }
   this.set_meter();
}
EVR.Level.Road.Path.prototype.set_meter = function()
{
   var meter = this.level.meter;
   if (this.sprinting)
   {
      meter.adjust(-this.rate);
   }
   var color = this.color;
   if (color != null && this.is_in_passage())
   {
      adjustment = PATH_METER_BONUS + this.level.streak * PATH_STREAK_BONUS;
      meter.adjust(adjustment);
   }
   this.set_meter_color();
}
EVR.Level.Road.Path.prototype.set_meter_color = function()
{
   var meter = this.level.meter;
   var reading = meter.read();
   if (this.sprinting)
   {
      meter.set_color(METER_DRAINING_COLOR);
   }
   else if (reading >= this.level.threshold || this.meter_unlocked)
   {
      meter.set_color(METER_READY_COLOR);
   }
   else
   {
      meter.set_color(METER_DISABLED_COLOR);
   }
}
EVR.Level.Road.Path.prototype.set_speed = function()
{
   if (this.sprinting && this.level.meter.read() > 0)
   {
      this.speed = PATH_SPRINT_SPEED;
   }
   else
   {
      this.speed = PATH_INITIAL_SPEED;
   }
   var color = this.color;
   if (color != null && !this.is_in_passage())
   {
      this.speed *= PATH_SPEED_PENALTY;
   }
}
EVR.Level.Road.Path.prototype.update_ghost = function(ii)
{
   var road = this.level.road;
   if (!!road.ghost)
   {
      road.ghost.update(this.speed, this.rate, ii);
   }
}
EVR.Level.Road.Path.prototype.update_sprint_state = function()
{
   if (this.sprinting && this.level.meter.read() <= 0)
   {
      this.sprinting = false;
   }
}
EVR.Level.Road.Path.prototype.update_flame = function()
{
   this.racer.flame.update(this.sprinting, this.speed);
}
EVR.Level.Road.Path.prototype.update_flash = function()
{
   this.racer.flash.update();
}
EVR.Level.Road.Path.prototype.update_scoreboard = function(item_index)
{
//    this.level.scoreboard.update(item_index - 1);
   this.level.scoreboard.update(this.speed);
}
EVR.Level.Road.Path.prototype.update_cheers = function()
{
   this.level.cheers.update(this.sprinting, this.speed);
}
EVR.Level.Road.Path.prototype.step = function()
{
   var dimensions = this.container.get_dimensions();
   var ratio = dimensions[1] / dimensions[0];
   this.offset += ratio * this.speed * this.rate;
}
EVR.Level.Road.Path.prototype.stop = function()
{
   EVR.Animation.prototype.stop.call(this);
   this.level.clock.stop();
   this.sprinting = false;
}
EVR.Level.Road.Path.prototype.draw = function()
{
   var cluster, y, boundary, started = false;
   for (var ii = 0, len = this.items.length; ii < len; ii++)
   {
      cluster = this.items[ii];
      if (cluster.attached)
      {
	 started = true;
	 cluster.shape();
	 y = cluster.get_coordinates()[0];
	 boundary = this.container.get_coordinates()[0];
	 if (y > boundary)
	 {
	    cluster.remove();
	 }
      }
      else if (started)
      {
	 break;
      }
   }
   this.set_origin();
   this.arrange();
   this.update_cluster_dimensions();
}
EVR.Level.Road.Path.prototype.update_cluster_dimensions = function()
{
   var items = this.items;
   for (var ii = 1; ii < items.length - 1; ii++)
   {
      items[ii].update_dimensions();
   }
}
EVR.Level.Road.Path.prototype.calculate_accuracy = function()
{
   var variance = this.variance;
   return 1 - (variance[1] / variance[0]);
}
EVR.Level.Road.Path.prototype.sprint = function()
{
   var level = this.level;
   if (level.meter.read() >= level.threshold || this.meter_unlocked)
   {
      this.sprinting = true;
   }
}
EVR.Level.Road.Path.prototype.remove = function()
{
   var item, items = this.items;
   for (var ii = 0; ii < items.length; ii++)
   {
      item = items[ii];
      item.attached && item.remove();
   }
}
EVR.Level.Road.Path.prototype.toString = function()
{
   return "[object EVR.Level.Road.Path]";
}
3.21.44.115
3.21.44.115
3.21.44.115
 
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 😇