from esp_hadouken import levels

from Road import *

class Void(levels.Void.Void):

    def __init__(self, parent):
        levels.Void.Void.__init__(self, parent)
        self.read_configuration()
        self.road = Road(self)

    def read_configuration(self):
        config = self.get_configuration()
        prefix = "diortem-level-"
        self.padding = config[prefix + "void-padding"]
        self.segment_width_range = config[prefix + "segment-width-range"]
        self.segment_height_range = config[prefix + "segment-height-range"]
        self.shift_range = config[prefix + "shift-range"]
        self.leg_range = config[prefix + "leg-range"]
        self.scroll_speed = config[prefix + "scroll-speed"]

    def update_area(self):
        self.road.update()
from random import randint

from pygame import Surface, Color, Rect

from esp_hadouken.GameChild import *
from Segment import *

class Road(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.init_surface()
        self.set_background()
        self.set_x_range()
        self.generate_segments()

    def init_surface(self):
        parent = self.parent
        width = parent.get_width()
        padding = parent.padding
        bandit_height = parent.parent.bandit.rect.h
        height = parent.get_height() - sum(padding) - bandit_height
        rect = Rect(0, padding[0] + bandit_height, width, height)
        Surface.__init__(self, rect.size)
        self.convert()
        self.rect = rect

    def set_background(self):
        background = Surface(self.get_size())
        background.fill(self.parent.opaque_color)
        background.convert()
        self.background = background

    def set_x_range(self):
        max_seg = self.parent.segment_width_range[1]
        self.x_range = max_seg / 2, self.get_width() - max_seg / 2

    def generate_segments(self):
        self.segments = []
        self.leg = Leg(self)
        y = self.get_height()
        while y > 0:
            self.add_segment(y)
            y -= self.segments[0].rect.h

    def add_segment(self, bottom=None):
        leg = self.leg
        segments = self.segments
        if leg.is_complete():
            leg = Leg(self)
        if bottom is None:
            bottom = segments[0].rect.top
        x = segments[0].rect.centerx if segments else 0
        segments.insert(0, Segment(self, leg, bottom, x))
        leg.advance()
        self.leg = leg

    def update(self):
        self.set_clip()
        self.clear()
        self.update_segments()
        self.draw()

    def set_clip(self):
        parent = self.parent
        y = parent.get_clip().top - parent.padding[0] - parent.parent.bandit.rect.h
        Surface.set_clip(self, Rect((0, y), parent.get_clip().size))

    def clear(self):
        self.blit(self.background, (0, 0))

    def update_segments(self):
        if self.segments[0].rect.top >= -self.parent.segment_height_range[1]:
            self.add_segment()
        for segment in self.segments:
            if segment.rect.top > self.get_height():
                self.segments.remove(segment)
            else:
                segment.update()

    def draw(self):
        self.parent.blit(self, self.rect)


class Leg:

    dirs = [-1, 1]

    def __init__(self, road):
        self.road = road
        self.length = randint(*road.parent.leg_range)
        self.direction = self.dirs[randint(0, len(self.dirs) - 1)]
        self.index = 0

    def advance(self):
        self.index += 1
        self.shift = randint(*self.road.parent.shift_range) * self.direction

    def change_direction(self):
        dirs = self.dirs
        direction = self.direction
        if direction == dirs[0]:
            direction = dirs[1]
        else:
            direction = dirs[0]
        self.direction = direction

    def is_complete(self):
        return self.index >= self.length
from operator import sub
from random import randint

from pygame import Surface, Rect, Color, transform, draw

from esp_hadouken.GameChild import *

class Segment(GameChild):

    def __init__(self, parent, leg, bottom, x):
        GameChild.__init__(self, parent)
        self.init_rect(leg, bottom, x)
        self.color = parent.parent.transparent_color

    def init_rect(self, leg, bottom, x):
        parent = self.parent
        rect = Rect(0, 0, 0, randint(*parent.parent.segment_height_range))
        x_range = parent.x_range
        if x is None:
            x = randint(*x_range)
        else:
            x += leg.direction * randint(*parent.parent.shift_range)
        if x < x_range[0] or x > x_range[1]:
            leg.change_direction()
            if x < x_range[0]:
                x = x_range[0]
            else:
                x = x_range[1]
        rect.centerx = x
        rect.bottom = bottom
        self.rect = rect

    def update(self):
        self.update_width()
        self.recenter()
        self.scroll()
        self.draw()

    def update_width(self):
        parent = self.parent
        rect = self.rect
        pos = float(rect.bottom) / parent.get_height()
        centerx = rect.centerx
        width_r = parent.parent.segment_width_range
        rect.w = pos * -sub(*width_r) + width_r[0]
        rect.centerx = centerx

    def recenter(self):
        rect = self.rect
        right_bound = self.parent.get_width()
        if rect.left < 0:
            rect.left = 0
        elif rect.right > right_bound:
            rect.right = right_bound

    def scroll(self):
        self.rect.top += self.parent.parent.scroll_speed

    def draw(self):
        draw.rect(self.parent, self.color, self.rect)
from random import randint

from pygame import Surface, Color

from esp_hadouken.levels.Void import *
from Barrier import *

class Gauntlet(Void):

    step_count = 0

    def __init__(self, parent):
        Void.__init__(self, parent)
        self.read_configuration()
        self.set_area()
        self.generate_barriers()

    def read_configuration(self):
        config = self.get_configuration()
        prefix = "horse-level-"
        self.padding = config[prefix + "void-padding"]
        self.size_range = config[prefix + "size-range"]
        self.step_limit = config[prefix + "step-limit"]

    def set_area(self):
        self.set_y_range()
        y_range = self.y_range
        height = y_range[1] - y_range[0]
        area = Surface((self.parent.get_width(), height)).convert()
        self.area_bg = Surface(area.get_size()).convert()
        self.area = area

    def set_y_range(self):
        padding = self.padding
        start = self.parent.bandit.rect.h + padding[0]
        end = self.get_height() - padding[1]
        self.y_range = start, end

    def generate_barriers(self):
        self.barriers = barriers = []
        y = 0
        while y < self.area.get_height():
            sibling = None if not barriers else barriers[-1]
            barriers.append(Barrier(self, y, sibling))
            y += barriers[-1].get_height()

    def update_area(self):
        self.update_step_count()
        self.clear_area()
        self.update_barriers()
        self.draw_area()

    def update_step_count(self):
        count = self.step_count + 1
        if count > self.step_limit:
            count = 0
            self.toggle_barrier_headings()
        self.step_count = count

    def toggle_barrier_headings(self):
        for barrier in self.barriers:
            barrier.toggle_heading()

    def clear_area(self):
        self.area.blit(self.area_bg, (0, 0))

    def update_barriers(self):
        for barrier in self.barriers:
            barrier.update()

    def draw_area(self):
        self.blit(self.area, (0, self.y_range[0]))
216.73.216.108
216.73.216.108
216.73.216.108
 
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 😇