root / trunk / code / projects / fp_math / test / server_test.c @ 1602
History | View | Annotate | Download (1.2 KB)
1 |
|
---|---|
2 |
#include <math.h> |
3 |
#include <stdio.h> |
4 |
#include <stdlib.h> |
5 |
#include "serial.h" |
6 |
|
7 |
#define X_IN 0 |
8 |
#define Y_OUT 1 |
9 |
|
10 |
#define FP_COS 2 |
11 |
#define FP_SIN 3 |
12 |
#define FP_TAN 4 |
13 |
|
14 |
#define EXIT 5 |
15 |
|
16 |
typedef union fp_t { |
17 |
char d[4]; |
18 |
int v;
|
19 |
} fp_t; |
20 |
|
21 |
int main(int argc, char** argv) { |
22 |
|
23 |
int fd, i, n = 0; |
24 |
float input, output, error, avg_error, max_error;
|
25 |
fp_t fp_input, fp_output; |
26 |
|
27 |
|
28 |
fd = openport("/dev/ttyUSB0");
|
29 |
|
30 |
printf("Beginning test loop.");
|
31 |
|
32 |
while(1) { |
33 |
|
34 |
switch(getbyte(fd)) {
|
35 |
|
36 |
case X_IN:
|
37 |
for(i=0; i < 4; i++) |
38 |
fp_input.d[i] = getbyte(fd); |
39 |
|
40 |
input = ((float)fp_input.v) / 65536.; |
41 |
|
42 |
break;
|
43 |
|
44 |
case Y_OUT:
|
45 |
for(i=0; i < 4; i++) |
46 |
fp_output.d[i] = getbyte(fd); |
47 |
|
48 |
output = ((float)fp_output.v) / 65536.; |
49 |
|
50 |
break;
|
51 |
|
52 |
case FP_COS:
|
53 |
|
54 |
n++; |
55 |
printf("%f = cos(%f)\n", output, input);
|
56 |
error = abs(cos(input) - output); |
57 |
if(error > max_error) max_error = error;
|
58 |
avg_error = (avg_error * (n - 1) + error) / n;
|
59 |
|
60 |
break;
|
61 |
|
62 |
case FP_SIN:
|
63 |
|
64 |
break;
|
65 |
|
66 |
case FP_TAN:
|
67 |
|
68 |
break;
|
69 |
|
70 |
case EXIT:
|
71 |
|
72 |
printf("Average error: %f\n", avg_error);
|
73 |
printf("Max error: %f\n", max_error);
|
74 |
exit(EXIT_SUCCESS); |
75 |
|
76 |
break;
|
77 |
|
78 |
default: break; |
79 |
printf("Uh oh - defauled\n");
|
80 |
|
81 |
break;
|
82 |
|
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
|