from esp_hadouken.levels.Level import *
from Void import *

class Tooth(Level):

    def __init__(self, parent):
        Level.__init__(self, parent)

    def set_void(self):
        self.void = Void(self)
from esp_hadouken.levels.Level import *
from Void import *

class Octo(Level):

    def __init__(self, parent):
        Level.__init__(self, parent)

    def set_void(self):
        self.void = Void(self)

    def set_dot(self):
        Level.set_dot(self, (None, (0, self.get_height())), (True, False))
from random import randint

from pygame import Surface

from esp_hadouken import levels
from Barrier import *

class Void(levels.Void.Void):

    def __init__(self, parent):
        levels.Void.Void.__init__(self, parent)
        self.frame_width = self.parent.get_width()
        self.read_configuration()
        self.init_barriers()

    def read_configuration(self):
        config = self.get_configuration()
        self.barrier_height = config["octo-level-barrier-height"]
        self.min_gap = config["octo-level-min-gap"]
        self.spawn_range = config["octo-level-spawn-range"]
        self.void_padding = config["octo-level-void-padding"]
        self.scroll_speed = config["octo-level-scroll-speed"]

    def init_barriers(self):
        self.set_y_range()
        y_range = self.y_range
        y = y_range[0]
        barriers = []
        y = self.generate_spawn_distance()
        while y < y_range[1]:
            barriers.append(Barrier(self, y))
            y += self.generate_spawn_distance()
        self.barriers = barriers
        self.next_spawn = self.generate_spawn_distance()

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

    def generate_spawn_distance(self):
        return randint(*self.spawn_range)

    def update_area(self):
        barriers = self.barriers
        if barriers[0].y - self.y_range[0] > self.next_spawn:
            barriers.insert(0, Barrier(self, self.y_range[0]))
            self.next_spawn = self.generate_spawn_distance()
        for barrier in barriers:
            if barrier.y > self.y_range[1]:
                barriers.remove(barrier)
            else:
                barrier.update()
from random import randint

from pygame import Surface, Color, Rect

from esp_hadouken.GameChild import *

class Barrier(GameChild, Surface):

    transparent_color = Color("magenta")

    def __init__(self, parent, y=0):
        GameChild.__init__(self, parent)
        Surface.__init__(self, (parent.frame_width, parent.barrier_height))
        self.set_colorkey(self.transparent_color)
        self.convert()
        self.y = y
        self.set_gap()

    def set_gap(self):
        gap = Surface(self.get_size())
        gap.fill(self.transparent_color)
        self.gap_center = randint(0, self.get_width())
        self.gap = gap.convert()

    def update(self):
        self.y += self.parent.scroll_speed
        self.blit_gap()
        self.draw()

    def blit_gap(self):
        gap = self.gap
        width = self.calculate_width()
        position = (self.gap_center - width / 2, 0)
        area = Rect(0, 0, width, gap.get_height())
        self.blit(gap, position, area)
        self.blit_overflow(width, position[0])

    def calculate_width(self):
        y_range = self.parent.y_range
        ratio = (self.y - y_range[0]) / float(y_range[1] - y_range[0])
        min_gap = self.parent.min_gap
        return int(min_gap + (self.get_width() - min_gap) * ratio)

    def blit_overflow(self, width, x):
        gap = self.gap
        frame_width = self.parent.frame_width
        if x < 0 or x + width > frame_width:
            if x < 0:
                overflow = 0 - x
                position = (frame_width - overflow, 0)
            else:
                overflow = x + width - frame_width
                position = (0, 0)
            self.blit(gap, position, Rect(0, 0, overflow, self.get_height()))

    def draw(self):
        self.parent.blit(self, (0, self.y))
from math import pi, sin, cos
from random import random, randint

from pygame import time, draw, Rect

from esp_hadouken import levels

class Trap(levels.Void.Void):

    def __init__(self, parent):
        levels.Void.Void.__init__(self, parent)
        self.read_configuration()
        self.reset()

    def read_configuration(self):
        config = self.get_configuration()
        prefix = "circulor-level-"
        self.radius_range = config[prefix + "radius-range"]
        self.trap_duration = config[prefix + "trap-duration"]
        self.speed = config[prefix + "speed"]
        self.thickness = config[prefix + "trap-thickness"]

    def reset(self):
        self.center = None
        self.total_elapsed = 0
        self.last_ticks = time.get_ticks()
        self.angle = random() * pi * 2

    def update_area(self):
        if self.parent.trapped:
            self.place()
            self.collide()
            if self.total_elapsed >= self.trap_duration:
                self.parent.trapped = False
                self.parent.escaped = True
            else:
                self.update_total_elapsed()
            self.draw_circle()

    def place(self):
        center = self.get_center()
        x_del, y_del = self.calculate_deltas()
        self.center = center[0] + x_del, center[1] + y_del

    def get_center(self):
        center = self.center
        if not center:
            center = self.parent.dot.rect.center
        return center

    def calculate_deltas(self):
        ang, distance = self.angle, self.speed
        return sin(ang) * distance, cos(ang) * distance

    def collide(self):
        angle = self.angle
        x, y = self.center
        radius = self.get_radius()
        rect = Rect(x - radius, y - radius, radius * 2, radius * 2)
        bandit = self.parent.bandit.rect
        if rect.colliderect(bandit):
            if bandit.left - rect.right > rect.top - bandit.bottom:
                angle = -angle
                rect.right = bandit.left
            else:
                angle = pi - angle
                rect.top = bandit.bottom
        field = self.parent.rect
        if rect.right > field.w or rect.left < 0:
            angle = -angle
            if rect.right > field.w:
                rect.right = field.w
            else:
                rect.left = 0
        if rect.top < 0 or rect.bottom > field.h:
            angle = pi - angle
            if rect.top < 0:
                rect.top = 0
            else:
                rect.bottom = field.h
        self.angle = angle
        self.center = rect.center

    def update_total_elapsed(self):
        current_ticks = time.get_ticks()
        self.total_elapsed += current_ticks - self.last_ticks
        self.last_ticks = current_ticks

    def draw_circle(self):
        center = tuple(map(int, self.center))
        color = self.opaque_color
        draw.circle(self, color, center, self.get_radius(), self.thickness)

    def get_radius(self):
        radius_r = self.radius_range
        pos = float(self.total_elapsed) / self.trap_duration
        return int(pos * (radius_r[1] - radius_r[0]) + radius_r[0])
216.73.216.214
216.73.216.214
216.73.216.214
 
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 😇