from esp_hadouken.pgfw.Sprite import Sprite

class Toy(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_from_path(self.get_resource("toy", "path"), True)
        self.set_framerate(self.get_configuration("toy", "framerate"))
from os.path import exists, join, basename
from sys import argv

from pygame import mixer

import Game

class GameChild:

    def __init__(self, parent=None):
        self.parent = parent

    def get_game(self):
        current = self
        while not isinstance(current, Game.Game):
            current = current.parent
        return current

    def get_configuration(self):
        return self.get_game().get_configuration()

    def get_input(self):
        return self.get_game().get_input()

    def get_screen(self):
        return self.get_game().display.get_screen()

    def get_timer(self):
        return self.get_game().timer

    def get_audio(self):
        return self.get_game().audio

    def get_delegate(self):
        return self.get_game().delegate

    def get_resource(self, key):
        config = self.get_configuration()
        path = config[key]
        installed_path = join(config["resources-install-path"], path)
        if exists(installed_path) and self.use_installed_resource():
            return installed_path
        elif exists(path):
            return path

    def use_installed_resource(self):
        if "-l" in argv:
            return False
        return True

    def get_pause_screen(self):
        return self.get_game().pause_screen

    def get_high_scores(self):
        return self.get_game().high_scores

    def get_glyph_palette(self):
        return self.get_game().glyph_palette

    def subscribe_to(self, kind, callback):
        self.get_game().delegate.add_subscriber(kind, callback)

    def unsubscribe_from(self, kind, callback):
        self.get_game().delegate.remove_subscriber(kind, callback)

    def is_debug_mode(self):
        return "-d" in argv
from time import time

from pygame.locals import *

from esp_hadouken.pgfw.GameChild import *
from esp_hadouken.pgfw.Input import *

class Timer(GameChild, dict):

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.max_time = self.get_configuration("timer", "max-time")
        self.subscribe_to_events()
        self.reset()

    def subscribe_to_events(self):
        self.subscribe(self.respond_to_event)

    def respond_to_event(self, event):
        if event.command == "reset":
            self.reset()

    def reset(self):
        self.clear_current_interval()
        self.concealed = False
        dict.__init__(self, {"octo": 0, "horse": 0, "diortem": 0, "circulor": 0,
                             "tooth": 0})

    def clear_current_interval(self):
        self.start_time = None
        self.pause_length = 0
        self.pause_start_time = None
        self.paused = False
        self.current_level = None

    def start(self, level):
        self.start_time = time()
        self.current_level = level

    def pause(self):
        if self.start_time:
            paused = self.paused
            if self.paused:
                self.pause_length += time() - self.pause_start_time
            else:
                self.pause_start_time = time()
            self.paused = not paused

    def stop(self):
        start = self.start_time
        level = self.current_level
        if None not in (start, level):
            interval = time() - start - self.pause_length
            self[level] += interval
        self.clear_current_interval()

    def total(self):
        if self.concealed:
            return self.max_time
        return sum(self.values())

    def conceal(self):
        self.concealed = True
import pygame
from pygame.locals import *

from GameChild import *

class EventDelegate(GameChild):

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.subscribers = dict()
        self.disable()
        if self.is_debug_mode():
            print "Event ID range: %i - %i" % (NOEVENT, NUMEVENTS)

    def enable(self):
        self.enabled = True

    def disable(self):
        self.enabled = False

    def dispatch_events(self):
        if self.enabled:
            subscribers = self.subscribers
            for event in pygame.event.get():
                kind = event.type
                if kind in subscribers:
                    for subscriber in subscribers[kind]:
                        if self.is_debug_mode():
                            print "Passing %s to %s" % (event, subscriber)
                        subscriber(event)
        else:
            pygame.event.pump()

    def add_subscriber(self, kind, callback):
        if self.is_debug_mode():
            print "Subscribing %s to %i" % (callback, kind)
        subscribers = self.subscribers
        if kind not in subscribers:
            subscribers[kind] = list()
        subscribers[kind].append(callback)

    def remove_subscriber(self, kind, callback):
        self.subscribers[kind].remove(callback)
from pygame import image

from GameChild import *

class PauseScreen(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_image()

    def load_image(self):
        self.img = image.load(self.get_resource("pause-image-path"))

    def show(self):
        self.get_screen().blit(self.img, (0, 0))
216.73.216.195
216.73.216.195
216.73.216.195
 
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 😇