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