OCCRA
Go to Post Like a wizard, Car Nack is never late. He always arrives precisely when he meets you. - EricH [more]
Home
Go Back   Chief Delphi > Technical > Programming > WindRiver C++
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-10-2009, 03:10 PM
ellisk ellisk is offline
Registered User
FRC #1540
 
Join Date: Dec 2008
Location: Vancouver, WA
Posts: 41
ellisk is on a distinguished road
Hardware integration of sensors?

Hi everyone,
I am trying to integrate the readings from an accelerometer to get velocity. When I tried to do this in software, it was laughingly inaccurate. So I looked at Gyro.cpp in WPILib to see how they integrate angular velocity to get heading. They use a hardware accumulator (integrator). I tried to copy their code and adapt it for use with an accelerometer (see accelerometer.cpp). I came up with this class:

Accel2Vel.h:
Code:
#pragma once

class AnalogChannel;

//This class is an acceleromiter which integrates over times to get velocity
// Uses hardware integration, just like the gyro :)
class Accel2Vel {
public:
	Accel2Vel(unsigned slot,unsigned channel);
	~Accel2Vel();
	double getVel();
	void reset();
private:
	AnalogChannel *achan; //different from 4chan
	double voltsPerG;
	double zeroGVoltage;
	double offset;
	
	void initAccel(); // sets voltsPerG and zeroGVoltage
};
Accel2Vel.cpp:
Code:
#include "Accel2Vel.h"
#include "AnalogModule.h"
#include "AnalogChannel.h"
#include "Utility.h"
#include "WPIStatus.h"
#include "Timer.h"

//stolen from gyro.cpp
static const UINT32 kOversampleBits = 7;
static const UINT32 kAverageBits = 3;
static const float kCalibrationSampleTime = 5.0;
static const float kSamplesPerSecond = 50.0;

Accel2Vel::Accel2Vel(unsigned slot,unsigned channel)
{
	achan = new AnalogChannel(slot,channel);
	initAccel();
}

void Accel2Vel::initAccel()
{
	if (!achan->IsAccumulatorChannel()) { // we need accumulator for integration
		printf ("ERROR: accel chan not accumulator\n");
		delete achan;
		achan = NULL;
		return;
	}
	
	voltsPerG = 1.0;
	zeroGVoltage = 2.5;
	
	// mostly stolen from Gyro.cpp
	achan->SetAverageBits(kAverageBits);
	achan->SetOversampleBits(kOversampleBits);
	float sampleRate = kSamplesPerSecond * (1 << (kAverageBits + kOversampleBits));
	achan->GetModule()->SetSampleRate(sampleRate);
	Wait(1.0); // calibration
	
	achan->InitAccumulator();
	Wait(kCalibrationSampleTime);
	
	INT64 value;
	UINT32 count;
	
	achan->GetAccumulatorOutput(&value, &count);
	
	UINT32 center = (UINT32)((float)value / (float)count + .5);
	
	offset = ((float)value / (float)count) - (float)center;
	
	achan->SetAccumulatorCenter(center);
	achan->SetAccumulatorDeadband(0); ///< TODO: compute / parameterize this
	achan->ResetAccumulator();
}

void Accel2Vel::reset()
{
	achan->ResetAccumulator();
}

// get the current velocity
double Accel2Vel::getVel()
{
	if (achan == NULL)
		return 0.0;
	
	// "adapted" (stolen) from gyro.cpp
	INT64 rawValue;
	UINT32 count;
	
	achan->GetAccumulatorOutput(&rawValue, &count);
	
	INT64 value = rawValue - (INT64)((float)count * offset);
	
	double scaledValue = value * 1e-9 * (double)achan->GetLSBWeight() * (double)(1 << achan->GetAverageBits()) /
	                       (achan->GetModule()->GetSampleRate() * voltsPerG);
	
	return scaledValue;
}


Accel2Vel::~Accel2Vel()
{
	if (achan != NULL)
		delete achan;
}
However, it seems to always fail when checking to make sure that the analog channel has an accumulator ( if (!achan->IsAccumulatorChannel()) ). Does anyone know why this might be? Do I need to do something special to give an analog channel an accumulator?

Thanks,
-Kevin
Reply With Quote
  #2   Spotlight this post!  
Unread 01-10-2009, 05:05 PM
virtuald virtuald is offline
Registered User
AKA: Dustin Spicuzza
FRC #1418
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Northern Virginia
Posts: 509
virtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to behold
Re: Hardware integration of sensors?

Thats a really neat idea.. try inheriting from SensorBase, that might do it (thats what Gyro inherits from).

Also.. #pragma once is MSVC only... it won't work here. Use the standard #ifndef .. #define .. #endif clauses.
__________________
Co-maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables (NetworkTables for Python)

Team #1418: 2014 VA Regional: Finalists, #2 seed overall, Industrial Design Award; DC Regional: Finalists, #6 alliance captain
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


FRC Software Resources (including 2014 python code): http://www.virtualroadside.com/FRC/
WPILib Doxygen Repo: http://www.virtualroadside.com/WPILib/index.html
Reply With Quote
  #3   Spotlight this post!  
Unread 01-10-2009, 05:09 PM
virtuald virtuald is offline
Registered User
AKA: Dustin Spicuzza
FRC #1418
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Northern Virginia
Posts: 509
virtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to behold
Re: Hardware integration of sensors?

Oh, I see. Thats not it at all. If you look at AnalogChannel.cpp, there appear to be two accumulator channels:

Code:
const UINT32 AnalogChannel::kAccumulatorChannels[] = {1, 2};
So if you're not using one of those, then it won't work.
__________________
Co-maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables (NetworkTables for Python)

Team #1418: 2014 VA Regional: Finalists, #2 seed overall, Industrial Design Award; DC Regional: Finalists, #6 alliance captain
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


FRC Software Resources (including 2014 python code): http://www.virtualroadside.com/FRC/
WPILib Doxygen Repo: http://www.virtualroadside.com/WPILib/index.html
Reply With Quote
  #4   Spotlight this post!  
Unread 01-11-2009, 10:49 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Dallas, Texas
Posts: 212
wt200999 will become famous soon enoughwt200999 will become famous soon enough
Send a message via MSN to wt200999
Re: Hardware integration of sensors?

I am looking into the same thing, post any progress if you could.

Take note of the following as well:

In Gyro.cpp
Code:
	if (!m_analog->IsAccumulatorChannel())
	{
		wpi_fatal(GyroNotAccumulatorChannel);
In WPIStatus.h

Code:
S(GyroNotAccumulatorChannel, -6, "Gyro can only be used with Analog Channel 1 on either module");
I guess I will have to use that 2nd breakout afterall...
__________________
FIRST LEGO League 2005
FRC Team 870 Student 2006 - 2009
FRC Team 870 College Student 2009 - 2013
FRC Team 3005 Mentor 2014

Reply With Quote
  #5   Spotlight this post!  
Unread 01-11-2009, 10:53 PM
virtuald virtuald is offline
Registered User
AKA: Dustin Spicuzza
FRC #1418
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Northern Virginia
Posts: 509
virtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to beholdvirtuald is a splendid one to behold
Re: Hardware integration of sensors?

Odd, I wonder why the code seems to indicate that channel 2 can be used as well. I haven't messed with it yet.
__________________
Co-maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables (NetworkTables for Python)

Team #1418: 2014 VA Regional: Finalists, #2 seed overall, Industrial Design Award; DC Regional: Finalists, #6 alliance captain
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


FRC Software Resources (including 2014 python code): http://www.virtualroadside.com/FRC/
WPILib Doxygen Repo: http://www.virtualroadside.com/WPILib/index.html
Reply With Quote
  #6   Spotlight this post!  
Unread 01-11-2009, 10:57 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Dallas, Texas
Posts: 212
wt200999 will become famous soon enoughwt200999 will become famous soon enough
Send a message via MSN to wt200999
Re: Hardware integration of sensors?

Yeah I was just wondering the same thing. Hopefully 1 and 2 work, then I can take out the 2nd analog thing and save some weight.

I remember getting that one fatal error, I had my gyro in slot 3, I guess one quick way to test it is to see if it gives that error in slot 2.
__________________
FIRST LEGO League 2005
FRC Team 870 Student 2006 - 2009
FRC Team 870 College Student 2009 - 2013
FRC Team 3005 Mentor 2014

Reply With Quote
  #7   Spotlight this post!  
Unread 01-12-2009, 12:38 AM
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 7,842
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Hardware integration of sensors?

During the beta period, the FPGA had 1 accumulator on channel 1 of module 1 and 1 accumulator on channel 1 of module 2. It was decided that it made more sense to have them both on the same module, to lessen the chance that people would have to use module 2.

The const in AnalogChannel.cpp is correct, the text of the assertion is not. As noted in the documentation, bugs should be on the forums at forums.usfirst.org. Patches are also welcome there.
Reply With Quote
  #8   Spotlight this post!  
Unread 01-12-2009, 06:54 PM
ellisk ellisk is offline
Registered User
FRC #1540
 
Join Date: Dec 2008
Location: Vancouver, WA
Posts: 41
ellisk is on a distinguished road
Re: Hardware integration of sensors?

Thanks for the help! When I put it on Channel 1, it (sort of) works. It works in the sense that it now gets values, but they are all bad (they're always increasing, and at a VERY high rate). Does anyone know what might be wrong?

Thanks.
Reply With Quote
  #9   Spotlight this post!  
Unread 01-12-2009, 07:56 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Dallas, Texas
Posts: 212
wt200999 will become famous soon enoughwt200999 will become famous soon enough
Send a message via MSN to wt200999
Re: Hardware integration of sensors?

I kind of have the same issue. It spits out numbers, it doesn't really go up at a really high rate, but the data is still completly unuseable as it is right now... How can I increase the useablility of this sensor...
__________________
FIRST LEGO League 2005
FRC Team 870 Student 2006 - 2009
FRC Team 870 College Student 2009 - 2013
FRC Team 3005 Mentor 2014

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible CD-TBA integration EHaskins CD Forum Support 33 03-24-2008 02:54 PM
Accelerometer integration Leav Programming 4 01-27-2006 10:56 AM
CMU Integration Overview???? Mumford 406 Programming 1 02-09-2005 05:17 PM
Autonomous Camera Integration GregC Programming 2 02-06-2005 01:34 PM
Autonomous Camera Integration GregC Programming 1 02-05-2005 05:56 PM


All times are GMT -5. The time now is 11:36 PM.

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


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