mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
[wpilib] Add support for Sharp IR sensors (#6023)
This commit is contained in:
64
wpilibc/src/main/native/cpp/SharpIR.cpp
Normal file
64
wpilibc/src/main/native/cpp/SharpIR.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/SharpIR.h"
|
||||
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/AnalogInput.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
SharpIR SharpIR::GP2Y0A02YK0F(int channel) {
|
||||
return SharpIR(channel, 62.28, -1.092, 20, 150.0);
|
||||
}
|
||||
|
||||
SharpIR SharpIR::GP2Y0A21YK0F(int channel) {
|
||||
return SharpIR(channel, 26.449, -1.226, 10.0, 80.0);
|
||||
}
|
||||
|
||||
SharpIR SharpIR::GP2Y0A41SK0F(int channel) {
|
||||
return SharpIR(channel, 12.354, -1.07, 4.0, 30.0);
|
||||
}
|
||||
|
||||
SharpIR SharpIR::GP2Y0A51SK0F(int channel) {
|
||||
return SharpIR(channel, 5.2819, -1.161, 2.0, 15.0);
|
||||
}
|
||||
|
||||
SharpIR::SharpIR(int channel, double a, double b, double minCM, double maxCM)
|
||||
: m_sensor(channel), m_A(a), m_B(b), m_minCM(minCM), m_maxCM(maxCM) {
|
||||
wpi::SendableRegistry::AddLW(this, "SharpIR", channel);
|
||||
|
||||
m_simDevice = hal::SimDevice("SharpIR", m_sensor.GetChannel());
|
||||
if (m_simDevice) {
|
||||
m_simRange = m_simDevice.CreateDouble("Range (cm)", false, 0.0);
|
||||
m_sensor.SetSimDevice(m_simDevice);
|
||||
}
|
||||
}
|
||||
|
||||
int SharpIR::GetChannel() const {
|
||||
return m_sensor.GetChannel();
|
||||
}
|
||||
|
||||
units::centimeter_t SharpIR::GetRange() const {
|
||||
double distance;
|
||||
|
||||
if (m_simRange) {
|
||||
distance = m_simRange.Get();
|
||||
} else {
|
||||
// Don't allow zero/negative values
|
||||
auto v = std::max(m_sensor.GetVoltage(), 0.00001);
|
||||
distance = m_A * std::pow(v, m_B);
|
||||
}
|
||||
|
||||
// Always constrain output
|
||||
return units::centimeter_t{std::max(std::min(distance, m_maxCM), m_minCM)};
|
||||
}
|
||||
|
||||
void SharpIR::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Ultrasonic");
|
||||
builder.AddDoubleProperty(
|
||||
"Value", [=, this] { return GetRange().value(); }, nullptr);
|
||||
}
|
||||
24
wpilibc/src/main/native/cpp/simulation/SharpIRSim.cpp
Normal file
24
wpilibc/src/main/native/cpp/simulation/SharpIRSim.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/simulation/SharpIRSim.h"
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/length.h>
|
||||
|
||||
#include "frc/simulation/SimDeviceSim.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
SharpIRSim::SharpIRSim(const SharpIR& sharpIR)
|
||||
: SharpIRSim(sharpIR.GetChannel()) {}
|
||||
|
||||
SharpIRSim::SharpIRSim(int channel) {
|
||||
frc::sim::SimDeviceSim deviceSim{"SharpIR", channel};
|
||||
m_simRange = deviceSim.GetDouble("Range (cm)");
|
||||
}
|
||||
|
||||
void SharpIRSim::SetRange(units::centimeter_t rng) {
|
||||
m_simRange.Set(rng.value());
|
||||
}
|
||||
99
wpilibc/src/main/native/include/frc/SharpIR.h
Normal file
99
wpilibc/src/main/native/include/frc/SharpIR.h
Normal file
@@ -0,0 +1,99 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/length.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/AnalogInput.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class SharpIR : public wpi::Sendable, public wpi::SendableHelper<SharpIR> {
|
||||
public:
|
||||
/**
|
||||
* Sharp GP2Y0A02YK0F is an analog IR sensor capable of measuring
|
||||
* distances from 20cm to 150cm.
|
||||
*
|
||||
* @param channel Analog input channel the sensor is connected to
|
||||
*
|
||||
* @return sensor object
|
||||
*/
|
||||
static SharpIR GP2Y0A02YK0F(int channel);
|
||||
|
||||
/**
|
||||
* Sharp GP2Y0A21YK0F is an analog IR sensor capable of measuring
|
||||
* distances from 10cm to 80cm.
|
||||
*
|
||||
* @param channel Analog input channel the sensor is connected to
|
||||
*
|
||||
* @return sensor object
|
||||
*/
|
||||
static SharpIR GP2Y0A21YK0F(int channel);
|
||||
|
||||
/**
|
||||
* Sharp GP2Y0A41SK0F is an analog IR sensor capable of measuring
|
||||
* distances from 4cm to 30cm.
|
||||
*
|
||||
* @param channel Analog input channel the sensor is connected to
|
||||
*
|
||||
* @return sensor object
|
||||
*/
|
||||
static SharpIR GP2Y0A41SK0F(int channel);
|
||||
|
||||
/**
|
||||
* Sharp GP2Y0A51SK0F is an analog IR sensor capable of measuring
|
||||
* distances from 2cm to 15cm.
|
||||
*
|
||||
* @param channel Analog input channel the sensor is connected to
|
||||
*
|
||||
* @return sensor object
|
||||
*/
|
||||
static SharpIR GP2Y0A51SK0F(int channel);
|
||||
|
||||
/**
|
||||
* Manually construct a SharpIR object. The distance is computed using this
|
||||
* formula: A*v ^ B. Prefer to use one of the static factories to create this
|
||||
* device instead.
|
||||
*
|
||||
* @param channel Analog input channel the sensor is connected to
|
||||
* @param a Constant A
|
||||
* @param b Constant B
|
||||
* @param minCM Minimum distance to report in centimeters
|
||||
* @param maxCM Maximum distance to report in centimeters
|
||||
*/
|
||||
SharpIR(int channel, double a, double b, double minCM, double maxCM);
|
||||
|
||||
/**
|
||||
* Get the analog input channel number.
|
||||
*
|
||||
* @return analog input channel
|
||||
*/
|
||||
int GetChannel() const;
|
||||
|
||||
/**
|
||||
* Get the range from the distance sensor.
|
||||
*
|
||||
* @return range of the target returned by the sensor
|
||||
*/
|
||||
units::centimeter_t GetRange() const;
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
AnalogInput m_sensor;
|
||||
|
||||
hal::SimDevice m_simDevice;
|
||||
hal::SimDouble m_simRange;
|
||||
|
||||
double m_A;
|
||||
double m_B;
|
||||
double m_minCM;
|
||||
double m_maxCM;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
25
wpilibc/src/main/native/include/frc/simulation/SharpIRSim.h
Normal file
25
wpilibc/src/main/native/include/frc/simulation/SharpIRSim.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/length.h>
|
||||
|
||||
#include "frc/SharpIR.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class SharpIRSim {
|
||||
public:
|
||||
explicit SharpIRSim(const SharpIR& sharpIR);
|
||||
explicit SharpIRSim(int channel);
|
||||
|
||||
void SetRange(units::centimeter_t rng);
|
||||
|
||||
private:
|
||||
hal::SimDouble m_simRange;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user