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

Last change on this file since 1858 was 1804, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 8.1 KB
Line 
1#include "shaftencoder.h"
2
3#include "base/timer.h"
4#include "network.h"
5
6#include <iostream.h> // cout
7#include <iomanip.h> // setw, setfill
8
9#include <TSystem.h> // gSystem
10#include <TGLabel.h> // TGLabel->SetText
11
12#include <pthread.h>
13#include <sys/resource.h> // PRIO_PROCESS
14
15ClassImp(ShaftEncoder);
16
17ShaftEncoder::ShaftEncoder(const BYTE_t nodeid, const char *name, MLog &out)
18 : NodeDrv(nodeid, name, out), fLabel(NULL), fPosHasChanged(false)
19{
20}
21
22void ShaftEncoder::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv)
23{
24 switch (idx)
25 {
26 case 0x1000:
27 lout << "- Model: ";
28 switch (val&0xffff)
29 {
30 case 0x0196:
31 lout << "Shaft Encoder Type: ";
32 switch ((val>>16)&0xff)
33 {
34 case 0x01:
35 lout << "Singleturn" << endl;
36 return;
37 case 0x02:
38 lout << "Multiturn" << endl;
39 return;
40 default:
41 lout << "?" << endl;
42 return;
43 }
44 default:
45 lout << "???" << endl;
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 << "- Possible Turns: " << 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::DisplayVal()
104{
105 if (IsZombieNode())
106 {
107 fLabel[0]->SetText(new TGString(""));
108 fLabel[1]->SetText(new TGString(""));
109 fLabel[2]->SetText(new TGString(""));
110 fUpdPos = ~fPos;
111 fUpdVel = ~fVel;
112 fUpdAcc = ~fAcc;
113 return;
114 }
115
116 char text[21];
117
118 if (fPos!=fUpdPos && fLabel[0])
119 {
120 sprintf(text, "%ld", fPos);
121 fLabel[0]->SetText(new TGString(text));
122 fUpdPos = fPos;
123 }
124
125 if (fVel!=fUpdVel && fLabel[1])
126 {
127 sprintf(text, "%d", fVel);
128 fLabel[1]->SetText(new TGString(text));
129 fUpdVel = fVel;
130 }
131
132 if (fAcc!=fUpdAcc && fLabel[2])
133 {
134 sprintf(text, "%d", fAcc);
135 fLabel[2]->SetText(new TGString(text));
136 fUpdAcc = fAcc;
137 }
138}
139
140void ShaftEncoder::HandlePDOType0(BYTE_t *data, timeval_t *tv)
141{
142 //
143 // Decode information, we have a 14bit only
144 //
145 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
146 if (pos==fPos)
147 return;
148
149 fPos = pos;
150 fTime.SetTimer(tv);
151 fPosHasChanged = true;
152}
153
154void ShaftEncoder::HandlePDOType1(BYTE_t *data, timeval_t *tv)
155{
156 //
157 // Decode information, we have a 14bit only
158 //
159 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
160 BYTE_t flag = data[4];
161
162 if (fPos==pos)
163 return;
164
165 fPos=pos;
166 fTime.SetTimer(tv);
167 fPosHasChanged=true;
168
169 flag=flag;
170}
171
172//#include <fstream.h>
173//ofstream fout("log/shaftencoder.log");
174
175void ShaftEncoder::HandlePDOType2(BYTE_t *data, timeval_t *tv)
176{
177 //
178 // Decode information, we have a 14bit only
179 //
180 LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
181
182 fVel = data[4] | (data[5]<<8);
183 fAcc = data[6] | (data[7]<<8);
184
185 const int uplim = 9*fTicks/10;
186 const int dnlim = 1*fTicks/10;
187
188 int turn = fTurn;
189
190 if (fPos > uplim && pos < dnlim)
191 turn++;
192
193 if (fPos < dnlim && pos > uplim)
194 turn--;
195
196 if (fPos==pos && fTurn==fTurn)
197 return;
198
199 fPos = pos;
200 fTurn = turn;
201
202 //fout << dec << (int)GetId() << " " << turn << " " << pos << endl;
203
204 fTime.SetTimer(tv);
205 fPosHasChanged=true;
206}
207
208double ShaftEncoder::GetTime()
209{
210 return fTime.Now();
211}
212
213double ShaftEncoder::GetMjd()
214{
215 return fTime.GetMjd();
216}
217
218void ShaftEncoder::Init()
219{
220 //-----------------------------------------------------------------------
221 // Start Setup of the Shaft Encoder
222 //-----------------------------------------------------------------------
223
224 StopGuarding();
225
226 //
227 // Requesting and checking (FIXME) type of encoder
228 //
229 lout << "- " << GetNodeName() << ": Requesting Hardware Type (0x1000)." << endl;
230 RequestSDO(0x1000);
231 WaitForSdo(0x1000);
232
233 if (IsZombieNode())
234 {
235 lout << GetNodeName() << " - Init failed!" << endl;
236 return;
237 }
238
239 //
240 // Read physical ticks per revolution
241 //
242 lout << "- " << GetNodeName() << ": Requesting physical ticks/revolution (SDO 0x6501)." << endl;
243 RequestSDO(0x6501);
244 WaitForSdo(0x6501);
245
246 //
247 // Read number of possible ticks per revolution
248 //
249 lout << "- " << GetNodeName() << ": Requesting possible ticks/revolution (SDO 0x6502)." << endl;
250 RequestSDO(0x6502);
251 WaitForSdo(0x6502);
252
253 //
254 // Request Lifetimefactor for unknown reason to make guarding
255 // working in SE/Az... (FIXME)
256 //
257 // lout << "- " << GetNodeName() << ": Requesting Lifetimefactor (Workaround, FIXME!) (SDO 0x100d)." << endl;
258 // RequestSDO(0x100c);
259 // WaitForSdo(0x100c);
260 // RequestSDO(0x100d);
261 // WaitForSdo(0x100d);
262
263 //
264 // Set logic ticks/revolution = physical ticks/revolution => scale factor = 1
265 //
266 lout << "- " << GetNodeName() << ": Configuring log. tick/rev (0x6001)." << endl;
267 SendSDO(0x6001, fTicks);
268 WaitForSdo(0x6001);
269
270 //
271 // Set maximum number of ticks (ticks * turns)
272 //
273 lout << "- " << GetNodeName() << ": Configuring max number of ticks (0x6002)." << endl;
274 SendSDO(0x6002, (LWORD_t)(fTicks*fTurns));
275 WaitForSdo(0x6002);
276
277 //
278 // Delete preset Value
279 //
280 lout << "- " << GetNodeName() << ": Delete preset value (0x6003)." << endl;
281 SendSDO(0x6003, (LWORD_t)0xffffffff);
282 WaitForSdo(0x6003);
283
284 //
285 // Configure PDOs
286 //
287 lout << "- " << GetNodeName() << ": Configuring PDOs (0x1802)." << endl;
288 SendSDO(0x1802, 1, (LWORD_t)0x281);
289 WaitForSdo(0x1802, 1);
290
291 //
292 // Request Parameter
293 //
294 lout << "- " << GetNodeName() << ": Requesting SDO 0x6000." << endl;
295 RequestSDO(0x6000);
296 WaitForSdo(0x6000);
297
298 ReqPos();
299
300 lout << "- " << GetNodeName() << ": Start Node (NMT)." << endl;
301 SendNMT(kNMT_START);
302
303 /*
304 cout << "---1---" << endl;
305 MTimeout t(1000);
306 while (!t.HasTimedOut())
307 usleep(1);
308 cout << "---2---" << endl;
309 */
310
311 // StartGuarding(175, 2); // 175
312 // StartGuarding(10*GetId(), 2); // 175
313}
314
315void ShaftEncoder::CheckConnection()
316{
317 // Request Node number
318 RequestSDO(0x100b);
319 WaitForSdo(0x100b);
320}
321
322void ShaftEncoder::ReqPos()
323{
324 //
325 // Request Position
326 //
327 lout << "- " << GetNodeName() << ": Requesting Position." << endl;
328 RequestSDO(0x6004);
329 WaitForSdo(0x6004);
330}
331
332void ShaftEncoder::SetPreset(LWORD_t pre)
333{
334 lout << "- " << GetNodeName() << ": Setting Preset." << endl;
335
336 SendSDO(0x6003, (LWORD_t)pre);
337 if (!WaitForSdo(0x6003))
338 return;
339
340 fPos = pre%16384;
341 fTurn = pre/16384;
342}
343
344void ShaftEncoder::StopDevice()
345{
346 lout << "- " << GetNodeName() << ": Stop Node (NMT)." << endl;
347 SendNMT(kNMT_STOP);
348}
349
Note: See TracBrowser for help on using the repository browser.