Go to Post Do you know how, say, Native American cultures were kept? They had storytellers, sages, and the like. They passed on their legends and culture to the next generation. So will we. - EricH [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #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
  #2   Spotlight this post!  
Unread 17-02-2012, 12:55
cjlane1138 cjlane1138 is offline
Team 1138
FRC #1138 (Eagle Engineering)
Team Role: Leadership
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Los Angeles
Posts: 88
cjlane1138 is an unknown quantity at this point
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

First, you cannot put code like that in a header file. Header files are meant to declare variables and functions, not use them. You have to put all of the initialization and actual code in a .cpp file. Second, you can just set it up like this:
Code:
void GetDistance()
{
AnalogChannel *range;
range = new AnalogChannel(1,1);
float volt;
volt = range->GetVoltage();
float inches;
inches = volt * 100;
return inches;
}
__________________
Eagle Engineering 1138
Reply With Quote
  #3   Spotlight this post!  
Unread 17-02-2012, 14:32
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

Quote:
Originally Posted by cjlane1138 View Post
First, you cannot put code like that in a header file. Header files are meant to declare variables and functions, not use them. You have to put all of the initialization and actual code in a .cpp file.
Not true. When declaring an object class in a header file, you have an option to include the method bodies in the class definition. We did this throughout all our code. This is because if you separate the definition part in the header file and method bodies in a CPP file, any changes you made to the method interface require you to change two files to match each other. This arrangement is beneficial for releasing libraries without revealing how the methods are implemented (i.e. releasing only the header file without releasing the CPP file). But for our purpose, we don't have this restriction and therefore, it is a lot simpler to include all method bodies in the class definitions in the header files.
__________________
Reply With Quote
  #4   Spotlight this post!  
Unread 17-02-2012, 14:48
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
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

I chose to write this as a class to make it more standardized with other similar sensors (and so I could use it with a PID).

As for all in the h file, my attempt was to just have all of the operations compiled as inline for the sake of simplicity, however it may not be completely legal (my native language is C, so I'm a bit rusty in C++fu). I know you can do this with individual functions, however the whole class may cause issues. I'll upload it as a cpp and h file just to avoid the confusion/possibility of broke tomorrow.

Thanks
Reply With Quote
  #5   Spotlight this post!  
Unread 18-02-2012, 16:06
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
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

So, I made some fixes to the code, the primary change is that it now (properly) uses an inches/volt conversion factor instead of millivolts (which the analog channel is not providing).

Our range finder isn't fully working yet, but we think we're down to an issue with the frame of the robot being slightly within the cone of the audio pulse, and causing some erratic responses.

If you end up using this code, please let me know (if nobody finds it helpful, I'll be alot less likely to make the effort to post this stuff in the future )

Here's the C file for those who just want to peak:

Code:
#include "AnalogRangeFinder.h"
#include "AnalogModule.h"
#include "AnalogChannel.h"

void AnalogRangeFinder::InitAnalogRangeFinder(void)
{
	inchesPerVolt = ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT;
}

AnalogRangeFinder::AnalogRangeFinder(UINT8 moduleNumber, UINT32 channel)
{
	m_analogChannel = new AnalogChannel(moduleNumber,channel);
	m_allocatedChannel = true;
	InitAnalogRangeFinder();
}

AnalogRangeFinder::AnalogRangeFinder(UINT32 channel)
{
	m_analogChannel = new AnalogChannel(channel);
	m_allocatedChannel = true;
	InitAnalogRangeFinder();
}

AnalogRangeFinder::AnalogRangeFinder(AnalogChannel *channel)
{
	if (channel != NULL)
	{
		m_analogChannel = channel;
		InitAnalogRangeFinder();
		m_allocatedChannel = false;
	}
}

AnalogRangeFinder::AnalogRangeFinder(AnalogChannel &channel)
{
	m_analogChannel = &channel;
	m_allocatedChannel = false;
	InitAnalogRangeFinder();
}

AnalogRangeFinder::~AnalogRangeFinder()
{
	if (m_allocatedChannel)
	{
		delete m_analogChannel;
	}
}

float AnalogRangeFinder::GetRangeInches()
{
	return m_analogChannel->GetVoltage() * inchesPerVolt;
}

float AnalogRangeFinder::GetVoltage()
{
	return m_analogChannel->GetVoltage();
}

void AnalogRangeFinder::SetSensitivity(float inches_per_volt)
{
	inchesPerVolt = inches_per_volt;
}


// PIDSource interface
double AnalogRangeFinder::PIDGet()
{
	return GetRangeInches();
}
Attached Files
File Type: cpp AnalogRangeFinder.cpp (1.4 KB, 41 views)
File Type: h AnalogRangeFinder.h (1,020 Bytes, 42 views)
Reply With Quote
  #6   Spotlight this post!  
Unread 18-02-2012, 21:56
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

Quote:
Originally Posted by DjScribbles View Post
So, I made some fixes to the code, the primary change is that it now (properly) uses an inches/volt conversion factor instead of millivolts (which the analog channel is not providing).

Our range finder isn't fully working yet, but we think we're down to an issue with the frame of the robot being slightly within the cone of the audio pulse, and causing some erratic responses.

If you end up using this code, please let me know (if nobody finds it helpful, I'll be alot less likely to make the effort to post this stuff in the future )

Here's the C file for those who just want to peak:
Just wanted to reply and say that I used you code, and it works very nice... Like you, we have some hardware issues, but I think that I've pretty much worked them out. One question I have is that the rangefinder seems to max out at about 250 inches, and not function well in small-mid sized rooms is that what other people are getting?

Last edited by agartner01 : 18-02-2012 at 22:05. Reason: Clarification
Reply With Quote
  #7   Spotlight this post!  
Unread 29-03-2012, 22:30
enrique's Avatar
enrique enrique is offline
Registered User
FRC #1251 (Tech Tigers)
Team Role: Electrical
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Florida
Posts: 87
enrique will become famous soon enough
Send a message via Yahoo to enrique
Angry Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

Been using this code until the other day we hit the sensor against the bridge and that was the end of that. It worked fine before that.
Reply With Quote
  #8   Spotlight this post!  
Unread 30-03-2012, 12:52
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
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

Great , sorry about your luck, you could try asking teams in your area (if there are any) for a loaner/spare.

I know we're really not using ours anymore, I found the potential for interference from robots (or the ball return under the hoop) to be too risky to use it. (the drivers also tend to have better luck eyeballing the shot than the pid's have at getting it right)
Reply With Quote
  #9   Spotlight this post!  
Unread 01-08-2012, 12:47
jawebste's Avatar
jawebste jawebste is offline
Mentor - HAZMATs 2145
AKA: Jean-Ann Webster
FRC #2145 (HAZMATs)
Team Role: Mentor
 
Join Date: Sep 2008
Rookie Year: 2007
Location: Lake Fenton
Posts: 27
jawebste is a jewel in the roughjawebste is a jewel in the roughjawebste is a jewel in the roughjawebste is a jewel in the rough
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

Quote:
Originally Posted by agartner01 View Post
Just wanted to reply and say that I used you code, and it works very nice... Like you, we have some hardware issues, but I think that I've pretty much worked them out. One question I have is that the rangefinder seems to max out at about 250 inches, and not function well in small-mid sized rooms is that what other people are getting?
How did you apply the sonar on your 2012 robot? We are working with sensors over the summer and program in LabVIEW, but can understand some equivalent C code. We were hoping to use the sonar to backup to a fixed object about 5" away, but the sonar does not update in real time for us and when testing the sensor of the bot, it varies wildly in accuracy. Outside of 6" we seem to have repeatability within an inch. Actually the farther away we get, the more accurate it seems to be. We would appreciate any feedback. Thanks.
Reply With Quote
  #10   Spotlight this post!  
Unread 01-08-2012, 13:14
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
Re: Freebie LV-MaxSonar-EZ1 Code (Sonar Range Finder)

It's been a while since I've looked at the sonar sensor, but if I remember right, near objects are not detected very accurately (if the echo returns quickly, it's likely interfering with itself as it emits the pulse).

We used ours from the key to detect how close the basket was. However, we found that the ball returns caused were being detected in competition rather than the backboard, and due to lack of time on the field to diagnose it, we just dropped the functionality.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:47.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi