-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.cpp
More file actions
564 lines (475 loc) · 10.7 KB
/
Algorithm.cpp
File metadata and controls
564 lines (475 loc) · 10.7 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#define pi 3.14
#define signal_len 320
#define counts_per_period 64
#define input_len 10
#define MAX_VAL 15
#define MIN_VAL 0
#define ONE_V_VAL 3
char Signal[signal_len] = {0}; //signal buffer
int F = 10; //frequency in Hz
float T = 1/(float)F; //Period in sec
int A = 5; //amplitude
float B = 0;//10;//0; //Amplitude of Pseudo-random flat noise
int C = 0; //Number of positive bumps per period T with amplitude 1 V.
int bump_idx; //input from serial monitor
char cur_mode = 1;
int j = 0;
void set_timer_1hz(); //64hz TIMER_1 setup,
void set_timer_10hz(); //640hz TIMER_1 setup, 10hz period
void set_timer_50hz(); //3200hz TIMER_1 setup, 50hz period
void set_timer();
char get_input(); //get input from user
float get_num_input();
void get_menu(); //print menu
void function_options(char val); //select function
void sin_wave();
void saw_wave();
void dump_cos_wave();
void set_wave();
void set_amp();
void set_period();
void set_freq();
void set_B_noise();
void set_bumps();
float calc_noise();
int calc_bumps(int idx);
void print_info();
char chk_sig(float val);
void setup() {
Serial.begin(115200); // initalize the serial connection
DDRB = B00001111; // sets Arduino pins 8 to 11 as outputs
randomSeed(analogRead(0));
set_wave();//SETS SIN
}
ISR(TIMER1_COMPA_vect) {
if (j > (signal_len - 1))
j = 0;
PORTB = (Signal[j++]);
}
void loop() {
function_options(get_input());
}
void get_menu()
{
Serial.println("************************** Signal Generator **************************");
Serial.println(" Please select an option");
Serial.println("1 ---> Sin wave");
Serial.println("2 ---> Sawtooth wave");
Serial.println("3 ---> Dumped cosine wave");
Serial.println("a ---> change amplitude");
Serial.println("t ---> change period time");
Serial.println("f ---> change frequency");
Serial.println("b ---> adding Pseudo-random flat noise (0-0.5[v])");
Serial.println("c ---> adding positive bumps per period (with amplitude 1[v]) in range of(0-5)");
}
void print_info()
{
Serial.println("__________________________________________________");
switch (cur_mode)
{
case '1':
Serial.println(" SIN WAVE");
break;
case '2':
Serial.println(" SAWTOOTH WAVE");
break;
case '3':
Serial.println(" DUMPED COSINE WAVE");
break;
default:
break;
}
Serial.print("Amplitude: ");
Serial.print(A);
Serial.println(" [v]");
Serial.print("Frequency: ");
Serial.print(F);
Serial.println(" [Hz]");
Serial.print("Time period: ");
Serial.print(T);
Serial.println(" [sec]");
Serial.print("Pseudo-random flat noise: ");
Serial.print(B);
Serial.println(" [v]");
Serial.print("Positive bumps per period: ");
Serial.println(C);
Serial.println("__________________________________________________");
}
void set_timer_1hz()
{
// TIMER 1 for interrupt frequency 64 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 64 Hz increments
OCR1A = 31249; // = 16000000 / (8 * 64) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 8 prescaler
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
}
void set_timer_10hz()
{
// TIMER 1 for interrupt frequency 640 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 640 Hz increments
OCR1A = 24999; // = 16000000 / (1 * 640) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 1 prescaler
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
}
void set_timer_50hz()
{
// TIMER 1 for interrupt frequency 3200 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 3200 Hz increments
OCR1A = 4999; // = 16000000 / (1 * 3200) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 1 prescaler
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
}
void set_timer()
{
switch(F)
{
case 1:
set_timer_1hz();
break;
case 10:
set_timer_10hz();
break;
case 50:
set_timer_50hz();
break;
default:
break;
}
}
char get_input()
{
char value[input_len] = {0};
int i = 0;
while (!Serial.available()); //wait for input
while (Serial.available() > 0)
{
value[i++] = Serial.read(); // read 1 character from serial monitor, store it in variable c
}
return value[0];
}
void function_options(char val)
{
switch (val)
{
case '1':
cur_mode = '1';
sin_wave();
break;
case '2':
cur_mode = '2';
saw_wave();
break;
case '3':
cur_mode = '3';
dump_cos_wave();
break;
case 'a':
set_amp();
break;
case 't':
set_period();
break;
case 'f':
set_freq();
break;
case 'b':
set_B_noise();
break;
case 'c':
set_bumps();
break;
default:
get_menu();
//functoin_option(get_input()); //TODO
break;
}
}
void sin_wave()
{
cli(); // stop any interrupts
int n = 0;
float x;
float s_n;
for (int i = 0; i < counts_per_period; i++)
{
x = (float)i/counts_per_period;
s_n = (1 + sin(2.0 * pi *x))*1.6* A+ calc_noise() + calc_bumps(i);
s_n = chk_sig(s_n);
Signal[n] = s_n;
Signal[n + counts_per_period * 1] = s_n;
Signal[n + counts_per_period * 2] = s_n;
Signal[n + counts_per_period * 3] = s_n;
Signal[n + counts_per_period * 4] = s_n;
n++;
//Serial.println("");
}
print_info();
set_timer();
}
void saw_wave()
{
cli(); // stop any interrupts
int n=0;
float x;
float sw_n;
for (int i = 0;i < MAX_VAL +1 ;i++)
{
for(int k = 0; k < 4 ; k++)
{
sw_n = i* (A/5.0) + calc_noise() + calc_bumps(i);
sw_n = chk_sig(sw_n);
//Serial.println(sw_n);
Signal[n] = sw_n;
Signal[n + counts_per_period * 1] = sw_n;
Signal[n + counts_per_period * 2] = sw_n;
Signal[n + counts_per_period * 3] = sw_n;
Signal[n + counts_per_period * 4] = sw_n;
//Serial.println(""); //tinkercard error
n++;
}
}
print_info();
set_timer();
}
void dump_cos_wave()
{
cli(); // stop any interrupts
int n = 0;
float x;
float s_n;
for (int i = 0; i < signal_len; i++)
{
x =(float)i/counts_per_period;
s_n = A *1.6* exp(-x /3.0 ) * (1 + cos( 2.0 * pi * x))+ calc_noise() + calc_bumps(i);
Signal[n] = chk_sig(s_n);
//Serial.println(s_n);
n++;
}
print_info();
set_timer();
}
void set_wave()
{
switch(cur_mode)
{
case '1':
sin_wave();
break;
case '2':
saw_wave();
break;
case '3':
dump_cos_wave();
break;
default:
sin_wave();
break;
}
}
void set_amp()
{
Serial.println("please set amplitude 0-5[v]");
float tmp = get_num_input();
if (tmp < 0 || tmp > 5)
Serial.println("amplitude can be 0-5[V]");
else
A = tmp;
set_wave();
}
void set_period()
{
Serial.println(".....Please select the signal period: 1sec/0.1sec/0.02sec.....");
Serial.println("1 ---> 1sec");
Serial.println("2 ---> 0.1sec");
Serial.println("3 ---> 0.02sec");
char period = get_input();
Serial.println(period);
if(period == '1')
{
T = 1;
F = 1;
set_wave();
}
else if(period == '2')
{
T = 0.1;
F = 10;
set_wave();
}
else if(period == '3')
{
T = 0.02;
F = 50;
set_wave();
}
else
{
Serial.println("Period llegale values are: 1sec/0.1sec/0.02sec");
}
}
void set_freq()
{
Serial.println(".....Please select the signal Frequency: 1Hz/10Hz/50Hz.....");
Serial.println("1 ---> 1Hz");
Serial.println("2 ---> 10Hz");
Serial.println("3 ---> 50Hz");
char period = get_input();
Serial.println(period);
if(period == '1')
{
T = 1;
F = 1;
set_wave();
}
else if(period == '2')
{
T = 0.1;
F = 10;
set_wave();
}
else if(period == '3')
{
T = 0.02;
F = 50;
set_wave();
}
else
{
Serial.println("Frequency llegale values are: 1Hz/10Hz/50Hz");
}
}
void set_B_noise() //Changed it from void to float..
{
Serial.println("Please enter a value between 0 to 0.5");
float value = get_num_input();
if(value >= 0.0 && value <= 0.5)
{
B = value;
Serial.println(B);
set_wave();
}
else
{
Serial.println("Invalid input");
B=0;
}
}
void set_bumps()
{
Serial.println("Please set num bumps in the range of 0-5");
int tmp = get_num_input();
if (tmp < 0 || tmp > 5)
Serial.println("bumps must be between 0-5");
else
{
C = tmp;
bump_idx = (counts_per_period/C);
set_wave();
}
}
int calc_bumps(int idx)
{
if(C)
{
if((idx + 1)% bump_idx == 0)
return ONE_V_VAL;
}
return 0;
}
char chk_sig(float val)
{
if (val > MAX_VAL)
return MAX_VAL;
else if (val < MIN_VAL)
return MIN_VAL;
else
return (char)val;
}
float get_num_input()
{
char c[10] = {0};
int i = 0 , k = 0;
float tmp = 0;
while (!Serial.available());
while (Serial.available() > 0 )
{
c[i++] = Serial.read();
delay(5);
}
while (c[k] != 0)
{
if (c[k] <= '9' && c[k] >= '0')
{
tmp *= 10;
tmp += (c[k] - '0');
k++;
}
else if (c[k] == '.')
{
i = k;
k++;
while (c[k] != 0)
{
if (c[k] <= '9' && c[k] >= '0')
{
float g = c[k] - '0';
g /= pow(10, k - i);
tmp += g;
k++;
}
else
{
Serial.println("only numbers!");
return 0;
}
}
return tmp;
}
else
{
Serial.println("only numbers!");
return 0;
}
}
return tmp;
}
float calc_noise()
{
float temp;
if(B!=0)
{
temp = B*4;
temp = random(temp + 1);
}
else
temp = 0.0;
//Serial.println(temp);
return temp;
}