]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/TestMasterSlaveLSS/TestMasterSlaveLSS.c
fixed : deprecation warning with the module "sets" when using python 2.6
[CanFestival-3.git] / examples / TestMasterSlaveLSS / TestMasterSlaveLSS.c
1 /*
2 This file is part of CanFestival, a library implementing CanOpen Stack. 
3
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
5
6 See COPYING file for copyrights details.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23 #if defined(WIN32) && !defined(__CYGWIN__)
24 #include <windows.h>
25 #include "getopt.h"
26 void pause(void)
27 {
28         system("PAUSE");
29 }
30 #else
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <signal.h>
36 #endif
37
38 #include "canfestival.h"
39 //#include <can_driver.h>
40 //#include <timers_driver.h>
41
42 #include "Master.h"
43 #include "SlaveA.h"
44 #include "SlaveB.h"
45 #include "TestMasterSlaveLSS.h"
46
47 s_BOARD SlaveBoardA = {"0", "125K"};
48 s_BOARD SlaveBoardB = {"1", "125K"};
49 s_BOARD MasterBoard = {"2", "125K"};
50
51 #if !defined(WIN32) || defined(__CYGWIN__)
52 void catch_signal(int sig)
53 {
54   signal(SIGTERM, catch_signal);
55   signal(SIGINT, catch_signal);
56   eprintf("Got Signal %d\n",sig);
57 }
58 #endif
59
60 void help(void)
61 {
62   printf("**************************************************************\n");
63   printf("*  TestMasterSlaveLSS                                        *\n");
64   printf("*                                                            *\n");
65   printf("*  A LSS example for PC. It does implement 3 CanOpen         *\n");
66   printf("*  nodes in the same process. A master and 2 slaves. All     *\n");
67   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
68   printf("*  SDO and PDO. Master configure heartbeat producer time     *\n");
69   printf("*  at 1000 ms for the slaves by concise DCF.                 *\n");                                  
70   printf("*                                                            *\n");
71   printf("*   Usage:                                                   *\n");
72   printf("*   ./TestMasterSlaveLSS  [OPTIONS]                          *\n");
73   printf("*                                                            *\n");
74   printf("*   OPTIONS:                                                 *\n");
75   printf("*     -l : Can library [\"libcanfestival_can_virtual.so\"]     *\n");
76   printf("*                                                            *\n");
77   printf("*    SlaveA:                                                 *\n");
78   printf("*     -a : bus name [\"0\"]                                    *\n");
79   printf("*     -A : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
80   printf("*                                                            *\n");
81   printf("*    SlaveB:                                                 *\n");
82   printf("*     -b : bus name [\"1\"]                                    *\n");
83   printf("*     -B : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
84   printf("*                                                            *\n");
85   printf("*    Master:                                                 *\n");
86   printf("*     -m : bus name [\"2\"]                                    *\n");
87   printf("*     -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
88   printf("*                                                            *\n");
89   printf("**************************************************************\n");
90 }
91
92 /***************************  INIT  *****************************************/
93 void InitNodes(CO_Data* d, UNS32 id)
94 {
95         /****************************** INITIALISATION SLAVE_A *******************************/
96         if(strcmp(SlaveBoardA.baudrate, "none")) {
97                 /* Set an invalid nodeID */
98                 setNodeId(&TestSlaveA_Data, 0xFF);
99
100                 /* init */
101                 setState(&TestSlaveA_Data, Initialisation);
102         }
103         
104         /****************************** INITIALISATION SLAVE_B *******************************/
105         if(strcmp(SlaveBoardB.baudrate, "none")) {
106
107                 /* Set an invalid nodeID */
108                 setNodeId(&TestSlaveB_Data, 0xFF);
109
110                 /* init */
111                 setState(&TestSlaveB_Data, Initialisation);
112         }
113
114         /****************************** INITIALISATION MASTER *******************************/
115         if(strcmp(MasterBoard.baudrate, "none")){
116                 
117                 /* Defining the node Id */
118                 setNodeId(&TestMaster_Data, 0x01);
119
120                 /* init */
121                 setState(&TestMaster_Data, Initialisation);             
122         }
123 }
124
125 /***************************  EXIT  *****************************************/
126 void Exit(CO_Data* d, UNS32 id)
127 {
128         if(strcmp(MasterBoard.baudrate, "none")){
129         eprintf("Finishing.\n");
130         masterSendNMTstateChange (&TestMaster_Data, 0x00, NMT_Stop_Node);
131
132         eprintf("reset\n");
133
134         // Stop master
135         setState(&TestMaster_Data, Stopped);
136         }
137 }
138
139 /****************************************************************************/
140 /***************************  MAIN  *****************************************/
141 /****************************************************************************/
142 int main(int argc,char **argv)
143 {
144
145   int c;
146   extern char *optarg;
147   char* LibraryPath="../../drivers/can_virtual/libcanfestival_can_virtual.so";
148
149   while ((c = getopt(argc, argv, "-m:a:b:M:A:B:l:")) != EOF)
150   {
151     switch(c)
152     {
153       case 'a' :
154         if (optarg[0] == 0)
155         {
156           help();
157           exit(1);
158         }
159         SlaveBoardA.busname = optarg;
160         break;
161       case 'b' :
162         if (optarg[0] == 0)
163         {
164           help();
165           exit(1);
166         }
167         SlaveBoardB.busname = optarg;
168         break;
169       case 'm' :
170         if (optarg[0] == 0)
171         {
172           help();
173           exit(1);
174         }
175         MasterBoard.busname = optarg;
176         break;
177       case 'A' :
178         if (optarg[0] == 0)
179         {
180           help();
181           exit(1);
182         }
183         SlaveBoardA.baudrate = optarg;
184         break;
185       case 'B' :
186         if (optarg[0] == 0)
187         {
188           help();
189           exit(1);
190         }
191         SlaveBoardB.baudrate = optarg;
192         break;
193       case 'M' :
194         if (optarg[0] == 0)
195         {
196           help();
197           exit(1);
198         }
199         MasterBoard.baudrate = optarg;
200         break;
201       case 'l' :
202         if (optarg[0] == 0)
203         {
204           help();
205           exit(1);
206         }
207         LibraryPath = optarg;
208         break;
209       default:
210         help();
211         exit(1);
212     }
213   }
214
215 #if !defined(WIN32) || defined(__CYGWIN__)
216   /* install signal handler for manual break */
217         signal(SIGTERM, catch_signal);
218         signal(SIGINT, catch_signal);
219         TimerInit();
220 #endif
221
222 #ifndef NOT_USE_DYNAMIC_LOADING
223         if (LoadCanDriver(LibraryPath) == NULL)
224             printf("Unable to load library: %s\n",LibraryPath);
225 #endif          
226         // Open CAN devices
227
228         if(strcmp(SlaveBoardA.baudrate, "none")){
229                 
230                 TestSlaveA_Data.heartbeatError = TestSlaveA_heartbeatError;
231                 TestSlaveA_Data.initialisation = TestSlaveA_initialisation;
232                 TestSlaveA_Data.preOperational = TestSlaveA_preOperational;
233                 TestSlaveA_Data.operational = TestSlaveA_operational;
234                 TestSlaveA_Data.stopped = TestSlaveA_stopped;
235                 TestSlaveA_Data.post_sync = TestSlaveA_post_sync;
236                 TestSlaveA_Data.post_TPDO = TestSlaveA_post_TPDO;
237                 TestSlaveA_Data.storeODSubIndex = TestSlaveA_storeODSubIndex;
238                 TestSlaveA_Data.post_emcy = TestSlaveA_post_emcy;
239                 /* in this example the slave doesn't implement NMT_Slave_Communications_Reset_Callback */
240                 //TestSlaveA_Data.NMT_Slave_Communications_Reset_Callback = TestSlaveA_NMT_Slave_Communications_Reset_Callback;
241                 TestSlaveA_Data.lss_StoreConfiguration = TestSlaveA_StoreConfiguration;
242
243                 if(!canOpen(&SlaveBoardA,&TestSlaveA_Data)){
244                         eprintf("Cannot open SlaveA Board (%s,%s)\n",SlaveBoardA.busname, SlaveBoardA.baudrate);
245                         goto fail_slaveA;
246                 }
247         }
248         
249         if(strcmp(SlaveBoardB.baudrate, "none")){
250                 
251                 TestSlaveB_Data.heartbeatError = TestSlaveB_heartbeatError;
252                 TestSlaveB_Data.initialisation = TestSlaveB_initialisation;
253                 TestSlaveB_Data.preOperational = TestSlaveB_preOperational;
254                 TestSlaveB_Data.operational = TestSlaveB_operational;
255                 TestSlaveB_Data.stopped = TestSlaveB_stopped;
256                 TestSlaveB_Data.post_sync = TestSlaveB_post_sync;
257                 TestSlaveB_Data.post_TPDO = TestSlaveB_post_TPDO;
258                 TestSlaveB_Data.storeODSubIndex = TestSlaveB_storeODSubIndex;
259                 TestSlaveB_Data.post_emcy = TestSlaveB_post_emcy;
260                 TestSlaveB_Data.NMT_Slave_Communications_Reset_Callback = TestSlaveB_NMT_Slave_Communications_Reset_Callback;
261                 TestSlaveB_Data.lss_StoreConfiguration = TestSlaveB_StoreConfiguration;
262
263                 if(!canOpen(&SlaveBoardB,&TestSlaveB_Data)){
264                         eprintf("Cannot open SlaveB Board (%s,%s)\n",SlaveBoardB.busname, SlaveBoardB.baudrate);
265                         goto fail_slaveB;
266                 }
267         }
268         
269         if(strcmp(MasterBoard.baudrate, "none")){
270                 
271                 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
272                 TestMaster_Data.initialisation = TestMaster_initialisation;
273                 TestMaster_Data.preOperational = TestMaster_preOperational;
274                 TestMaster_Data.operational = TestMaster_operational;
275                 TestMaster_Data.stopped = TestMaster_stopped;
276                 TestMaster_Data.post_sync = TestMaster_post_sync;
277                 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
278                 TestMaster_Data.post_emcy = TestMaster_post_emcy;
279                 TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
280                 
281                 if(!canOpen(&MasterBoard,&TestMaster_Data)){
282                         eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
283                         goto fail_master;
284                 }
285         }
286
287         // Start timer thread
288         StartTimerLoop(&InitNodes);
289
290         // wait Ctrl-C
291         
292         pause();
293
294         // Stop timer thread
295         StopTimerLoop(&Exit);
296         
297         // Close CAN devices (and can threads)
298         if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);    
299 fail_master:
300         if(strcmp(SlaveBoardB.baudrate, "none")) canClose(&TestSlaveB_Data);
301 fail_slaveB:
302         if(strcmp(SlaveBoardA.baudrate, "none")) canClose(&TestSlaveA_Data);
303 fail_slaveA:
304         TimerCleanup();
305         return 0;
306 }