View Single Post
  #1   Spotlight this post!  
Unread 16-02-2012, 20:10
DjScribbles DjScribbles is offline
Programming Mentor
AKA: Joe S
FRC #2474 (Team Excel)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Niles MI
Posts: 284
DjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to behold
Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

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.
Attached Files
File Type: h AnalogRangeFinder.h (1.5 KB, 11 views)

Last edited by DjScribbles : 16-02-2012 at 20:14. Reason: Added code
Reply With Quote