|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Analog potentiometer to counter
I'm trying to connect an analog pot to a counter to keep track of how many revolutions the pot has made. There are a just a few examples of this. Can anybody look at this class and see if there are obvious errors? I know AnalogTriggerOutput isn't the most tested code path.
getturns() currently doesn't return anything but zero. I want it to increase on a clockwise revolution and decrease on a counterclockwise revolution. Code:
#include "AnalogChannelVolt.h"
#include <math.h>
static const float rev = 5.0;
static const float halfrev = rev/2;
static const float scale = rev/(4.8-.2);
static const int ratio = 2; // ratio of pot to finished gear, must be int
#define INV
AnalogChannelVolt::AnalogChannelVolt(UINT8 modulenumber, UINT32 channel)
: AnalogChannel(modulenumber, channel)
{
m_trig = new AnalogTrigger(modulenumber, channel);
m_trig->SetFiltered(true);
m_trig->SetLimitsVoltage(0.5,4.5);
m_count = new Counter();
m_count->SetUpDownCounterMode();
m_count->SetUpSource(m_trig, AnalogTriggerOutput::kRisingPulse);
m_count->SetDownSource(m_trig, AnalogTriggerOutput::kFallingPulse);
m_count->SetUpSourceEdge(true,false);
m_count->SetDownSourceEdge(true,false);
m_count->Reset();
m_count->Start();
}
float AnalogChannelVolt::GetAverageVoltage()
{
return GetVoltage();
}
void AnalogChannelVolt::ResetTurns()
{
m_count->Stop();
m_count->Reset();
m_count->Start();
}
float AnalogChannelVolt::GetVoltage()
{
float temp = AnalogChannel::GetVoltage();
temp = (((temp - halfrev) * scale) + halfrev); // scale
if(temp < 0) temp = 0; // min
if(temp > rev) temp = rev; // max
temp = (temp / ratio) + ((m_count->Get() % ratio) * halfrev); // half scale
#ifdef INV
temp = rev - temp; // inverse
#endif
return temp;
}
int AnalogChannelVolt::getturns()
{
return m_count->Get();
}
double AnalogChannelVolt::PIDGet()
{
return GetVoltage();
}
AnalogChannelVolt::~AnalogChannelVolt()
{
delete m_trig;
delete m_count;
}
4143 |
|
#2
|
||||||
|
||||||
|
Re: Analog potentiometer to counter
I haven't played with Analog triggers for a few years, and that was in LabVIEW. I just pulled up my old LabVIEW code and it looks very similar to yours.
Are you sure the pot value is getting below 0.5v and above 4.5v? If it isn't the Analog Trigger won't trigger. It may also be usefull to look at the AnalogTrigger getInWindow and getTriggerState values, to see if it is the Analog Trigger that isn't triggering, or the counter that isn't counting. |
|
#3
|
|||
|
|||
|
Re: Analog potentiometer to counter
The analog voltage goes between .2 and 4.8v. I can see getinwindow change state once a rotation. I'm going to try just using the upsource just to see what happens. The FPGA code is not released, correct?
|
|
#4
|
|||
|
|||
|
Re: Analog potentiometer to counter
changing
Code:
m_count->SetUpSource(m_trig, AnalogTriggerOutput::kRisingPulse); Code:
m_count->SetUpSource(m_trig, AnalogTriggerOutput::kState); I think AnalogTriggerOutput::kRisingPulse is implemented in the FPGA which I can't see inside. |
|
#5
|
||||||
|
||||||
|
Re: Analog potentiometer to counter
|
|
#6
|
|||
|
|||
|
Re: Analog potentiometer to counter
Quote:
|
|
#7
|
|||
|
|||
|
Re: Analog potentiometer to counter
I'm making progress. I does look like the signal does not transition fast enough to completely jump the window.
One other question, Is there a maximum number of AnalogTriggers available in the FPGA? I tried hooking up two AnalogTriggers (with different windows) to the same channel and that seems to work. |
|
#8
|
|||
|
|||
|
Re: Analog potentiometer to counter
The documentation for the LV VIs state that you have eight analog triggers.
Greg McKaskle |
|
#9
|
|||
|
|||
|
Re: Analog potentiometer to counter
I finally got the AnalogTriggers working well counting rotations.
I was using these pots: http://www.mouser.com/ProductDetail/...1hvDsngmWGdg== The key was reducing the sample rate to 1000 Hz. Code:
this->GetModule()->SetSampleRate(1000); At the max rpm of the sensor (500 rpm) that is still 120 samples per rev. I tested two implementations. Both worked fine. One used the rising/falling pulse feature to catch the rollover. The other used two AnalogTriggers to convert the pot to a quadrature signal. That was fed into an Encoder object. Code is here: https://github.com/FRC-Team-4143/TMW...hannelVolt.cpp |
|
#10
|
|||
|
|||
|
Re: Analog potentiometer to counter
Congratulations!
|
|
#11
|
||||||
|
||||||
|
Re: Analog potentiometer to counter
Another option might be to use the averaged value, rather then the filtered value. You could use either 5 or 6 bits of averaging, and not change the sample rate of the module. This might occasionally have trouble at the transitions, however.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|