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
15
16#define CAN_BAUD 1000000
17
19 private:
20 CANMsg txMsg; //Message object reused to send messages to motors
21 CANMsg rxMsg; //Message object reused to recieve messages from motors
22
23
24 public:
25 CAN can;
26
28
29 bool exists = false;
30 // Declaring CanHandler, can1, and can2
31
33 can(PA_11,PA_12,CAN_BAUD)
34 {exists = false;}
35
36 CANHandler(PinName canRx, PinName canTx):
37 can(canRx,canTx,CAN_BAUD)
38 {exists = true;}
39
40 void attach (Callback< void()> func, CAN::IrqType type = CAN::IrqType::RxIrq){
41 can.attach(func,type);
42 }
43
44 void updateCANs(PinName canRx, PinName canTx){
45 //can = new CAN(canRx,canTx,1000000);
46 CAN can(canRx,canTx,CAN_BAUD);
47 }
48
49 CAN* getCAN(){
50 return &can;
51 }
52
57 bool getFeedback(int *id, uint8_t bytes[], int canbus){
58 bool gotMsg = false;
59 rxMsg.clear();
60 rxMsg.len = 8;
61 if (can.read(rxMsg)) {
62 int err = can.rderror();
63 if (err){
64 printf("[%d CAN Read Errors] on CANBus_%d\n", err, canbus + 1);
65 can.reset();
66 return false;
67 }
68 *id = rxMsg.id;
69 for(int i = 0; i < 8; i ++){
70 rxMsg >> bytes[i]; //Extract information from rxMsg and store it into the bytes array
71 }
72 gotMsg = true;
73 //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]);
74 }
75 return gotMsg;
76 //CAN Recieving from feedback IDs
77 }
78
79 bool rawRead(int *id, uint8_t bytes[], int length){
80 bool gotMsg = false;
81 rxMsg.clear();
82 rxMsg.len = length;
83 if (can.read(rxMsg)) {
84 int err = can.rderror();
85 if (err){
86 printf("[%d CAN Read Errors]\n", err);
87 can.reset();
88 return false;
89 }
90 *id = rxMsg.id;
91 for(int i = 0; i < length; i ++){
92 rxMsg >> bytes[i]; //Extract information from rxMsg and store it into the bytes array
93 }
94 gotMsg = true;
95 //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]);
96 }
97 return gotMsg;
98 //CAN Recieving from feedback IDs
99 }
100
106 static void printMsg(CANMessage& msg)
107 {
108 printf(" ID = 0x%.3x\t", msg.id);
109 for (int i = 0; i < msg.len; i++)
110 printf(" 0x%.2X", msg.data[i]);
111 printf("\n");
112 }
113
121 bool rawSend(int id, int8_t bytes[]){
122 static int errorCount = 0;
123 txMsg.clear(); // clear Tx message storage
124 txMsg.id = id;
125
126 for(int i = 0; i < 8; i++){
127 txMsg << int8_t(bytes[i]); //Take data from bytes array and one at a time store it into txMsg
128 }
129
130 //printMsg(txMsg);
131
132 bool isWrite = can.write(txMsg);
133
134 if(isWrite == 0) {
135 errorCount++;
136 can.reset();
137 }
138 else{
139 errorCount = 0;
140 }
141
142 if (errorCount > 1000){
143 //printf("[CAN Connection Issues SEND]\n");
144 }
145 //printMsg(txMsg);
146 return isWrite;
147 }
148};
149#endif
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define CAN_BAUD
__/ _ / // (_-</ -_)\ \/ _ \/ _/ _ ‘/ _ \/ -_) __/ __/ / /__/ _ \/ ’ \/ -_|_-</ /_/ / _ \/ __/ -_)/ /...
Definition CANHandler.h:16
Definition CANHandler.h:18
CAN can
Definition CANHandler.h:25
bool rawSend(int id, int8_t bytes[])
Raw sending of CAN Messages.
Definition CANHandler.h:121
CANBus
Definition CANHandler.h:27
@ CANBUS_2
Definition CANHandler.h:27
@ CANBUS_1
Definition CANHandler.h:27
@ NOBUS
Definition CANHandler.h:27
void attach(Callback< void()> func, CAN::IrqType type=CAN::IrqType::RxIrq)
Definition CANHandler.h:40
void updateCANs(PinName canRx, PinName canTx)
Definition CANHandler.h:44
static void printMsg(CANMessage &msg)
Prints a CANMessage nicely.
Definition CANHandler.h:106
CANHandler(PinName canRx, PinName canTx)
Definition CANHandler.h:36
CANHandler()
Definition CANHandler.h:32
bool rawRead(int *id, uint8_t bytes[], int length)
Definition CANHandler.h:79
bool getFeedback(int *id, uint8_t bytes[], int canbus)
Get feedback back from the motor.
Definition CANHandler.h:57
CAN * getCAN()
Definition CANHandler.h:49
bool exists
Definition CANHandler.h:29
Definition CANMsg.h:16
void clear(void)
Definition CANMsg.h:35
Definition benchGeometry.cpp:23