Skip to content

Instantly share code, notes, and snippets.

@nicklasfrahm
Created February 27, 2019 11:02
Show Gist options
  • Save nicklasfrahm/b2fe0c28eb807fe41541eda1e457cb83 to your computer and use it in GitHub Desktop.
Save nicklasfrahm/b2fe0c28eb807fe41541eda1e457cb83 to your computer and use it in GitHub Desktop.
A velocity model for a simple DC motor
#include <stdio.h>
#define ITERATIONS 10
typedef struct
{
double a;
double b;
} motor_props_t;
double voltage(int *it)
{
return 5;
}
double dc_motor(motor_props_t *motor_props, double (*vol)(int *), int *it)
{
if (*it == 0)
{
return 0;
}
double mem = 0;
for (unsigned int i = 0; i < *it; ++i)
{
mem = motor_props->a * mem + motor_props->b * vol(it);
}
return mem;
}
int main(void)
{
motor_props_t motor_props;
motor_props.a = 0.5;
motor_props.b = 1;
double results[ITERATIONS];
for (unsigned int i = 0; i < ITERATIONS; ++i)
{
results[i] = dc_motor(&motor_props, voltage, &i);
}
for (unsigned int i = 0; i < ITERATIONS; ++i)
{
printf("%d: ", i);
for (unsigned int j = 0; j < results[i]; ++j)
{
printf("—");
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment