Skip to content

Commit 7a97ec4

Browse files
committed
Comments and cleanup
1 parent 286d5f3 commit 7a97ec4

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

.DS_Store

8 KB
Binary file not shown.

src/NodeControllerCore.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
NodeControllerCore::NodeControllerCore(){
44
}
55

6+
67
twai_message_t NodeControllerCore::create_message(uint32_t id, uint64_t *data) {
78
twai_message_t message;
89

@@ -19,10 +20,11 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
1920
this->onMessageReceived = onMessageReceived;
2021
this->nodeID = nodeID;
2122

22-
// Initialize configuration structures using macro initializers
23+
// Initialize configuration structures using macro initializers
2324
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN,
2425
(gpio_num_t)RX_PIN,
2526
TWAI_MODE_NORMAL);
27+
//TODO: deside on the timing config
2628
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
2729
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
2830

@@ -43,6 +45,7 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
4345
}
4446

4547
// Reconfigure alerts to detect frame receive, Bus-Off error and RX queue full states
48+
//Not really sure what most of this means
4649
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL | TWAI_ALERT_BUS_OFF;
4750
if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
4851
Serial.println("CAN Alerts reconfigured");
@@ -51,11 +54,11 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
5154
return false;
5255
}
5356

57+
//Create tx_queue and rx_queue
5458
tx_queue = xQueueCreate(TX_QUEUE_LENGTH, sizeof(twai_message_t));
55-
5659
rx_queue = xQueueCreate(RX_QUEUE_LENGTH, sizeof(twai_message_t));
5760

58-
//Create task to receive messages to rx_queue
61+
//Start tasks
5962
xTaskCreate(this->start_receive_to_rx_queue_task,
6063
"start_rx_task_impl",
6164
2048,
@@ -78,7 +81,7 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
7881
30,
7982
NULL);
8083

81-
84+
//Return true since everything is successful
8285
return true;
8386

