from os import makedirs, walk, sep, remove
from os.path import join, dirname, basename, exists
from shutil import rmtree, copy, rmtree
from itertools import chain
from zipfile import ZipFile

import py2exe

from Setup import Setup

class SetupWin(Setup):

    def __init__(self):
        Setup.__init__(self)
        self.replace_isSystemDLL()

    def replace_isSystemDLL(self):
        origIsSystemDLL = py2exe.build_exe.isSystemDLL
        def isSystemDLL(pathname):
            if basename(pathname).lower() in ("libogg-0.dll", "sdl_ttf.dll"):
                return 0
            return origIsSystemDLL(pathname)
        py2exe.build_exe.isSystemDLL = isSystemDLL

    def setup(self):
        config = self.config.get_section("setup")
	windows = [{}]
	if config["init-script"]:
	    windows[0]["script"] = config["init-script"]
	if config["windows-icon-path"]:
	    windows[0]["icon-resources"] = [(1, config["windows-icon-path"])]
        Setup.setup(self, windows,
                    {"py2exe": {"packages": self.build_package_list(),
                                "dist_dir": config["windows-dist-path"]}})
        rmtree("build")
        self.copy_data_files()
        self.create_archive()

    def copy_data_files(self):
	root = self.config.get("setup", "windows-dist-path")
        for path in chain(*zip(*self.build_data_map())[1]):
            dest = join(root, dirname(path))
            if not exists(dest):
                makedirs(dest)
            copy(path, dest)
	self.include_readme(root)

    def include_readme(self, root):
	name = "README"
	if exists(name):
	    readme = open(name, "r")
	    reformatted = open(join(root, name + ".txt"), "w")
	    for line in open(name, "r"):
	    	reformatted.write(line.rstrip() + "\r\n")

    def create_archive(self):
        config = self.config.get_section("setup")
        title = self.translate_title() + "-" + config["version"] + "-win"
        archive_name = title + ".zip"
        archive = ZipFile(archive_name, "w")
        destination = config["windows-dist-path"]
        for root, dirs, names in walk(destination):
            for name in names:
                path = join(root, name)
                archive.write(path, path.replace(destination, title + sep))
        archive.close()
        copy(archive_name, "dist")
        remove(archive_name)
        rmtree(destination)
from random import randint
from math import sin, log, pi
from array import array

from pygame.mixer import Sound, get_init

class Samples(Sound):

    def __init__(self):
        self.set_amplitude()
        Sound.__init__(self, self.build())

    def set_amplitude(self):
        self.amplitude = (1 << (self.get_sample_width() * 8 - 1)) - 1

    def get_sample_width(self):
        return abs(get_init()[1] / 8)

    def build(self):
        pass

    def get_empty_array(self, length):
        return array(self.get_array_typecode(), [0] * length)

    def get_array_typecode(self):
        return [None, "b", "h"][self.get_sample_width()]


class Note(Samples):

    base_frequency = 440.0
    base_octave = 4
    base_name = "A"
    names = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
    SQUARE, TRIANGLE, SAW, SINE, DIRTY = range(5)

    def __init__(self, name=None, octave=4, frequency=None, shape=SQUARE,
                 volume=1.0):
        names = self.names
        self.shape = shape
        if frequency is None:
            self.name = name
            self.octave = octave
            self.set_frequency()
        elif name is None:
            self.frequency = float(frequency)
            self.set_name_and_octave()
        Samples.__init__(self)
        self.set_volume(volume)

    def set_frequency(self):
        name, octave = self.name, self.octave
        names = self.names
        octave_length = len(names)
        offset = (octave - self.base_octave) * octave_length + \
                 names.index(name) - names.index(self.base_name)
        self.frequency = self.base_frequency * 2 ** \
                         (offset / float(octave_length))

    def set_name_and_octave(self):
        names = self.names
        octave_length = len(names)
        offset = int(round(log(self.frequency / self.base_frequency, 2) * \
                           octave_length)) + names.index(self.base_name)
        self.octave = self.base_octave + offset / octave_length
        self.name = names[offset % octave_length]

    def __repr__(self):
        return "%s%i %.2f" % (self.name, self.octave, self.frequency)

    def build(self):
        period = int(round(get_init()[0] / self.frequency))
        samples = self.get_empty_array(period)
        shape = self.shape
        if shape == self.TRIANGLE:
            self.store_triangle_wave(samples, period)
        elif shape == self.SAW:
            self.store_saw_wave(samples, period)
        elif shape == self.SINE:
            self.store_sine_wave(samples, period)
        elif shape == self.DIRTY:
            self.store_dirty_wave(samples)
        else:
            self.store_square_wave(samples, period)
        return samples

    def store_triangle_wave(self, samples, period):
        amplitude = self.amplitude
        coefficient = 4 * amplitude / float(period - 1)
        for time in xrange(int(round(period / 2.0))):
            y = int((coefficient * time) - amplitude)
            samples[time] = y
            samples[-time - 1] = y

    def store_saw_wave(self, samples, period):
        amplitude = self.amplitude
        for time in xrange(period):
            samples[time] = int(2 * amplitude / float(period - 1) * time - \
                              amplitude)

    def store_sine_wave(self, samples, period):
        amplitude = self.amplitude
        for time in xrange(period):
            samples[time] = int(round(sin(time / (period / pi / 2)) * \
                                      amplitude))

    def store_dirty_wave(self, samples):
        amplitude = self.amplitude
        for time in xrange(len(samples)):
            samples[time] = randint(-amplitude, amplitude)

    def store_square_wave(self, samples, period):
        amplitude = self.amplitude
        for time in xrange(period):
            if time < period / 2:
                samples[time] = amplitude
            else:
                samples[time] = -amplitude

    def play(self, maxtime=0, fadeout=None, panning=None, fade_in=0):
        channel = Samples.play(self, -1, maxtime, fade_in)
        if fadeout:
            self.fadeout(fadeout)
        if channel and panning:
            channel.set_volume(*panning)
        return channel
import pygame
from pygame.locals import *

from GameChild import GameChild
from Mainloop import Mainloop
from Audio import Audio
from Display import Display
from Configuration import Configuration
from Delegate import Delegate
from Input import Input
from ScreenGrabber import ScreenGrabber
from Profile import Profile
from VideoRecorder import VideoRecorder
from Interpolator import Interpolator
from TimeFilter import TimeFilter

class Game(GameChild):

    resource_path = None

    def __init__(self, config_rel_path=None, type_declarations=None):
        self.profile = Profile(self)
        GameChild.__init__(self)
        self.print_debug(pygame.version.ver)
        self.config_rel_path = config_rel_path
        self.type_declarations = type_declarations
        self.set_configuration()
        pygame.init()
        self.set_children()
        self.subscribe(self.end, QUIT)
        self.subscribe(self.end)
        self.delegate.enable()

    def set_configuration(self):
        self.configuration = Configuration(self.config_rel_path,
                                           self.resource_path,
                                           self.type_declarations)

    def set_children(self):
        self.time_filter = TimeFilter(self)
        self.delegate = Delegate(self)
        self.display = Display(self)
        self.mainloop = Mainloop(self)
        self.input = Input(self)
        self.audio = Audio(self)
        self.screen_grabber = ScreenGrabber(self)
        self.video_recorder = VideoRecorder(self)
        self.interpolator = Interpolator(self)

    def frame(self):
        self.time_filter.update()
        self.delegate.dispatch()
        if not self.interpolator.is_gui_active():
            self.update()
        else:
            self.interpolator.gui.update()
        if self.video_recorder.requested:
            self.video_recorder.update()

    def run(self):
        self.mainloop.run()

    def update(self):
        pass

    def blit(self, source, destination, area=None, special_flags=0):
        self.get_screen().blit(source, destination, area, special_flags)

    def get_rect(self):
        return self.get_screen().get_rect()

    def end(self, evt):
        if evt.type == QUIT or self.delegate.compare(evt, "quit"):
            self.mainloop.stop()
            self.profile.end()
3.239.129.52
3.239.129.52
3.239.129.52
 
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 😇