2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Ultrasonic.h"
|
|
|
|
|
|
|
|
|
|
#include "Counter.h"
|
|
|
|
|
#include "DigitalInput.h"
|
|
|
|
|
#include "DigitalOutput.h"
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "Utility.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
constexpr double
|
|
|
|
|
Ultrasonic::kPingTime; ///< Time (sec) for the ping trigger pulse.
|
|
|
|
|
const uint32_t Ultrasonic::kPriority; ///< Priority that the ultrasonic round
|
|
|
|
|
///robin task runs.
|
|
|
|
|
constexpr double
|
|
|
|
|
Ultrasonic::kMaxUltrasonicTime; ///< Max time (ms) between readings.
|
2013-12-15 18:30:16 -05:00
|
|
|
constexpr double Ultrasonic::kSpeedOfSoundInchesPerSec;
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic *Ultrasonic::m_firstSensor =
|
2015-06-23 04:49:51 -07:00
|
|
|
nullptr; // head of the ultrasonic sensor list
|
2014-08-14 00:07:02 -07:00
|
|
|
Task Ultrasonic::m_task;
|
|
|
|
|
std::atomic<bool> Ultrasonic::m_automaticEnabled{false}; // automatic round robin mode
|
2015-06-25 01:54:20 -07:00
|
|
|
priority_mutex Ultrasonic::m_mutex;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Background task that goes through the list of ultrasonic sensors and pings
|
|
|
|
|
* each one in turn. The counter
|
2013-12-15 18:30:16 -05:00
|
|
|
* is configured to read the timing of the returned echo pulse.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* DANGER WILL ROBINSON, DANGER WILL ROBINSON:
|
2015-06-25 15:07:55 -04:00
|
|
|
* This code runs as a task and assumes that none of the ultrasonic sensors will
|
|
|
|
|
* change while it's
|
|
|
|
|
* running. If one does, then this will certainly break. Make sure to disable
|
|
|
|
|
* automatic mode before changing
|
2013-12-15 18:30:16 -05:00
|
|
|
* anything with the sensors!!
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::UltrasonicChecker() {
|
2015-06-23 04:49:51 -07:00
|
|
|
Ultrasonic *u = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
while (m_automaticEnabled) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (u == nullptr) u = m_firstSensor;
|
|
|
|
|
if (u == nullptr) return;
|
2015-06-25 15:07:55 -04:00
|
|
|
if (u->IsEnabled()) u->m_pingChannel->Pulse(kPingTime); // do the ping
|
|
|
|
|
u = u->m_nextSensor;
|
|
|
|
|
Wait(0.1); // wait for ping to return
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the Ultrasonic Sensor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is the common code that initializes the ultrasonic sensor given that
|
|
|
|
|
* there
|
|
|
|
|
* are two digital I/O channels allocated. If the system was running in
|
|
|
|
|
* automatic mode (round robin)
|
|
|
|
|
* when the new sensor is added, it is stopped, the sensor is added, then
|
|
|
|
|
* automatic mode is
|
2013-12-15 18:30:16 -05:00
|
|
|
* restored.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::Initialize() {
|
|
|
|
|
bool originalMode = m_automaticEnabled;
|
|
|
|
|
SetAutomaticMode(false); // kill task when adding a new sensor
|
2015-06-25 01:54:20 -07:00
|
|
|
// link this instance on the list
|
2015-06-25 15:07:55 -04:00
|
|
|
{
|
2015-06-25 01:54:20 -07:00
|
|
|
std::unique_lock<priority_mutex> lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_nextSensor = m_firstSensor;
|
|
|
|
|
m_firstSensor = this;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_counter = new Counter(m_echoChannel); // set up counter for this sensor
|
|
|
|
|
m_counter->SetMaxPeriod(1.0);
|
|
|
|
|
m_counter->SetSemiPeriodMode(true);
|
|
|
|
|
m_counter->Reset();
|
|
|
|
|
m_enabled = true; // make it available for round robin scheduling
|
|
|
|
|
SetAutomaticMode(originalMode);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
static int instances = 0;
|
|
|
|
|
instances++;
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Ultrasonic, instances);
|
|
|
|
|
LiveWindow::GetInstance()->AddSensor("Ultrasonic",
|
|
|
|
|
m_echoChannel->GetChannel(), this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-13 17:45:10 -04:00
|
|
|
* Create an instance of the Ultrasonic Sensor
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is designed to supchannel the Daventech SRF04 and Vex ultrasonic
|
|
|
|
|
* sensors.
|
|
|
|
|
* @param pingChannel The digital output channel that sends the pulse to
|
|
|
|
|
* initiate the sensor sending
|
2013-12-15 18:30:16 -05:00
|
|
|
* the ping.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param echoChannel The digital input channel that receives the echo. The
|
|
|
|
|
* length of time that the
|
2013-12-15 18:30:16 -05:00
|
|
|
* echo is high represents the round trip time of the ping, and the distance.
|
|
|
|
|
* @param units The units returned in either kInches or kMilliMeters
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::Ultrasonic(uint32_t pingChannel, uint32_t echoChannel,
|
|
|
|
|
DistanceUnit units) {
|
|
|
|
|
m_pingChannel = new DigitalOutput(pingChannel);
|
|
|
|
|
m_echoChannel = new DigitalInput(echoChannel);
|
|
|
|
|
m_allocatedChannels = true;
|
|
|
|
|
m_units = units;
|
|
|
|
|
Initialize();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
|
|
|
|
* channel and a DigitalOutput
|
2013-12-15 18:30:16 -05:00
|
|
|
* for the ping channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param pingChannel The digital output object that starts the sensor doing a
|
|
|
|
|
* ping. Requires a 10uS pulse to start.
|
|
|
|
|
* @param echoChannel The digital input object that times the return pulse to
|
|
|
|
|
* determine the range.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param units The units returned in either kInches or kMilliMeters
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel,
|
|
|
|
|
DistanceUnit units) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (pingChannel == nullptr || echoChannel == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIError(NullParameter);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_allocatedChannels = false;
|
|
|
|
|
m_pingChannel = pingChannel;
|
|
|
|
|
m_echoChannel = echoChannel;
|
|
|
|
|
m_units = units;
|
|
|
|
|
Initialize();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
|
|
|
|
* channel and a DigitalOutput
|
2013-12-15 18:30:16 -05:00
|
|
|
* for the ping channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param pingChannel The digital output object that starts the sensor doing a
|
|
|
|
|
* ping. Requires a 10uS pulse to start.
|
|
|
|
|
* @param echoChannel The digital input object that times the return pulse to
|
|
|
|
|
* determine the range.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param units The units returned in either kInches or kMilliMeters
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::Ultrasonic(DigitalOutput &pingChannel, DigitalInput &echoChannel,
|
|
|
|
|
DistanceUnit units) {
|
|
|
|
|
m_allocatedChannels = false;
|
|
|
|
|
m_pingChannel = &pingChannel;
|
|
|
|
|
m_echoChannel = &echoChannel;
|
|
|
|
|
m_units = units;
|
|
|
|
|
Initialize();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor for the ultrasonic sensor.
|
2015-06-25 15:07:55 -04: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
|
2013-12-15 18:30:16 -05:00
|
|
|
* after this sensor is removed (provided this wasn't the last sensor).
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::~Ultrasonic() {
|
|
|
|
|
bool wasAutomaticMode = m_automaticEnabled;
|
|
|
|
|
SetAutomaticMode(false);
|
|
|
|
|
if (m_allocatedChannels) {
|
|
|
|
|
delete m_pingChannel;
|
|
|
|
|
delete m_echoChannel;
|
|
|
|
|
}
|
2015-06-23 04:49:51 -07:00
|
|
|
wpi_assert(m_firstSensor != nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
{
|
2015-06-25 01:54:20 -07:00
|
|
|
std::unique_lock<priority_mutex> lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (this == m_firstSensor) {
|
|
|
|
|
m_firstSensor = m_nextSensor;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_firstSensor == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
SetAutomaticMode(false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2015-06-23 04:49:51 -07:00
|
|
|
wpi_assert(m_firstSensor->m_nextSensor != nullptr);
|
|
|
|
|
for (Ultrasonic *s = m_firstSensor; s != nullptr; s = s->m_nextSensor) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (this == s->m_nextSensor) {
|
|
|
|
|
s->m_nextSensor = s->m_nextSensor->m_nextSensor;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_firstSensor != nullptr && wasAutomaticMode) SetAutomaticMode(true);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Turn Automatic mode on/off.
|
|
|
|
|
* When in Automatic mode, all sensors will fire in round robin, waiting a set
|
|
|
|
|
* time between each sensor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param enabling Set to true if round robin scheduling should start for all
|
|
|
|
|
* the ultrasonic sensors. This
|
|
|
|
|
* scheduling method assures that the sensors are non-interfering because no two
|
|
|
|
|
* sensors fire at the same time.
|
|
|
|
|
* If another scheduling algorithm is preffered, it can be implemented by
|
|
|
|
|
* pinging the sensors manually and waiting
|
2013-12-15 18:30:16 -05:00
|
|
|
* for the results to come back.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::SetAutomaticMode(bool enabling) {
|
|
|
|
|
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;
|
|
|
|
|
if (enabling) {
|
|
|
|
|
// enabling automatic mode.
|
|
|
|
|
// Clear all the counters so no data is valid
|
2015-06-23 04:49:51 -07:00
|
|
|
for (Ultrasonic *u = m_firstSensor; u != nullptr; u = u->m_nextSensor) {
|
2015-06-25 15:07:55 -04:00
|
|
|
u->m_counter->Reset();
|
|
|
|
|
}
|
|
|
|
|
// Start round robin task
|
|
|
|
|
wpi_assert(m_task.Verify() ==
|
|
|
|
|
false); // should be false since was previously disabled
|
2014-08-14 00:07:02 -07:00
|
|
|
m_task = Task("UltrasonicChecker", &Ultrasonic::UltrasonicChecker);
|
|
|
|
|
|
|
|
|
|
// TODO: Currently, lvuser does not have permissions to set task priorities.
|
|
|
|
|
// Until that is the case, uncommenting this will break user code that calls
|
|
|
|
|
// Ultrasonic::SetAutomicMode().
|
|
|
|
|
//m_task.SetPriority(kPriority);
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
|
|
|
|
// disabling automatic mode. Wait for background task to stop running.
|
|
|
|
|
while (m_task.Verify())
|
|
|
|
|
Wait(0.15); // just a little longer than the ping time for round-robin to
|
|
|
|
|
// stop
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// clear all the counters (data now invalid) since automatic mode is stopped
|
2015-06-23 04:49:51 -07:00
|
|
|
for (Ultrasonic *u = m_firstSensor; u != nullptr; u = u->m_nextSensor) {
|
2015-06-25 15:07:55 -04:00
|
|
|
u->m_counter->Reset();
|
|
|
|
|
}
|
2014-08-14 00:07:02 -07:00
|
|
|
m_automaticEnabled = false;
|
|
|
|
|
m_task.join();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Single ping to ultrasonic sensor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Send out a single ping to the ultrasonic sensor. This only works if automatic
|
|
|
|
|
* (round robin)
|
|
|
|
|
* mode is disabled. A single ping is sent out, and the counter should count the
|
|
|
|
|
* semi-period
|
2013-12-15 18:30:16 -05:00
|
|
|
* when it comes in. The counter is reset to make the current value invalid.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::Ping() {
|
|
|
|
|
wpi_assert(!m_automaticEnabled);
|
|
|
|
|
m_counter->Reset(); // reset the counter to zero (invalid data now)
|
|
|
|
|
m_pingChannel->Pulse(
|
|
|
|
|
kPingTime); // do the ping to start getting a single range
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if there is a valid range measurement.
|
2015-06-25 15:07:55 -04:00
|
|
|
* The ranges are accumulated in a counter that will increment on each edge of
|
|
|
|
|
* the echo (return)
|
|
|
|
|
* signal. If the count is not at least 2, then the range has not yet been
|
|
|
|
|
* measured, and is invalid.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Ultrasonic::IsRangeValid() const { return m_counter->Get() > 1; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the range in inches from the ultrasonic sensor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return double Range in inches of the target returned from the ultrasonic
|
|
|
|
|
* sensor. If there is
|
|
|
|
|
* no valid value yet, i.e. at least one measurement hasn't completed, then
|
|
|
|
|
* return 0.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Ultrasonic::GetRangeInches() const {
|
|
|
|
|
if (IsRangeValid())
|
|
|
|
|
return m_counter->GetPeriod() * kSpeedOfSoundInchesPerSec / 2.0;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the range in millimeters from the ultrasonic sensor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return double Range in millimeters of the target returned by the ultrasonic
|
|
|
|
|
* sensor.
|
|
|
|
|
* If there is no valid value yet, i.e. at least one measurement hasn't
|
|
|
|
|
* complted, then return 0.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Ultrasonic::GetRangeMM() const { return GetRangeInches() * 25.4; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the range in the current DistanceUnit for the PIDSource base object.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The range in DistanceUnit
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Ultrasonic::PIDGet() const {
|
|
|
|
|
switch (m_units) {
|
|
|
|
|
case Ultrasonic::kInches:
|
|
|
|
|
return GetRangeInches();
|
|
|
|
|
case Ultrasonic::kMilliMeters:
|
|
|
|
|
return GetRangeMM();
|
|
|
|
|
default:
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the current DistanceUnit that should be used for the PIDSource base
|
|
|
|
|
* object.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param units The DistanceUnit that should be used.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::SetDistanceUnits(DistanceUnit units) { m_units = units; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current DistanceUnit that is used for the PIDSource base object.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The type of DistanceUnit that is being used.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Ultrasonic::DistanceUnit Ultrasonic::GetDistanceUnits() const {
|
|
|
|
|
return m_units;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ultrasonic::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("Value", GetRangeInches());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::StartLiveWindowMode() {}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Ultrasonic::StopLiveWindowMode() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Ultrasonic::GetSmartDashboardType() const { return "Ultrasonic"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
void Ultrasonic::InitTable(ITable *subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *Ultrasonic::GetTable() const { return m_table; }
|