M5Unit-DISTANCE 0.2.0 git rev:2044122
Loading...
Searching...
No Matches
unit_RCWL9620.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_DISTANCE_UNIT_RCWL9620_HPP
11#define M5_UNIT_DISTANCE_UNIT_RCWL9620_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16#include <cmath>
17#include <array>
18
19namespace m5 {
20namespace unit {
21
26namespace rcwl9620 {
27
32struct Data {
33 std::array<uint8_t, 3> raw{}; // Raw data
34
35 static constexpr float MAX_DISTANCE{4500.f};
36 static constexpr float MIN_DISTANCE{20.f};
37
39 inline float distance() const
40 {
41 float fd = raw_distance() / 1000.f;
42 return std::fmax(std::fmin(fd, MAX_DISTANCE), MIN_DISTANCE);
43 }
44 inline uint32_t raw_distance() const
45 {
46 return ((uint32_t)raw[0] << 16) | ((uint32_t)raw[1] << 8) | (uint32_t)raw[2];
47 }
48};
49
50} // namespace rcwl9620
51
56class UnitRCWL9620 : public Component, public PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data> {
57 M5_UNIT_COMPONENT_HPP_BUILDER(UnitRCWL9620, 0x57);
58
59public:
64 struct config_t {
66 bool start_periodic{true};
68 uint32_t interval_ms{250};
69 };
70
71 explicit UnitRCWL9620(const uint8_t addr = DEFAULT_ADDRESS)
72 : Component(addr), _data{new m5::container::CircularBuffer<rcwl9620::Data>(1)}
73 {
74 auto ccfg = component_config();
75 ccfg.clock = 100 * 1000U;
76 component_config(ccfg);
77 }
78 virtual ~UnitRCWL9620()
79 {
80 }
81
82 virtual bool begin() override;
83 virtual void update(const bool force = false) override;
84
87
89 {
90 return _cfg;
91 }
93 inline void config(const config_t& cfg)
94 {
95 _cfg = cfg;
96 }
98
102 float distance() const
103 {
104 return !empty() ? oldest().distance() : std::numeric_limits<float>::quiet_NaN();
105 }
107
110
115 inline bool startPeriodicMeasurement(const uint32_t interval)
116 {
117 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::startPeriodicMeasurement(interval);
118 }
124 {
125 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::stopPeriodicMeasurement();
126 }
128
131
139
141 // Class that abstracts the interaction between classes and adapters
142 class Interface {
143 public:
144 explicit Interface(UnitRCWL9620& u) : _unit{u}
145 {
146 }
147 virtual ~Interface()
148 {
149 }
150 virtual bool read_measurement(rcwl9620::Data&, bool&) = 0;
151 virtual bool request_measurement() = 0;
152
153 protected:
154 UnitRCWL9620& _unit;
155 };
157
158protected:
159 bool request_measurement();
160 bool read_measurement(rcwl9620::Data& d, bool& timeouted);
161
162 bool start_periodic_measurement(const uint32_t interval);
163 bool stop_periodic_measurement();
164
165 inline Interface* interface()
166 {
167 return _interface.get();
168 }
169
170 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitRCWL9620, rcwl9620::Data);
171
172 std::unique_ptr<m5::container::CircularBuffer<rcwl9620::Data>> _data{};
173
174 inline virtual uint32_t minimum_interval() const
175 {
176 // Datasheet says
177 // 向模块写入 0X01 ,模块开始测距;等待 100mS 模块最大测距时间
178 // Note : Max is assumed to be 50+100 since there is no description of Max.
179 // A larger value would be considered better?
180
181 // As a result of the experiment, it seems that in 100ms, 263 in requestFrom is returned in many cases.
182 // Therefore, the value should be increased.
183 return 150; // for I2C
184 }
185
186private:
187 std::unique_ptr<Interface> _interface{};
188 config_t _cfg{};
189};
190
192namespace rcwl9620 {
193namespace command {
194constexpr uint8_t MEASURE_DISTANCE{0x01};
195} // namespace command
196} // namespace rcwl9620
198
199} // namespace unit
200} // namespace m5
201#endif
@cond0
Definition unit_RCWL9620.hpp:142
An ultrasonic distance measuring sensor unit.
config_t config()
Gets the configration.
Definition unit_RCWL9620.hpp:88
bool measureSingleshot(rcwl9620::Data &d)
Measurement single shot.
Definition unit_RCWL9620.cpp:163
bool startPeriodicMeasurement(const uint32_t interval)
Start periodic measurement.
Definition unit_RCWL9620.hpp:115
float distance() const
Oldest distance (mm)
Definition unit_RCWL9620.hpp:102
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_RCWL9620.hpp:123
void config(const config_t &cfg)
Set the configration.
Definition unit_RCWL9620.hpp:93
Top level namespace of M5stack.
For RCWL9620.
Unit-related namespace.
Settings for begin.
Definition unit_RCWL9620.hpp:64
uint32_t interval_ms
Interval time if start on begin (ms) (100-)
Definition unit_RCWL9620.hpp:68
bool start_periodic
Start periodic measurement on begin?
Definition unit_RCWL9620.hpp:66
Measurement data group.
Definition unit_RCWL9620.hpp:32
float distance() const
Get distance(mm)
Definition unit_RCWL9620.hpp:39