Disclaimer!! Currently this isn't tested!
So, apparantly this code wasn't included in WPILib, so I wrote one myself. I haven't tested it yet so please use with caution, but it's primarily copy-pasted from a mashup of gyroscope and accellerometer classes.
Reviews and corrections are welcome. This weekend when we get the thing actually wired I'll post with any updates if needed.
(this is the h file)
Code:
#ifndef ANALOGRANGEFINDER_H
#define ANALOGRANGEFINDER_H
#include "SensorBase.h"
#include "PIDSource.h"
class AnalogChannel;
class AnalogModule;
#define ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_MV 0.1024F
//Designed for the MaxBotix LV-MaxSonarŽ-EZ1, but should work for any analog output sonar module
class AnalogRangeFinder : public SensorBase, public PIDSource
{
private:
float inchesPerMillivolt;
AnalogChannel *m_analogChannel;
bool m_allocatedChannel;
void InitAnalogRangeFinder(void)
{
inchesPerMillivolt = ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_MV;
}
public:
AnalogRangeFinder(UINT8 moduleNumber, UINT32 channel);
explicit AnalogRangeFinder(UINT32 channel)
{
m_analogChannel = new AnalogChannel(channel);
m_allocatedChannel = true;
InitAnalogRangeFinder();
}
explicit AnalogRangeFinder(AnalogChannel *channel)
{
if (channel != NULL)
{
m_analogChannel = channel;
InitAnalogRangeFinder();
m_allocatedChannel = false;
}
}
explicit AnalogRangeFinder(AnalogChannel &channel)
{
m_analogChannel = &channel;
m_allocatedChannel = false;
InitAnalogRangeFinder();
}
virtual ~AnalogRangeFinder()
{
if (m_allocatedChannel)
{
delete m_analogChannel;
}
}
virtual float GetRangeInches()
{
return m_analogChannel->GetVoltage() * inchesPerMillivolt;
}
void SetSensitivity(float inches_per_millivolt)
{
inchesPerMillivolt = inches_per_millivolt;
}
// PIDSource interface
double PIDGet()
{
return GetRangeInches();
}
};
#endif
I would rather have tested this before putting it out here, but seeing as everyones at a crunch for time, I'd rather give someone hope. If this thing happens to be buggy, check back here saturday afternoon/evening and I should have it fixed.