Skip to content

Instantly share code, notes, and snippets.

View edxmorgan's full-sized avatar
🚀
burdened with glorious purpose

edward.ix edxmorgan

🚀
burdened with glorious purpose
View GitHub Profile
@edxmorgan
edxmorgan / proxy.py
Last active December 19, 2023 04:37
Proxy integrated into selenium in scrapy middleware
# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import sys
import time
import logging
from scrapy import signals
from scrapy.mail import MailSender
from scrapy.utils.project import get_project_settings

I believe the article was originally written by fede.tft.

It appears they have copied source code to github and updated it for C++11: https://github.com/fedetft/serial-port

Introduction

The serial port protocol is one of the most long lived protocols currently in use. According to wikipedia, it has been standadized in 1969. First, a note: here we're talking about the RS232 serial protocol. This note is necessary because there are many other serial protocols, like SPI, I2C, CAN, and even USB and SATA.

Some time ago, when the Internet connections were done using a 56k modem, the serial port was the most common way of connecting a modem to a computer. Now that we have ADSL modems, the serial ports have disappeared from newer computers, but the protocol is still widely used.

In fact, most microcontrollers, even the newer ones have one or more peripherals capable of communicating using this protocol, and from the PC side, all operating system

#include <iostream>
#include <chrono>
#include "casadi/casadi.hpp"
using namespace std;
using namespace chrono;
using namespace casadi;
int main()
#include <iostream>
#include <chrono>
#include "casadi/casadi.hpp"
using namespace std;
using namespace chrono;
using namespace casadi;
int main()
{
@edxmorgan
edxmorgan / sym_eigen.py
Last active March 3, 2024 06:57
symbolic expression for eigenvalues and eigenvectors of a system implemented in casadi
def qr_eigen(A, iterations=100):
pQ = SX.eye(A.size1())
X = copy.deepcopy(A)
for _ in range(iterations):
Q, R = qr(X) # QR decomposition in CasADi
pQ = pQ @ Q # Update eigenvector matrix for next iteration
X = R @ Q # Update eigenvalue matrix for next iteration
return diag(X), pQ
# example usage for a symbolic SX square matrix in casadi
@edxmorgan
edxmorgan / signal_variance.py
Last active October 20, 2024 22:02
Evaluation of loss functions for discrete time systems with a pulse transfer function B(z)/A(z).
import numpy as np
from numpy.polynomial.polynomial import Polynomial
import copy
# Define the coefficients of the polynomials A(z) and B(z)
# Introduction to Stochastic Control theory. Karl J. Astrom example
# A_coeffs = [1, 0.7, 0.5, -0.3]
# B_coeffs = [1, 0.3, 0.2, 0.1]
@edxmorgan
edxmorgan / linearize.py
Last active October 20, 2024 22:02
linearize a nonlinear function in casadi
#method 0
def linearize_new(f, x, x0):
x_lim = SX.sym('x_lim', x.sparsity())
return substitute(f + jtimes(f, x, x_lim - x0), vertcat(x_lim, x), vertcat(x, x0))
f_linear_new = Function('f_linear_new', [x, x0, u, mu, I_sym], [linearize_new(x_next, x, x0)])
#method 1
F_next_linear = Function('Xnext_linear', [x, x0, u, mu, I_sym], [linearize(x_next, x, x0)])
@edxmorgan
edxmorgan / recursive_least_squares__casadi.py
Created March 21, 2024 01:57
a recursive least squares algorithm implemented for closed form in casadi
from casadi import SX, inv, Function, sqrt, jacobian, vertcat
import numpy as np
import matplotlib.pyplot as plt
#true value of the parameters that will be estimated
initialPosition = 10
acceleration = 5
initialVelocity= -2
#noise standard deviation
@edxmorgan
edxmorgan / lqg.py
Last active May 4, 2024 03:22
lqg for an inverted pendulum
"""
Inverted Pendulum LQG control
author: Edward Morgan
"""
import casadi as cs
import math
import time
from numpy.linalg import inv, eig
import matplotlib.pyplot as plt
@edxmorgan
edxmorgan / schur_qr.py
Last active October 30, 2024 17:30
QR decomposition
import copy
from casadi import *
import numpy as np
def qr_eigen(A, iterations=100):
pQ = SX.eye(A.size1())
X = copy.deepcopy(A)
for _ in range(iterations):
Q, R = qr(X) # QR decomposition in CasADi
pQ = pQ @ Q # Update eigenvector matrix for next iteration