Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Analog potentiometer to counter (http://www.chiefdelphi.com/forums/showthread.php?t=123458)

RyanShoff 20-12-2013 16:18

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

Ryan Shoff
4143

Joe Ross 20-12-2013 16:55

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.

RyanShoff 20-12-2013 18:41

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?

RyanShoff 20-12-2013 21:26

Re: Analog potentiometer to counter
 
changing
Code:

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

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

Re: Analog potentiometer to counter
 
Quote:

Originally Posted by RyanShoff (Post 1315285)
The FPGA code is not released, correct?

That's correct.

Joe Hershberger 22-12-2013 01:34

Re: Analog potentiometer to counter
 
Quote:

Originally Posted by RyanShoff (Post 1315427)
changing
Code:

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

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

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.

Greg McKaskle 24-12-2013 08:44

Re: Analog potentiometer to counter
 
The documentation for the LV VIs state that you have eight analog triggers.

Greg McKaskle

RyanShoff 24-12-2013 23:28

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);
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/TMW...hannelVolt.cpp

Joe Hershberger 25-12-2013 12:08

Re: Analog potentiometer to counter
 
Quote:

Originally Posted by RyanShoff (Post 1316800)
I finally got the AnalogTriggers working well counting rotations.

Congratulations!

Joe Ross 25-12-2013 20:56

Re: Analog potentiometer to counter
 
Quote:

Originally Posted by RyanShoff (Post 1316800)
The key was reducing the sample rate to 1000 Hz.
Code:

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.


All times are GMT -5. The time now is 12:53.

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