MCU
Loading...
Searching...
No Matches
Typedefs | Functions | Variables
test_can_interface.cpp File Reference
#include <FlexCAN_T4.h>
#include "HytechCANInterface.h"
#include "InverterInterface.h"
Include dependency graph for test_can_interface.cpp:

Go to the source code of this file.

Typedefs

using CircularBufferType = CANBufferType
 
using InverterInterfaceType = InverterInterface< CircularBufferType >
 

Functions

void init_can_interface ()
 
void setup (void)
 
void loop ()
 

Variables

FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > TEST_CAN1
 
FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_16 > TEST_CAN2
 
CAN_message_t msg
 
InverterInterfaceType fl_inv = InverterInterfaceType(&CAN2_txBuffer, MC1_SETPOINTS_COMMAND_CANID)
 
InverterInterfaceType fr_inv = InverterInterfaceType(&CAN2_txBuffer, MC2_SETPOINTS_COMMAND_CANID)
 
InverterInterfaceType rl_inv = InverterInterfaceType(&CAN2_txBuffer, MC3_SETPOINTS_COMMAND_CANID)
 
InverterInterfaceType rr_inv = InverterInterfaceType(&CAN2_txBuffer, MC4_SETPOINTS_COMMAND_CANID)
 
CANInterfaces< CircularBufferTypeCAN_interfaces = {&fl_inv, &fr_inv, &rl_inv, &rr_inv, 0, 0, 0}
 
bool ran_test = false
 

Typedef Documentation

◆ CircularBufferType

Definition at line 11 of file test_can_interface.cpp.

◆ InverterInterfaceType

Definition at line 12 of file test_can_interface.cpp.

Function Documentation

◆ init_can_interface()

void init_can_interface ( )

Definition at line 20 of file test_can_interface.cpp.

21{
22 TEST_CAN2.begin();
23 TEST_CAN2.setBaudRate(500000);
24 TEST_CAN2.setMaxMB(16);
25 TEST_CAN2.enableFIFO();
26 TEST_CAN2.enableFIFOInterrupt();
27 TEST_CAN2.onReceive(on_can2_receive);
28 TEST_CAN2.mailboxStatus();
29
30 TEST_CAN1.begin();
31 TEST_CAN1.setBaudRate(500000);
32 TEST_CAN1.setMaxMB(16);
33 TEST_CAN1.enableFIFO();
34 TEST_CAN1.enableFIFOInterrupt();
35 TEST_CAN1.enableMBInterrupts();
36 TEST_CAN1.onReceive(on_can1_receive);
37 TEST_CAN1.mailboxStatus();
38}
void on_can2_receive(const CAN_message_t &msg)
void on_can1_receive(const CAN_message_t &msg)
FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_16 > TEST_CAN2
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > TEST_CAN1

◆ loop()

void loop ( )

Definition at line 49 of file test_can_interface.cpp.

