-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
183 lines (169 loc) · 3.86 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "CyberryPotter.h"
#include "weights.h"
#include "nnom.h"
extern Cyberry_Potter_Status_Typedef Cyberry_Potter_Status;
//extern uint16_t IMU_Data_ACC[3*IMU_SEQUENCE_LENGTH_MAX];
//extern float IMU_Data_ACC[3*IMU_SEQUENCE_LENGTH_MAX];
extern float IMU_Data_mAngle[IMU_SEQUENCE_LENGTH_MAX][3];
extern float IMU_Data_mAcc[IMU_SEQUENCE_LENGTH_MAX][3];
int8_t model_output = -1;
#define SCALE (pow(2,INPUT_1_OUTPUT_DEC))
typedef enum eModel_Output{
Unrecognized = -1,
RightAngle = 0,
SharpAngle = 1,
Lightning = 2,
Triangle = 3,
Letter_h = 4,
letter_R = 5,
letter_W = 6,
letter_phi = 7,
Circle = 8,
UpAndDown = 9,
Horn = 10,
Wave = 11,
NoMotion = 12
}eModel_Output;
#ifdef NNOM_USING_STATIC_MEMORY
uint8_t static_buf[1024 * 8];
#endif //NNOM_USING_STATIC_MEMORY
void model_feed_data(void)
{
const double scale = SCALE;
uint16_t i = 0;
for(i = 0; i < IMU_SEQUENCE_LENGTH_MAX;i++){
nnom_input_data[i*3] = (int8_t)round(IMU_Data_mAcc[i][0] * scale);
nnom_input_data[i*3+1] = (int8_t)round(IMU_Data_mAcc[i][1] * scale);
nnom_input_data[i*3+2] = (int8_t)round(IMU_Data_mAcc[i][2] * scale);
}
}
int8_t model_get_output(void)
{
uint8_t i = 0;
int8_t max_output = -128;
int8_t ret = 0;
for(i = 0; i < 13;i++){
printf("Output[%d] = %.2f %%\n",i,(nnom_output_data[i] / 127.0)*100);
if(nnom_output_data[i] >= max_output){
max_output = nnom_output_data[i] ;
ret = i;
}
}
if(max_output >= OUPUT_THRESHOLD){
ret = ret;
}
else{
ret = -1;
}
switch(ret){
case Unrecognized:
printf("Unrecognized");
break;
case RightAngle:
printf("RightAngle");
break;
case SharpAngle:
printf("SharpAngle");
break;
case Lightning:
printf("Lightning");
break;
case Triangle:
printf("Triangle");
break;
case Letter_h:
printf("Letter_h");
break;
case letter_R:
printf("Letter_R");
break;
case letter_W:
printf("Letter_W");
break;
case letter_phi:
printf("Letter_phi");
break;
case Circle:
printf("Circle");
break;
case UpAndDown:
printf("UpAndDown");
break;
case Horn:
printf("Horn");
break;
case Wave:
printf("Wave");
break;
case NoMotion:
printf("Unrecognized");
break;
}
printf("\n");
return ret;
}
void Model_LED(int8_t model_output)
{
switch(model_output){
case RightAngle:
GPIO_WriteBit(GPIOA,GPIO_Pin_6,Bit_SET);
break;
case SharpAngle:
GPIO_WriteBit(GPIOA,GPIO_Pin_6,Bit_RESET);
break;
case Lightning:
GPIO_WriteBit(GPIOA,GPIO_Pin_5,Bit_SET);
break;
case Triangle:
GPIO_WriteBit(GPIOA,GPIO_Pin_5,Bit_RESET);
break;
case Letter_h:
GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_SET);
break;
case letter_R:
GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_RESET);
break;
case letter_W:
GPIO_WriteBit(GPIOA,GPIO_Pin_3,Bit_SET);
break;
case letter_phi:
GPIO_WriteBit(GPIOA,GPIO_Pin_3,Bit_RESET);
break;
}
}
int main(void)
{
System_Init();
printf("Hello!\n");
GPIO_WriteBit(GPIOA,GPIO_Pin_7,Bit_SET);
#ifdef NNOM_USING_STATIC_MEMORY
nnom_set_static_buf(static_buf, sizeof(static_buf));
#endif //NNOM_USING_STATIC_MEMORY
nnom_model_t* model;
model = nnom_model_create();
while(1){
if(Cyberry_Potter_Status.Button_Status == BUTTON_HOLD && Cyberry_Potter_Status.IMU_Status == IMU_Idle){
IMU_Sample_Start();
//printf("HOLD\n");
}
else if(Cyberry_Potter_Status.Button_Status != BUTTON_IDLE){
Cyberry_Potter_Status.Button_Status = BUTTON_IDLE;
}
if(Cyberry_Potter_Status.IMU_Status == IMU_Sampled){
LED_ON;
#ifdef SYSTEM_MODE_DATA_COLLECT
Delay_ms(200);
IMU_Data_Print();
#endif //SYSTEM_MODE_DATA_COLLECT
#ifndef SYSTEM_MODE_DATA_COLLECT
model_feed_data();
model_run(model);
model_output = model_get_output();
printf("%d",model_output);
Model_LED(model_output);
#endif //SYSTEM_MODE_DATA_COLLECT
Cyberry_Potter_Status.IMU_Status = IMU_Idle;
EXTI_Restore();
}
}
}