source: trunk/MagicSoft/Cosy/candrv/canopen.cc@ 1135

Last change on this file since 1135 was 1109, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 5.8 KB
Line 
1#include "canopen.h"
2
3#include <iostream.h> // cout
4#include <iomanip.h> // setw, setfill
5
6ClassImp(CanOpen);
7
8CanOpen::CanOpen(const char *dev, const int baud, MLog &out) : VmodIcan(dev, baud, out)
9{
10 for (int i=0; i<32; i++)
11 for (int j=0; j<4; j++)
12 {
13 pthread_cond_init(&fPdoCond[i][j], NULL);
14 pthread_mutex_init(&fPdoMux[i][j], NULL);
15 pthread_mutex_lock(&fPdoMux[i][j]);
16 }
17}
18
19CanOpen::~CanOpen()
20{
21 for (int i=0; i<32; i++)
22 for (int j=0; j<4; j++)
23 {
24 pthread_cond_destroy(&fPdoCond[i][j]);
25 pthread_mutex_destroy(&fPdoMux[i][j]);
26 }
27}
28
29void CanOpen::HandleCanMessage(WORD_t cobid, BYTE_t *data, struct timeval *tv)
30{
31 const WORD_t fcode = cobid >> 7;
32
33 switch (fcode)
34 {
35 case kNMT:
36 cout << "NMT: " << hex ;
37 cout << "CobId: 0x" << cobid << " ";
38 cout << "cmd=0x" << (int)data[0] << " ";
39 cout << "node=" << dec << (int)data[1] << endl;
40 return;
41
42 case kSYNC:
43 cout << "Sync" << endl;
44 return;
45
46 case kSDO_RX:
47 {
48 const BYTE_t node = cobid & 0x1f;
49 const BYTE_t cmd = data[0];
50 const LWORD_t dat = data[4] | (data[5]<<8) | (data[6]<<16) | (data[7]<<24);
51 const WORD_t idx = data[1] | (data[2]<<8);
52 const WORD_t subidx = data[3];
53
54 HandleSDO(node, cmd, idx, subidx, dat, tv);
55
56 fSdoList.Del(node, idx, subidx);
57 }
58 return;
59
60 case kPDO1_TX:
61 {
62 const BYTE_t node = cobid & 0x1f;
63 HandlePDO1(node, data, tv);
64 pthread_cond_broadcast(&fPdoCond[node-1][0]);
65 }
66 return;
67
68 case kPDO2_TX:
69 {
70 const BYTE_t node = cobid & 0x1f;
71 HandlePDO2(node, data, tv);
72 pthread_cond_broadcast(&fPdoCond[node-1][1]);
73 }
74 return;
75
76 case kPDO3_TX:
77 {
78 const BYTE_t node = cobid & 0x1f;
79 HandlePDO3(node, data, tv);
80 pthread_cond_broadcast(&fPdoCond[node-1][2]);
81 }
82 return;
83
84 case kPDO4_TX:
85 {
86 const BYTE_t node = cobid & 0x1f;
87 HandlePDO4(node, data, tv);
88 pthread_cond_broadcast(&fPdoCond[node-1][3]);
89 }
90 return;
91 }
92
93 const BYTE_t node = cobid & 0x1f;
94 cout << "Function Code: 0x" << hex << fcode << " Node: " << dec << (int)node << endl;
95}
96
97void CanOpen::EnableCanMsg(BYTE_t node, BYTE_t fcode, int flag)
98{
99 if (node>=0x20)
100 return;
101
102 EnableCobId(CobId(node, fcode), flag);
103}
104
105void CanOpen::EnableEmcy(BYTE_t node)
106{
107 EnableCanMsg(node, kEMERGENCY);
108}
109
110
111void CanOpen::EnableSdoRx(BYTE_t node)
112{
113 EnableCanMsg(node, kSDO_RX);
114}
115
116void CanOpen::EnablePdo1Rx(BYTE_t node)
117{
118 EnableCanMsg(node, kPDO1_TX);
119}
120
121void CanOpen::EnablePdo2Rx(BYTE_t node)
122{
123 EnableCanMsg(node, kPDO2_TX);
124}
125
126void CanOpen::EnablePdo3Rx(BYTE_t node)
127{
128 EnableCanMsg(node, kPDO1_TX);
129}
130
131void CanOpen::EnablePdo4Rx(BYTE_t node)
132{
133 EnableCanMsg(node, kPDO2_TX);
134}
135
136void CanOpen::SendPDO1(BYTE_t node, BYTE_t data[8])
137{
138 SendCanFrame(CobId(node, kPDO1_TX), data);
139}
140
141void CanOpen::SendPDO2(BYTE_t node, BYTE_t data[8])
142{
143 SendCanFrame(CobId(node, kPDO2_TX), data);
144}
145
146void CanOpen::SendPDO3(BYTE_t node, BYTE_t data[8])
147{
148 SendCanFrame(CobId(node, kPDO3_TX), data);
149}
150
151void CanOpen::SendPDO4(BYTE_t node, BYTE_t data[8])
152{
153 SendCanFrame(CobId(node, kPDO4_TX), data);
154}
155
156void CanOpen::SendPDO1(BYTE_t node,
157 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
158 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
159{
160 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
161 SendCanFrame(CobId(node, kPDO2_TX), msg);
162}
163
164void CanOpen::SendPDO2(BYTE_t node,
165 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
166 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
167{
168 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
169 SendCanFrame(CobId(node, kPDO2_TX), msg);
170}
171
172void CanOpen::SendPDO3(BYTE_t node,
173 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
174 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
175{
176 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
177 SendCanFrame(CobId(node, kPDO3_TX), msg);
178}
179
180void CanOpen::SendPDO4(BYTE_t node,
181 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
182 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
183{
184 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
185 SendCanFrame(CobId(node, kPDO4_TX), msg);
186}
187
188void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
189{
190 if (store)
191 fSdoList.Add(node, idx, subidx);
192
193 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX1,
194 word_to_lsb(idx), word_to_msb(idx), subidx, val);
195}
196
197void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
198{
199 if (store)
200 fSdoList.Add(node, idx, subidx);
201
202 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX2,
203 word_to_lsb(idx), word_to_msb(idx), subidx,
204 word_to_lsb(val), word_to_msb(val));
205}
206
207void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
208{
209 if (store)
210 fSdoList.Add(node, idx, subidx);
211
212 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX4,
213 word_to_lsb(idx), word_to_msb(idx), subidx,
214 word_to_lsb(val&0xffff), word_to_msb(val&0xffff),
215 word_to_lsb(val>>16), word_to_msb(val>>16));
216}
217
218void CanOpen::RequestSDO(BYTE_t node, WORD_t idx, BYTE_t subidx)
219{
220 fSdoList.Add(node, idx, subidx);
221
222 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX_DATA, word_to_lsb(idx), word_to_msb(idx), subidx);
223}
224
225void CanOpen::SendNMT(BYTE_t node, BYTE_t cmd)
226{
227 SendCanFrame(CobId(0, kNMT), cmd, node);
228}
229
230WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode) const
231{
232 return (fcode<<7) | node&0x1f;
233}
Note: See TracBrowser for help on using the repository browser.