from os.path import join

from pygame import Rect
from pygame.image import load
from pygame.transform import flip

from dark_stew.pgfw.Sprite import Sprite

class Rod(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_frames()
        self.center = self.display_surface.get_rect().center
        self.load_line()
        self.deactivate()
        self.frames = [self.normal_frame]
        self.measure_rect()

    def load_frames(self):
        image = load(join(self.parent.sitting_path,
                          self.get_configuration("aphids", "rod-path")))
        self.normal_frame = self.fill_colorkey(image)
        self.mirrored_frame = self.fill_colorkey(flip(image, True, False))

    def load_line(self):
        image = load(join(self.parent.sitting_path,
                          self.get_configuration("aphids",
                                                 "line-path"))).convert_alpha()
        self.line = image
        self.line_rect = self.line.get_rect()

    def activate(self, dx, dy, mirror=False):
        self.active = True
        cx, cy = self.center
        self.rect.topleft = cx + dx, cy + dy
        if mirror:
            self.frames = [self.mirrored_frame]
            self.line_rect.topleft = self.rect.topright
        else:
            self.frames = [self.normal_frame]
            self.line_rect.topleft = self.rect.topleft

    def deactivate(self):
        self.active = False

    def draw(self):
        if self.active:
            Sprite.draw(self)
            self.display_surface.blit(self.line, self.line_rect)
from pygame import event, key as keys, mouse
from pygame.locals import *

from GameChild import *
from EventDelegate import *

class Input(GameChild):

    command_event = EventDelegate.command_event
    left_mouse_button = 1

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.subscribe_to_events()

    def subscribe_to_events(self):
        self.subscribe_to(KEYDOWN, self.translate_key_press)
        self.subscribe_to(MOUSEBUTTONDOWN, self.translate_mouse_down)
        self.subscribe_to(MOUSEBUTTONUP, self.translate_mouse_up)
        self.subscribe_to(MOUSEMOTION, self.translate_mouse_motion)

    def translate_key_press(self, evt):
        key = evt.key
        config = self.get_configuration()
        if key in config["keys-quit"]:
            self.post_command("quit")
        if key in config["keys-capture-screen"]:
            self.post_command("capture-screen")
        if key in config["keys-reset"]:
            self.post_command("reset")

    def post_command(self, name):
        EventDelegate.post_event(self.command_event, command=name)

    def translate_mouse_down(self, evt):
        if evt.button == self.left_mouse_button:
            self.post_command("grab")

    def translate_mouse_up(self, evt):
        if evt.button == self.left_mouse_button:
            self.post_command("release")

    def translate_mouse_motion(self, evt):
        if mouse.get_pressed()[0]:
            self.post_command("drag")

    def is_key_pressed(self, identifier):
        poll = keys.get_pressed()
        for key in self.get_configuration()[identifier]:
            if poll[key]:
                return True
import os
import sys
import time

from pygame import image

from GameChild import *
from Input import *

class ScreenGrabber(GameChild):

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.subscribe_to(Input.command_event, self.save_display)

    def save_display(self, event):
        if event.command == "capture-screen":
            directory = self.get_resource("capture-path")
            try:
                if not os.path.exists(directory):
                    os.mkdir(directory)
                name = self.build_name()
                path = os.path.join(directory, name)
                capture = image.save(self.get_screen(), path)
                print "Saved screen capture to %s" % directory + name
            except:
                print "Couldn't save screen capture to %s, %s" % \
                      (directory, sys.exc_info()[1])

    def build_name(self):
        config = self.get_configuration()
        prefix = config["capture-file-name-format"]
        extension = config["capture-extension"]
        return time.strftime(prefix) + extension
from pygame import display

from GameChild import *

class Display(GameChild):

    def __init__(self, game):
        GameChild.__init__(self, game)
        self.set_screen()
        self.set_caption()

    def set_screen(self):
        configuration = self.get_configuration()
        self.screen = display.set_mode(configuration["display-dimensions"])

    def set_caption(self):
        display.set_caption(self.get_configuration()["game-title"])

    def get_screen(self):
        return self.screen

    def get_size(self):
        return self.screen.get_size()
from os.path import exists, join

from pygame import mixer

import Game

class GameChild:

    def __init__(self, parent=None):
        self.parent = parent

    def get_game(self):
        current = self
        while not isinstance(current, Game.Game):
            current = current.parent
        return current

    def get_configuration(self):
        return self.get_game().get_configuration()

    def get_input(self):
        return self.get_game().get_input()

    def get_screen(self):
        return self.get_game().display.get_screen()

    def get_timer(self):
        return self.get_game().timer

    def get_audio(self):
        return self.get_game().audio

    def get_delegate(self):
        return self.get_game().delegate

    def get_resource(self, key):
        config = self.get_configuration()
        path = config[key]
        if exists(path):
            return path
        else:
            return join(config["media-install-path"], path)

    def subscribe_to(self, kind, callback):
        self.get_game().delegate.add_subscriber(kind, callback)
216.73.216.51
216.73.216.51
216.73.216.51
 
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 😇