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