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

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