Skip to content

Commit

Permalink
Add mutexes for Camera
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Apr 16, 2024
1 parent 264fbbf commit 548e36b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/admin_panel/js/behaviour_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function stopBehaviourTree(_event, _args) {

function getBehaviourTreeList() {
const store = getStore();
return store.behaviour_tree_list;
return store.get("behaviour_tree_list");
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/admin_panel/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const schema = {
}
}

/** @returns {Store} */
function getStore() {
const store = new Store(schema);
return store;
Expand Down
14 changes: 10 additions & 4 deletions app/admin_panel/src/lib/BehaviourTreeHandler.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
show_behaviour_tree_save_modal,
} from "../store/behaviour_tree_code_store";
let behaviour_tree_list = [];
let behaviour_tree_list = []
async function main() {
behaviour_tree_list = await api.getBehaviourTreeList();
}
main()
</script>

<div class="w-full h-full bg-white dark:bg-gray-800">
Expand All @@ -22,12 +26,14 @@
<Card
class="w-full max-w-full h-full max-h-full overflow-scroll my-4 gap-y-2"
>
{#each behaviour_tree_list as a}
{#each behaviour_tree_list as behaviour_tree}
<Card class="w-full max-w-full">
<h3>
{a}
{behaviour_tree.name === "" ? "[Empty Name]" : behaviour_tree.name}
</h3>
<Button color="blue">Load</Button>
<Button color="blue" on:click={()=>{
$behaviour_tree_xml_code = behaviour_tree.code;
}}>Load</Button>
</Card>
{/each}
</Card>
Expand Down
4 changes: 3 additions & 1 deletion app/rpi/common/include/car/system/device/CameraDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace car::system::device
class CameraDevice
{
public:
CameraDevice(std::unique_ptr<cv::VideoCapture> camera, int camera_index) : camera_(std::move(camera)), camera_index_(camera_index) {}
CameraDevice(int camera_index) : camera_index_(camera_index) {}

CameraDevice(const CameraDevice&) = delete;
CameraDevice& operator=(const CameraDevice&) = delete;
Expand Down Expand Up @@ -46,6 +46,8 @@ namespace car::system::device

bool connected_ = false;
std::string frame_buffer_;

std::mutex camera_mutex_;
};
}

Expand Down
2 changes: 0 additions & 2 deletions app/rpi/common/include/car/system/device/lidar/LidarDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ namespace car::system::device::lidar
}

std::vector<Measure> scan_data_;

std::mutex scan_data_mutex_;
};
}

Expand Down
2 changes: 2 additions & 0 deletions app/rpi/common/include/car/system/device/lidar/LidarScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ namespace car::system::device::lidar

std::unique_ptr<RPLidar> lidar_;
std::variant<std::function<std::vector<Measure>()>, nullptr_t> scan_generator_ = nullptr;

std::mutex scan_data_mutex_;
};
}

Expand Down
7 changes: 5 additions & 2 deletions app/rpi/common/src/car/system/device/CameraDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ namespace car::system::device
tl::expected<std::unique_ptr<CameraDevice>, std::string> CameraDevice::create(std::shared_ptr<configuration::Configuration> configuration)
{
try {
std::unique_ptr<cv::VideoCapture> video_capture = std::make_unique<cv::VideoCapture>();
return std::make_unique<CameraDevice>(std::move(video_capture), configuration->camera_index);
return std::make_unique<CameraDevice>(configuration->camera_index);
}
catch (const std::exception& e) {
return tl::make_unexpected(e.what());
}
}

void CameraDevice::start() {
std::lock_guard<std::mutex> lock(this->camera_mutex_);
this->camera_ = std::make_unique<cv::VideoCapture>();
this->connected_ = this->camera_->open(this->camera_index_);
}

void CameraDevice::update() {
std::lock_guard<std::mutex> lock(this->camera_mutex_);
if (!this->connected_ || !this->camera_->isOpened()) {
this->frame_buffer_ = "";
return;
Expand All @@ -38,6 +40,7 @@ namespace car::system::device
}

void CameraDevice::stop() {
std::lock_guard<std::mutex> lock(this->camera_mutex_);
this->camera_->release();
}

Expand Down

0 comments on commit 548e36b

Please sign in to comment.