test.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19 */
20 #include <visualstates/test.h>
21 #include <iostream>
22 #include <easyiceconfig/EasyIce.h>
23 #include <string>
25 
27  std::cout << " runcode for state0" << std::endl;
28 }
29 
31  std::cout << " runcode for state1" << std::endl;
32 }
33 
35  std::cout << " runcode for state2" << std::endl;
36 }
37 
38 void Tran0::init() {
39  std::cout << " tran1 init" << std::endl;
40 }
41 
43  std::cout << " tran1 checkcondition" << std::endl;
44  return false;
45 }
46 
48  std::cout << " tran1 runcode" << std::endl;
49 }
50 
51 void MyInterfaces::connectProxies(int argc, char* argv[]) {
52  ice = EasyIce::initialize(argc, argv);
53 
54  Ice::ObjectPrx tempmyMotors = ice->propertyToProxy("automata.myMotors.Proxy");
55  if (tempmyMotors == 0) {
56  throw "cannot create proxy from automata.myMotors.Proxy";
57  }
58  myMotors = jderobot::MotorsPrx::checkedCast(tempmyMotors);
59  if (myMotors == 0) {
60  throw "invalid proxy automata.myMotors.Proxy";
61  }
62  std::cout << "myMotors connected" << std::endl;
63 }
64 
66  if (ice != 0) {
67  ice->destroy();
68  }
69 }
70 
71 pthread_t guiThread;
73 bool displayGui = false;
74 
75 void readArgs(int *argc, char* argv[]) {
76  int i;
77  std::string splitedArg;
78 
79  for(i = 0; i < *argc; i++){
80  splitedArg = strtok(argv[i], "=");
81  if (splitedArg.compare("--displaygui") == 0){
82  splitedArg = strtok(NULL, "=");
83  if (splitedArg.compare("true") == 0 || splitedArg.compare("True") == 0){
84  displayGui = true;
85  std::cout << "displayGui ENABLED" << std::endl;
86  }else{
87  displayGui = false;
88  std::cout << "displayGui DISABLED" << std::endl;
89  }
90  }
91  if(i == *argc -1){
92  (*argc)--;
93  }
94  }
95 }
96 
97 void* runGui(void*) {
98  runTimeGui = new RunTimeGui();
99 }
100 
101 int main (int argc, char* argv[]) {
102  MyInterfaces interfaces;
103  try {
104  interfaces.connectProxies(argc, argv);
105  } catch (const Ice::Exception& ex) {
106  std::cerr << ex << std::endl;
107  interfaces.destroyProxies();
108  return 1;
109  } catch (const char* msg) {
110  std::cerr << msg << std::endl;
111  interfaces.destroyProxies();
112  return 1;
113  }
114 
115  readArgs(&argc, argv);
116  displayGui = true;
117  std::cout << "displaygui:" << displayGui << std::endl;
118 
119  if (displayGui) {
120  pthread_create(&guiThread, NULL, &runGui, NULL);
121  while (runTimeGui == NULL) {
122  usleep(10000);
123  }
124 
125  runTimeGui->addState(0, "root", true, 0.0, 0.0, -1);
126  runTimeGui->addState(1, "state1", true, 0.0, 0.0, 0);
127  runTimeGui->addState(2, "state2", false, 0.0, 0.0, 0);
128 
129  runTimeGui->emitActiveStateById(0);
130  runTimeGui->emitLoadFromRoot();
131  }
132 
133  // create transitions
134  State* state0 = new State0(0, true, (Interfaces*)&interfaces, 100, NULL, runTimeGui);
135 
136  State* state1 = new State1(1, true, (Interfaces*)&interfaces, 100, state0, runTimeGui);
137  State* state2 = new State2(2, false, (Interfaces*)&interfaces, 100, state0, runTimeGui);
138 
139  // create state transitions
140  Transition* tran0 = new Tran0(0, 2, (Interfaces*)&interfaces);
141  state1->addTransition(tran0);
142 
143  std::cout << "just before state thread" << std::endl;
144 
145  state0->startThread();
146  std::cout << "thread started" << std::endl;
147  state0->join();
148  std::cout << "thread joined" << std::endl;
149 
150 }
virtual void runCode()
Definition: test.cpp:34
virtual void connectProxies(int argc, char *argv[])
Definition: test.cpp:51
bool displayGui
Definition: state.h:36
virtual void destroyProxies()
Definition: test.cpp:65
void emitLoadFromRoot()
Definition: runtimegui.cpp:36
virtual bool checkCondition()
Definition: test.cpp:42
virtual void init()
Definition: test.cpp:38
pthread_t guiThread
Definition: test.cpp:71
void emitActiveStateById(int id)
Definition: runtimegui.cpp:39
void * runGui(void *)
Definition: test.cpp:97
Definition: state.h:28
void readArgs(int *argc, char *argv[])
Definition: test.cpp:75
RunTimeGui * runTimeGui
Definition: test.cpp:72
void addTransition(Transition *transition)
Definition: state.cpp:70
void join()
Definition: state.cpp:135
Definition: test.h:48
virtual void runCode()
Definition: test.cpp:30
Definition: test.h:42
virtual void runCode()
Definition: test.cpp:47
virtual void runCode()
Definition: test.cpp:26
int main(int argc, char *argv[])
Definition: test.cpp:101
void startThread()
Definition: state.cpp:78
Definition: test.h:36
State0(int id, bool initial, Interfaces *interfaces, int cycleDuration, State *parent, RunTimeGui *gui)
Definition: test.h:32


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20