@@ -183,12 +183,16 @@ class Robot : public franka::Robot {
183
183
franka::RobotState current_state_;
184
184
std::mutex state_mutex_;
185
185
std::mutex control_mutex_;
186
- std::optional<std::shared_future<std::exception_ptr>> control_future_;
186
+ std::condition_variable control_finished_condition_;
187
+ std::exception_ptr control_exception_;
188
+ std::thread control_thread_;
187
189
MotionGeneratorVariant motion_generator_{std::nullopt};
188
190
bool motion_generator_running_{false };
189
191
190
192
[[nodiscard]] bool is_in_control_unsafe () const ;
191
193
194
+ void joinMotionUnsafe (std::unique_lock<std::mutex> &lock);
195
+
192
196
template <typename ControlSignalType>
193
197
void moveInternal (
194
198
const std::shared_ptr<Motion<ControlSignalType>> &motion,
@@ -204,13 +208,7 @@ class Robot : public franka::Robot {
204
208
std::get<MotionGenerator<ControlSignalType>>(motion_generator_).updateMotion (motion);
205
209
}
206
210
} else {
207
- if (control_future_.has_value ()) {
208
- control_future_->wait ();
209
- auto control_exception = control_future_->get ();
210
- if (control_exception != nullptr )
211
- std::rethrow_exception (control_exception);
212
- control_future_ = std::nullopt;
213
- }
211
+ joinMotionUnsafe (lock);
214
212
215
213
motion_generator_.emplace <MotionGenerator<ControlSignalType>>(this , motion);
216
214
auto motion_generator = &std::get<MotionGenerator<ControlSignalType>>(motion_generator_);
@@ -220,8 +218,7 @@ class Robot : public franka::Robot {
220
218
current_state_ = robot_state;
221
219
});
222
220
motion_generator_running_ = true ;
223
- control_future_ = std::async (
224
- std::launch::async,
221
+ control_thread_ = std::thread (
225
222
[this , control_func_executor, motion_generator]() {
226
223
try {
227
224
bool done = false ;
@@ -240,16 +237,15 @@ class Robot : public franka::Robot {
240
237
} else {
241
238
done = true ;
242
239
motion_generator_running_ = false ;
240
+ control_finished_condition_.notify_all ();
243
241
}
244
242
}
245
243
} catch (...) {
246
- {
247
- std::unique_lock<std::mutex> lock (control_mutex_);
248
- motion_generator_running_ = false ;
249
- }
250
- return std::current_exception ();
244
+ std::unique_lock<std::mutex> lock (control_mutex_);
245
+ control_exception_ = std::current_exception ();
246
+ motion_generator_running_ = false ;
247
+ control_finished_condition_.notify_all ();
251
248
}
252
- return std::exception_ptr{nullptr };
253
249
}
254
250
);
255
251
}
0 commit comments