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

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