Log in

View Full Version : Analog potentiometer to counter


RyanShoff
20-12-2013, 16:18
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.


#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;
}



Ryan Shoff
4143

Joe Ross
20-12-2013, 16:55
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.

RyanShoff
20-12-2013, 18:41
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?

RyanShoff
20-12-2013, 21:26
changing

m_count->SetUpSource(m_trig, AnalogTriggerOutput::kRisingPulse);

to

m_count->SetUpSource(m_trig, AnalogTriggerOutput::kState);

gets the counter moving. Of course that doesn't make it functional.

I think AnalogTriggerOutput::kRisingPulse is implemented in the FPGA which I can't see inside.

Joe Ross
21-12-2013, 00:55
The FPGA code is not released, correct?

That's correct.

Joe Hershberger
22-12-2013, 01:34
changing

m_count->SetUpSource(m_trig, AnalogTriggerOutput::kRisingPulse);

to

m_count->SetUpSource(m_trig, AnalogTriggerOutput::kState);

gets the counter moving. Of course that doesn't make it functional.

I think AnalogTriggerOutput::kRisingPulse is implemented in the FPGA which I can't see inside.

The rising / falling pulse only indicates a transition from one sample to the next that entirely leaps over the window. Given how large you set the window and the impedance of the sensor it is unlikely that the signal is fast enough to make that leap. This is likely the reason you are seeing 0 all the time. It otherwise appears that you have it configured correctly. You can also try lowering the sampling rate to affect how quickly the signal changes from one sample to the next. Be aware the sample rate will affect every channel on that module.

RyanShoff
23-12-2013, 00:32
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.

Greg McKaskle
24-12-2013, 08:44
The documentation for the LV VIs state that you have eight analog triggers.

Greg McKaskle

RyanShoff
24-12-2013, 23:28
I finally got the AnalogTriggers working well counting rotations.

I was using these pots:
http://www.mouser.com/ProductDetail/BI-Technologies/6127V1A360L5FS/?qs=tPr5ar9/tz1hvDsngmWGdg==

The key was reducing the sample rate to 1000 Hz.
this->GetModule()->SetSampleRate(1000);
This value was determined experimentally.

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/TMW2013/blob/master/TMW2013/AnalogChannelVolt.cpp

Joe Hershberger
25-12-2013, 12:08
I finally got the AnalogTriggers working well counting rotations.

Congratulations!

Joe Ross
25-12-2013, 20:56
The key was reducing the sample rate to 1000 Hz.
this->GetModule()->SetSampleRate(1000);
This value was determined experimentally.

At the max rpm of the sensor (500 rpm) that is still 120 samples per rev.


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.