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

Last change on this file since 8865 was 8863, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 12.0 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), fTurn(0), fTicks(0),
18 fDirection(kUndefined), fHysteresisPos(0), fHysteresisNeg(0),
19 fLabel(NULL), fUpdPos(0),
20 fPosHasChanged(false), fDirHasChanged(false),
21 fReport(NULL), fMotor(0), fOffset(0), fDirChangedPos(0)
22{
23}
24/*
25void ShaftEncoder::CheckTwin(Int_t diff) const
26{
27 if (!fTwin)
28 return;
29
30 if (fTwin->fIsUpdated)
31 fTwin->fIsUpdated = kFALSE;
32 else
33 fTwin->fOffset += diff;
34}
35*/
36void ShaftEncoder::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, const timeval_t &tv)
37{
38 switch (idx)
39 {
40 case 0x1000:
41 gLog << inf2 << "- Model: ";
42 switch (val&0xffff)
43 {
44 case 0x0196:
45 gLog << inf2 << "Shaft Encoder Type: ";
46 switch ((val>>16)&0xff)
47 {
48 case 0x01:
49 gLog << "Singleturn" << endl;
50 return;
51 case 0x02:
52 gLog << "Multiturn" << endl;
53 return;
54 default:
55 gLog << err << "?" << endl;
56 SetZombie();
57 return;
58 }
59 default:
60 gLog << err << "???" << endl;
61 SetZombie();
62 return;
63 }
64 case 0x100b:
65 // Do not display, this is used for CheckConnection
66 // lout << "Node ID: " << dec << val << endl;
67 return;
68
69 case 0x100c:
70 gLog << inf2 << "- Guardtime: " << dec << val << "ms" << endl;
71 return;
72
73 case 0x100d:
74 gLog << inf2 << "- Lifetimefactor: " << dec << val << endl;
75 return;
76
77 case 0x100e:
78 gLog << inf2 << "- CobId for guarding: 0x" << hex << val << endl;
79 return;
80
81 case 0x6000:
82 case 0x6500:
83 gLog << inf2 << "- Counting: " << (val&1 ?"anti-clockwise":"clockwise") << " ";
84 gLog << "HwTest: " << (val&2 ?"on":"off") << " ";
85 gLog << "Scaling: " << (val&4 ?"on":"off") << " ";
86 gLog << "Modulo: " << (val&4096?"on":"off") << endl;
87 return;
88
89 case 0x6001:
90 gLog << inf2 << "- Logical Ticks/Revolution: " << dec << val << endl;
91 return;
92
93 case 0x6004:
94 gLog << inf2 << "- Reported position: " << dec << (Int_t)val << endl;
95 fPos = val;
96 fTurn = 0;
97 fDirChangedPos = val;
98 fOffset = fMotor ? fMotor->GetPdoPos() : 0;
99 fPosHasChanged = true;
100 fDirHasChanged = true;
101 fDirection = kUndefined;
102 //fIsUpdated=kTRUE;
103 //fOffset = 0;
104 return;
105
106
107 case 0x6501:
108 gLog << inf2 << "- Phys. Ticks/Revolution: " << dec << val << endl;
109 fTicks = val;
110 return;
111
112 case 0x6502:
113 //if (val==0)
114 // val = 1; // Single Turn = Multiturn with one turn
115 gLog << inf2 << "- Number of Revolutions: " << dec << val << endl;
116 fTurns = val;
117 return;
118
119
120 }
121
122 NodeDrv::HandleSDO(idx, subidx, val, tv);
123
124// gLog << err << hex << setfill('0');
125// gLog << "Sdo=" << idx << "/" << (int)subidx << ": 0x" << setw(8) << val;
126// gLog << endl;
127}
128
129void ShaftEncoder::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, const timeval_t &tv)
130{
131 switch (idx)
132 {
133 case 0x1802:
134 switch (subidx)
135 {
136 case 1:
137 //lout << ddev(MLog::eGui);
138 gLog << inf2 << "- " << GetNodeName() << ": PDOs configured." << endl;
139 //lout << edev(MLog::eGui);
140 return;
141 }
142 break;
143
144 case 0x6001:
145 switch (subidx)
146 {
147 case 0:
148 //lout << ddev(MLog::eGui);
149 gLog << inf2 << "- " << GetNodeName() << ": Log.ticks/revolution set." << endl;
150 //lout << edev(MLog::eGui);
151 return;
152 }
153 break;
154
155 case 0x6002:
156 switch (subidx)
157 {
158 case 0:
159 //lout << ddev(MLog::eGui);
160 gLog << inf2 << "- " << GetNodeName() << ": Max number of ticks set." << endl;
161 //lout << edev(MLog::eGui);
162 return;
163 }
164 break;
165
166 case 0x6003:
167 switch (subidx)
168 {
169 case 0:
170 //lout << ddev(MLog::eGui);
171 gLog << inf2 << "- " << GetNodeName() << ": Preset value set." << endl;
172 //lout << edev(MLog::eGui);
173 return;
174 }
175 break;
176 }
177 NodeDrv::HandleSDOOK(idx, subidx, data, tv);
178}
179
180void ShaftEncoder::DisplayVal()
181{
182 const LWORDS_t pos = GetPos();
183 if (IsZombieNode())
184 {
185 fLabel->SetText(new TGString(""));
186 fUpdPos = ~pos;
187 return;
188 }
189
190 char text[21]="";
191 if (pos!=fUpdPos && fLabel)
192 {
193 sprintf(text, "%ld", pos);
194 fLabel->SetText(new TGString(text));
195 fUpdPos = pos;
196 }
197}
198
199void ShaftEncoder::HandlePDOType0(const BYTE_t *data, const timeval_t &tv)
200{
201 //
202 // Decode information, we have a 14bit only
203 //
204 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
205
206 //if (pos==fPos)
207 // return;
208
209 // Warning: A multiturn shaftencoder is assumed!
210 if ((pos>fDirChangedPos && pos<fPos) ||
211 (pos<fDirChangedPos && pos>fPos))
212 {
213 fDirChangedPos = pos;
214 fDirHasChanged = true;
215 }
216 else
217 fOffset = fMotor ? fMotor->GetPdoPos() : 0;
218
219 fPos = pos;
220 fTime.Set(tv);
221 fPosHasChanged = true;
222
223 //CheckTwin(fPos-pos);
224 //fIsUpdated=kTRUE;
225
226 if (fReport)
227 {
228 fReport->Lock("ShaftEncoder::HandlePDOType0");
229 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO0 " << pos << " " << GetNodeName() << endl;
230 fReport->UnLock("ShaftEncoder::HandlePDOType0");
231 }
232}
233
234void ShaftEncoder::HandlePDOType1(const BYTE_t *data, const timeval_t &tv)
235{
236 //
237 // Decode information, we have a 14bit only
238 //
239 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
240 BYTE_t flag = data[4];
241
242 //if (fPos==pos)
243 // return;
244
245 // Warning: A multiturn shaftencoder is assumed!
246 if ((pos>fDirChangedPos && pos<fPos) ||
247 (pos<fDirChangedPos && pos>fPos))
248 {
249 fDirChangedPos = pos;
250 fDirHasChanged = true;
251 }
252 else
253 fOffset = fMotor ? fMotor->GetPdoPos() : 0;
254
255 //CheckTwin(fPos-pos);
256 fPos=pos;
257 fTime.Set(tv);
258 fPosHasChanged=true;
259 //fIsUpdated=kTRUE;
260 //fOffset = 0;
261
262 flag=flag;
263
264 if (fReport)
265 {
266 fReport->Lock("ShaftEncoder::HandlePDOType1");
267 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO1 " << pos << " " << (int)flag << " " << GetNodeName() << endl;
268 fReport->UnLock("ShaftEncoder::HandlePDOType1");
269 }
270}
271
272//#include <fstream.h>
273//ofstream fout("log/shaftencoder.log");
274
275void ShaftEncoder::HandlePDOType2(const BYTE_t *data, const timeval_t &tv)
276{
277 //
278 // Decode information, we have a 14bit only
279 //
280 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
281
282 fVel = data[4] | (data[5]<<8);
283 fAcc = data[6] | (data[7]<<8);
284
285 const int dnlim = fTicks/10;
286 const int uplim = fTurns*fTicks-dnlim;
287
288 int turn = fTurn;
289
290 if (fPos > uplim && pos < dnlim)
291 turn++;
292
293 if (fPos < dnlim && pos > uplim)
294 turn--;
295
296 LWORDS_t multipos = pos+turn*fTicks; // SE position of multiturn shaftencoder
297
298 //if (fPos==pos && fTurn==fTurn)
299 // return;
300
301 //CheckTwin(fPos-pos);
302
303 fOffset = fMotor ? fMotor->GetPdoPos() : 0;
304
305 // Warning: A multiturn shaftencoder is assumed!
306 if ((multipos>fDirChangedPos && multipos<fPos) ||
307 (multipos<fDirChangedPos && multipos>fPos))
308 {
309 fDirChangedPos = GetPos();
310 fDirHasChanged = true;
311 fDirChangedOffset = fOffset;
312 }
313 else
314 {
315 fDirHasChanged = false;
316 }
317
318 if (multipos>GetPos())
319 fDirection = kForward;
320 else
321 if (multipos<GetPos())
322 fDirection = kBackward;
323 else
324 fDirection = kUndefined;
325
326
327 fPos = pos;
328 fTurn = turn;
329
330 fTime.Set(tv);
331 fPosHasChanged=true;
332 //fIsUpdated=kTRUE;
333 //fOffset = 0;
334
335 if (fReport)
336 {
337 fReport->Lock("ShaftEncoder::HandlePDOType2");
338 *fReport << "SE-REPORT " << (int)GetId() << " " << fTime << " PDO2 " << pos << " " << fVel << " " << fAcc << " " << GetNodeName() << endl;
339 *fReport << "DIR-REPORT " << (int)GetId() << " " << (Int_t)fDirHasChanged << " " << (Int_t)fDirChangedPos << endl;
340 fReport->UnLock("ShaftEncoder::HandlePDOType2");
341 }
342}
343
344double ShaftEncoder::GetMjd()
345{
346 return fTime.GetMjd();
347}
348
349void ShaftEncoder::Init()
350{
351 //-----------------------------------------------------------------------
352 // Start Setup of the Shaft Encoder
353 //-----------------------------------------------------------------------
354
355 StopGuarding();
356
357 //
358 // Requesting and checking (FIXME) type of encoder
359 //
360 gLog << inf2 << "- " << GetNodeName() << ": Requesting Hardware Type (0x1000)." << endl;
361 RequestSDO(0x1000);
362 WaitForSdo(0x1000);
363 if (IsZombieNode())
364 return;
365
366 //
367 // Read physical ticks per revolution
368 //
369 gLog << inf2 << "- " << GetNodeName() << ": Requesting physical ticks/revolution (SDO 0x6501)." << endl;
370 RequestSDO(0x6501);
371 WaitForSdo(0x6501);
372
373 //
374 // Read number of possible ticks per revolution
375 //
376 gLog << inf2 << "- " << GetNodeName() << ": Requesting possible ticks/revolution (SDO 0x6502)." << endl;
377 RequestSDO(0x6502);
378 WaitForSdo(0x6502);
379
380 //
381 // Request Lifetimefactor for unknown reason to make guarding
382 // working in SE/Az... (FIXME)
383 //
384 // lout << "- " << GetNodeName() << ": Requesting Lifetimefactor (Workaround, FIXME!) (SDO 0x100d)." << endl;
385 // RequestSDO(0x100c);
386 // WaitForSdo(0x100c);
387 // RequestSDO(0x100d);
388 // WaitForSdo(0x100d);
389
390 //
391 // Set logic ticks/revolution = physical ticks/revolution => scale factor = 1
392 //
393 gLog << inf2 << "- " << GetNodeName() << ": Configuring log. tick/rev (0x6001)." << endl;
394 SendSDO(0x6001, fTicks);
395 WaitForSdo(0x6001);
396
397 //
398 // Set maximum number of ticks (ticks * turns)
399 //
400 gLog << inf2 << "- " << GetNodeName() << ": Configuring max number of ticks (0x6002)." << endl;
401 SendSDO(0x6002, (LWORD_t)(fTicks*fTurns));
402 WaitForSdo(0x6002);
403
404 //
405 // Delete preset Value
406 //
407 gLog << inf2 << "- " << GetNodeName() << ": Delete preset value (0x6003)." << endl;
408 SendSDO(0x6003, (LWORD_t)0xffffffff);
409 WaitForSdo(0x6003);
410
411 //
412 // Configure PDOs
413 //
414 gLog << inf2 << "- " << GetNodeName() << ": Configuring PDOs (0x1802)." << endl;
415 SendSDO(0x1802, 1, (LWORD_t)0x281);
416 WaitForSdo(0x1802, 1);
417
418 //
419 // Request Parameter
420 //
421 gLog << inf2 << "- " << GetNodeName() << ": Requesting SDO 0x6000." << endl;
422 RequestSDO(0x6000);
423 WaitForSdo(0x6000);
424
425 ReqPos();
426
427 gLog << inf2 << "- " << GetNodeName() << ": Start Node (NMT)." << endl;
428 SendNMT(kNMT_START);
429
430 /*
431 cout << "---1---" << endl;
432 MTimeout t(1000);
433 while (!t.HasTimedOut())
434 usleep(1);
435 cout << "---2---" << endl;
436 */
437
438 // StartGuarding(200, 1, kTRUE); // 175
439 // StartGuarding(10*GetId(), 2); // 175
440}
441
442void ShaftEncoder::CheckConnection()
443{
444 // Request Node number
445 RequestSDO(0x100b);
446 WaitForSdo(0x100b);
447}
448
449void ShaftEncoder::ReqPos()
450{
451 //
452 // Request Position
453 //
454 gLog << inf2 << "- " << GetNodeName() << ": Requesting Position." << endl;
455 RequestSDO(0x6004);
456 WaitForSdo(0x6004);
457}
458
459void ShaftEncoder::SetPreset(LWORD_t pre)
460{
461 gLog << inf2 << "- " << GetNodeName() << ": Setting Preset." << endl;
462
463 SendSDO(0x6003, (LWORD_t)pre);
464 if (!WaitForSdo(0x6003))
465 return;
466
467 fPos = pre%fTicks;
468 fTurn = pre/fTicks;
469 fDirChangedPos = fPos;
470 fOffset = fMotor ? fMotor->GetPdoPos() : 0;
471
472 fPosHasChanged = true;
473 fDirHasChanged = true;
474
475 fDirection = kUndefined;
476}
477
478void ShaftEncoder::StopDevice()
479{
480 gLog << inf2 << "- " << GetNodeName() << ": Stop Node (NMT)." << endl;
481 SendNMT(kNMT_STOP);
482}
483
Note: See TracBrowser for help on using the repository browser.