-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.h
139 lines (119 loc) · 3.6 KB
/
common.h
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
#ifndef H_COMMON
#define H_COMMON
#define _us 1
#include <Arduino.h>
#define DEBUG 1
const char ADDRESS = '1';
const char MANUFACTURER[8] = { 'T', 'e', 's', 'l', 'a', ' ', ' ', ' ' };
const char MODEL[8] = { 'M', 'o', 'd', 'e', 'l', ' ', 'S', ' ' };
/**
* FFT Parameters
*/
#define LOG_OUT 0
#define FHT_N 128 // Numero samples
#define WINDOW 0
#define LIN_OUT 1
/**
* Sampling period, in microseconds.
*
* The sampling frequency must be high enough to be able to read the signal multiple times, and
* must be low enough to be able to read enough samples to see an entire period of the signal:
*
* · SAMPLING_FREQ >= 2 * SIGNAL_MAX_FREQ
* · 1/SAMPLING_FREQ * FFT_N >= 2 * 1/SIGNAL_MIN_FREQ
*
* In other terms, you must satisfy these two inequalities:
*
* · SAMPLING_FREQ >= 2 * SIGNAL_MAX_FREQ
* · SAMPLING_FREQ <= SIGNAL_MIN_FREQ * FFT_N / 2
*
*/
#define SAMPLING_PERIOD (470*_us)
/**
* Basic frequency in µs for the timer.
*/
#define TIMER_PERIOD (100*_us)
/*
* Unit: TIMER_PERIOD. These values MUST be even.
*
* Example of what happens with value 4:
*
* 0 ____ 1 ____ 2 ^^^^ 3 ^^^^ 0
*
* (between each number there is a delay of TIMER_PERIOD)
*/
const uint8_t LED1_PERIOD = 160;
const uint8_t LED2_PERIOD = 40;
const uint8_t LED3_PERIOD = 24;
const uint8_t LED4_PERIOD = 16;
const uint8_t LED5_PERIOD = 12;
const uint8_t LED_CCS_PERIOD = 10;
const uint16_t LED_TURN_PERIOD = 10000;
const uint8_t LED1_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED1_PERIOD * TIMER_PERIOD);
const uint8_t LED2_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED2_PERIOD * TIMER_PERIOD);
const uint8_t LED3_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED3_PERIOD * TIMER_PERIOD);
const uint8_t LED4_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED4_PERIOD * TIMER_PERIOD);
const uint8_t LED5_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED5_PERIOD * TIMER_PERIOD);
const uint8_t LED_CCS_BIN = (FHT_N * (uint16_t)SAMPLING_PERIOD) / (LED_CCS_PERIOD * TIMER_PERIOD);
/**
* Maximum execution time of a loop().
*
* Equivalent to:
* Maximum measured execution time of loop() without handleCCS()
* +
* max_time{FUN_ST_BEGIN, FUN_ST_WAIT_TO_BLINK, FUN_ST_BLINK, FUN_ST_INTERPRETATE}
*/
const uint16_t TIMESPAN_LOOP_MAX = 280 + 0;
typedef enum RequestedAction : char {
ERA_NONE = '0',
ERA_STRAIGHT = 'F',
ERA_TURN_LEFT = 'L',
ERA_TURN_RIGHT = 'R'
} RequestedAction;
typedef enum CurrentAction : char {
ECA_NONE = '0',
ECA_STRAIGHT = 'F',
ECA_TURN_LEFT = 'L',
ECA_TURN_RIGHT = 'R',
ECA_STILL = 'S',
} CurrentAction;
typedef struct RoadInfo {
char address;
unsigned long validUntil;
char manufacturer[8];
char model[8];
uint16_t orientation;
bool priority;
RequestedAction requestedAction;
CurrentAction currentAction;
} RoadInfo;
inline bool isValidRequestedAction(int val) {
return val == ERA_STRAIGHT || val == ERA_TURN_LEFT || val == ERA_TURN_RIGHT;
}
inline bool isValidCurrentAction(int val) {
return val == ECA_STRAIGHT || val == ECA_TURN_LEFT || val == ECA_TURN_RIGHT || val == ECA_STILL;
}
/**
* Action advertised by the car
*/
extern RequestedAction requestedAction;
/**
* The action agreed with the network
*/
extern CurrentAction currentAction;
/**
* Whether the car should be the first to act on the crossroad
*/
extern bool hasPriority;
/**
* Current model of the crossroad.
*
* Position 0: left road
* Position 1: front road
* Position 2: right road
*/
extern RoadInfo crossroad[3];
#if DEBUG
void __assert(bool success, String msg);
#endif
#endif // H_COMMON