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