Created
February 27, 2019 11:02
-
-
Save nicklasfrahm/b2fe0c28eb807fe41541eda1e457cb83 to your computer and use it in GitHub Desktop.
A velocity model for a simple DC motor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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