source: trunk/MagicSoft/Cosy/candrv/nodedrv.cc@ 9029

Last change on this file since 9029 was 8863, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 17.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of Stesy, the MAGIC Steering System
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 2001
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25///////////////////////////////////////////////////////////////////////
26//
27// NodeDrv
28//
29// Base class for a class describing the interface for the CAN nodes.
30//
31// to be overloaded:
32// virtual void Init()
33// virtual void StopDevice()
34// virtual void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv)
35// virtual void HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv)
36// virtual void HandleSDOError(LWORD_t data)
37// virtual void HandlePDO1(BYTE_t *data, timeval_t *tv)
38// virtual void HandlePDO2(BYTE_t *data, timeval_t *tv)
39// virtual void HandlePDO3(BYTE_t *data, timeval_t *tv)
40// virtual void HandlePDO4(BYTE_t *data, timeval_t *tv)
41// virtual bool Reboot();
42// virtual void CheckConnection();
43//
44///////////////////////////////////////////////////////////////////////
45#include "nodedrv.h"
46
47#include <iomanip>
48#include <iostream>
49
50//#include <TTimer.h>
51
52#include "MTime.h"
53#include "network.h"
54
55#include "MLogManip.h"
56
57#include "MThread.h"
58
59ClassImp(NodeDrv);
60
61using namespace std;
62
63// --------------------------------------------------------------------------
64//
65// Constructor for one node. Sets the Node Id (<32) the logging stream
66// and the node name. The name is a name for debug output.
67//
68NodeDrv::NodeDrv(BYTE_t nodeid, const char *name) : fNetwork(NULL), fId(32), fError(0), fIsZombie(kTRUE), fGuard(NULL)
69{
70 if (nodeid>0x1f)
71 {
72 gLog << err << "ERROR - NodeDrv::NodeDrv: Only node Numbers < 32 are allowed"<< endl;
73 return;
74 }
75
76 fId = nodeid;
77
78 if (name)
79 fName = name;
80 else
81 {
82 fName = "Node#";
83 fName += (int)nodeid;
84 }
85
86 gLog << inf2 << "- Node #" << (int)nodeid << " (" << name << ") initialized." << endl;
87
88}
89
90// --------------------------------------------------------------------------
91//
92// destructor
93//
94NodeDrv::~NodeDrv()
95{
96}
97
98// --------------------------------------------------------------------------
99//
100// This should be called from a master or main thread to get a node out
101// of the Zombie-Status. Overload it by your needs.
102//
103bool NodeDrv::Reboot()
104{
105 fIsZombie = false;
106
107 Init();
108
109 return !fIsZombie;
110}
111
112// --------------------------------------------------------------------------
113//
114// Init device, sets the pointer to the whole network and enables
115// the Can messages to be passed through the interface:
116// PDO1 tx
117// PDO2 tx
118// PDO3 tx
119// PDO4 tx
120// SDO rx
121// SDO tx
122//
123bool NodeDrv::InitDevice(Network *net)
124{
125 fNetwork = net;
126
127 EnableCanMsg(kPDO1_TX);
128 EnableCanMsg(kPDO2_TX);
129 EnableCanMsg(kPDO3_TX);
130 EnableCanMsg(kPDO4_TX);
131 EnableCanMsg(kSDO_RX);
132 EnableCanMsg(kSDO_TX);
133 EnableCanMsg(kNodeguard);
134 EnableCanMsg(kEMERGENCY);
135
136 fIsZombie = kFALSE;
137
138 Init();
139
140 return !fIsZombie;
141}
142
143// --------------------------------------------------------------------------
144//
145// Print an "SDO idx/subidx set." from this device message.
146// This output is never redirected to the GUI.
147// In standard CANOpen operation data is meaningless (we are using
148// it in the 'non-standard' CANOpen communication with the MACS)
149//
150void NodeDrv::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, const timeval_t &tv)
151{
152 const Bool_t gui = gLog.IsOutputDeviceEnabled(MLog::eGui);
153
154 if (gui)
155 gLog << ddev(MLog::eGui);
156
157 gLog << warn << setfill('0') << "WARNING - Nodedrv::HandleSDOOK: ";
158 gLog << "Node #" << dec << (int)fId << ": Sdo=" << hex << idx << "/" << (int)subidx << " set.";
159 gLog << endl;
160
161 if (gui)
162 gLog << edev(MLog::eGui);
163}
164
165// --------------------------------------------------------------------------
166//
167// Print an error message with the corresponding data from this device.
168//
169void NodeDrv::HandleSDOError(WORD_t idx, BYTE_t subidx)
170{
171 gLog << warn << "WARNING - Nodedrv::HandleSDOError: Node #" << dec << (int)fId << ": Entry not found in dictionary (idx=0x";
172 gLog << hex << setfill('0') << setw(4) << idx << "/" << (int)subidx << dec << ")";
173 gLog << endl;
174}
175
176// --------------------------------------------------------------------------
177//
178// Prints the received SDo from this device
179//
180void NodeDrv::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, const timeval_t &tv)
181{
182 gLog << warn << "WARNING - Nodedrv::HandleSDO: Idx=0x"<< hex << idx << "/" << (int)subidx;
183 gLog << ", val=0x" << val << endl;
184}
185
186// --------------------------------------------------------------------------
187//
188// Sends the given PDO1 through the network to this device
189// A PDO is carrying up to eight bytes of information.
190//
191// The message is not send if the node has the status Zombie.
192// In this case false is returned, otherwise true
193//
194bool NodeDrv::SendPDO1(BYTE_t data[8])
195{
196 if (!fIsZombie)
197 fNetwork->SendPDO1(fId, data);
198 return !fIsZombie;
199}
200
201// --------------------------------------------------------------------------
202//
203// Sends the given PDO2 through the network to this device
204// A PDO is carrying up to eight bytes of information.
205//
206// The message is not send if the node has the status Zombie.
207// In this case false is returned, otherwise true
208//
209bool NodeDrv::SendPDO2(BYTE_t data[8])
210{
211 if (!fIsZombie)
212 fNetwork->SendPDO2(fId, data);
213 return !fIsZombie;
214}
215
216// --------------------------------------------------------------------------
217//
218// Sends the given PDO1 through the network to this device
219// A PDO is carrying up to eight bytes of information.
220//
221// The message is not send if the node has the status Zombie.
222// In this case false is returned, otherwise true
223//
224bool NodeDrv::SendPDO1(BYTE_t m0, BYTE_t m1, BYTE_t m2, BYTE_t m3,
225 BYTE_t m4, BYTE_t m5, BYTE_t m6, BYTE_t m7)
226{
227 if (!fIsZombie)
228 fNetwork->SendPDO1(fId, m0, m1, m2, m3, m4, m5, m6, m7);
229 return !fIsZombie;
230}
231
232// --------------------------------------------------------------------------
233//
234// Sends the given PDO2 through the network to this device
235// A PDO is carrying up to eight bytes of information.
236//
237// The message is not send if the node has the status Zombie.
238// In this case false is returned, otherwise true
239//
240bool NodeDrv::SendPDO2(BYTE_t m0, BYTE_t m1, BYTE_t m2, BYTE_t m3,
241 BYTE_t m4, BYTE_t m5, BYTE_t m6, BYTE_t m7)
242{
243 if (!fIsZombie)
244 fNetwork->SendPDO2(fId, m0, m1, m2, m3, m4, m5, m6, m7);
245 return !fIsZombie;
246}
247
248// --------------------------------------------------------------------------
249//
250// Sends the given SDO through the network to this device
251// An SDO message contains
252// an address (this device)
253// an index of the dictionary entry to address
254// a subindex of this dictionary entry to access
255// and a value to set for this dictionary entry
256//
257// The message is not send if the node has the status Zombie.
258// In this case false is returned, otherwise true
259//
260bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
261{
262 if (!fIsZombie)
263 fNetwork->SendSDO(fId, idx, subidx, val, store);
264 return !fIsZombie;
265}
266
267// --------------------------------------------------------------------------
268//
269// Sends the given SDO through the network to this device
270// An SDO message contains
271// an address (this device)
272// an index of the dictionary entry to address
273// a subindex of this dictionary entry to access
274// and a value to set for this dictionary entry
275//
276// The message is not send if the node has the status Zombie.
277// In this case false is returned, otherwise true
278//
279bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
280{
281 if (!fIsZombie)
282 fNetwork->SendSDO(fId, idx, subidx, val, store);
283 return !fIsZombie;
284}
285
286// --------------------------------------------------------------------------
287//
288// Sends the given SDO through the network to this device
289// An SDO message contains
290// an address (this device)
291// an index of the dictionary entry to address
292// a subindex of this dictionary entry to access
293// and a value to set for this dictionary entry
294//
295// The message is not send if the node has the status Zombie.
296// In this case false is returned, otherwise true
297//
298bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
299{
300 if (!fIsZombie)
301 fNetwork->SendSDO(fId, idx, subidx, val, store);
302 return !fIsZombie;
303}
304
305// --------------------------------------------------------------------------
306//
307// Sends the given SDO through the network to this device
308// An SDO message contains
309// an address (this device)
310// an index of the dictionary entry to address
311// a subindex of this dictionary entry to access
312// and a value to set for this dictionary entry
313//
314// The message is not send if the node has the status Zombie.
315// In this case false is returned, otherwise true
316//
317bool NodeDrv::SendSDO(WORD_t idx, BYTE_t val)
318{
319 if (!fIsZombie)
320 fNetwork->SendSDO(fId, idx, val, true);
321 return !fIsZombie;
322}
323
324// --------------------------------------------------------------------------
325//
326// Sends the given SDO through the network to this device
327// An SDO message contains
328// an address (this device)
329// an index of the dictionary entry to address
330// a subindex of this dictionary entry to access
331// and a value to set for this dictionary entry
332//
333// The message is not send if the node has the status Zombie.
334// In this case false is returned, otherwise true
335//
336bool NodeDrv::SendSDO(WORD_t idx, WORD_t val)
337{
338 if (!fIsZombie)
339 fNetwork->SendSDO(fId, idx, val, true);
340 return !fIsZombie;
341}
342
343// --------------------------------------------------------------------------
344//
345// Sends the given SDO through the network to this device
346// An SDO message contains
347// an address (this device)
348// an index of the dictionary entry to address
349// a subindex of this dictionary entry to access
350// and a value to set for this dictionary entry
351//
352// The message is not send if the node has the status Zombie.
353// In this case false is returned, otherwise true
354//
355bool NodeDrv::SendSDO(WORD_t idx, LWORD_t val)
356{
357 if (!fIsZombie)
358 fNetwork->SendSDO(fId, idx, val, true);
359 return !fIsZombie;
360}
361
362// --------------------------------------------------------------------------
363//
364// Request a SDO for a given idx/subidx
365// An SDO message contains
366// an address (this device)
367// an index of the dictionary entry to read
368// a subindex of this dictionary entry to access
369//
370// The message is not send if the node has the status Zombie.
371// In this case false is returned, otherwise true
372//
373bool NodeDrv::RequestSDO(WORD_t idx, BYTE_t subidx)
374{
375 if (!fIsZombie)
376 fNetwork->RequestSDO(fId, idx, subidx);
377 return !fIsZombie;
378}
379
380// --------------------------------------------------------------------------
381//
382// Send a NMT message (command) to this device
383//
384// The message is not send if the node has the status Zombie.
385// In this case false is returned, otherwise true
386//
387bool NodeDrv::SendNMT(BYTE_t cmd)
388{
389 if (!fIsZombie)
390 fNetwork->SendNMT(fId, cmd);
391 return !fIsZombie;
392}
393
394// --------------------------------------------------------------------------
395//
396// Send a Nodeguard message (command) to this device
397//
398void NodeDrv::SendNodeguard()
399{
400 fNetwork->SendNodeguard(fId);
401}
402
403// --------------------------------------------------------------------------
404//
405// Enable passthrough for the given functioncode of this device
406//
407void NodeDrv::EnableCanMsg(BYTE_t fcode)
408{
409 fNetwork->EnableCanMsg(fId, fcode, TRUE);
410}
411
412// --------------------------------------------------------------------------
413//
414// Wait a given timeout until the SDO with the given idx/subidx from
415// this device has been received.
416// You can stop waiting by StopWaitingForSDO.
417// Return false if waiting timed out.
418// If waiting timed out the node is set to status Zombie.
419//
420// If the node is already a zombie node, the message is deleted from the
421// queue and no waiting is done, false is returned..
422//
423bool NodeDrv::WaitForSdo(WORD_t idx, BYTE_t subidx, WORDS_t timeout, bool zombie)
424{
425 bool rc = fNetwork->WaitForSdo(fId, idx, subidx, fIsZombie?-1:timeout);
426 if (rc)
427 return true;
428
429 gLog << inf2 << " + " << GetNodeName() << ": NodeDrv::WaitForSdo: 0x" << hex << idx << "/" << dec << (int)subidx << " --> ZOMBIE! " << MTime(-1) << endl;
430 if (zombie)
431 SetZombie();
432 return false;
433}
434
435/*
436void NodeDrv::WaitForSdos()
437{
438 while (fNetwork->WaitingForSdo(fId))
439 usleep(1);
440}
441*/
442
443// --------------------------------------------------------------------------
444//
445// Waits until the next Pdo1 from this device has been received
446//
447void NodeDrv::WaitForNextPdo1()
448{
449 fNetwork->WaitForNextPdo1(fId);
450}
451
452// --------------------------------------------------------------------------
453//
454// Waits until the next Pdo2 from this device has been received
455//
456void NodeDrv::WaitForNextPdo2()
457{
458 fNetwork->WaitForNextPdo2(fId);
459}
460
461// --------------------------------------------------------------------------
462//
463// Waits until the next Pdo3 from this device has been received
464//
465void NodeDrv::WaitForNextPdo3()
466{
467 fNetwork->WaitForNextPdo3(fId);
468}
469
470// --------------------------------------------------------------------------
471//
472// Waits until the next Pdo4 from this device has been received
473//
474void NodeDrv::WaitForNextPdo4()
475{
476 fNetwork->WaitForNextPdo4(fId);
477}
478
479// --------------------------------------------------------------------------
480//
481// Start the standard CANopen guarding of the device.
482// While ms is the guard time in millisec. This is the time between
483// two requests for a Nodeguard message.
484// ltf is the LifeTimeFactor. This means how often it is checked, that at
485// least one Nodeguard message was answered.
486//
487class NodeGuard : public MThread
488{
489 Double_t fTimeoutTime; //[s]
490 Double_t fGuardTime; //[s]
491 Int_t fLifeTimeFactor;
492
493 Bool_t fIsCanOpen;
494
495 NodeDrv *fDrv;
496
497public:
498 NodeGuard(NodeDrv *drv, Int_t guard, Int_t ltf, Bool_t canopen)
499 : MThread("NodeGuard"), fGuardTime(guard/1000.), fLifeTimeFactor(ltf), fIsCanOpen(canopen), fDrv(drv) { }
500
501 void Reset(const timeval_t *tv=NULL)
502 {
503 MTime t;
504 if (tv)
505 t.Set(*tv);
506 else
507 t.Now();
508
509 fTimeoutTime = t + (fGuardTime*fLifeTimeFactor);
510 }
511
512 Int_t Thread()
513 {
514 Reset();
515
516 while (!IsThreadCanceled())
517 {
518 // Sending nodeguards seems to result in
519 // loosing CANbus messages or CANbus answers...
520 // strange. Also protecting VmodIcan::SendCanFrame
521 // by a Mutex doesn't help.
522 if (fIsCanOpen)
523 fDrv->SendNodeguard();
524
525 MTime t;
526 t.Now();
527
528 const Double_t t0 = t+fGuardTime;
529
530 while ((double)t<t0 && (double)t<fTimeoutTime)
531 {
532 Sleep(100);
533 t.Now();
534 }
535
536 //cout << "-d-> " << (Long_t)((fTimeoutTime-t)*1000) << endl;
537 //cout << "-o-> " << (ULong_t)((fTimeoutTime)*1000)<< " " << (ULong_t)((t)*1000) << endl;
538 //cout << "-g-> " << (Long_t)((t-t0)*1000)<< endl;
539
540 if ((double)t<fTimeoutTime)
541 continue;
542
543 fDrv->SetZombie(false);
544 return 0;
545 }
546 return 0;
547 }
548};
549
550void NodeDrv::StartGuarding(Bool_t real)
551{
552 if (fGuard)
553 return;
554
555 fGuard = new NodeGuard(this, fGuardTime, fLifeTimeFactor, real);
556 fGuard->RunThread();
557
558 gLog << inf << "- " << GetNodeName() << ": Guarding (" << dec;
559 gLog << fLifeTimeFactor << "*" << fGuardTime << "ms) started." << endl;
560}
561
562void NodeDrv::StartGuarding(Int_t ms, Int_t ltf, Bool_t real)
563{
564 if (fGuard)
565 {
566 gLog << err << "- " << GetNodeName() << ": ERROR - Guarding already started." << endl;
567 return;
568 }
569
570 fGuardTime = ms;
571 fLifeTimeFactor = ltf;
572
573 StartGuarding(real);
574}
575
576void NodeDrv::StopGuarding()
577{
578 if (!fGuard)
579 return;
580
581 delete fGuard;
582 fGuard=NULL;
583
584 gLog << inf << "- " << GetNodeName() << ": Guarding stopped." << endl;
585}
586
587// --------------------------------------------------------------------------
588//
589// Set the timeout timer to the time the event was received plus the
590// guard time times lifetimefactor.
591//
592void NodeDrv::HandleNodeguard(const timeval_t &tv)
593{
594 if (fGuard)
595 fGuard->Reset(&tv);
596}
597
598void NodeDrv::SetZombie(bool stopguard)
599{
600 fIsZombie = true;
601 if (stopguard)
602 StopGuarding();
603 else
604 gLog << warn << " - " << GetNodeName() << ": Zombie set due to timeout." << endl;
605}
Note: See TracBrowser for help on using the repository browser.