1 | #include "shaftencoder.h"
|
---|
2 |
|
---|
3 | #include "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 | ShaftEncoder::ShaftEncoder(BYTE_t nodeid, ostream &out=cout) : NodeDrv(nodeid, out), fLabel(NULL)
|
---|
16 | {
|
---|
17 | //
|
---|
18 | // Show information
|
---|
19 | //
|
---|
20 | // pthread_create(&fThread, NULL, MapUpdateThread, this);
|
---|
21 |
|
---|
22 | }
|
---|
23 |
|
---|
24 | ShaftEncoder::~ShaftEncoder()
|
---|
25 | {
|
---|
26 | // pthread_cancel(fThread);
|
---|
27 | }
|
---|
28 |
|
---|
29 | void ShaftEncoder::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, struct timeval *tv)
|
---|
30 | {
|
---|
31 | switch (idx)
|
---|
32 | {
|
---|
33 | case 0x1000:
|
---|
34 | lout << "- Model: ";
|
---|
35 | switch (val&0xffff)
|
---|
36 | {
|
---|
37 | case 0x0196:
|
---|
38 | lout << "Shaft Encoder Type: ";
|
---|
39 | switch ((val>>16)&0xff)
|
---|
40 | {
|
---|
41 | case 0x01:
|
---|
42 | lout << "Singleturn" << endl;
|
---|
43 | return;
|
---|
44 | case 0x02:
|
---|
45 | lout << "Multiturn" << endl;
|
---|
46 | return;
|
---|
47 | default:
|
---|
48 | lout << "?" << endl;
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | default:
|
---|
52 | lout << "???" << endl;
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | case 0x100b:
|
---|
56 | lout << "Node ID: " << dec << val << endl;
|
---|
57 | return;
|
---|
58 |
|
---|
59 | case 0x6000:
|
---|
60 | case 0x6500:
|
---|
61 | lout << "- Counting: " << (val&1 ?"anti-clockwise":"clockwise") << " ";
|
---|
62 | lout << "HwTest: " << (val&2 ?"on":"off") << " ";
|
---|
63 | lout << "Scaling: " << (val&4 ?"on":"off") << " ";
|
---|
64 | lout << "Modulo: " << (val&4096?"on":"off") << endl;
|
---|
65 |
|
---|
66 | case 0x6001:
|
---|
67 | lout << "- Logical Ticks/Revolution: " << dec << val << endl;
|
---|
68 | return;
|
---|
69 |
|
---|
70 | case 0x6004:
|
---|
71 | lout << "- Position: " << dec << val << endl;
|
---|
72 | fPos = val;
|
---|
73 | fTurn = 0;
|
---|
74 | return;
|
---|
75 |
|
---|
76 |
|
---|
77 | case 0x6501:
|
---|
78 | lout << "- Phys. Ticks/Revolution: " << dec << val << endl;
|
---|
79 | fTicks = val;
|
---|
80 | return;
|
---|
81 |
|
---|
82 | case 0x6502:
|
---|
83 | lout << "- Possible Turns: " << dec << val << endl;
|
---|
84 | fTurns = val ? val : 1; // Single Turn = Multiturn with one turn
|
---|
85 | return;
|
---|
86 |
|
---|
87 |
|
---|
88 | }
|
---|
89 | cout << hex << setfill('0');
|
---|
90 | cout << "Sdo=" << idx << "/" << (int)subidx << ": 0x" << setw(8) << val;
|
---|
91 | cout << endl;
|
---|
92 | }
|
---|
93 | /*
|
---|
94 | void *ShaftEncoder::MapUpdateThread(void *data)
|
---|
95 | {
|
---|
96 | ShaftEncoder *se = (ShaftEncoder*) data;
|
---|
97 |
|
---|
98 | //
|
---|
99 | // Detach thread (make sure that it is really removed from memory)
|
---|
100 | //
|
---|
101 | pthread_detach(pthread_self());
|
---|
102 | setpriority(PRIO_PROCESS, 0, 5);
|
---|
103 |
|
---|
104 | se->UpdateThread();
|
---|
105 |
|
---|
106 | return NULL;
|
---|
107 | }
|
---|
108 | */
|
---|
109 | void ShaftEncoder::DisplayVal()
|
---|
110 | {
|
---|
111 | static LWORDS_t pos; // ticks
|
---|
112 | static WORDS_t vel; // ticks per 5ms
|
---|
113 | static WORDS_t acc; // ticks per 25ms^2
|
---|
114 |
|
---|
115 | char text[21];
|
---|
116 |
|
---|
117 | if (fPos!=pos)
|
---|
118 | {
|
---|
119 | sprintf(text, "%ld", fPos);
|
---|
120 | fLabel[0]->SetText(new TGString(text));
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (fVel!=vel)
|
---|
124 | {
|
---|
125 | sprintf(text, "%d", fVel);
|
---|
126 | fLabel[1]->SetText(new TGString(text));
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (fAcc!=acc)
|
---|
130 | {
|
---|
131 | sprintf(text, "%d", fAcc);
|
---|
132 | fLabel[2]->SetText(new TGString(text));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | /*
|
---|
136 | void ShaftEncoder::UpdateThread()
|
---|
137 | {
|
---|
138 | return;
|
---|
139 | //
|
---|
140 | // check for a running thread and lock mutex
|
---|
141 | //
|
---|
142 |
|
---|
143 | //
|
---|
144 | // Wait until the network and the output widget is initialized
|
---|
145 | //
|
---|
146 | while (!GetNetwork() || !fLabel)
|
---|
147 | usleep(1);
|
---|
148 |
|
---|
149 | while (1)
|
---|
150 | {
|
---|
151 | usleep(40000);
|
---|
152 | //WaitForNextPdo1();
|
---|
153 |
|
---|
154 | //
|
---|
155 | // Update information
|
---|
156 | //
|
---|
157 |
|
---|
158 |
|
---|
159 | //
|
---|
160 | // make updated information visible
|
---|
161 | //
|
---|
162 | //gSystem->ProcessEvents(); ----> MCosy::GuiThread
|
---|
163 | }
|
---|
164 | }
|
---|
165 | */
|
---|
166 | void ShaftEncoder::HandlePDOType0(BYTE_t *data)
|
---|
167 | {
|
---|
168 | //
|
---|
169 | // Decode information, we have a 14bit only
|
---|
170 | //
|
---|
171 | fPos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void ShaftEncoder::HandlePDOType1(BYTE_t *data)
|
---|
175 | {
|
---|
176 | //
|
---|
177 | // Decode information, we have a 14bit only
|
---|
178 | //
|
---|
179 | LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
|
---|
180 | BYTE_t flag = data[4];
|
---|
181 | pos=pos;
|
---|
182 | flag=flag;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void ShaftEncoder::HandlePDOType2(BYTE_t *data, struct timeval *tv)
|
---|
186 | {
|
---|
187 | //
|
---|
188 | // Decode information, we have a 14bit only
|
---|
189 | //
|
---|
190 | LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
|
---|
191 |
|
---|
192 | fVel = data[4] | (data[5]<<8);
|
---|
193 | fAcc = data[6] | (data[7]<<8);
|
---|
194 |
|
---|
195 | fTime.SetTimer(tv);
|
---|
196 |
|
---|
197 | const int uplim = 9*fTicks/10;
|
---|
198 | const int dnlim = 1*fTicks/10;
|
---|
199 |
|
---|
200 | if (fPos > uplim && pos < dnlim)
|
---|
201 | fTurn++;
|
---|
202 |
|
---|
203 | if (fPos < dnlim && pos > uplim)
|
---|
204 | fTurn--;
|
---|
205 |
|
---|
206 | fPos = pos;
|
---|
207 | }
|
---|
208 |
|
---|
209 | double ShaftEncoder::GetTime()
|
---|
210 | {
|
---|
211 | return fTime.Now();
|
---|
212 | }
|
---|
213 |
|
---|
214 | double ShaftEncoder::GetMjd()
|
---|
215 | {
|
---|
216 | return fTime.CalcMjd();
|
---|
217 | }
|
---|
218 |
|
---|
219 | void ShaftEncoder::InitDevice(Network *net)
|
---|
220 | {
|
---|
221 | NodeDrv::InitDevice(net);
|
---|
222 |
|
---|
223 | //-----------------------------------------------------------------------
|
---|
224 | // Start Setup of the Shaft Encoder
|
---|
225 | //-----------------------------------------------------------------------
|
---|
226 |
|
---|
227 | //
|
---|
228 | // Requesting and checking (FIXME) type of encoder
|
---|
229 | //
|
---|
230 | lout << "- Requesting Hardware Type (SDO 0x1000) of " << (int)GetId() << endl;
|
---|
231 | RequestSDO(0x1000);
|
---|
232 | WaitForSdo(0x1000);
|
---|
233 |
|
---|
234 | //
|
---|
235 | // Read physical ticks per revolution
|
---|
236 | //
|
---|
237 | lout << "- Requesting physical ticks/revolution (SDO 0x6501) of " << (int)GetId() << endl;
|
---|
238 | RequestSDO(0x6501);
|
---|
239 | WaitForSdo(0x6501);
|
---|
240 |
|
---|
241 | //
|
---|
242 | // Read number of possible ticks per revolution
|
---|
243 | //
|
---|
244 | lout << "- Requesting possible ticks/revolution (SDO 0x6502) of " << (int)GetId() << endl;
|
---|
245 | RequestSDO(0x6502);
|
---|
246 | WaitForSdo(0x6502);
|
---|
247 |
|
---|
248 | //
|
---|
249 | // Set logic ticks/revolution = physical ticks/revolution => scale factor = 1
|
---|
250 | //
|
---|
251 | lout << "- Configuring SDO 0x6001 of " << (int)GetId() << endl;
|
---|
252 | SendSDO(0x6001, fTicks);
|
---|
253 | WaitForSdo(0x6001);
|
---|
254 |
|
---|
255 | //
|
---|
256 | // Set maximum number of ticks (ticks * turns)
|
---|
257 | //
|
---|
258 | lout << "- Configuring SDO 0x6002 of " << (int)GetId() << endl;
|
---|
259 | SendSDO(0x6002, (LWORD_t)(fTicks*fTurns));
|
---|
260 | WaitForSdo(0x6002);
|
---|
261 |
|
---|
262 | //
|
---|
263 | // Configure PDOs
|
---|
264 | //
|
---|
265 | lout << "- Configuring SDO 0x1802 of " << (int)GetId() << endl;
|
---|
266 | SendSDO(0x1802, 1, (LWORD_t)0x281);
|
---|
267 | WaitForSdo(0x1802, 1);
|
---|
268 |
|
---|
269 | //
|
---|
270 | // Delete preset Value
|
---|
271 | //
|
---|
272 | lout << "- Configuring SDO 0x6003 of " << (int)GetId() << endl;
|
---|
273 | SendSDO(0x6003, (LWORD_t)0xffffffff);
|
---|
274 | WaitForSdo(0x6003);
|
---|
275 |
|
---|
276 | //
|
---|
277 | // Request Parameter
|
---|
278 | //
|
---|
279 | lout << "- Requesting SDO 0x6000 of " << (int)GetId() << endl;
|
---|
280 | RequestSDO(0x6000);
|
---|
281 | WaitForSdo(0x6000);
|
---|
282 |
|
---|
283 | ReqPos();
|
---|
284 |
|
---|
285 | lout << "- Start Node " << (int)GetId() << endl;
|
---|
286 | SendNMT(kNMT_START);
|
---|
287 | }
|
---|
288 |
|
---|
289 | void ShaftEncoder::ReqPos()
|
---|
290 | {
|
---|
291 | //
|
---|
292 | // Request Position
|
---|
293 | //
|
---|
294 | lout << "- Requesting Position of #" << (int)GetId() << endl;
|
---|
295 | RequestSDO(0x6004);
|
---|
296 | WaitForSdo(0x6004);
|
---|
297 | }
|
---|
298 |
|
---|
299 | void ShaftEncoder::SetPreset(LWORD_t pre)
|
---|
300 | {
|
---|
301 | fPos = pre%16384;
|
---|
302 | fTurn = pre/16384;
|
---|
303 |
|
---|
304 | lout << " - Setting Preset #" << (int)GetId() << endl;
|
---|
305 | SendSDO(0x6003, (LWORD_t)fPos);
|
---|
306 | WaitForSdo(0x6003);
|
---|
307 | }
|
---|
308 |
|
---|
309 | void ShaftEncoder::StopDevice()
|
---|
310 | {
|
---|
311 | lout << "- Start Node " << (int)GetId() << endl;
|
---|
312 | SendNMT(kNMT_STOP);
|
---|
313 | }
|
---|
314 |
|
---|