Skip to content

Instantly share code, notes, and snippets.

View plusk01's full-sized avatar

Parker Lusk plusk01

View GitHub Profile
@plusk01
plusk01 / nonoverlapping_random_circles.sh
Last active January 3, 2020 18:54
Sample non-overlapping circles from a uniform square - useful for initialization of multiple objects
#!/bin/bash
function draw_uniform {
# generates a random float in [a b)
# n.b.: a, b must be ints
a=$1
b=$2
echo "$((a+RANDOM%(b-a))).$((RANDOM%999))"
}
@plusk01
plusk01 / lastbag.sh
Last active November 24, 2019 04:27
#!/bin/bash
# Given a directory with at least one bag file, find the filename with the
# latest timestamp (%Y-%m-%d-%H-%M-%S) in name and open it with `rqt_bag`.
# This is particularly useful given a directory of many rosbags that were
# created via `rosbag record` without an explicit filename.
# If the files were created with `rosbag record -o <name>`, then <name>
# will be appended with a timestamp, in which case the latest of *these*
# bags can be found with `./lastbag <name>`.
#
# Parker Lusk
@plusk01
plusk01 / snippet.cpp
Last active August 7, 2019 21:22
ROS sensor_msgs PointCloud2 from cv::Mat
void reverse_memcpy(unsigned char* dst, const unsigned char* src, size_t n)
{
for (size_t i=0; i<n; ++i)
dst[n-1-i] = src[i];
}
// ----------------------------------------------------------------------------
sensor_msgs::PointCloud2 cloudmsg;
cloudmsg.header.stamp = _img->header.stamp;
@plusk01
plusk01 / wifi_select.sh
Created April 22, 2019 14:59
Utility for switching between wpa_supplicant wireless connections
#!/bin/bash
#
# NetworkManager is trash for robots (periodic scanning causes
# ~3s latency every 2 minutes). We have disabled Network Manager
# (systemctl disable network-manager) and network interfaces are
# now managed via /etc/network/interfaces, with authenticated
# wireless managed by wpa_supplicant.
#
# Your /etc/network/interfaces probably looks like:
#
@plusk01
plusk01 / taskset_example.md
Last active February 14, 2023 20:43
Understanding CPU affinity with taskset

CPU Affinity

In a single core system, the OS allows multiple processes to run by sharing CPU time with the multiple processes. This is called concurrency, which gives the illusion of multiple processes executing at once, but is in fact just using a scheduler to give each process dedicated time on the CPU. The time associated with switching processes on a single core is overhead caused by context switching.

When a multi-core processor is available to an OS (e.g., Linux), the scheduler will do its best to allow processes to run simultaneously (by placing processes on different cores) in addition to running concurrently (different processes on the same core).

To control which core a process runs on, we can tell the scheduler to give a process a certain affinity towards a given set of CPUs.

Using taskset, we can get/set the CPU affinity of a particular process. Consider the following example.

@plusk01
plusk01 / bagcut.sh
Created May 3, 2018 23:23
BASH function to split ROS bags
# Use rosbag filter to create a new bag
#
# $ bagcut original.bag 0 336 # cuts from 0 to 336s
#
# $ bagcut original.bag 50 # new bag starts 50s in
#
# $ bagcut original.bag 10 -20 # cuts from 10 to END_TIME-20s
function bagcut {
BAG=${1:0:-4}
OFFSET=$2
@plusk01
plusk01 / gazebo-kill.md
Created August 25, 2017 15:58
Kill gazebo process quicker

To make Gazebo die sooner, edit the file at /opt/ros/kinetic/lib/python2.7/dist-packages/roslaunch/nodeprocess.py:

Near line 57, change the timeouts to be:

_TIMEOUT_SIGINT  = 0.5 #seconds
_TIMEOUT_SIGTERM = 0.5 #seconds

This will cause ROS to send a kill signal much sooner.

@plusk01
plusk01 / triple-boot-macos.md
Last active August 19, 2017 14:19
MacBook Pro 2012 10,1: macOS Sierra + Win10 + Ubuntu 16.04
@plusk01
plusk01 / beecore-stm32f3x-debug.md
Last active March 9, 2017 04:49
Programming/Debugging a Beecore STM32F3 board
@plusk01
plusk01 / yaml_to_latex.py
Last active February 22, 2017 00:57
Python script to convert OpenCV YAML to LaTeX bmatrix
#!/usr/bin/python
"""
I'm expecting a YAML file from OpenCV that looks like:
%YAML:1.0
K: !!opencv-matrix
rows: 3
cols: 3
dt: d