Skip to content

Instantly share code, notes, and snippets.

@antonvh
antonvh / get_voltage.py
Last active September 29, 2015 20:02
Get Voltage from BrickPi+
import smbus
def get_voltage():
"""
Reads the digital output code of the MCP3021 chip on the BrickPi+ over i2c.
Some bit operation magic to get a voltage floating number.
If this doesnt work try this on the command line: i2cdetect -y 1
The 1 in there is the bus number, same as in bus = smbus.SMBus(1)
Google the resulting error.
@antonvh
antonvh / gist:7439560
Created November 12, 2013 22:01
Installation steps for lego robot sumo
sudo easy_install pip
pip install numpy
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
brew tap homebrew/science
brew install opencv
pip install jaraco.nxt
hg clone https://bitbucket.org/antonvh/robot-sumo robot-sumo
cd robot-sumo/blob_recog
python blob_recognition.py
@antonvh
antonvh / gist:8455505
Created January 16, 2014 14:04
Getting joystick input in python
import sdl2
sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
joystick = sdl2.SDL_JoystickOpen(0)
while True:
sdl2.SDL_PumpEvents()
#depending on the gamepad this gives you a value between -32768 and +32768
#or between 0 and 32768
joy_x = sdl2.SDL_JoystickGetAxis(joystick, 0)
@antonvh
antonvh / gist:8458252
Created January 16, 2014 16:38
Sending a motor command to mindstorms NXT using python and the jaraco library.
from jaraco.nxt import *
from jaraco.nxt.messages import *
conn = Connection('/dev/tty.NXT-DevB')
cmd = SetOutputState(
i,
motor_on=True,
set_power=motorpower,
run_state=RunState.running,
@antonvh
antonvh / gist:8458472
Created January 16, 2014 16:48
Controlling a mindstorms NXT robot using a gamepad and python
#!python
from jaraco.nxt import *
from jaraco.nxt.messages import *
import time
import sdl2
import math
sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
@antonvh
antonvh / coordinates.py
Last active November 4, 2020 12:26
Generate coordinates for a vertical plotter with Python
__author__ = 'anton'
from math import sin, cos, pi
from PIL import Image, ImageDraw
NUM_POINTS = 150
PREVIEW_SIZE = 500
CIRCLE = 1
SQUARE = 2
@antonvh
antonvh / connection.py
Last active June 15, 2021 08:43
Remote control a MINDSTORMS EV3 robot with a SPIKE Prime Hub
# This code was originally written by Pybricks
# Paste all of it into a file named 'connection.py' and place it next to your 'main.py' file
# Nothing below is written by Anton's Mindstorms
# I found it here. https://github.com/pybricks/pybricks-projects
from uctypes import addressof, sizeof, struct
from usocket import socket, SOCK_STREAM
from _thread import start_new_thread
# Brick to brick remote control program. Run this on the remote control steering wheel.
# (c) 2021 Anton's Mindstorms & Ste7an
# TO STOP THIS SCRIPT ALWAYS USE THE BUTTON ON THE HUB.
# AVOID THE STOP BUTTON IN THE MINDSTORSM APP!
# Building instructions here:
# https://antonsmindstorms.com/product/remote-control-steering-wheel-with-spike-prime/
# Use with the the car script here:
# This is the car code for the remote controlled Hot Rod
# (c) Anton's Mindstorms & Ste7an
# Full tutorial here:
# https://antonsmindstorms.com/2021/06/19/how-to-remote-control-lego-spike-prime-and-robot-inventor-with-python/
# Building instructions here:
# https://antonsmindstorms.com/product/remote-controlled-hot-rod-with-51515/
# Use with the the remote control script here:
@antonvh
antonvh / main-car.py
Created April 22, 2020 16:27
Pybricks 2.0 code for remote controlled race car with another EV3 brick
#!/usr/bin/env pybricks-micropython
# car code
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch