Skip to content

Instantly share code, notes, and snippets.

@Divelix
Divelix / terminal_teleop.py
Created February 9, 2023 14:49
Preprocess terminal input without printing
# catches 'a', 'b', 'c' chars and Ctrl+C to stop program
import sys
import termios
import tty
from select import select
def getKey(settings, timeout):
tty.setraw(sys.stdin.fileno())
rlist, _, _ = select([sys.stdin], [], [], timeout)
key = sys.stdin.read(1) if rlist else ''
@Divelix
Divelix / timer1_ABC.cpp
Created August 4, 2022 12:53
Arduino MEGA Timers PWM
const uint8_t pin_pwm_a = 11; // OC1A
const uint8_t pin_pwm_b = 12; // OC1B
const uint8_t pin_pwm_c = 13; // OC1C (LED pin!!!)
const uint16_t pwm_period = 40; // microseconds (== 25 kHz PWM)
const uint16_t timer_resolution = pwm_period * 8; // PWM resolution
uint16_t input_value = 0;
void setup() {
Serial.begin(57600);
pinMode(pin_pwm_a, OUTPUT);
@Divelix
Divelix / machine.js
Last active September 3, 2021 14:46
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@Divelix
Divelix / threaded_client.cpp
Created May 5, 2021 10:41
Server: UDP transmit (JPEG from OpenCV) + TCP receive; Client: UDP receive + TCP transmit (input from terminal)
#include <iostream>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include "opencv2/opencv.hpp"
#define SERVER_IP "127.0.0.1"
@Divelix
Divelix / cv_client.cpp
Created April 30, 2021 15:52
Transmit and receive video from web camera as JPEG images via UDP and OpenCV on C++
#include <iostream>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include "opencv2/opencv.hpp"
using namespace cv;
#define SERVER_IP "127.0.0.1"
@Divelix
Divelix / tcp_client.cpp
Created April 29, 2021 13:42
C++ TCP/UDP echo client/server for Linux
#include <iostream>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 9999
#define BUFLEN 4096
@Divelix
Divelix / tcp_client.py
Last active April 29, 2021 13:43
Python TCP/UDP echo client/server + OpenCV video transfer via UDP
#!/usr/bin/env python3
from socket import *
server_address = ("localhost", 10000)
buff_size = 1024 # 1Kb
with socket(AF_INET, SOCK_STREAM) as client_socket:
print(f"Connecting to {server_address}")
client_socket.connect(server_address)
while True:
@Divelix
Divelix / plot_live_data.py
Created February 19, 2021 11:24
Plot live data in Python
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
x = []
y = []
def animate(i):
x.append(random.randint(0, 50))
y.append(random.randint(0, 50))
@Divelix
Divelix / optimization.m
Created June 5, 2019 03:22
Optimization methods in Matlab
%% Initialization
clear;clc;
% symbols
syms X Y Z real
F = 0.5*(1 - X)^2 + 0.5*(Y - X^2)^2; % Функция Розенблата
%F = sin(X) + sin(Y);
dFx = diff(F, X);
dFy = diff(F, Y);
dFxx = diff(dFx, X);
@Divelix
Divelix / gist:34d21234e1e50de2820bf5d49e220ac4
Last active November 15, 2019 10:54
Texture generation
fun createTexture(color: Color, circle: Boolean, w: Int, h: Int): TextureRegion {
val pixmap = Pixmap(w, h, Pixmap.Format.RGBA8888)
pixmap.setColor(color)
if (circle) {
pixmap.fillCircle(w/2, h/2, w/2)
} else {
pixmap.fill()
}
val textureRegion = TextureRegion(Texture(pixmap))
pixmap.dispose()