8487
}
@@ -89,7 +92,7 @@ void NodeControllerCore::transmit_tx_queue(void *queue) {
8992

9093
while(1){
9194
if(xQueueReceive(*(QueueHandle_t *) queue, &message, RX_TX_BLOCK_TIME) == pdTRUE){
92-
95+
//If there is a message in the queue, try to transmit it
9396
if (twai_transmit(&message, 2000) == ESP_OK) {
9497
Serial.println("Message queued for transmission");
9598
} else {
@@ -111,7 +114,6 @@ void NodeControllerCore::receive_to_rx_queue() {
111114
Serial.println("Receive to rx queue task created");
112115

113116
while(1){
114-
115117
// Check if alert happened
116118
uint32_t alerts_triggered;
117119
twai_read_alerts(&alerts_triggered, 0);
@@ -139,15 +141,18 @@ void NodeControllerCore::receive_to_rx_queue() {
139141
}
140142

141143
bool receive = false;
144+
145+
//TODO: Not sure if this do while loop for receiving messages is the best way to do it, but seems to be working right now
142146
do {
147+
//Try to receive a message
143148
receive = twai_receive(&message, RX_TX_BLOCK_TIME);
144-
145149
if (receive == ESP_OK) {
150+
//If message is received succesfully, put it in the rx_queue
146151
xQueueSend(this->rx_queue, &message, 0);
147152
} else if(receive == ESP_ERR_INVALID_STATE || receive == ESP_ERR_INVALID_ARG) {
148153
Serial.println("Failed to receive message");
149154
}
150-
} while (receive != ESP_ERR_TIMEOUT);
155+
} while (receive != ESP_ERR_TIMEOUT); //If the message is not received, try again, not sure if this is the best way to do it
151156
}
152157
}
153158

@@ -160,10 +165,18 @@ void NodeControllerCore::rx_queue_event() {
160165

161166
while(1){
162167
if(xQueueReceive(rx_queue, &message, RX_TX_BLOCK_TIME) == pdTRUE){
168+
//If there is a message in the rx_queue
163169
uint64_t data = 0;
164170
uint8_t nodeID = 0;
165171
uint16_t messageID = 0;
172+
173+
//Extract the data from the message data
166174
memcpy(&data, message.data, 8);
175+
176+
//Extract the nodeID and messageID from the message identifier with format:
177+
//8bit node ID, 16bit message ID, 5bit reserved
178+
//NNMMMMRR
179+
//(NNNN)(NNNN)(MMMM)(MMMM)(MMMM)(MMMM)(RRRR)(R000)
167180
nodeID = message.identifier >> 21;
168181
messageID = message.identifier >> 5;
169182

@@ -175,20 +188,28 @@ void NodeControllerCore::rx_queue_event() {
175188
Serial.print(" Data: ");
176189
Serial.println(data, HEX);
177190

191+
//Call the onMessageReceived function in the device code
178192
this->onMessageReceived(nodeID,messageID, data);
179193
}
180194
}
181195
}
182196

183197
void NodeControllerCore::sendMessage(uint16_t messageID, uint64_t *data) {
198+
//Create a CAN Bus ID with format:
199+
//8bit node ID, 16bit message ID, 5bit reserved
200+
//NNMMMMRR
201+
//(NNNN)(NNNN)(MMMM)(MMMM)(MMMM)(MMMM)(RRRR)(R000)
184202
uint32_t id = 0;
185203
id = ((this->nodeID << 24) | (messageID << 8)) >> 3;
204+
186205
Serial.print("Sending message with Node ID: ");
187206
Serial.print(nodeID, HEX);
188207
Serial.print(" Message ID: ");
189208
Serial.print(messageID, HEX);
190209
Serial.print(" Data: ");
191210
Serial.println(*data, HEX);
211+
//Create a CAN Bus message
192212
twai_message_t message = create_message(id, data);
213+
//Send the message to the tx_queue
193214
xQueueSend(tx_queue, &message, portMAX_DELAY);
194215
}

src/NodeControllerCore.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,58 @@
1010
#define TX_PIN 7
1111

1212

13+
//Queue length for tx_queue and rx_queue
1314
#define TX_QUEUE_LENGTH 10
14-
1515
#define RX_QUEUE_LENGTH 10
1616

17+
//Time to wait for a message to be received TODO: not sure what this does, set to one tick=1ms right now
1718
#define RX_TX_BLOCK_TIME (1 * portTICK_PERIOD_MS)
1819

1920
class NodeControllerCore
2021
{
2122
private:
23+
//Creates a TWAI message
2224
twai_message_t create_message(uint32_t id, uint64_t *data);
23-
static void transmit_tx_queue(void *queue);
2425

2526
//Pattern from:
2627
//https://stackoverflow.com/questions/45831114/c-freertos-task-invalid-use-of-non-static-member-function
28+
//Helper functions to start receive_to_rx_queue_task
2729
static void start_receive_to_rx_queue_task(void* _this);
30+
31+
//Receive messages from CAN bus and put them in rx_queue
2832
void receive_to_rx_queue();
2933

34+
//Helper functions to start rx_queue_event_task
3035
static void start_rx_queue_event_task(void* _this);
36+
//Transmit messages from tx_queue to CAN bus
37+
static void transmit_tx_queue(void *queue);
38+
39+
//Receive messages from rx_queue and call onMessageReceived function in the device code
3140
void rx_queue_event();
3241

42+
//Queue to transmit messages
3343
QueueHandle_t tx_queue;
3444

45+
//Queue to receive messages
3546
QueueHandle_t rx_queue;
3647

48+
//Function to call when a message is received
3749
std::function<void(uint8_t nodeID, uint16_t messageID, uint64_t data)> onMessageReceived;
3850

3951
public:
52+
//TODO: Add a debug flag to print messages
4053
bool debug;
4154

55+
//Constructor
4256
NodeControllerCore();
57+
58+
//Initialize various the node controller core
4359
bool Init(std::function<void(uint8_t nodeID, uint16_t messageID, uint64_t data)> onMessageReceived, uint8_t nodeID);
60+
61+
//Send a message to the CAN bus
4462
void sendMessage(uint16_t messageID, uint64_t *data);
4563

64+
//Node ID of the node
4665
uint8_t nodeID = 0;
4766

4867
};

0 commit comments

Comments
 (0)