from random import random, choice

from pygame import Color, Rect
from pygame.font import Font

from ball_cup.pgfw.Animation import Animation

class Result(Animation):

    def __init__(self, parent):
        Animation.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.background_color = self.parent.color
        self.load_configuration()
        self.set_font()
        self.reset()
        self.register(self.render, self.framerate)

    def load_configuration(self):
        config = self.get_configuration("result")
        self.success_text = config["success-text"]
        self.miss_text = config["miss-text"]
        self.success_colors = self.build_color_list(config["success-colors"])
        self.miss_color = Color(config["miss-color"] + "FF")
        self.font_size = config["font-size"]
        self.margin = config["margin"]
        self.framerate = config["framerate"]
        self.main_color_probability = config["main-color-probability"]
        self.font_path = self.get_resource("result", "font-path")

    def build_color_list(self, colors):
        return [Color(color + "FF") for color in colors]

    def set_font(self):
        self.font = Font(self.font_path, self.font_size)

    def reset(self):
        self.halt(self.render)
        self.deactivate()
        self.rect = Rect(0, 0, 0, 0)

    def deactivate(self):
        self.active = False

    def render(self):
        color = self.color
        if self.text != self.miss_text:
            color = self.get_color()
        surface = self.font.render(self.text, True, color,
                                   self.background_color)
        rect = surface.get_rect()
        relative = self.display_surface.get_rect()
        rect.centerx = relative.centerx
        rect.bottom = relative.bottom - self.margin
        self.surface = surface
        self.rect = rect

    def get_color(self):
        color = self.color
        if random() > self.main_color_probability:
            colors = self.success_colors[:]
            colors.remove(self.color)
            color = choice(colors)
        return color

    def activate(self):
        self.active = True
        self.set_text()
        self.play(self.render)

    def set_text(self):
        proximity = self.parent.proximity
        if proximity is None:
            self.text = self.miss_text
            self.color = self.miss_color
        else:
            text = self.success_text
            count = len(text)
            index = min(count - 1, int(proximity * count))
            self.text = text[index]
            self.color = self.success_colors[index]

    def update(self):
        Animation.update(self)
        if self.active:
            self.draw()

    def clear(self):
        self.parent.clear(self.rect)

    def draw(self):
        self.display_surface.blit(self.surface, self.rect)
from math import sqrt

from pygame import Surface
from pygame.draw import circle
from pygame.locals import *

from ball_cup.pgfw.Animation import Animation
from ball_cup.field.Levels import Levels
from ball_cup.field.Cup import Cup
from ball_cup.field.ball.Ball import Ball
from ball_cup.field.Result import Result
from ball_cup.field.scoreboard.Scoreboard import Scoreboard

class Field(Animation):

    def __init__(self, parent):
        Animation.__init__(self, parent)
        self.delegate = self.get_delegate()
        self.display_surface = self.get_screen()
        self.level_index = 0
        self.waiting_for_key_up = False
        self.ended = False
        self.load_configuration()
        self.set_background()
        self.set_children()
        self.set_max_distance()
        self.draw()
        self.subscribe(self.respond)
        self.subscribe(self.skip, KEYDOWN)
        self.register(self.activate_result, self.submit_result,
                      self.show_game_over)

    def load_configuration(self):
        config = self.get_configuration("field")
        self.color = config["color"]
        self.result_delay = config["result-delay"]
        self.submit_delay = config["submit-delay"]
        self.game_over_delay = config["game-over-delay"]

    def set_background(self):
        background = Surface(self.display_surface.get_size())
        background.fill(self.color)
        self.background = background

    def set_children(self):
        self.levels = Levels(self)
        self.cup = Cup(self)
        self.ball = Ball(self)
        self.result = Result(self)
        self.scoreboard = Scoreboard(self)

    def set_max_distance(self):
        cup_w, cup_h = self.cup.rect.size
        ball_w, ball_h = self.ball.rect.size
        limit = cup_w / 2.0 + ball_w / 2.0 - 1, \
                cup_h / 2.0 + ball_h / 2.0 - 1
        self.max_distance = sqrt(limit[0] ** 2 + limit[1] ** 2)

    def draw(self):
        self.clear(self.display_surface.get_rect())

    def respond(self, event):
        compare = self.delegate.compare
        if compare(event, "reset-game"):
            self.reset()
        if compare(event, "left", cancel=True):
            if self.waiting_for_key_up:
                if self.ended:
                    self.reset()
                else:
                    self.advance_level()

    def reset(self):
        self.advance_level(0)
        self.scoreboard.reset()

    def advance_level(self, index=None):
        if index is None:
            index = self.level_index + 1
        if index >= len(self.levels):
            self.reset()
            return
        self.ended = False
        self.waiting_for_key_up = False
        self.level_index = index
        self.halt(self.activate_result)
        self.halt(self.submit_result)
        self.clear_children()
        self.cup.reset()
        self.ball.reset()
        self.result.reset()
        self.set_max_distance()

    def skip(self, event):
        if self.check_command_line("sk"):
            if event.key == K_UP:
                self.advance_level()
                self.halt()
            elif event.key == K_DOWN:
                self.advance_level(self.level_index - 1)
                self.halt()
            elif event.key == K_RIGHT:
                self.advance_level(self.level_index)
                self.halt()

    def get_current_level(self):
        return self.levels[self.level_index]

    def evaluate(self):
        proximity = None
        cr = self.cup.rect
        br = self.ball.rect
        if br.colliderect(cr):
            target = cr.left + cr.w / 2.0, cr.top + cr.h / 2.0
            position = br.left + br.w / 2.0, br.top + br.h / 2.0
            distance = sqrt((position[0] - target[0]) ** 2 + \
                            (position[1] - target[1]) ** 2)
            proximity = 1 - (distance / self.max_distance)
            self.play(self.submit_result, delay=self.submit_delay,
                      play_once=True)
        else:
            self.play(self.show_game_over, delay=self.game_over_delay,
                      play_once=True)
        self.proximity = proximity
        self.play(self.activate_result, delay=self.result_delay, play_once=True)

    def activate_result(self):
        self.result.activate()
        if self.proximity is None:
            self.scoreboard.reset()

    def submit_result(self):
        self.scoreboard.refresh()
        self.waiting_for_key_up = True

    def show_game_over(self):
        self.ended = True
        self.waiting_for_key_up = True

    def update(self):
        self.clear_children()
        Animation.update(self)
        self.cup.update()
        self.ball.update()
        self.result.update()
        self.scoreboard.update()

    def clear_children(self):
        self.cup.clear()
        self.ball.clear()
        self.result.clear()
        self.scoreboard.clear()

    def clear(self, rect):
        self.display_surface.blit(self.background, rect, rect)
216.73.216.212
216.73.216.212
216.73.216.212
 
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 😇