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

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