-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
StageRobotFactory.cc
195 lines (175 loc) · 7.84 KB
/
StageRobotFactory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* AMRISim is based on MobileSim (Copyright 2005 ActivMedia Robotics, 2006-2010
* MobileRobots Inc, 2011-2015 Adept Technology, 2016-2017 Omron Adept Technologies)
* and Stage version 2 (Copyright Richard Vaughan, Brian Gerkey, Andrew Howard, and
* others), published under the terms of the GNU General Public License version 2.
*
* Copyright 2018 Reed Hedges and others
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "AMRISim.hh"
#include "StageRobotFactory.hh"
#include "StageInterface.hh"
StageRobotFactory::StageRobotFactory(stg_world_t* world, const std::string& modelName,
double start_x, double start_y, double start_th,
const AMRISim::Options *userOpts)
:
RobotFactory(modelName, userOpts),
myWorld(world),
myUseFixedStartPos(true),
myGetStartCB(nullptr),
myGetBoundsCB(nullptr),
myStartOutsideBounds(false)
{
myStartPos.x = start_x / 1000.0;
myStartPos.y = start_y / 1000.0;
myStartPos.a = start_th / 1000.0;
}
StageRobotFactory::StageRobotFactory(stg_world_t *world, const std::string& modelName,
mobilesim_get_pose_cb_t *get_start_cb,
mobilesim_get_bounds_cb_t *get_bounds_cb, bool start_outside_bounds,
const AMRISim::Options *userOpts)
:
RobotFactory(modelName, userOpts),
myWorld(world),
myUseFixedStartPos(false),
myGetStartCB(get_start_cb),
myGetBoundsCB(get_bounds_cb),
myStartOutsideBounds(start_outside_bounds)
{
}
RobotInterface* StageRobotFactory::createRobot(const std::string& modelName, const std::string& requestedRobotName)
{
//stg_world_lock(myWorld);
stg_model_t* model;
if(requestedRobotName == "")
{
stg_print_msg("StageRobotFactory: Creating robot of type %s...", modelName.c_str());
model = stg_world_new_model(myWorld, (char*)modelName.c_str(), NULL, NULL);
}
else
{
stg_print_msg("StageRobotFactory: Creating robot of type %s with name %s...", modelName.c_str(), requestedRobotName.c_str());
model = stg_world_new_model(myWorld, (char*)modelName.c_str(), NULL, (char*) requestedRobotName.c_str());
}
//print_debug("created new position model 0x%x", model);
// cast should be safe, new_model should be making copies of those strings
if(!model) {
stg_print_error("StageRobotFactory: stage could not create a model of type \"%s\".", modelName.c_str());
return NULL;
}
//if(!stg_model_lock(model)) return NULL; // destroyed somehow
stg_model_set_property(model, "source", (void*)"StageRobotFactory", strlen("StageRobotFactory"));
std::string name = stg_model_get_token(model);
if(myUseFixedStartPos)
{
stg_print_msg("StageRobotFactory: New robot will have starting position (fixed) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
else if(myStartOutsideBounds && myGetBoundsCB)
{
double min_x, max_y;
myGetBoundsCB(&min_x, NULL, NULL, &max_y);
myStartPos.x = (min_x / 1000.0) - 2.0;
myStartPos.y = (max_y / 1000.0);
myStartPos.a = 0;
//stg_print_msg("Robot Factory: New robot will have starting position (outside map bounds) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
else if(myGetStartCB)
{
myGetStartCB(&myStartPos.x, &myStartPos.y, &myStartPos.a);
myStartPos.x /= 1000.0;
myStartPos.y /= 1000.0;
myStartPos.a = DTOR(myStartPos.a);
stg_print_msg("StageRobotFactory: New robot will have starting position (obtained) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
// add a messages model (for gui messages)
//stg_model_t *msgs = stg_world_new_model(myWorld, "messages", model, NULL);
//assert(msgs);
//if(!stg_model_lock(msgs)) return NULL; // messages model destroyed (somehow!)
//stg_messages_send(msgs, NULL, STG_MSG_INFORMATION, "Robot factory for model type %s created a new robot (%s).", modelName.c_str(), name.c_str());
//stg_model_unlock(msgs);
// TODO: test. does this cause the crash?
//stg_world_display_message(myWorld, 0, NULL, STG_MSG_INFORMATION, "Robot factory for model type %s created a new robot (%s).", modelName.c_str(), name.c_str());
//stg_model_unlock(model);
//stg_world_unlock(myWorld);
StageInterface *retInterface = new StageInterface(myWorld, model, modelName, name);
return retInterface;
}
RobotInterface* StageRobotFactory::createStubRobot(const std::string& modelName, const std::string& requestedRobotName)
{
//stg_world_lock(myWorld);
print_debug("StageRobotInterface::createStubRobot(%s, %s)", modelName.c_str(), requestedRobotName.c_str());
stg_model_t *model;
if(requestedRobotName == "")
{
stg_print_msg("StageRobotFactory: Creating robot of type %s...", "position");
model = stg_world_new_model(myWorld, "position", NULL, NULL);
}
else
{
stg_print_msg("StageRobotFactory: Creating robot of type %s with name %s...", "position", requestedRobotName.c_str());
model = stg_world_new_model(myWorld, "position", NULL, (char*) requestedRobotName.c_str());
}
//print_debug("created new position model 0x%x", model);
// cast should be safe, new_model should be making copies of those strings
if(!model) {
stg_print_error("StageRobotFactory: stage could not create a model of type \"%s\".", "position");
return NULL;
}
//if(!stg_model_lock(model)) return NULL; // destroyed somehow
stg_model_set_property(model, "source", (void*)"StageRobotFactory", strlen("StageRobotFactory"));
std::string name = stg_model_get_token(model);
if(myUseFixedStartPos)
{
stg_print_msg("StageRobotFactory: New robot will have starting position (fixed) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
else if(myStartOutsideBounds && myGetBoundsCB)
{
double min_x, max_y;
myGetBoundsCB(&min_x, NULL, NULL, &max_y);
myStartPos.x = (min_x / 1000.0) - 2.0;
myStartPos.y = (max_y / 1000.0);
myStartPos.a = 0;
//stg_print_msg("Robot Factory: New robot will have starting position (outside map bounds) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
else if(myGetStartCB)
{
myGetStartCB(&myStartPos.x, &myStartPos.y, &myStartPos.a);
myStartPos.x /= 1000.0;
myStartPos.y /= 1000.0;
myStartPos.a = DTOR(myStartPos.a);
stg_print_msg("StageRobotFactory: New robot will have starting position (obtained) %f,%f,%f.", myStartPos.x, myStartPos.y, myStartPos.a);
stg_model_set_pose(model, myStartPos);
}
// add a messages model (for gui messages)
//stg_model_t *msgs = stg_world_new_model(myWorld, "messages", model, NULL);
//assert(msgs);
//if(!stg_model_lock(msgs)) return NULL; // messages model destroyed (somehow!)
//stg_messages_send(msgs, NULL, STG_MSG_INFORMATION, "Robot factory for model type %s created a new robot (%s).", modelName.c_str(), name.c_str());
//stg_model_unlock(msgs);
// TODO: test. does this cause the crash?
//stg_world_display_message(myWorld, 0, NULL, STG_MSG_INFORMATION, "Robot factory for model type %s created a new robot (%s).", modelName.c_str(), name.c_str());
//stg_model_unlock(model);
//stg_world_unlock(myWorld);
StageInterface *retInterface = new StageInterface(myWorld, model, "position", name);
return retInterface;
}