source: trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc@ 9431

Last change on this file since 9431 was 9431, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 8.6 KB
Line 
1#include "shaftencoder.h"
2
3#include <TGLabel.h> // TGLabel->SetText
4
5#include "MLog.h"
6#include "MLogManip.h"
7
8#include "macs.h"
9#include "network.h"
10
11ClassImp(ShaftEncoder);
12
13using namespace std;
14
15ShaftEncoder::ShaftEncoder(const BYTE_t nodeid, const char *name)
16 : NodeDrv(nodeid, name),
17 fPos(0), fVel(0), fAcc(0), fPosRes(0), fTurns(0),
18 fLabel(NULL), fUpdPos(0),
19 fPosHasChanged(false), fDirHasChanged(false),
20 fReport(NULL)
21{
22}
23
24void ShaftEncoder::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, const timeval_t &tv)
25{
26 switch (idx)
27 {
28 case 0x1000:
29 gLog << inf2 << "- Model: ";
30 switch (val&0xffff)
31 {
32 case 0x0196:
33 gLog << inf2 << "Shaft Encoder Type: ";
34 switch ((val>>16)&0xff)
35 {
36 case 0x01:
37 gLog << "Singleturn" << endl;
38 return;
39 case 0x02:
40 gLog << "Multiturn" << endl;
41 return;
42 default:
43 gLog << err << "?" << endl;
44 SetZombie();
45 return;
46 }
47 default:
48 gLog << err << "???" << endl;
49 SetZombie();
50 return;
51 }
52 case 0x100b:
53 // Do not display, this is used for CheckConnection
54 // lout << "Node ID: " << dec << val << endl;
55 return;
56
57 case 0x100c:
58 gLog << inf2 << "- Guardtime: " << dec << val << "ms" << endl;
59 return;
60
61 case 0x100d:
62 gLog << inf2 << "- Lifetimefactor: " << dec << val << endl;
63 return;
64
65 case 0x100e:
66 gLog << inf2 << "- CobId for guarding: 0x" << hex << val << endl;
67 return;
68
69 case 0x6000:
70 case 0x6500:
71 gLog << inf2 << "- Counting: " << (val&1 ?"anti-clockwise":"clockwise") << " ";
72 gLog << "HwTest: " << (val&2 ?"on":"off") << " ";
73 gLog << "Scaling: " << (val&4 ?"on":"off") << " ";
74 gLog << "Modulo: " << (val&4096?"on":"off") << endl;
75 return;
76
77 case 0x6001:
78 gLog << inf2 << "- Logical Ticks/Revolution: " << dec << val << endl;
79 return;
80
81 case 0x6004:
82 gLog << inf2 << "- Reported position: " << dec << (Int_t)val << endl;
83 fPos = val;
84 return;
85
86
87 case 0x6501:
88 gLog << inf2 << "- Position resolution Ticks/Revolution: " << dec << val << endl;
89 fPosRes = val;
90 return;
91
92 case 0x6502:
93 //if (val==0)
94 // val = 1; // Single Turn = Multiturn with one turn
95 gLog << inf2 << "- Number of Revolutions: " << dec << val << endl;
96 fTurns = val;
97 return;
98
99
100 }
101
102 NodeDrv::HandleSDO(idx, subidx, val, tv);
103
104// gLog << err << hex << setfill('0');
105// gLog << "Sdo=" << idx << "/" << (int)subidx << ": 0x" << setw(8) << val;
106// gLog << endl;
107}
108
109void ShaftEncoder::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, const timeval_t &tv)
110{
111 switch (idx)
112 {
113 case 0x1802:
114 switch (subidx)
115 {
116 case 1:
117 //lout << ddev(MLog::eGui);
118 gLog << inf2 << "- " << GetNodeName() << ": PDOs configured." << endl;
119 //lout << edev(MLog::eGui);
120 return;
121 }
122 break;
123
124 case 0x6001:
125 switch (subidx)
126 {
127 case 0:
128 //lout << ddev(MLog::eGui);
129 gLog << inf2 << "- " << GetNodeName() << ": Log.ticks/revolution set." << endl;
130 //lout << edev(MLog::eGui);
131 return;
132 }
133 break;
134
135 case 0x6002:
136 switch (subidx)
137 {
138 case 0:
139 //lout << ddev(MLog::eGui);
140 gLog << inf2 << "- " << GetNodeName() << ": Max number of ticks set." << endl;
141 //lout << edev(MLog::eGui);
142 return;
143 }
144 break;
145
146 case 0x6003:
147 switch (subidx)
148 {
149 case 0:
150 //lout << ddev(MLog::eGui);
151 gLog << inf2 << "- " << GetNodeName() << ": Preset value set." << endl;
152 //lout << edev(MLog::eGui);
153 return;
154 }
155 break;
156 }
157 NodeDrv::HandleSDOOK(idx, subidx, data, tv);
158}
159
160void ShaftEncoder::HandlePDOType0(const BYTE_t *data, const timeval_t &tv)
161{
162 //
163 // Decode information, we have a 14bit only
164 //
165 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
166
167 //if (pos==fPos)
168 // return;
169
170 fPos = pos;
171 fTime.Set(tv);
172 fPosHasChanged = true;
173
174 //CheckTwin(fPos-pos);
175 //fIsUpdated=kTRUE;
176
177 if (fReport)
178 {
179 fReport->Lock("ShaftEncoder::HandlePDOType0");
180 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO0 " << pos << " " << GetNodeName() << endl;
181 fReport->UnLock("ShaftEncoder::HandlePDOType0");
182 }
183}
184
185void ShaftEncoder::HandlePDOType1(const BYTE_t *data, const timeval_t &tv)
186{
187 //
188 // Decode information, we have a 14bit only
189 //
190 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
191 BYTE_t flag = data[4];
192
193 //if (fPos==pos)
194 // return;
195
196 //CheckTwin(fPos-pos);
197 fPos=pos;
198 fTime.Set(tv);
199 fPosHasChanged=true;
200 //fIsUpdated=kTRUE;
201 //fOffset = 0;
202
203 flag=flag;
204
205 if (fReport)
206 {
207 fReport->Lock("ShaftEncoder::HandlePDOType1");
208 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO1 " << pos << " " << (int)flag << " " << GetNodeName() << endl;
209 fReport->UnLock("ShaftEncoder::HandlePDOType1");
210 }
211}
212
213//#include <fstream.h>
214//ofstream fout("log/shaftencoder.log");
215
216void ShaftEncoder::HandlePDOType2(const BYTE_t *data, const timeval_t &tv)
217{
218 //
219 // Decode information, we have a 14bit only
220 //
221 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
222
223 fVel = data[4] | (data[5]<<8);
224 fAcc = data[6] | (data[7]<<8);
225
226// LWORDS_t multipos = pos+turn*fTicks; // SE position of multiturn shaftencoder
227
228 //if (fPos==pos && fTurn==fTurn)
229 // return;
230
231 //CheckTwin(fPos-pos);
232
233 fPos = pos;
234 //fTurn = turn;
235
236 fTime.Set(tv);
237 fPosHasChanged=true;
238 //fIsUpdated=kTRUE;
239 //fOffset = 0;
240
241 if (fReport)
242 {
243 fReport->Lock("ShaftEncoder::HandlePDOType2");
244 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO2 " << pos << " " << fVel << " " << fAcc << " " << GetNodeName() << endl;
245 //*fReport << "DIR-REPORT " << (int)GetId() << " " << (Int_t)fDirHasChanged << " " << (Int_t)fDirChangedPos << endl;
246 fReport->UnLock("ShaftEncoder::HandlePDOType2");
247 }
248}
249
250double ShaftEncoder::GetMjd()
251{
252 return fTime.GetMjd();
253}
254
255void ShaftEncoder::Init()
256{
257 //-----------------------------------------------------------------------
258 // Start Setup of the Shaft Encoder
259 //-----------------------------------------------------------------------
260
261 StopGuarding();
262
263 //
264 // Requesting and checking (FIXME) type of encoder
265 //
266 gLog << inf2 << "- " << GetNodeName() << ": Requesting Hardware Type (0x1000)." << endl;
267 RequestSDO(0x1000);
268 WaitForSdo(0x1000);
269 if (IsZombieNode())
270 return;
271
272 //
273 // Read physical ticks per revolution
274 //
275 gLog << inf2 << "- " << GetNodeName() << ": Requesting physical ticks/revolution (SDO 0x6501)." << endl;
276 RequestSDO(0x6501);
277 WaitForSdo(0x6501);
278
279 //
280 // Read number of possible ticks per revolution
281 //
282 gLog << inf2 << "- " << GetNodeName() << ": Requesting possible ticks/revolution (SDO 0x6502)." << endl;
283 RequestSDO(0x6502);
284 WaitForSdo(0x6502);
285
286 //
287 // Request Lifetimefactor for unknown reason to make guarding
288 // working in SE/Az... (FIXME)
289 //
290 // lout << "- " << GetNodeName() << ": Requesting Lifetimefactor (Workaround, FIXME!) (SDO 0x100d)." << endl;
291 // RequestSDO(0x100c);
292 // WaitForSdo(0x100c);
293 // RequestSDO(0x100d);
294 // WaitForSdo(0x100d);
295
296 //
297 // Configure PDOs
298 //
299 gLog << inf2 << "- " << GetNodeName() << ": Configuring PDOs (0x1802)." << endl;
300 SendSDO(0x1802, 1, (LWORD_t)0x281);
301 WaitForSdo(0x1802, 1);
302
303 //
304 // Request Parameter
305 //
306 gLog << inf2 << "- " << GetNodeName() << ": Requesting SDO 0x6000." << endl;
307 RequestSDO(0x6000);
308 WaitForSdo(0x6000);
309
310 ReqPos();
311
312 gLog << inf2 << "- " << GetNodeName() << ": Start Node (NMT)." << endl;
313 SendNMT(kNMT_START);
314}
315
316void ShaftEncoder::CheckConnection()
317{
318 // Request Node number
319 RequestSDO(0x100b);
320 WaitForSdo(0x100b);
321}
322
323void ShaftEncoder::ReqPos()
324{
325 //
326 // Request Position
327 //
328 gLog << inf2 << "- " << GetNodeName() << ": Requesting Position." << endl;
329 RequestSDO(0x6004);
330 WaitForSdo(0x6004);
331}
332
333void ShaftEncoder::StopDevice()
334{
335 gLog << inf2 << "- " << GetNodeName() << ": Stop Node (NMT)." << endl;
336 SendNMT(kNMT_STOP);
337}
Note: See TracBrowser for help on using the repository browser.