1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-25 19:10:18 +02:00

tornado/fw/sim/: crude simulation of sensor behaviour and signal processing

This commit is contained in:
Werner Almesberger 2012-12-03 21:23:46 -03:00
parent 8c868198ed
commit bc00a4d979
3 changed files with 82 additions and 0 deletions

4
tornado/fw/sim/Makefile Normal file
View File

@ -0,0 +1,4 @@
CFLAGS = -g -Wall
LDLIBS = -lm
all: alg

71
tornado/fw/sim/alg.c Executable file
View File

@ -0,0 +1,71 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define G 28 /* 1 Earth gravity in accelerometer units */
#define MID 512 /* accelerometer middle value */
#define S 1000 /* sample rate (samples/s) */
#define F 2 /* rotational speed in Hz */
#define R 0.46 /* radius of accelerometer orbit */
#define H (G/2) /* hysteresis, in accelerometer units */
/* noise pattern: frequency, amplitude pairs */
static struct noise {
int f; /* frequency (in samples) */
int a; /* amplitude (in accelerometer units) */
} noise[] = {
{ 1, G/3 }, /* general noise */
{ S/F/10, 5*G }, /* hits caused by the chain "jumping" */
{ 0, }
};
static uint16_t sample(double t)
{
int fz = R*4*M_PI*M_PI*F*F/9.81*G;
int fg = cos(2*M_PI*F*t)*G;
int f = fz+fg;
const struct noise *n;
for (n = noise; n->f; n++)
if (!(random() % n->f))
f += random() % (2*n->a) - n->a;
return f+MID;
}
#define E_SHIFT 3 /* ~ 0.1 */
#define M_SHIFT 10 /* ~ 1/S */
int main(void)
{
uint16_t e = MID << E_SHIFT;
uint32_t m = MID << M_SHIFT;
int d;
int i;
bool up = 0;
for (i = 0; i != 10*S; i++) {
unsigned v = sample((double) i/S);
e = v+(e-(e >> E_SHIFT));
m = v+(m-(m >> M_SHIFT));
d = (e >> E_SHIFT)-(m >> M_SHIFT);
if (up) {
if (d < -H)
up = 0;
} else {
if (d > H)
up = 1;
}
printf("%d %d %d %d %d\n",
v, e >> E_SHIFT, m >> M_SHIFT, d, up);
}
return 0;
}

7
tornado/fw/sim/p Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
./alg >_out
gnuplot -persist <<EOF
set style data lines
plot "_out" using 1, "_out" using 2, "_out" using 3, "_out" using 4, \
"_out" using (100*\$5)
EOF