-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanMessageHandler.cpp
More file actions
152 lines (122 loc) · 5.26 KB
/
CanMessageHandler.cpp
File metadata and controls
152 lines (122 loc) · 5.26 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
/****************************************************************************************
*
* File:
* CanMessageHandler.cpp
*
* Purpose:
* The purpose of this class is a unified use of CanMsg handling
* from both Arduino and RPI
*
* Developer Notes:
* There is only 7 bytes of data that can be encoded by using this class,
* because the last byte of the CanMsg is reserved for an error message.
*
* TODO: implement the use of bitsets for the actuators (already done but commented in the files), and
* modify the CANbus files to delete the conversion steps from int8_t[8] to bitset<64> on the
* arduino scripts and raspberry files (hardware nodes mostly)
* TODO: update the error handling for all the messages
*
***************************************************************************************/
#include "CanMessageHandler.h"
CanMessageHandler::CanMessageHandler(CanMsg message) : m_message(message){
}
CanMessageHandler::CanMessageHandler(uint32_t messageId) {
m_message.id = messageId;
m_message.header.ide = 0;
m_message.header.length = 8;
for(auto& byteData : m_message.data) {
byteData = 0;
}
m_message.data[INDEX_ERROR_CODE] = NO_ERRORS;
}
uint32_t CanMessageHandler::getMessageId() {
return m_message.id;
}
CanMsg CanMessageHandler::getMessage() {
return m_message;
}
std::bitset<64> CanMessageHandler::getMessageInBitset() {
return m_message_bitset;
}
uint8_t CanMessageHandler::getErrorMessage() {
uint8_t errorMessage;
switch(m_message.id) {
case MSG_ID_CURRENT_SENSOR_REQUEST:
getData(&errorMessage, CURRENT_SENSOR_ERROR_START,
CURRENT_SENSOR_ERROR_DATASIZE, CURRENT_SENSOR_ERROR_IN_BYTE);
break;
case MSG_ID_MARINE_SENSOR_DATA:
getData(&errorMessage, SENSOR_ERROR_START, SENSOR_ERROR_DATASIZE, SENSOR_ERROR_IN_BYTE);
break;
default:
errorMessage = m_message.data[INDEX_ERROR_CODE];
break;
}
return errorMessage;
}
void CanMessageHandler::setErrorMessage(uint8_t errorMessage) {
switch(m_message.id) {
case MSG_ID_CURRENT_SENSOR_REQUEST:
if(errorMessage >= 8) {
#ifndef ON_ARDUINO_BOARD
Logger::error("In CanMessageHandler::setErrorMessage(): error code value > current_sensor_max_error_value. Wrong error coded.");
#endif
// encode error 7('111') to make sure an error is still coded
encodeMessage(7, CURRENT_SENSOR_ERROR_START,
CURRENT_SENSOR_ERROR_DATASIZE, CURRENT_SENSOR_ERROR_IN_BYTE);
} else {
encodeMessage(errorMessage, CURRENT_SENSOR_ERROR_START,
CURRENT_SENSOR_ERROR_DATASIZE, CURRENT_SENSOR_ERROR_IN_BYTE);
}
break;
case MSG_ID_MARINE_SENSOR_DATA:
encodeMessage(errorMessage, SENSOR_ERROR_START, SENSOR_ERROR_DATASIZE, SENSOR_ERROR_IN_BYTE);
break;
default: // other part of the code still use this version
if (m_message.data[INDEX_ERROR_CODE] == NO_ERRORS) {
m_message.data[INDEX_ERROR_CODE] = errorMessage;
}
break;
}
}
bool CanMessageHandler::canMsgToBitset() {
m_message_bitset = 0;
m_message_bitset |= (static_cast<std::bitset<64>>(m_message.data[7])); // Arduino crash when shifting a bitset by zero...
// So we do the first iteration before the loop in this case
for(int i=1; i<8; i++){
m_message_bitset |= (static_cast<std::bitset<64>>(m_message.data[7-i])) << i*8;
}
if(!(m_message_bitset.any())){ // In case of overflow and some other wrong operations, the returned bitset is zeros only
// Check if we are compiling for arduino board, so we don't use the logger on it, arduino use AVR architecture.
#ifndef ON_ARDUINO_BOARD
Logger::error("In CanMessageHandler::canMsgToBitset(): Data bits are unset, most likely a wrong operation");
#endif
return false;
}
return true;
}
bool CanMessageHandler::bitsetToCanMsg() { // no false output at the moment
for(int i=0; i<8; i++) {
m_message.data[i] = 0; // reset here before copying in the bitset
getData(&(m_message.data[i]), 7-i, 1, true);
}
return true;
}
/*bool generateHeader(int msgType) {
// Add definition of canMsgType or just use the already defined ID defs
switch (canMsgType) {
case MSG_ID_CURRENT_SENSOR_REQUEST: // temporarily using this value because it's unused
generateCurrentSensorHeader();
encodeMessage(T data, uint start, uint length, bool varInBytes = true)
return true;
}
}*/
bool CanMessageHandler::generateCurrentSensorHeader(uint8_t sensorID, uint8_t rolling_number) {
// Current sensor header infos: ID | rol_num | error_value
// Number of bits : 3 2 3
bool success = true;
bool varInBytes = false;
success &= encodeMessage(sensorID, 7*8 + 5, 3, varInBytes); // May define things like current_sensor_id_start_bits etc.
success &= encodeMessage(rolling_number, 7*8 + 3, 2, varInBytes);
return success;
}