Skip to content

Instantly share code, notes, and snippets.

View Btremaine's full-sized avatar

Brian Btremaine

View GitHub Profile
@Btremaine
Btremaine / prbs_data9.m
Created July 13, 2022 16:06
Sample data for system ID test
% data file loaded by prbs_scr.m
% speed filter not in feedback, but in forward loop at input to PI
% control.
% btremaine 2022
U = [
-5 -3861
-5 -3878
-5 -3911
5 -3949
@Btremaine
Btremaine / prbs_scr.m
Last active July 13, 2022 16:07
Matlab m-file to process system ID data
%% prbs.m -- analysis of bode using u(k) and y(k)
% load data, should be >> 2^15 values so transient dies out
% btremaine 2022
% run m file to load data (prbs_data7, 8, 9
prbs_data9
% grab last 32767 data points
N= length(U);
@Btremaine
Btremaine / STM32G431 code for System ID
Created June 25, 2022 21:46
Code added to STM32 project to add system ID to BLDC Motor Control
/* Run the speed control loop ================================ */
/* this code part of speed_torq_cntrl.c */
/* Compute speed error */
hTargetSpeed = ( int16_t )( wCurrentReference / 65536 );
// Uses Moving Average Filter updated every PWM period
// and averaged for xxxx samples
hMeasuredSpeed = SPD_GetAvrgMecSpeedUnit( pHandle->SPD );
/* system ID */
@Btremaine
Btremaine / prbs.c
Last active August 22, 2021 19:55
prbs C code Example 1
// replicated from wikipeda, https://en.wikipedia.org/wiki/Pseudorandom_binary_sequence
// Example of PRBS7
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
uint8_t start = 0x02;
uint8_t a = start;
int i;
@Btremaine
Btremaine / prbs C code Example 2
Last active June 25, 2022 20:29
prbs C code to generate newbit each call every Ts
Prbs_t is a struct used in the prbs function:
typedef struct
{
uint16_t start; // start LFSR value
uint16_t lfsr; // LFSR current register
uint16_t index; // LFSR index
uint16_t newbit; // current newbit
} Prbs_t;
/**
@Btremaine
Btremaine / prbs Python
Last active November 13, 2022 20:19
PRBS code in Python
# TremaineConsultingGroup
# Brian Tremaine
# prbs.py
# August 11, 2021
# Prototype PRBS generator
#
# PRBS15 = x^15 + x^14 + 1 Wikipeda
# .--->X-------------------------------------------------------->
%% System ID Example
% Aug 5, 2021
% Uses MATLAB System ID Toolbox
%% Generate u(k)
Band= [0 1];
Range= [-1 1]; % amplitude limits
Ts=5E-3;
N=2^16-1; % # points
%
@Btremaine
Btremaine / main.c
Created July 31, 2020 20:24
phase detector blog main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
@Btremaine
Btremaine / stm32f3xx_it.c
Last active July 31, 2020 20:27
phase_detector blog interrupt handlers
/**
* @brief This function handles DMA1 channel1 global interrupt.
*/
void DMA1_Channel1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
/* USER CODE END DMA1_Channel1_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_tim4_ch1);
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */