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

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