File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " hardware/Distance/V5DistanceSensor.hpp"
2+ #include " hardware/util.hpp"
3+ #include < limits.h>
4+ #include < mutex>
5+
6+ namespace lemlib {
7+ V5DistanceSensor::V5DistanceSensor (SmartPort port)
8+ : m_port(port) {}
9+
10+ V5DistanceSensor::V5DistanceSensor (const V5DistanceSensor& other)
11+ : m_port(other.m_port),
12+ m_offset (other.m_offset) {}
13+
14+ V5DistanceSensor V5DistanceSensor::from_pros_dist (pros::Distance distanceSensor) {
15+ return V5DistanceSensor {{distanceSensor.get_port (), runtime_check_port}};
16+ }
17+
18+ int32_t V5DistanceSensor::isConnected () const {
19+ if (pros::c::distance_get (m_port) == INT_MAX) return 0 ;
20+ else return 1 ;
21+ }
22+
23+ Length V5DistanceSensor::getDistance () const {
24+ std::lock_guard lock (m_mutex);
25+ const int32_t raw = pros::c::distance_get (m_port);
26+ if (raw == INT_MAX) return from_in (INFINITY);
27+ // the rotation sensor returns mm
28+ const Length distance = from_mm (raw);
29+ return distance + m_offset;
30+ }
31+
32+ int32_t V5DistanceSensor::setOffset (Length offset) {
33+ const int32_t raw = pros::c::distance_get (m_port);
34+ if (raw == INT_MAX) return INT_MAX;
35+ m_offset = offset;
36+ return 0 ;
37+ }
38+ } // namespace lemlib
You can’t perform that action at this time.
0 commit comments