Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enableInterrupts gives error on teensy 3.2 #68

Closed
emilhse opened this issue Sep 27, 2020 · 1 comment
Closed

enableInterrupts gives error on teensy 3.2 #68

emilhse opened this issue Sep 27, 2020 · 1 comment

Comments

@emilhse
Copy link

emilhse commented Sep 27, 2020

I'm newbe in programming i try to compile simple TeensyLoadcell by https://github.com/luni64/TeensyLoadcell

it keeps giving me an error can you please help?

void enableInterrupts(void (*isr)(void), uint8_t priority = 255);

  ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\ADC/ADC_Module.h:184:10: note: candidate expects 2 arguments, 0 provided

Error compiling for board Teensy 3.2 / 3.1.

`#include "ADC.h"
#include "TeensyLoadcell.h"
using namespace TeensyLoadcell;

ADC adc;
Loadcell loadcell(adc.adc0);

void setup(){
loadcell.start();
}

void loop(){
Serial.println(loadcell.getValue());
delay(100);
}`

`#include "TeensyLoadcell.h"
#include "ADC.h"

#if ADC_USE_PGA == 0
#error "Board has no PGA"
#endif

namespace TeensyLoadcell
{
static Loadcell *activeLCO[2]; // will point to current Loadcell objects, the pointers are required by the ISR to access the object

// ISR, Read values and calculate exponential average.
template
void Loadcell_ISR()
{
Loadcell *l = activeLCO[n];
float curVal = l->adc->readSingle();
l->average = l->average + l->alpha * (curVal - l->average); //https://en.wikipedia.org/wiki/Exponential_smoothing
}

//----------------------------------------------------------------------------------

Loadcell::Loadcell(ADC_Module *adc, float tau, Gain gain, unsigned sampleFreq)
{
this->adc = adc;
this->sampleFreq = sampleFreq;

  pinP = adc->ADC_num == 0 ? A10 : A12;
  pinN = adc->ADC_num == 0 ? A11 : A13;

  pinMode(pinP, INPUT);
  pinMode(pinN, INPUT);

  adc->setAveraging(32);
  adc->setResolution(13);
  adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);
  adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED);
  adc->setReference(ADC_REFERENCE::REF_1V2);
  adc->enablePGA(gain);

  c0 = 0.0f;
  c1 = 1.0f;

  setTau(tau);

}

//-----------------------------------------------------------------

Loadcell::~Loadcell()
{
stop();
}

//-----------------------------------------------------------------

void Loadcell::start()
{

  // attach correct ISR depending on ADC module number
  if (adc->ADC_num == 0)
  {
     activeLCO[0] = this;
     attachInterruptVector(IRQ_ADC0, Loadcell_ISR<0>);
  }
  else
  {
     activeLCO[1] = this;
     attachInterruptVector(IRQ_ADC1, Loadcell_ISR<1>);
  }
  average = 0.0f;

  adc->stopPDB();
  adc->startSingleDifferential(pinP, pinN);
  adc->enableInterrupts();
  adc->startPDB(sampleFreq);

}

//-----------------------------------------------------------------

void Loadcell::stop()
{
adc->stopPDB();
adc->disableInterrupts();
activeLCO[adc->ADC_num] = nullptr;
}

//-----------------------------------------------------------------

void Loadcell::setTau(float tau)
{
if (tau <= 0.0f)
{
alpha = 1.0f;
}
else
{
alpha = 1 - expf(-1.0f / (tau * sampleFreq)); //https://en.wikipedia.org/wiki/Exponential_smoothing
}
}

//-----------------------------------------------------------------

void Loadcell::tare()
{
c0 = average;
}

} // namespace TeensyLoadcell

#pragma once

#include "ADC.h"

namespace TeensyLoadcell
{
enum Gain : uint8_t
{
gain_1 = 1,
gain_2 = 2,
gain_4 = 4,
gain_8 = 8,
gain_16 = 16,
gain_32 = 32,
gain_64 = 64
};

class Loadcell
{
public:
// Constructor, tau is the smoothing time constant in seconds.
Loadcell(ADC_Module *adc, float tau = 1.0, Gain gain = gain_64, unsigned sampleFreq = 5000u);
~Loadcell();

  /* Starts continous background conversion.*/
  void start();

  /*Stops background conversions*/
  void stop();

  /* Gets the averaged, calibrated value. Calibration coefficients can be set by setCalib(c0, c1) */
  inline float getValue() { return (average - c0) * c1; }

  // set the smoothing time constant in seconds. Values will be stable after about 5* tau.
  void setTau(float tau);


  // tares the load cell (sets c0 such that getValue() returns 0 )
  void tare();

  // calibration factors. getValue() returns "(average-c0) * c1"
  float c0 = 0.0f, c1 = 1.0f;

  // delete copy constructor
  Loadcell(const Loadcell &) = delete;

protected:
ADC_Module *adc;
volatile float average = 0.0f; // current averaged measurement value
uint8_t pinP, pinN; // used input pin pair

  float alpha;                     // smoothing factor
  unsigned sampleFreq;             // used sample frequency for the PDB


  template<unsigned n> friend void Loadcell_ISR(); // let the ADC ISR update the measurement value (average)

};

} // namespace TeensyLoadcell`

@pedvide
Copy link
Owner

pedvide commented Jan 15, 2021

That library is using an old version of this library. Some function calls are different.for example adc->adc0->enableInterrupts(); has become now adc->enableInterrupts(adc0_isr);, so you need to pass the isr directly. You can try to fix it yourself or ask the owner of that lib.

@pedvide pedvide closed this as completed Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants