<?php

require_once (dirname(__FILE__) . "/config.inc.php");
require_once (dirname(__FILE__) . "/Element.php");
require_once (dirname(__FILE__) . "/fetch.php");

// Check if char is a whitespace character.
function char_is_white_space($char)
{
   if ($char == " " || $char == "\t" || $char == "\n")
   {
      return True;
   }
   return False;
}

// Move the file pointer along until it finds a non-whitespace character.  If
// the user wants to the next non-whitespace character, first move the file
// pointer one step.
function skip_white_space($fp, &$char, $next=False)
{
   if ($next == True || char_is_white_space($char) == True)
   {
      while (char_is_white_space($char = fgetc($fp)) == True) {}
   }
}

// Move the file pointer along until it finds the desired character.  If the
// user wants the next instance of the character, first move the pointer one
// step away from its current position.
function move_to_char($fp, &$char, $sought, $next=False)
{
   if ($next == True || $char != $sought)
   {
      while (($char = fgetc($fp)) != $sought && !feof($fp)) {}
   }
}

// Skip any initial white space.  Read and store characters until white space or
// a bracket is reached.
function parse_name_or_id($fp, &$char, &$data=NULL)
{
   skip_white_space($fp, $char);
   while (
      !char_is_white_space($char) && $char != OPEN_ELEMENT_CHAR &&
      $char != CLOSE_ELEMENT_CHAR && !feof($fp))
   {
      $data .= $char;
      $char = fgetc($fp);
   }
}

// Create a new element & store it as the first child of the current element or
// the next child of a parent element.
function build_new_element($fp, &$char, &$element, $ii)
{
   $new = new Element();
   if ($ii == 0)
   {
      $element->data = $new;
   }
   else
   {
      $element->next = $new;
   }
   parse_element($fp, $new, $char);
   $element = $new;
}

// Read the last component of an element.  If there is a bracket in the data,
// treat it as a new element, and add its contents to the current element.  If
// not, treat the data as a string, and store it.
function parse_data($fp, &$char, $element)
{
   skip_white_space($fp, $char);
   while ($char != CLOSE_ELEMENT_CHAR && !feof($fp))
   {
      for ($ii = 0; $char == OPEN_ELEMENT_CHAR; $ii++)
      {
         build_new_element($fp, $char, $element, $ii);
         $char = fgetc($fp);
         skip_white_space($fp, $char);
         $flag = True;
      }

      if ($flag == True) break;

      $element->data .= $char;
      $char = fgetc($fp);
   }
}

// Decide which component is being parsed and dispatch to the appropriate
// parsing function.
function parse_component($fp, $element, &$char, $component)
{
   switch ($component)
   {
      case NAME_COMPONENT:
         parse_name_or_id($fp, &$char, $element->name);
         break;
      case ID_COMPONENT:
         parse_name_or_id($fp, &$char, $element->id);
         break;
      case DATA_COMPONENT:
         parse_data($fp, &$char, $element);
         break;
   }
}

// Ensure we've found an element by moving to the first open bracket.  Parse the
// contents within the brackets (one or more elements).
function parse_element($fp, $element, &$char=NULL)
{
   move_to_char($fp, $char, OPEN_ELEMENT_CHAR);
   $char = fgetc($fp);
   for ($ii = 0; $ii < COMPONENT_COUNT; $ii++)
   {
      parse_component($fp, $element, $char, $ii);
   }
}

// Parse a file or subset of a file, creating a tree of elements.
function run_parse($file_name, $id=NULL)
{
   if (!file_exists($file_name)) return;

   $elements = fetch_elements($file_name, $id);
   $elements = ($id) ? array($elements) : $elements;
   foreach ($elements as $element)
   {
      $message .= $element->convert_to_tabbed_string() . "\n";
      $count += $element->count();
      $depth = max($element->measure_depth(), $depth);
   }
   $message .= "DEPTH: $depth\n";
   $message .= "TOTAL: $count\n";

   return $message;
}
<?php

require_once (dirname(__FILE__) . "/Element.php");
require_once (dirname(__FILE__) . "/parse.php");

// Parse the name of an element without storing it.
function skip_name($fp, &$char)
{
   parse_name_or_id($fp, $char);
}

// Read the id of the element listed at the current position.
function read_element_id($fp, &$char)
{
   skip_name($fp, $char);
   parse_name_or_id($fp, $char, $id);

   return $id;
}

// Move file pointer to element with requested ID.  After finding the ID, move
// the file pointer backward past the element name and open bracket.
function move_to_element_by_id($fp, &$char=NULL, $id)
{
   while (!feof($fp))
   {
      move_to_char($fp, $char, OPEN_ELEMENT_CHAR);
      $position = ftell($fp);
      $saved_char = $char;
      $char = fgetc($fp);
      if (read_element_id($fp, $char) == $id) break;
   }

   if (feof($fp)) return;

   fseek($fp, $position);
   $char = $saved_char;
}

// Find and return the element identified by the requested ID.
function fetch_element_by_id($file_name, $id)
{
   $fp = fopen($file_name, 'r');
   move_to_element_by_id($fp, $char, $id);
   $element = new Element();
   parse_element($fp, $element, $char);

   return $element;
}

// Return an array of all elements in a file.
function fetch_elements_by_file($file_name)
{
   $fp = fopen($file_name, 'r');

   skip_white_space($fp, $char, True);
   while (!feof($fp))
   {
      $elements[] = new Element();
      parse_element($fp, $elements[count($elements)-1], $char);
      $char = fgetc($fp);
      skip_white_space($fp, $char);
   }

   return $elements;
}

// Dispatch to appropriate function depending upon whether single or
// multiple elements are being requested.
function fetch_elements($file_name, $id=NULL)
{
   if (file_exists($file_name))
   {
      if ($id == NULL)
      {
         $elements = fetch_elements_by_file($file_name);
      }
      else
      {
         $elements = fetch_element_by_id($file_name, $id);
      }
   }
   
   return $elements;
}
216.73.216.170
216.73.216.170
216.73.216.170
 
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 😇