This repository has been archived by the owner on Oct 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCiA301CommPort.h
186 lines (140 loc) · 5.31 KB
/
CiA301CommPort.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef CIA301COMMPORT_H
#define CIA301COMMPORT_H
#include <iostream>
#include <bitset>
#include <err.h>
#include <unistd.h>
#include <vector>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include "candatatypes.h"
#include "PortBase.h"
#define USE_TIMEOUT 200 //miliseconds timeout
#define FIND_RETRY 20
using namespace std;
class CiA301CommPort
{
public:
CiA301CommPort(int newPortFileDescriptor, uint8_t new_id);
CiA301CommPort(PortBase *new_port, uint8_t new_id);
long ReadSDO(vector<uint8_t> address, int subindex);
ulong ReadSDO(const vector<uint8_t> &address);
long ReadNMT(const vector<uint8_t> &nmtCode);
///
/// \brief WriteNMT This function sends NMT message from node to CanBus.
/// \param nmtCommand The message that is sent.
/// \return 0 if no error.
///
long WriteNMT(const vector<uint8_t> &nmtCommand);
///
/// \brief WritePDO This function sends PDO message to a node (the mode id is
/// configured on constructor).
/// \param command The mesage that is sent.
/// \return 0 if no error.
///
long WritePDO(long number, const vector<uint8_t> &command);
long FlushBuffer();
///
/// \brief FlushBuffer Removes the msgs number of can frames from buffer
/// \param msgs Number of messages to remove.
/// \return 0 if no error.
///
long FlushBuffer(int msgs);
///
/// \brief WriteSDO Writes 4 byte value to specific address on device.
/// This function sends SDO message requesting write on the object dictionary
/// address. Also block and wait for the SDO msg for write ack, and returns negative if error.
/// As it reads the ack SDO message, the buffer should be same size after this function
/// returns.
/// \param address The address that needs SDO writing.
/// \param value The value that will be writed.
/// \return 0 if no error. Negative if error (see cerr).
///
long WriteSDO(const vector<uint8_t> &address, const vector<uint8_t> &value);
long WritePDO4(const vector<uint8_t> &command);
long ReadErrorNMT();
long ReadPDO(long number);
long DisablePDOs();
long EnablePDOs();
uint32_t data4x8to32(const uint8_t *in, int dlc);
//co_msg SetCanOpenMsg(unsigned short id_co, unsigned short rtr, vector<uint8_t> coDataFrame, int size);
long WritePDO1(const vector<uint8_t> &command);
protected:
//variables
int portFileDescriptor;
uint8_t id;
PortBase* port;
bool usesockets;
private:
//methods
///
/// \brief CanOpenToCanBus: Converts a CanOpen object or message into a CanBus object or message.
///For this, it receives two parameters passed by reference, one of an object
///CanOpen that will become the other CanBus object. To do this, copy the ids,
///the dlc, the rtr, the ts, select the normal frame for the CanBus message and
///finally copy the message information.
/// \param input CanOpen message.
/// \param output Can message.
/// \return
///
long CanOpenToCanBus(const co_msg &input, can_msg &output);
///
/// \brief CanBusToCanOpen: It performs the inverse task to the previous function. This function converts a
///CanBus object or message in another CanOpen object or message both passed
///by reference. To do this, copy the information in the CanOpen message
///CanBus message data: the id, the dlc, the rtr, the ts and the data information
///which contains (the content of the message itself).
/// \param input Can message.
/// \param output CanOpen message.
/// \return
///
long CanBusToCanOpen(const can_msg &input, co_msg &output);
// uint32_t data4x8to32(const uint8_t *in, int dlc);
ulong DataToInt(const uint8_t *in, unsigned short size);
int SendMessage(co_msg input);
int SendCanMessage(can_msg &input);
int WaitForReadMessage(co_msg &output, unsigned int canIndex);
int ReadCobId(uint16_t expected_cobid, co_msg &output);
int read_timeout(int fd, can_msg *buf, unsigned int timeout);
long GetMsg(can_msg &msg);
can_msg send_msg;
can_msg input;
vector<can_msg> readBuffer;
vector<unsigned int> readInbox;
/**
* @brief CiA301CommPort::SetCanOpenMsg : Constructs canopen message from parameters
* @param id_co: cob id canopen parameter.
* @param rtr: request for remote.
* @param msg_start : canopen data frame.
* @return : canopen constructed message in co_msg data type.
*/
co_msg SetCanOpenMsg(unsigned short id_co, unsigned short rtr, vector<uint8_t> coDataFrame);};
namespace sdo
{
const uint16_t tx0=0x580;
const uint16_t rx0=0x600;
}
namespace pdo
{
const vector<uint8_t> tx0enable={0x00,0x1A,0x00};//TPDO1
const vector<uint8_t> tx0sub1={0x00,0x1A,0x01};//TPDO1.1
const vector<uint8_t> tx0sub2={0x00,0x1A,0x02};//TPDO1.2
const vector<uint8_t> tx1enable={0x01,0x1A,0x00};//TPDO2
const vector<uint8_t> tx2enable={0x02,0x1A,0x00};//TPDO3
const vector<uint8_t> tx3enable={0x03,0x1A,0x00};//TPDO4
const uint16_t tx0=0x180;//TPDO1
const uint16_t rx0=0x200;//TPDO1
const uint16_t tx1=0x280;//TPDO2
const uint16_t rx1=0x300;//TPDO2
const uint16_t tx2=0x380;//TPDO3
const uint16_t rx2=0x400;//TPDO3
const uint16_t tx3=0x480;//TPDO4
const uint16_t rx3=0x500;//TPDO4
}
namespace nmt
{
const vector<uint8_t> started={0x01};
}
#endif // CIA301COMMPORT_H