from pygame import Surface
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild

class Background(Surface, GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_screen()
        self.init_surface()
        self.load_tile()
        self.draw_tiles()

    def init_surface(self):
        Surface.__init__(self, self.display_surface.get_size())

    def load_tile(self):
        self.tile = load(self.get_resource("overworld",
                                           "background-tile-path")).convert()

    def draw_tiles(self):
        tile = self.tile
        width = tile.get_width()
        x_limit, y_limit = self.get_size()
        x, y = 0, 0
        for y in xrange(0, y_limit, width):
            for x in xrange(0, x_limit, width):
                self.blit(tile, (x, y))

    def update(self):
        self.display_surface.blit(self, (0, 0))
from pygame import Surface, Rect
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild

class Section(Surface, GameChild):

    def __init__(self, parent, left=True):
        GameChild.__init__(self, parent)
        Surface.__init__(self, self.parent.size)
        self.left = left
        self.display_surface = self.get_display_surface()
        self.rect = self.get_rect()
        self.draw_tiles()
        self.fill_borders()
        self.place()

    def draw_tiles(self):
        tile = self.parent.tile
        x_limit, y_limit = self.get_size()
        width = tile.get_width()
        for y in xrange(0, y_limit, width):
            for x in xrange(0, x_limit, width):
                self.blit(tile, (x, y))

    def place(self):
        rect = self.rect
        rect.top = self.parent.top
        if not self.left:
            rect.right = self.display_surface.get_rect().right

    def fill_borders(self):
        reference = self.rect
        top_rect = Rect(reference.topleft, (reference.w, 1))
        bottom_rect = Rect((0, 0), (reference.w, 1))
        bottom_rect.bottom = reference.bottom
        side_rect = Rect((0, 0), (1, reference.h))
        if self.left:
            side_rect.right = reference.right
        color = self.parent.border
        self.fill(color, top_rect)
        self.fill(color, bottom_rect)
        self.fill(color, side_rect)

    def update(self):
        self.display_surface.blit(self, self.rect)
from pygame import Surface
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.overworld.wall.Section import Section

class Wall(Surface, GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.load_tile()
        self.set_sections()
        # 224x25 gap: 52

    def load_configuration(self):
        config = self.get_configuration("overworld")
        self.size = config["wall-size"]
        self.top = config["wall-position"]
        self.border = config["wall-border"]

    def load_tile(self):
        self.tile = load(self.get_resource("overworld",
                                           "wall-tile-path")).convert()

    def set_sections(self):
        self.sections = [Section(self, left) for left in False, True]

    def update(self):
        for section in self.sections:
            section.update()
from random import choice

from pygame import PixelArray, Color
from pygame.image import load

from esp_hadouken.pgfw.Sprite import Sprite
from esp_hadouken.dot.engine.Engine import Engine

class Dot(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.engine = Engine(self)
        self.delegate = self.get_delegate()
        self.load_configuration()
        self.load_image()
        self.load_palette()
        self.add_frames()
        self.reset()
        self.subscribe(self.respond)

    def load_configuration(self):
        config = self.get_configuration("dot")
        self.framerate_range = config["framerate-range"]
        self.transparent_color = config["transparent-color"]
        self.frame_count = config["frame-count"]

    def load_image(self):
        image = load(self.get_resource("dot", "image-path")).convert()
        image.set_colorkey(self.transparent_color)
        self.image = image

    def load_palette(self):
        config = self.get_configuration("sprite")
        self.palette = [map(Color, palette) for palette in
                        config["dark-palette"], config["light-palette"]]

    def add_frames(self):
        for ii in xrange(self.frame_count):
            surface = self.image.copy()
            self.paint_frame(surface)
            self.add_frame(surface)

    def paint_frame(self, surface):
        pixels = PixelArray(surface)
        dark, light = map(choice, self.palette)
        pixels.replace((255, 255, 255), light)
        pixels.replace((0, 0, 0), dark)

    def reset(self):
        self.set_framerate(self.framerate_range[1])
        self.engine.reset()

    def respond(self, event):
        if self.delegate.compare(event, "reset-game"):
            self.reset()

    def is_active(self):
        return self.parent.active

    def update(self):
        if self.is_active():
            self.engine.update()
            self.move(*self.engine)
            Sprite.update(self)
from pygame.font import Font

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.pgfw.Vector import Vector
from esp_hadouken.dot.engine.accelerator.Accelerator import Accelerator
from esp_hadouken.dot.engine.Calibrator import Calibrator
from esp_hadouken.dot.engine.Profile import Profile

class Engine(GameChild, Vector):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.input = self.get_input()
        self.display_surface = self.get_screen()
        self.load_configuration()
        self.display_active = self.check_command_line(self.display_flag)
        self.calibration_active = self.check_command_line(self.calibrate_flag)
        self.accelerator = Accelerator(self)
        self.profile = Profile(self)
        self.accelerator.set_slopes()
        self.add_calibrator()
        self.reset()
        self.init_display()

    def load_configuration(self):
        config = self.get_configuration("engine")
        self.display_flag = config["display-flag"]
        self.calibrate_flag = config["calibrate-flag"]
        self.initial_profile = config["initial-profile"]
        self.font_path = self.get_resource("engine", "font-path")

    def add_calibrator(self):
        if self.calibration_active:
            self.calibrator = Calibrator(self)

    def reset(self):
        Vector.__init__(self)
        self.accelerator.reset()

    def init_display(self):
        if self.display_active:
            self.display_surface = self.get_screen()
            self.font = Font(self.font_path, 14)
            self.render()

    def render(self):
        string = str(self)
        self.text = self.font.render(string, False, (0, 0, 0), (255, 255, 255))
        self.string = string

    def set(self, max_velocity, deceleration):
        self.max_velocity = max_velocity
        self.deceleration = deceleration

    def update(self):
        if self.calibration_active:
            self.calibrator.update()
        self.accelerator.update()
        self.apply_accelerator()
        self.constrain()
        self.decelerate()
        self.display()

    def apply_accelerator(self):
        self += self.accelerator.get_sum()

    def constrain(self):
        self.apply_to_components(self.constrain_component)

    def constrain_component(self, magnitude):
        if magnitude > self.max_velocity:
            return self.max_velocity
        if magnitude < -self.max_velocity:
            return -self.max_velocity
        return magnitude

    def decelerate(self):
        self.apply_to_components(self.decelerate_component)

    def decelerate_component(self, magnitude):
        deceleration = self.deceleration
        if abs(magnitude) < deceleration:
            magnitude = 0
        else:
            if magnitude > 0:
                magnitude -= deceleration
            else:
                magnitude += deceleration
        return magnitude

    def display(self):
        if self.display_active:
            if self.string != str(self):
                self.render()
            self.display_surface.blit(self.text, (0, 0))

    def __str__(self):
        return "[{0: .2f}, {1: .2f}]".format(*self)
216.73.216.28
216.73.216.28
216.73.216.28
 
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 😇