Custom Sensor objects and Live Window

We implemented two custom sensor classes so we could utilize some sensors not handled natively by the WPI Library.

In each, we register them with the LiveWindow; our goal is to see the sensor values in the LiveWindow after they have been transformed by our code to real-world values.

Internally, these sensor classes use an AnalogChannel.

We do see the AnalogChannel objects corresponding to our sensors in the Live Window. But we never see the sensor objects in the Live Window, even though we register them.

Attached below is code for one of the sensor classes.

My best guess is because the AnalogChannel objects use the same module/channel as our sensor class does, and the AnalogChannel is registered with LiveWindow first, our sensor class gets ignored for some reason.

If you have any ideas on how to solve this problem, please let me know…

  • scott

#include “ProximitySensor.h”
#include “NetworkCommunication/UsageReporting.h”
#include “Timer.h”
#include “WPIErrors.h”
#include “LiveWindow/LiveWindow.h”
#include <time.h>
#include <math.h>
#include “AnalogModule.h”

ProximitySensor::ProximitySensor(UINT8 analog_module,UINT32 analog_channel, SensorRange range) :
input(analog_module, analog_channel)
{
this->range = range;
InitProximitySensor();
}

/**

  • Initialize the ProximitySensor.
    */
    void ProximitySensor::InitProximitySensor()
    {
    // Be careful not to set the average bits more than once.
    UINT32 num_average_bits = 4;
    if ( input.GetAverageBits() != num_average_bits )
    {
    input.SetAverageBits(num_average_bits); // 2^4 = 16-sample average
    float sampleRate = 50.0 *
    (1 << (num_average_bits));
    input.GetModule()->SetSampleRate(sampleRate);
    Wait(0.2);
    }
    LiveWindow::GetInstance()->AddSensor(“Proximity”, input.GetModuleNumber(), input.GetChannel(), this);
    }

/**

  • Delete the ProximitySensor.
    */
    ProximitySensor::~ProximitySensor()
    {
    }

/**

  • Return the actual distance in millimeters.
  • @return the current distance in millimeters.
    */

float ProximitySensor::GetDistanceMM( void )
{
float voltage = input.GetAverageVoltage();

// Transform voltage to distance.  Exponential values
// were derived from the datasheets:
//
// GP2D120XJ00F (Short Range [30cm]):  https://www.sparkfun.com/datasheets/Sensors/Infrared/GP2D120XJ00F_SS.pdf
// GP2Y0A21YK (Medium Range [80cm]):  http://www.sparkfun.com/datasheets/Components/GP2Y0A21YK.pdf
// GP2Y0A02YK0F (Long Range [150cm]):  https://www.sparkfun.com/datasheets/Sensors/Infrared/gp2y0a02yk_e.pdf

float distance_mm;
switch ( range )
{
case kShortRange:

	distance_mm = 126.27 * pow(voltage, -1.102);
	break;
	
case kMediumRange:
	distance_mm = 267.1 * pow(voltage, -1.23);
	break;
	
case kLongRange:
	distance_mm = 0.0;	// TODO
	break;
}

return distance_mm;

}

/**

  • Get the angle in degrees for the PIDSource base object.
  • @return The angle in degrees.
    */
    double ProximitySensor::PIDGet()
    {
    return GetDistanceMM();
    }

void ProximitySensor::UpdateTable() {
if (m_table != NULL) {
m_table->PutNumber(“Value”, GetDistanceMM());
}
}

void ProximitySensor::StartLiveWindowMode() {

}

void ProximitySensor::StopLiveWindowMode() {

}

std::string ProximitySensor::GetSmartDashboardType() {
return “ProximitySensor”;
}

void ProximitySensor::InitTable(ITable *subTable) {
m_table = subTable;
UpdateTable();
}

ITable * ProximitySensor::GetTable() {
return m_table;
}

I think this needs to correspond to a known type in the SmartDashboard to work with LiveWindow.

List of types (actual type names I think are all the same -“type”): http://firstforge.wpi.edu/integration/viewcvs/viewcvs.cgi/trunk/smartdashboard/src/edu/wpi/first/smartdashboard/types/named/?root=smart_dashboard&system=exsy1002

Thanks, that really helps a lot.