Skip to content

Instantly share code, notes, and snippets.

View pavel-kirienko's full-sized avatar
:shipit:

Pavel Kirienko pavel-kirienko

:shipit:
View GitHub Profile
@pavel-kirienko
pavel-kirienko / dump-sram.sh
Last active March 27, 2025 15:05
Dump SRAM into a file via Black Magic Probe with GDB
#!/bin/sh
ln -fs /dev/serial/by-id/usb-*Black*Magic*-if00 .bmp.tmp
# Replace arm-zephyr-eabi-gdb with the correct toolchain prefix.
# Update the end address depending on the SRAM size.
arm-zephyr-eabi-gdb --batch -ex 'tar ext .bmp.tmp' -ex 'mon swdp_scan' -ex 'attach 1' -ex 'dump bin mem ram.bin 0x20000000 0x20008000'
# To read all Cortex-M4 core peripheral registers:
# mem 0xE000E000 0xE000F000 rw 32 # This is not necessary if "set mem inaccessible-by-default off"
@pavel-kirienko
pavel-kirienko / trace_image.py
Last active March 6, 2025 12:22
A simple script for extracting numerical data from plots: load an image and click on it to record point coordinates. The first click sets the origin, the second sets the unit scale, the subsequent clicks sample the data points.
#!/usr/bin/env python3
"""
A simple script for extracting numerical data from plots: load an image and click on it to record point coordinates.
Usage example:
./trace_image.py my_plot.jpg
Copyright (C) 2025 Pavel Kirienko <[email protected]>
"""
@pavel-kirienko
pavel-kirienko / pinv.hpp
Created August 14, 2024 14:48
Pseudo-inverse of a statically sized matrix in Eigen
/// Pseudo-inverse of a matrix. There may be a more efficient solution for square matrices; this is a general one.
/// Tolerance less than epsilon*10 may cause numerical instability.
template <auto R, auto C, typename S>
[[nodiscard]] Eigen::Matrix<S, C, R> pinv(const Eigen::Matrix<S, R, C>& a, const S tolerance = std::numeric_limits<S>::epsilon() * 10)
{
const Eigen::JacobiSVD<Eigen::Matrix<S, R, C>> svd(a, Eigen::ComputeFullU | Eigen::ComputeFullV);
Eigen::Matrix<S, C, R> si;
si.setZero();
for (int i = 0; i < svd.singularValues().size(); ++i)
{
@pavel-kirienko
pavel-kirienko / setup_slcan
Last active March 25, 2025 21:00
setup_slcan -- a simple script for managing SocketCAN SLCAN interfaces on GNU/Linux
#!/bin/bash
# https://gist.github.com/pavel-kirienko/32e395683e8b7f49e71413aebf5e1a89
# Pavel Kirienko <[email protected]>
HELP="Register slcan-enabled serial-to-CAN adapters as network interfaces.
Usage:
`basename $0` [options] <tty0> [[options] <tty1> ...]
Interface indexes will be assigned automatically in ascending order, i.e., the
first device will be mapped to slcan0, second to slcan1, and so on.
@pavel-kirienko
pavel-kirienko / recomment.py
Created June 29, 2021 10:14
Transform C-style comments into C++-style comments preserving Doxygen notation
#!/usr/bin/env python
# Bulk usage:
# find . -name '*.[ch]pp' -exec bash -c 'recomment.py < "{}" > "{}.out" ; mv "{}.out" "{}"' \;
import sys, re
RE_REPLACE = re.compile(r"(?m)^(\s*?) ?\*(.*)")
def replace_one(match) -> str:
use_doc = bool(match[1])
@pavel-kirienko
pavel-kirienko / capture.sh
Created April 16, 2021 14:54
Capture high-frame-rate, high-resolution video of the desktop using ffmpeg
# Capture high-frame-rate, high-resolution video of the desktop using ffmpeg.
# The key options are: -c:v libx264 -f flv -vb 1000M -deadline realtime -cpu-used 12
# Without these, the output video peaks at ~5 fps or so.
ffmpeg -video_size 3840x2080 -f x11grab -i :0.0+3840,0 -c:v libx264 -f flv -vb 1000M -deadline realtime -cpu-used 12 output.mp4 -y
@pavel-kirienko
pavel-kirienko / 51-xbox-gamepad.rules
Last active January 30, 2025 13:06 — forked from dnmodder/fixcontroller.py
Use Microsoft X-Box 360 Gamepad with GNU/Linux
# By default, X-Box 360-compatible controllers are detectable but dysfunctional because they expect the host
# to send a particular USB control transfer with some sort of initialization command.
# This udev rule will invoke a trivial Python script automatically when the gamepad is connected that emits
# the required initialization command.
#
# Integration:
# 1. Put this rule into /etc/udev/rules.d/51-xbox-gamepad.rules
# 2. Install pyusb for the root's Python: sudo pip install pyusb
# 3. Reload udev rules as root: udevadm control --reload-rules && udevadm trigger
#
@pavel-kirienko
pavel-kirienko / video-to-gif.sh
Created June 9, 2020 15:42
Convert video to gif
#!/bin/bash
# Convert all video files in the current directory into gifs.
# Silently overwrite existing files.
# This is useful for working with matplotlib animations.
for src in *.mp4
do
bn="${src%.*}"
echo "$src $bn"
ffmpeg -y -i "$src" "${bn}.gif" || exit 1
done
@pavel-kirienko
pavel-kirienko / arm-bin-to-elf.sh
Last active December 6, 2024 12:43
Converting flat binary into ARM ELF format, useful for firmware uploading via GDB
# The part .data=0x08000000 should be replaced with the correct base offset of the ROM.
# The value 0x08000000 is valid for STM32.
arm-none-eabi-objcopy -I binary -O elf32-little --change-section-address .data=0x08000000 input.bin output.elf
@pavel-kirienko
pavel-kirienko / rpm_snippet.py
Created November 6, 2017 20:53
A simple snippet for the UAVCAN GUI Tool (v0.9) that sweeps an ESC RPM setpoint from zero to maximum and then back
def run():
rpm_setpoint = 0
going_down = False
step = 2
def update():
nonlocal rpm_setpoint
nonlocal going_down
rpm_setpoint += (-step if going_down else step)
broadcast(uavcan.equipment.esc.RPMCommand(rpm=[rpm_setpoint]))
if going_down: