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

Last change on this file since 2771 was 2615, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.7 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)
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
187void ShaftEncoder::HandlePDOType1(BYTE_t *data, timeval_t *tv)
188{
189 //
190 // Decode information, we have a 14bit only
191 //
192 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
193 BYTE_t flag = data[4];
194
195 if (fPos==pos)
196 return;
197
198 fPos=pos;
199 fTime.Set(*tv);
200 fPosHasChanged=true;
201
202 flag=flag;
203}
204
205//#include <fstream.h>
206//ofstream fout("log/shaftencoder.log");
207
208void ShaftEncoder::HandlePDOType2(BYTE_t *data, timeval_t *tv)
209{
210 //
211 // Decode information, we have a 14bit only
212 //
213 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
214
215 fVel = data[4] | (data[5]<<8);
216 fAcc = data[6] | (data[7]<<8);
217
218 const int dnlim = fTicks/10;
219 const int uplim = fTurns*fTicks-dnlim;
220
221 int turn = fTurn;
222
223 if (fPos > uplim && pos < dnlim)
224 turn++;
225
226 if (fPos < dnlim && pos > uplim)
227 turn--;
228
229 if (fPos==pos && fTurn==fTurn)
230 return;
231
232 fPos = pos;
233 fTurn = turn;
234
235 fTime.Set(*tv);
236 fPosHasChanged=true;
237}
238
239double ShaftEncoder::GetMjd()
240{
241 return fTime.GetMjd();
242}
243
244void ShaftEncoder::Init()
245{
246 //-----------------------------------------------------------------------
247 // Start Setup of the Shaft Encoder
248 //-----------------------------------------------------------------------
249
250 StopGuarding();
251
252 //
253 // Requesting and checking (FIXME) type of encoder
254 //
255 lout << "- " << GetNodeName() << ": Requesting Hardware Type (0x1000)." << endl;
256 RequestSDO(0x1000);
257 WaitForSdo(0x1000);
258 if (IsZombieNode())
259 return;
260
261 //
262 // Read physical ticks per revolution
263 //
264 lout << "- " << GetNodeName() << ": Requesting physical ticks/revolution (SDO 0x6501)." << endl;
265 RequestSDO(0x6501);
266 WaitForSdo(0x6501);
267
268 //
269 // Read number of possible ticks per revolution
270 //
271 lout << "- " << GetNodeName() << ": Requesting possible ticks/revolution (SDO 0x6502)." << endl;
272 RequestSDO(0x6502);
273 WaitForSdo(0x6502);
274
275 //
276 // Request Lifetimefactor for unknown reason to make guarding
277 // working in SE/Az... (FIXME)
278 //
279 // lout << "- " << GetNodeName() << ": Requesting Lifetimefactor (Workaround, FIXME!) (SDO 0x100d)." << endl;
280 // RequestSDO(0x100c);
281 // WaitForSdo(0x100c);
282 // RequestSDO(0x100d);
283 // WaitForSdo(0x100d);
284
285 //
286 // Set logic ticks/revolution = physical ticks/revolution => scale factor = 1
287 //
288 lout << "- " << GetNodeName() << ": Configuring log. tick/rev (0x6001)." << endl;
289 SendSDO(0x6001, fTicks);
290 WaitForSdo(0x6001);
291
292 //
293 // Set maximum number of ticks (ticks * turns)
294 //
295 lout << "- " << GetNodeName() << ": Configuring max number of ticks (0x6002)." << endl;
296 SendSDO(0x6002, (LWORD_t)(fTicks*fTurns));
297 WaitForSdo(0x6002);
298
299 //
300 // Delete preset Value
301 //
302 lout << "- " << GetNodeName() << ": Delete preset value (0x6003)." << endl;
303 SendSDO(0x6003, (LWORD_t)0xffffffff);
304 WaitForSdo(0x6003);
305
306 //
307 // Configure PDOs
308 //
309 lout << "- " << GetNodeName() << ": Configuring PDOs (0x1802)." << endl;
310 SendSDO(0x1802, 1, (LWORD_t)0x281);
311 WaitForSdo(0x1802, 1);
312
313 //
314 // Request Parameter
315 //
316 lout << "- " << GetNodeName() << ": Requesting SDO 0x6000." << endl;
317 RequestSDO(0x6000);
318 WaitForSdo(0x6000);
319
320 ReqPos();
321
322 lout << "- " << GetNodeName() << ": Start Node (NMT)." << endl;
323 SendNMT(kNMT_START);
324
325 /*
326 cout << "---1---" << endl;
327 MTimeout t(1000);
328 while (!t.HasTimedOut())
329 usleep(1);
330 cout << "---2---" << endl;
331 */
332
333 // StartGuarding(200, 1, kTRUE); // 175
334 // StartGuarding(10*GetId(), 2); // 175
335}
336
337void ShaftEncoder::CheckConnection()
338{
339 // Request Node number
340 RequestSDO(0x100b);
341 WaitForSdo(0x100b);
342}
343
344void ShaftEncoder::ReqPos()
345{
346 //
347 // Request Position
348 //
349 lout << "- " << GetNodeName() << ": Requesting Position." << endl;
350 RequestSDO(0x6004);
351 WaitForSdo(0x6004);
352}
353
354void ShaftEncoder::SetPreset(LWORD_t pre)
355{
356 lout << "- " << GetNodeName() << ": Setting Preset." << endl;
357
358 SendSDO(0x6003, (LWORD_t)pre);
359 if (!WaitForSdo(0x6003))
360 return;
361
362 fPos = pre%16384;
363 fTurn = pre/16384;
364}
365
366void ShaftEncoder::StopDevice()
367{
368 lout << "- " << GetNodeName() << ": Stop Node (NMT)." << endl;
369 SendNMT(kNMT_STOP);
370}
371
Note: See TracBrowser for help on using the repository browser.