Skip to content

Commit

Permalink
Add mutex to safely shut down scan_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Apr 10, 2024
1 parent 844b6a6 commit cf370e7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/admin_panel/src/store/websocket_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const frame_buffer = writable("");

window.api.onMessage((value) => {
value = JSON.parse(value);
console.log(value);
lidar.set(value.lidar);
frame_buffer.set(value.frame_buffer);
});
2 changes: 2 additions & 0 deletions app/rpi/common/include/car/system/device/lidar/LidarDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace car::system::device::lidar
}

std::vector<Measure> scan_data_;

std::mutex scan_data_mutex_;
};
}

Expand Down
17 changes: 14 additions & 3 deletions app/rpi/common/include/car/system/device/lidar/LidarScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,25 @@ namespace car::system::device::lidar

void start() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = true;
this->lidar_->start_motor();
this->scan_generator_ = this->lidar_->iter_scans();
};

void update() final override
{
assert(std::holds_alternative<std::function<std::vector<Measure>()>>(this->scan_generator_) && "LidarScanner::scan() called before start()");
const auto& scan_generator = std::get<std::function<std::vector<Measure>()>>(this->scan_generator_);
this->setScanData(scan_generator());
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
if (this->running) {
const auto& scan_generator = std::get<std::function<std::vector<Measure>()>>(this->scan_generator_);
this->setScanData(scan_generator());
}
};

void stop() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = false;
this->scan_generator_ = nullptr;
this->lidar_->stop();
this->lidar_->stop_motor();
Expand All @@ -63,7 +69,10 @@ namespace car::system::device::lidar

void disconnect() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = false;
this->lidar_->disconnect();
this->scan_generator_ = nullptr;
}

void terminate() final override
Expand All @@ -73,6 +82,8 @@ namespace car::system::device::lidar
}

private:
bool running = false;

std::shared_ptr<configuration::Configuration> configuration_;

std::vector<Measure> scan_data_;
Expand Down
1 change: 0 additions & 1 deletion app/rpi/common/src/car/system/CarSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ namespace car::system
void CarSystem::start()
{
assert(this->initialized && "Car System has not been initialized yet.");
// this->lidar_device->start(); // Starting LidarDevice in connect instead.
this->movement_system_->start();
this->started = true;
}
Expand Down
2 changes: 1 addition & 1 deletion app/rpi/common/src/car/system/device/CameraDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace car::system::device
}

void CameraDevice::update() {
if (!this->camera_->isOpened()) {
if (!this->connected_ || !this->camera_->isOpened()) {
this->frame_buffer_ = "";
return;
}
Expand Down
13 changes: 11 additions & 2 deletions app/rpi/common/src/car/system/device/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ namespace car::system::device {
{
return;
}
this->lidar_device_->update();
this->camera_device_->update();

std::thread lidar_thread([&]() {
this->lidar_device_->update();
});

std::thread camera_thread([&]() {
this->camera_device_->update();
});

lidar_thread.join();
camera_thread.join();
}

void DeviceManager::stop()
Expand Down

0 comments on commit cf370e7

Please sign in to comment.