-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcan_servizi.cpp
More file actions
261 lines (227 loc) · 7.98 KB
/
can_servizi.cpp
File metadata and controls
261 lines (227 loc) · 7.98 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
/**
* @file can_servizi.cpp
* @author Arella Matteo <br/>
* (mail: arella.1646983@studenti.uniroma1.it)
* @date 2018
* @brief CAN servizi module implementation
*/
#include "can_servizi.h"
#include <due_can.h>
#include <DueTimer.h>
/** @addtogroup CAN_servizi_group
*
* @{
*/
/**
* @var volatile bool can_serv_initialized;
* @brief CAN servizi initialization status flag (true if initialized)
*/
volatile bool can_serv_initialized = false;
/**
* @var volatile bool SCU_F_online;
* @brief Frontal SCU online status flag (true if online)
*/
volatile bool SCU_F_online = false;
/**
* @var volatile bool TCS_online;
* @brief TCS online status flag (true if online)
*/
volatile bool TCS_online = false;
/**
* @var volatile uint32_t curr_pedals_seq_num;
* @brief Frontal SCU PDOtx1 current sequence number
*/
volatile uint32_t curr_pedals_seq_num = 0;
/**
* @var volatile uint32_t next_pedals_seq_num;
* @brief Frontal SCU PDOtx1 next sequence number
*/
volatile uint32_t next_pedals_seq_num = 0;
/**
* @var volatile uint8_t tps1_percentage;
* @brief First APPS percentage value retrieved by frontal SCU node
*/
volatile uint8_t tps1_percentage = 0;
/**
* @var volatile uint8_t tps2_percentage;
* @brief Second APPS percentage value retrieved by frontal SCU node
*/
volatile uint8_t tps2_percentage = 0;
/**
* @var volatile uint8_t brake_percentage;
* @brief Brake pedal position sensor percentage value retrieved by frontal SCU node
*/
volatile uint8_t brake_percentage = 0;
/**
* @var volatile bool apps_plausibility;
* @brief APPS plausibility status retrieved by frontal SCU node
*/
volatile bool apps_plausibility = true;
/**
* @var volatile bool brake_plausibility;
* @brief Brake plausibility status retrieved by frontal SCU node
*/
volatile bool brake_plausibility = true;
/**
* @var volatile uint8_t tcs_coefficient;
* @brief torque limiter percentage retrieved by TCU node
*/
volatile uint8_t tcs_coefficient = 0;
/**
* @brief This function is executed periodically after CAN servizi
* 'go Operational' NMT request is sent.
* When timeout occurs if @ref next_pedals_seq_num is greater than @ref curr_pedals_seq_num
* then frontal SCU is considered active, viceversa it is considered offline.
*
* @author Arella Matteo <br/>
* (mail: arella.1646983@studenti.uniroma1.it)
*/
void timeout() {
if (next_pedals_seq_num > curr_pedals_seq_num) {
SCU_F_online = true;
curr_pedals_seq_num = next_pedals_seq_num;
} else
SCU_F_online = false;
}
__attribute__((__inline__))
volatile bool can_servizi_initialized() {
return can_serv_initialized;
}
/**
* @brief This function manage boot-up messages sent over CAN servizi network
* by slave nodes.
*
* @author Arella Matteo <br/>
* (mail: arella.1646983@studenti.uniroma1.it)
*
* @param[in] frame CAN frame received from CAN servizi port
*/
void CAN_SERV_BOOTUP_CB(CAN_FRAME* frame) {
if (frame -> id == 0x700 + SCU_FRONTAL_NODE_ID)
SCU_F_online = true;
else if (frame -> id == 0x700 + TCU_NODE_ID)
TCS_online = true;
}
/**
* @brief This function manage PDOs received over CAN servizi network and
* deserializes data:
* <TABLE>
* <TR>
* <TD><b>TPDO num</b></TD><TD><b>NODE-ID</b></TD><TD><b>Length</b></TD><TD><b>Data</b></TD>
* </TR>
* <TR>
* <TD rowspan="5">1</TD><TD rowspan="5">#SCU_FRONTAL_NODE_ID</TD><TD rowspan="5">4</TD>
* <TD>APPS1 percentage</TD>
* <TR><TD>APPS2 percentage</TD></TR>
* <TR><TD>Brake percentage</TD></TR>
* <TR><TD>APPS plausibility</TD></TR>
* <TR><TD>BRAKE plausibility</TD></TR>
* </TR>
* <TR><TD>1</TD><TD>#TCU_NODE_ID</TD><TD>1</TD><TD>TCU torque limiter</TD></TR>
* </TABLE>
*
* When PDOtx1 message is received from frontal SCU node then
* @ref next_pedals_seq_num is incremented for keep track of last
* pedals message received.
*
* @author Arella Matteo <br/>
* (mail: arella.1646983@studenti.uniroma1.it)
*
* @param[in] frame CAN frame received from CAN servizi port
*/
void CAN_SERV_GENERAL_CB(CAN_FRAME* frame) {
switch (frame -> id) {
case (0x180 + SCU_FRONTAL_NODE_ID): // (pedals)
tps1_percentage = (uint8_t) frame -> data.byte[0];
tps2_percentage = (uint8_t) frame -> data.byte[1];
brake_percentage = (uint8_t) frame -> data.byte[2];
apps_plausibility = (frame -> data.byte[3] & 0xF0) ? true : false;
brake_plausibility = (frame -> data.byte[3] & 0x0F) ? true : false;
// fault check
next_pedals_seq_num++; // received new frame
break;
case (0x180 + TCU_NODE_ID): // TCU coefficient limiter
tcs_coefficient = (uint8_t) frame -> data.byte[0];
break;
default: {}
}
}
/**
* @brief This function initialize CAN servizi hardware port with baudrate
* #CAN_SERV_BAUDRATE.
* Mailbox 0 is configured for receiving boot-up messages from CAN
* servizi slave nodes (filter = 0x00000700, mask = 0x1FFFFF80);
* remaining mailboxes are configured for receiving TPDOs from CAN
* servizi slave nodes (filter = 0x00000080, mask = 0x1FFFFC80).
*
* @author Arella Matteo <br/>
* (mail: arella.1646983@studenti.uniroma1.it)
*
* @retval true CAN servizi initialized
* @retval false CAN servizi not initialized
*/
bool can_servizi_init() {
if (!CAN_SERVIZI.begin(CAN_SERV_BAUDRATE))
return (can_serv_initialized = false);
uint32_t i;
// set mailbox 0 for receiving boot-up frames
// FILTER = 00000000000000000011100000000,
// MASK = 11111111111111111111110000000
CAN_SERVIZI.setRXFilter(0, 0x00000700, 0x1FFFFF80, false);
CAN_SERVIZI.setCallback(0, CAN_SERV_BOOTUP_CB);
// set remaining mailboxes for receiving TPDOs
// FILTER = 00000000000000000000010000000,
// MASK = 11111111111111111110010000000
for (i = 2; i < 8; i++) {
CAN_SERVIZI.setRXFilter(i, 0x00000080, 0x1FFFFC80, false);
CAN_SERVIZI.setCallback(i, CAN_SERV_GENERAL_CB);
}
return (can_serv_initialized = true);
}
__attribute__((__inline__))
void can_servizi_go_operational() {
// broadcast NMT request go Operational
CAN_FRAME output;
output.id = 0x00000000 ;
output.length = 2 ;
output.data.byte[0] = 0x01;
output.data.byte[1] = 0x00;
output.extended = 0;
CAN_SERVIZI.sendFrame(output);
DueTimer::getAvailable().attachInterrupt(timeout).start(CAN_SERVIZI_TIMEOUT_PERIOD);
}
__attribute__((__inline__))
volatile bool can_servizi_online() {
return (can_serv_initialized && SCU_F_online);
}
__attribute__((__inline__))
volatile bool tcs_online() {
return (can_serv_initialized && TCS_online);
}
__attribute__((__inline__))
volatile uint8_t get_servizi_tps1() {
return tps1_percentage;
}
__attribute__((__inline__))
volatile uint8_t get_servizi_tps2() {
return tps2_percentage;
}
__attribute__((__inline__))
volatile uint8_t get_servizi_brake() {
return brake_percentage;
}
__attribute__((__inline__))
volatile bool get_servizi_apps_plausibility() {
return apps_plausibility;
}
__attribute__((__inline__))
volatile bool get_servizi_brake_plausibility() {
return brake_plausibility;
}
__attribute__((__inline__))
volatile uint8_t get_tcs_torque_coefficient() {
return TCS_online ? tcs_coefficient : 100;
}
/**
* @}
*/