Skip to content

✨ Add getPort to inertial sensor #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/hardware/IMU/V5InertialSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ class V5InertialSensor : public IMU {
* @endcode
*/
static V5InertialSensor from_pros_imu(pros::Imu imu, Number scalar = 1.0);
/**
* @brief Get the port the inertial sensor is connected to
*
* This function returns the port number of the inertial sensor.
*
* @return SmartPort the port the inertial sensor is connected to
*
* @b Example:
* @code {.cpp}
* void initialize() {
* lemlib::V5InertialSensor imu(1);
* std::cout << "Inertial sensor is connected to port " << imu.getPort() << std::endl;
* }
* @endcode
*/
SmartPort getPort() const;
/**
* @brief calibrate the V5 Inertial Sensor
*
Expand Down
7 changes: 7 additions & 0 deletions src/hardware/IMU/V5InertialSensor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "hardware/IMU/V5InertialSensor.hpp"
#include "hardware/Port.hpp"
#include "pros/imu.hpp"
#include <cstdint>
#include <mutex>

namespace lemlib {
Expand All @@ -17,6 +18,12 @@ V5InertialSensor V5InertialSensor::from_pros_imu(pros::IMU imu, Number scalar) {
return V5InertialSensor({imu.get_port(), runtime_check_port}, scalar);
}

SmartPort V5InertialSensor::getPort() const {
std::lock_guard lock(m_mutex);
SmartPort port(m_imu.get_port(), DynamicPort {});
return port;
}

int32_t V5InertialSensor::calibrate() {
std::lock_guard lock(m_mutex);
m_offset = 0_stRot;
Expand Down