from lib.pgfw.pgfw.Sprite import Sprite

class StaticEnvironment(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_from_path(self.get_resource("home",
                                              "static-environment-path"),
                            True)
from pygame import mixer
from pygame.image import load

from lib.pgfw.pgfw.GameChild import GameChild
from food_spring.home.View import View
from food_spring.home.StaticEnvironment import StaticEnvironment
from food_spring.home.Foods import Foods
from food_spring.home.collection.Collection import Collection

class Home(GameChild):

    any_press_ignored = ["left", "right"]

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.input = self.get_input()
        self.compare = self.get_delegate().compare
        self.audio = self.get_audio()
        self.audio_path = self.get_resource("home", "audio")
        self.static_environment = StaticEnvironment(self)
        self.view = View(self)
        self.foods = Foods(self)
        self.collection = Collection(self)
        self.reset()
        self.deactivate()

    def deactivate(self):
        self.active = False
        self.input.unregister_any_press_ignore(*self.any_press_ignored)
        self.audio.stop_current_channel()
        mixer.quit()
        mixer.init(self.stored_frequency)

    def reset(self):
        self.foods.reset()
        self.store_frequency()

    def store_frequency(self):
        self.stored_frequency = mixer.get_init()[0]

    def activate(self):
        self.active = True
        self.input.register_any_press_ignore(*self.any_press_ignored)
        self.foods.activate()
        self.start_audio()

    def start_audio(self):
        self.store_frequency()
        mixer.quit()
        mixer.init(int(self.parent.timer.get_ratio_remaining() * \
                       self.stored_frequency))
        self.audio.play_bgm(self.audio_path)

    def update(self):
        if self.active:
            self.view.update()
            self.static_environment.update()
            self.foods.update()
from pygame import Rect

from lib.pgfw.pgfw.GameChild import GameChild

class Foods(GameChild, list):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.active_index = 0
        self.display_surface = self.get_display_surface()
        self.compare = self.get_delegate().compare
        self.bounds = self.get_display_surface().get_rect()
        self.load_configuration()
        self.set_foods()
        self.set_tv_rect()
        self.reset()
        self.subscribe(self.respond)

    def load_configuration(self):
        config = self.get_configuration("home")
        self.inactive_x = config["food-inactive-x"]
        self.inactive_y = config["food-inactive-y"]
        self.active_y = config["food-active-y"]
        self.margin = config["food-margin"]
        self.step = config["food-step"]
        self.tv_edge = config["tv-edge"]

    def set_foods(self):
        levels = self.get_game().levels
        for ii in range(len(levels)):
            self.append(levels[ii].food)

    def set_tv_rect(self):
        self.tv_rect = Rect(0, 0, self.tv_edge,
                            self.display_surface.get_height())

    def swap_in(self, index):
        self.store(self.get_active())
        self.active_index = index
        self.step_forward()

    def step_forward(self):
        self.get_active().location.bottomleft = self.get_x(self.active_index), \
                                                self.active_y

    def get_x(self, index):
        x = self.inactive_x
        for ii in range(index):
            x += self[ii].location.w + self.margin
        return x

    def respond(self, event):
        if self.parent.active:
            if self.compare(event, "left"):
                self.moving[0] = True
            elif self.compare(event, "left", True):
                self.moving[0] = False
            elif self.compare(event, "right"):
                self.moving[1] = True
            elif self.compare(event, "right", True):
                self.moving[1] = False
            elif self.compare(event, "any"):
                self.do()

    def do(self):
        rect = self.get_active().location
        if not self.collide_other():
            if rect.colliderect(self.tv_rect):
                self.stop_moving()
                self.store_location()
                self.parent.deactivate()
                self.get_game().levels.activate(self.active_index)
            elif rect.colliderect(self.parent.collection):
                pass

    def stop_moving(self):
        self.moving = [False, False]

    def collide_other(self):
        active = self.get_active()
        rect = active.location
        clip = None
        closest = None
        for food in self:
            if active != food and rect.colliderect(food):
                if not clip or rect.clip(food).w > clip.w:
                    clip = rect.clip(food)
                    closest = food
        if closest:
            self.swap_in(self.index(closest))
            return True

    def store_location(self):
        self.stored_location = self.get_active().location.topleft

    def reset(self):
        for food in self:
            self.store(food)
        self.swap_in(0)
        self.stop_moving()
        self.store_location()

    def activate(self):
        for food in self:
            if self.index(food) != self.active_index:
                self.store(food)
        self.get_active().location.topleft = self.stored_location

    def store(self, food):
        index = self.index(food)
        self[index].location.bottomleft = self.get_x(index), self.inactive_y

    def update(self):
        active = self.get_active()
        if True in self.moving:
            if self.moving[0]:
                active.move(-self.step)
            if self.moving[1]:
                active.move(self.step)
            self.wrap()
        for food in self:
            if self.index(food) != self.active_index:
                food.update()
        active.update()

    def get_active(self):
        return self[self.active_index]

    def wrap(self):
        rect = self.get_active().location
        bounds = self.bounds
        if rect.left > bounds.right:
            rect.right = bounds.left
        elif rect.right < bounds.left:
            rect.left = bounds.right
216.73.216.44
216.73.216.44
216.73.216.44
 
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 😇