Compare commits
No commits in common. "d87abd3ec0a5ff691a66c71bea19ee18ff4c03fd" and "45ef1c42718f19577f839f9a33cb0db4d8f06f8a" have entirely different histories.
d87abd3ec0
...
45ef1c4271
6 changed files with 4 additions and 188 deletions
|
|
@ -1,8 +1,8 @@
|
|||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ColumnLimit: 120
|
||||
IndentWidth: 4 # Use 4 spaces for indentation
|
||||
TabWidth: 4 # Tab width is also 4 spaces
|
||||
UseTab: Never # Always use spaces instead of tabs
|
||||
ColumnLimit: 120 # Wrap lines after 80 characters
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
CC ?= gcc
|
||||
CFLAGS ?= -Wall -O2
|
||||
|
||||
TARGET = main.elf
|
||||
SRC = message.c main_demo.c
|
||||
|
||||
$(TARGET): $(SRC)
|
||||
@echo CC $@
|
||||
@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define N 8
|
||||
|
||||
uint16_t buffer[N];
|
||||
uint8_t idx = 0;
|
||||
|
||||
/*
|
||||
* Pros: Simple and smooths well.
|
||||
* Cons: Lag, slow to respond to real changes.
|
||||
*/
|
||||
uint16_t moving_average(uint16_t new_sample) {
|
||||
buffer[idx++] = new_sample;
|
||||
if (idx >= N)
|
||||
idx = 0;
|
||||
|
||||
uint32_t sum = 0;
|
||||
for (int i = 0; i < N; i++) sum += buffer[i];
|
||||
return sum / N;
|
||||
}
|
||||
|
||||
float alpha = 0.1f; // Between 0 (more smoothing) and 1 (less)
|
||||
float filtered = 0;
|
||||
|
||||
/*
|
||||
* Pros: Very lightweight, tunable.
|
||||
* Cons: Might not remove all burst noise
|
||||
*/
|
||||
uint16_t low_pass_filter(uint16_t new_sample) {
|
||||
filtered = alpha * new_sample + (1 - alpha) * filtered;
|
||||
return (uint16_t)filtered;
|
||||
}
|
||||
|
||||
int compare_uint16(const void *a, const void *b) {
|
||||
return (*(uint16_t *)a - *(uint16_t *)b);
|
||||
}
|
||||
|
||||
uint16_t median_filter(uint16_t new_sample) {
|
||||
static uint16_t window[N];
|
||||
static uint8_t index = 0;
|
||||
window[index++] = new_sample;
|
||||
if (index >= N)
|
||||
index = 0;
|
||||
|
||||
// Copy and sort
|
||||
uint16_t sorted[N];
|
||||
memcpy(sorted, window, sizeof(sorted));
|
||||
qsort(sorted, N, sizeof(uint16_t), compare_uint16);
|
||||
return sorted[N / 2];
|
||||
}
|
||||
|
||||
#undef N
|
||||
#define N 5
|
||||
float coeffs[N] = {0.1, 0.15, 0.5, 0.15, 0.1}; // Symmetric LPF
|
||||
float buffer[N] = {0};
|
||||
int index = 0;
|
||||
|
||||
float fir_filter(float sample) {
|
||||
buffer[index] = sample;
|
||||
float result = 0;
|
||||
int i, j = index;
|
||||
for (i = 0; i < N; i++) {
|
||||
result += coeffs[i] * buffer[j];
|
||||
j = (j - 1 + N) % N;
|
||||
}
|
||||
index = (index + 1) % N;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
#include <lauxlib.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct EscConfig {
|
||||
int maxspeed;
|
||||
float odometer;
|
||||
bool started;
|
||||
};
|
||||
|
||||
static struct EscConfig esc_config = {
|
||||
.maxspeed = 100,
|
||||
.odometer = 12.5f,
|
||||
.started = false,
|
||||
};
|
||||
|
||||
/* Lua: get_esc_config() -> table */
|
||||
static int l_get_esc_config(lua_State *L) {
|
||||
lua_newtable(L);
|
||||
|
||||
lua_pushstring(L, "maxspeed");
|
||||
lua_pushinteger(L, esc_config.maxspeed);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L, "odometer");
|
||||
lua_pushnumber(L, esc_config.odometer);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L, "started");
|
||||
lua_pushboolean(L, esc_config.started);
|
||||
lua_settable(L, -3);
|
||||
|
||||
return 1; // return the table
|
||||
}
|
||||
|
||||
/* Lua: set_maxspeed(value) */
|
||||
static int l_set_maxspeed(lua_State *L) {
|
||||
int maxspeed = luaL_checkinteger(L, 1);
|
||||
esc_config.maxspeed = maxspeed;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Lua: set_started(value) */
|
||||
static int l_set_started(lua_State *L) {
|
||||
int started = lua_toboolean(L, 1);
|
||||
esc_config.started = started;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Lua: add_odometer(delta) */
|
||||
static int l_add_odometer(lua_State *L) {
|
||||
float delta = luaL_checknumber(L, 1);
|
||||
esc_config.odometer += delta;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg esc_funcs[] = {
|
||||
{"get_esc_config", l_get_esc_config},
|
||||
{"set_maxspeed", l_set_maxspeed},
|
||||
{"set_started", l_set_started},
|
||||
{"add_odometer", l_add_odometer},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
/* Register the functions into Lua as global "esc" */
|
||||
void register_esc(lua_State *L) {
|
||||
luaL_newlib(L, esc_funcs); // create table with all funcs
|
||||
lua_setglobal(L, "esc"); // set global "esc"
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
#pragma once
|
||||
#include <lua.h>
|
||||
|
||||
void register_esc(lua_State *L);
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
---@meta
|
||||
|
||||
---@class EscConfig
|
||||
---@field maxspeed integer
|
||||
---@field odometer number
|
||||
---@field started boolean
|
||||
|
||||
---@class esc
|
||||
esc = {}
|
||||
|
||||
---Get current ESC config
|
||||
---@return EscConfig
|
||||
function esc.get_esc_config() end
|
||||
|
||||
---Set maximum speed
|
||||
---@param value integer
|
||||
function esc.set_maxspeed(value) end
|
||||
|
||||
---Set started flag
|
||||
---@param value boolean
|
||||
function esc.set_started(value) end
|
||||
|
||||
---Add to odometer
|
||||
---@param delta number
|
||||
function esc.add_odometer(delta) end
|
||||
|
||||
return esc
|
||||
Loading…
Add table
Add a link
Reference in a new issue