2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Ultrasonic.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-05-26 07:24:53 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Counter.h"
|
|
|
|
|
#include "frc/DigitalInput.h"
|
|
|
|
|
#include "frc/DigitalOutput.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Timer.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-11-19 19:04:28 -08:00
|
|
|
// Automatic round robin mode
|
2015-10-30 01:07:14 -07:00
|
|
|
std::atomic<bool> Ultrasonic::m_automaticEnabled{false};
|
2017-11-16 00:33:51 -08:00
|
|
|
|
2017-11-23 20:46:24 -08:00
|
|
|
std::vector<Ultrasonic*> Ultrasonic::m_sensors;
|
2016-11-04 15:02:29 -07:00
|
|
|
std::thread Ultrasonic::m_thread;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-03-12 15:41:52 -08:00
|
|
|
Ultrasonic::Ultrasonic(int pingChannel, int echoChannel)
|
2015-07-29 16:48:04 -04:00
|
|
|
: m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)),
|
|
|
|
|
m_echoChannel(std::make_shared<DigitalInput>(echoChannel)),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_counter(m_echoChannel) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Initialize();
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddChild(this, m_pingChannel.get());
|
|
|
|
|
wpi::SendableRegistry::AddChild(this, m_echoChannel.get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 15:41:52 -08:00
|
|
|
Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_pingChannel(pingChannel, wpi::NullDeleter<DigitalOutput>()),
|
|
|
|
|
m_echoChannel(echoChannel, wpi::NullDeleter<DigitalInput>()),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_counter(m_echoChannel) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!pingChannel) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "pingChannel");
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|
|
|
|
|
if (!echoChannel) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "echoChannel");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
Initialize();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 15:41:52 -08:00
|
|
|
Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_pingChannel(&pingChannel, wpi::NullDeleter<DigitalOutput>()),
|
|
|
|
|
m_echoChannel(&echoChannel, wpi::NullDeleter<DigitalInput>()),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_counter(m_echoChannel) {
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
|
2021-03-12 15:41:52 -08:00
|
|
|
std::shared_ptr<DigitalInput> echoChannel)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_pingChannel(std::move(pingChannel)),
|
|
|
|
|
m_echoChannel(std::move(echoChannel)),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_counter(m_echoChannel) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Initialize();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::~Ultrasonic() {
|
2018-05-31 20:47:15 -07:00
|
|
|
// Delete the instance of the ultrasonic sensor by freeing the allocated
|
|
|
|
|
// digital channels. If the system was in automatic mode (round robin), then
|
|
|
|
|
// it is stopped, then started again after this sensor is removed (provided
|
|
|
|
|
// this wasn't the last sensor).
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool wasAutomaticMode = m_automaticEnabled;
|
|
|
|
|
SetAutomaticMode(false);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-10-30 01:07:14 -07:00
|
|
|
// No synchronization needed because the background task is stopped.
|
2017-11-23 20:46:24 -08:00
|
|
|
m_sensors.erase(std::remove(m_sensors.begin(), m_sensors.end(), this),
|
|
|
|
|
m_sensors.end());
|
2015-10-30 01:07:14 -07:00
|
|
|
|
|
|
|
|
if (!m_sensors.empty() && wasAutomaticMode) {
|
|
|
|
|
SetAutomaticMode(true);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-21 02:19:59 -04:00
|
|
|
int Ultrasonic::GetEchoChannel() const {
|
|
|
|
|
return m_echoChannel->GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Ultrasonic::Ping() {
|
2023-01-09 02:33:07 +02:00
|
|
|
SetAutomaticMode(false); // turn off automatic round-robin if pinging
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
// Reset the counter to zero (invalid data now)
|
|
|
|
|
m_counter.Reset();
|
|
|
|
|
|
|
|
|
|
// Do the ping to start getting a single range
|
|
|
|
|
m_pingChannel->Pulse(kPingTime);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
bool Ultrasonic::IsRangeValid() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_simRangeValid) {
|
|
|
|
|
return m_simRangeValid.Get();
|
|
|
|
|
}
|
2019-09-29 00:57:16 -07:00
|
|
|
return m_counter.Get() > 1;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::SetAutomaticMode(bool enabling) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (enabling == m_automaticEnabled) {
|
|
|
|
|
return; // ignore the case of no change
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_automaticEnabled = enabling;
|
2015-10-30 01:07:14 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (enabling) {
|
2015-10-30 01:07:14 -07:00
|
|
|
/* Clear all the counters so no data is valid. No synchronization is needed
|
|
|
|
|
* because the background task is stopped.
|
|
|
|
|
*/
|
|
|
|
|
for (auto& sensor : m_sensors) {
|
|
|
|
|
sensor->m_counter.Reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2015-10-30 01:07:14 -07:00
|
|
|
|
2016-11-01 20:12:08 -07:00
|
|
|
m_thread = std::thread(&Ultrasonic::UltrasonicChecker);
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
2015-10-30 01:07:14 -07:00
|
|
|
// Wait for background task to stop running
|
2018-09-24 00:08:25 -07:00
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
m_thread.join();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-16 00:33:51 -08:00
|
|
|
// Clear all the counters (data now invalid) since automatic mode is
|
|
|
|
|
// disabled. No synchronization is needed because the background task is
|
|
|
|
|
// stopped.
|
2015-10-30 01:07:14 -07:00
|
|
|
for (auto& sensor : m_sensors) {
|
|
|
|
|
sensor->m_counter.Reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-01 21:54:56 -07:00
|
|
|
units::meter_t Ultrasonic::GetRange() const {
|
2019-09-29 00:57:16 -07:00
|
|
|
if (IsRangeValid()) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_simRange) {
|
2023-01-09 02:33:07 +02:00
|
|
|
return units::inch_t{m_simRange.Get()};
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2021-06-01 21:54:56 -07:00
|
|
|
return m_counter.GetPeriod() * kSpeedOfSound / 2.0;
|
2019-09-29 00:57:16 -07:00
|
|
|
} else {
|
2021-06-01 21:54:56 -07:00
|
|
|
return 0_m;
|
2019-09-29 00:57:16 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Ultrasonic::IsEnabled() const {
|
|
|
|
|
return m_enabled;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Ultrasonic::SetEnabled(bool enable) {
|
|
|
|
|
m_enabled = enable;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Ultrasonic::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Ultrasonic");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Value", [=, this] { return units::inch_t{GetRange()}.value(); },
|
|
|
|
|
nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
void Ultrasonic::Initialize() {
|
2019-09-29 00:57:16 -07:00
|
|
|
m_simDevice = hal::SimDevice("Ultrasonic", m_echoChannel->GetChannel());
|
|
|
|
|
if (m_simDevice) {
|
|
|
|
|
m_simRangeValid = m_simDevice.CreateBoolean("Range Valid", false, true);
|
|
|
|
|
m_simRange = m_simDevice.CreateDouble("Range (in)", false, 0.0);
|
|
|
|
|
m_pingChannel->SetSimDevice(m_simDevice);
|
|
|
|
|
m_echoChannel->SetSimDevice(m_simDevice);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
bool originalMode = m_automaticEnabled;
|
|
|
|
|
SetAutomaticMode(false); // Kill task when adding a new sensor
|
|
|
|
|
// Link this instance on the list
|
|
|
|
|
m_sensors.emplace_back(this);
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
m_counter.SetMaxPeriod(1_s);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_counter.SetSemiPeriodMode(true);
|
|
|
|
|
m_counter.Reset();
|
|
|
|
|
m_enabled = true; // Make it available for round robin scheduling
|
|
|
|
|
SetAutomaticMode(originalMode);
|
|
|
|
|
|
|
|
|
|
static int instances = 0;
|
|
|
|
|
instances++;
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Ultrasonic, instances);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddLW(this, "Ultrasonic", m_echoChannel->GetChannel());
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ultrasonic::UltrasonicChecker() {
|
|
|
|
|
while (m_automaticEnabled) {
|
|
|
|
|
for (auto& sensor : m_sensors) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_automaticEnabled) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
if (sensor->IsEnabled()) {
|
|
|
|
|
sensor->m_pingChannel->Pulse(kPingTime); // do the ping
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Wait(100_ms); // wait for ping to return
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|