TR-mbed 1.0
Loading...
Searching...
No Matches
CANHandler.h
Go to the documentation of this file.
1
2// ____ __ __ __ _ _____ ____ ____ ___ __ _ ___ __ _
3// _______ ___ _____ ___ / __/__ ___ ____ _/ / ___ / /_/ /_(_) ___/__ __ _ ___ ___ / __ \___ _______ / _/__ / _ | / / (_) _/__ / /_(_)_ _ ___
5//\__/\_,_/\_,_/___/\__/___/ .__/\_,_/\_, /_//_/\__/\__/\__/_/\___/\___/_/_/_/\__/___/\____/_//_/\__/\__/___/_//_/_/ |_/____/_/_/ \__/\__/_/_/_/_/\__/
6// /_/ /___/
7//
9#ifndef CANHandler_hpp
10#define CANHandler_hpp
11
12#include "mbed.h"
13#include "CANMsg.h"
14#include <cstdint>
15#include <vector>
16
17
18#define CAN_BAUD 1000000
19
20// Exact ID callback
22 uint32_t id;
23 std::function<void(const CANMsg*)> func;
24};
25
26// Range callback (inclusive)
28 uint32_t start_id;
29 uint32_t end_id;
30 std::function<void(const CANMsg*)> func;
31};
32
34 private:
35 CANMsg txMsg; //Message object reused to send messages to motors
36 CANMsg rxMsg; //Message object reused to recieve messages from motors
37
38 public:
39 CAN can;
40
41 // callback lists
42 std::vector<ExactCallback> exact_;
43 std::vector<RangeCallback> range_;
44
46
47 bool exists = false;
48 // Declaring CanHandler, can1, and can2
49
51 can(PA_11,PA_12,CAN_BAUD), exact_(), range_()
52 {exists = false;}
53
54 CANHandler(PinName canRx, PinName canTx):
55 can(canRx,canTx,CAN_BAUD), exact_(), range_()
56 {exists = true;}
57
58 void attach (Callback< void()> func, CAN::IrqType type = CAN::IrqType::RxIrq){
59 can.attach(func,type);
60 }
61
62 void updateCANs(PinName canRx, PinName canTx){
63 //can = new CAN(canRx,canTx,1000000);
64 CAN can(canRx,canTx,CAN_BAUD);
65 }
66
67 CAN* getCAN(){
68 return &can;
69 }
70
77 void registerCallback(uint32_t id, std::function<void(const CANMsg*)> func) {
78 ExactCallback exact_id = {id, std::move(func)};
79 exact_.push_back(exact_id);
80 }
81
89 void registerCallback(uint32_t start_id, uint32_t end_id, std::function<void(const CANMsg*)> func) {
90 RangeCallback range_ids = {start_id, end_id, std::move(func)};
91 range_.push_back(range_ids);
92 }
93
97 void readAllCan() {
98 rxMsg.clear();
99 // Read through all CAN messages in this frame
100 while(can.read(rxMsg)) {
101 if (can.rderror()) {
102 break;
103 }
104
105 // Look through all direct callbacks
106 for (auto callbacks : exact_) {
107 if (rxMsg.id == callbacks.id) {
108 callbacks.func(&rxMsg);
109 }
110 }
111
112 // Look through all range callbacks
113 for (auto callbacks : range_) {
114 if (rxMsg.id >= callbacks.start_id && rxMsg.id <= callbacks.end_id) {
115 callbacks.func(&rxMsg);
116 }
117 }
118 rxMsg.clear();
119 }
120 }
121
126 bool getFeedback(int *id, uint8_t bytes[], int canbus){
127 bool gotMsg = false;
128 rxMsg.clear();
129 rxMsg.len = 8;
130 if (can.read(rxMsg)) {
131 int err = can.rderror();
132 if (err){
133 printf("[%d CAN Read Errors] on CANBus_%d\n", err, canbus + 1);
134 can.reset();
135 return false;
136 }
137 *id = rxMsg.id;
138 for(int i = 0; i < 8; i ++){
139 rxMsg >> bytes[i]; //Extract information from rxMsg and store it into the bytes array
140 }
141 gotMsg = true;
142 //printf("Motor 0x%x:\tAngle (0,8191):%d\tSpeed ( RPM ):%d\tTorque ( CUR ):%d\tTemperature(C):%d \n",rxMsg.id,feedback[motorID][0],feedback[motorID][1],feedback[motorID][2],feedback[motorID][3]);
143 }
144 return gotMsg;
145 //CAN Recieving from feedback IDs
146 }
147
148 bool rawRead(int *id, uint8_t bytes[], int length){
149 bool gotMsg = false;
150 rxMsg.clear();
151 rxMsg.len = length;
152 if (can.read(rxMsg)) {
153 int err = can.rderror();
154 if (err){
155 printf("[%d CAN Read Errors]\n", err);
156 can.reset();
157 return false;
158 }
159 *id = rxMsg.id;
160 for(int i = 0; i < rxMsg.len; i ++){
161 rxMsg >> bytes[i]; //Extract information from rxMsg and store it into the bytes array
162 }
163 gotMsg = true;
164 //printf("Motor 0x%x:\tAngle (0,8191):%d\tSpeed ( RPM ):%d\tTorque ( CUR ):%d\tTemperature(C):%d \n",rxMsg.id,feedback[motorID][0],feedback[motorID][1],feedback[motorID][2],feedback[motorID][3]);
165 }
166 return gotMsg;
167 //CAN Recieving from feedback IDs
168 }
169
175 static void printMsg(CANMessage& msg)
176 {
177 printf(" ID = 0x%.3x\t", msg.id);
178 for (int i = 0; i < msg.len; i++)
179 printf(" 0x%.2X", msg.data[i]);
180 printf("\n");
181 }
182
190 bool rawSend(int id, int8_t bytes[], int length = 8){
191 static int errorCount = 0;
192 txMsg.clear(); // clear Tx message storage
193 txMsg.id = id;
194
195 for(int i = 0; i < length; i++){
196 txMsg << int8_t(bytes[i]); //Take data from bytes array and one at a time store it into txMsg
197 }
198
199 //printMsg(txMsg);
200
201 bool isWrite = can.write(txMsg);
202
203 if(isWrite == 0) {
204 errorCount++;
205 can.reset();
206 }
207 else{
208 errorCount = 0;
209 }
210
211 if (errorCount > 1000){
212 //printf("[CAN Connection Issues SEND]\n");
213 }
214 //printMsg(txMsg);
215 return isWrite;
216 }
217};
218#endif
#define CAN_BAUD
__/ _ / // (_-</ -_)\ \/ _ \/ _/ _ ‘/ _ \/ -_) __/ __/ / /__/ _ \/ ’ \/ -_|_-</ /_/ / _ \/ __/ -_)/ /...
Definition CANHandler.h:18
Definition CANHandler.h:33
CAN can
Definition CANHandler.h:39
CANBus
Definition CANHandler.h:45
@ CANBUS_2
Definition CANHandler.h:45
@ CANBUS_1
Definition CANHandler.h:45
@ NOBUS
Definition CANHandler.h:45
void attach(Callback< void()> func, CAN::IrqType type=CAN::IrqType::RxIrq)
Definition CANHandler.h:58
void updateCANs(PinName canRx, PinName canTx)
Definition CANHandler.h:62
static void printMsg(CANMessage &msg)
Prints a CANMessage nicely.
Definition CANHandler.h:175
std::vector< ExactCallback > exact_
Definition CANHandler.h:42
CANHandler(PinName canRx, PinName canTx)
Definition CANHandler.h:54
void registerCallback(uint32_t id, std::function< void(const CANMsg *)> func)
Add a callback for a specific id.
Definition CANHandler.h:77
std::vector< RangeCallback > range_
Definition CANHandler.h:43
void readAllCan()
Get feedback from all CAN devices, and send to relevant callbacks.
Definition CANHandler.h:97
CANHandler()
Definition CANHandler.h:50
void registerCallback(uint32_t start_id, uint32_t end_id, std::function< void(const CANMsg *)> func)
Add a callback for multiple ids.
Definition CANHandler.h:89
bool rawRead(int *id, uint8_t bytes[], int length)
Definition CANHandler.h:148
bool rawSend(int id, int8_t bytes[], int length=8)
Raw sending of CAN Messages.
Definition CANHandler.h:190
bool getFeedback(int *id, uint8_t bytes[], int canbus)
Get feedback back from the motor.
Definition CANHandler.h:126
CAN * getCAN()
Definition CANHandler.h:67
bool exists
Definition CANHandler.h:47
Definition CANMsg.h:16
void clear(void)
Definition CANMsg.h:35
Definition CANHandler.h:21
uint32_t id
Definition CANHandler.h:22
std::function< void(const CANMsg *)> func
Definition CANHandler.h:23
Definition CANHandler.h:27
uint32_t start_id
Definition CANHandler.h:28
uint32_t end_id
Definition CANHandler.h:29
std::function< void(const CANMsg *)> func
Definition CANHandler.h:30