RBCX
Library for the RB3204-RBCX board with the ESP32 by RoboticsBrno.
Loading...
Searching...
No Matches
RBCXUltrasound.h
Go to the documentation of this file.
1#pragma once
2
3#include <condition_variable>
4#include <functional>
5#include <mutex>
6#include <stdint.h>
7#include <vector>
8
9#include "RBCXTimers.h"
10
11#include "rbcx.pb.h"
12
13namespace rb {
14
15class Manager;
16
18 friend class Manager;
19
20public:
22 static constexpr float defaultSpeedOfSound = 347.13f;
23
24 typedef std::function<bool(uint32_t)> callback_t;
25
31 uint32_t lastDistanceMm() const { return m_lastDistanceMm; }
32
38 uint32_t lastDurationUs() const { return m_lastUs; }
39
45 bool isLastMeasurementValid() const { return m_lastUs != 0; }
46
50 void setSpeedOfSound(float speedOfSoundInMetersPerSecond = defaultSpeedOfSound);
51
55 float getSpeedOfSound() const { return m_speedOfSound; }
56
62 void measureAsync(callback_t callback = nullptr);
63
69 uint32_t measure();
70
71private:
72 Ultrasound();
73 Ultrasound(const Ultrasound&) = delete;
75
76 void init(uint8_t index);
77
78 void recalculateLastDistanceLocked();
79 void onMeasuringDone(const CoprocStat_UltrasoundStat& result);
80
81 uint8_t m_index;
82
83 uint32_t m_lastUs;
84 uint32_t m_lastDistanceMm;
85 float m_speedOfSound;
86 bool m_measuring;
87 std::vector<callback_t> m_callbacks;
88 std::recursive_mutex m_mutex;
89 std::condition_variable_any m_measuringDone;
90 uint16_t m_timeoutTimer;
91};
92
93};
The main library class for working with the RBCX board. Call the install() method at the start of you...
Definition: RBCXManager.h:61
Definition: RBCXUltrasound.h:17
void setSpeedOfSound(float speedOfSoundInMetersPerSecond=defaultSpeedOfSound)
Set the speed of sound in m/s.
Definition: RBCXUltrasound.cpp:33
void measureAsync(callback_t callback=nullptr)
Asynchronously measure the distance to an object. The callback will be called when the measurement is...
Definition: RBCXUltrasound.cpp:40
std::function< bool(uint32_t)> callback_t
Definition: RBCXUltrasound.h:24
float getSpeedOfSound() const
Get the speed of sound in m/s.
Definition: RBCXUltrasound.h:55
bool isLastMeasurementValid() const
Returns true if the last measurement was valid Tests if the last measurement was`nt 0.
Definition: RBCXUltrasound.h:45
static constexpr float defaultSpeedOfSound
< Default is speed at 25C, 50%, 101 kPa
Definition: RBCXUltrasound.h:22
uint32_t lastDurationUs() const
Get the last measured duration in us. If the last measurement was invalid or not yet done,...
Definition: RBCXUltrasound.h:38
uint32_t lastDistanceMm() const
Get the last measured distance in mm. If the last measurement was invalid or not yet done,...
Definition: RBCXUltrasound.h:31
uint32_t measure()
Synchronously measure the distance to an object. This function will block until the measurement is do...
Definition: RBCXUltrasound.cpp:61
Definition: RBCXAngle.cpp:3