50{
51 // test for seeing if when CAN2 is connected to CAN1 the can1 rx buffer will get stuff
52 // testing whether or not a CAN message can be sent
53
54 if (!ran_test)
55 {
56 msg.id = random(0x1, 0x7FE);
57 for (uint8_t i = 0; i < 8; i++)
58 msg.buf[i] = i + 1;
59 TEST_CAN2.write(msg);
60 ran_test = true;
61 delay(10);
62 Serial.println("======= TEST 1 =======");
63 Serial.println("can1 rx buffer size: (should be 1)");
64 Serial.println(CAN1_rxBuffer.available());
65 Serial.println("======= TEST 2 =======");
66
67 Serial.println("can1 received message equal to sent msg");
68 delay(10);
69 if (CAN1_rxBuffer.available())
70 {
71 CAN_message_t recvd_msg;
72 uint8_t buf[sizeof(CAN_message_t)];
73 CAN1_rxBuffer.pop_front(buf, sizeof(CAN_message_t));
74 memmove(&recvd_msg, buf, sizeof(recvd_msg));
75
76 Serial.println("\tid = prev: (should be 1)");
77 Serial.print("\t");
78 Serial.println(recvd_msg.id == msg.id);
79 }
80
81 Serial.println("======= TEST 3 =======");
82 // command the rl inverter to reset (sets the remove error flag on mc setpoints command)
83 // queues onto tx buffer
85 // msg.id = random(0x1, 0x7FE);
86 // for (uint8_t i = 0; i < 8; i++) msg.buf[i] = i + 1;
87 // uint8_t buf2[sizeof(CAN_message_t)];
88 // memmove(buf2, &msg, sizeof(msg));
89 // CAN2_txBuffer.push_back(buf2, sizeof(CAN_message_t));
90 // send all messages on tx buffer
91
92 Serial.println("can2 tx buffer size: (should be 1)");
93
94 Serial.println(CAN2_txBuffer.available());
96
97 Serial.println("can2 tx buffer size: (should be 0)");
98
99 Serial.println(CAN2_txBuffer.available());
100 Serial.println("sent on can2");
101 delay(10);
102 Serial.println("can1 rx buffer size: (should be 1) ");
103 Serial.print(CAN1_rxBuffer.available());
104 Serial.println();
106
107 Serial.println("sent on can2");
108 Serial.println("can1 rx buffer size: (should be 0) ");
109 Serial.println(CAN1_rxBuffer.available());
110
111 Serial.println("======= TEST 4 =======");
112
115
116 delay(10);
117 CAN_message_t recvd_msg;
118 uint8_t buf[sizeof(CAN_message_t)];
119 CAN1_rxBuffer.pop_front(buf, sizeof(CAN_message_t));
120 memmove(&recvd_msg, buf, sizeof(recvd_msg));
121 if (recvd_msg.id == ID_MC3_SETPOINTS_COMMAND)
122 {
123 MC_setpoints_command msg;
124 msg.load(recvd_msg.buf);
125 if (msg.get_remove_error())
126 {
127
128 Serial.println("GOOD received correct inverter reset");
129 }
130 } else{
131
132 Serial.println("ERROR didnt receive correct id");
133 Serial.println(recvd_msg.id);
134 Serial.println(ID_MC3_SETPOINTS_COMMAND);
135 }
136 }
137}
void process_ring_buffer(BufferType &rx_buffer, const InterfaceType &interfaces, unsigned long curr_millis)
void send_all_CAN_msgs(bufferType &buffer, FlexCAN_T4_Base *can_interface)
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN1_rxBuffer
CANBufferType CAN2_txBuffer
CAN_message_t msg
CANInterfaces< CircularBufferType > CAN_interfaces
InverterInterfaceType rl_inv
bool ran_test

◆ setup()

void setup ( void  )

Definition at line 40 of file test_can_interface.cpp.

41{
42 while (!Serial)
43 {
44 // wait for Arduino Serial Monitor to be ready
45 }
47}
void init_can_interface()

Variable Documentation

◆ CAN_interfaces

CANInterfaces<CircularBufferType> CAN_interfaces = {&fl_inv, &fr_inv, &rl_inv, &rr_inv, 0, 0, 0}

Definition at line 18 of file test_can_interface.cpp.

◆ fl_inv

InverterInterfaceType fl_inv = InverterInterfaceType(&CAN2_txBuffer, MC1_SETPOINTS_COMMAND_CANID)

Definition at line 13 of file test_can_interface.cpp.

◆ fr_inv

InverterInterfaceType fr_inv = InverterInterfaceType(&CAN2_txBuffer, MC2_SETPOINTS_COMMAND_CANID)

Definition at line 14 of file test_can_interface.cpp.

◆ msg

CAN_message_t msg

Definition at line 9 of file test_can_interface.cpp.

◆ ran_test

bool ran_test = false

Definition at line 48 of file test_can_interface.cpp.

◆ rl_inv

InverterInterfaceType rl_inv = InverterInterfaceType(&CAN2_txBuffer, MC3_SETPOINTS_COMMAND_CANID)

Definition at line 15 of file test_can_interface.cpp.

◆ rr_inv

InverterInterfaceType rr_inv = InverterInterfaceType(&CAN2_txBuffer, MC4_SETPOINTS_COMMAND_CANID)

Definition at line 16 of file test_can_interface.cpp.

◆ TEST_CAN1

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> TEST_CAN1

Definition at line 6 of file test_can_interface.cpp.

◆ TEST_CAN2

FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> TEST_CAN2

Definition at line 7 of file test_can_interface.cpp.