Using Variables from a library in a pde.

Hi all,

This is my first post here, and the first time I’m messing round with libraries and I am stuck…

I want to use multiple variables which I calculate inside a library to use in my main loop. So the question is: How do I “export” variables from the library to the pde?

Searching here and elsewhere on the internet lead me to pointers, but I have no idea what these are. (I`m a mechanical engineer, not a software engineer). Is this the right way to go?

In the end I wanted to create a library to use the Wii nunchuk and motion plus as a IMU. I modified the code as follow:

.PDE code

#include <WiiMU.h>
#include <Wire.h>

WiiMU WiiMU;

void setup()
{
WiiMU.startup();
}

void loop()
{
  WiiMU.read
}

.H code:

/*
  WiiMU.h - Library for Using the Wii nunchuk and Wii motion plus as a IMU.
  based on: arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248889032/0  
  Created by Maarten Lamers, November 12, 2010.
*/

#ifndef WiiMU_h

#define WiiMU_h

#include "WProgram.h"
#include <Wire.h>

class WiiMU
{
  
public:
    
void startup();
void read();

private:

void readData();
void send_zero ();
void initializeSensors();
void calibrateZeroes();

};
#endif

.CPP code is a bit to long to post here but the idea is that I have 6 varaibles(x,y,z, yaw,pitch,roll) that is want to procces in a filter in the pde file.

Can anyone help me please,

Maarten Lamers

The proper way to do that is to either return the variables by your function such as:

int myfoo = WiiMU.readfoo();

or to declare the variables a properties of your class and access them via the class:

WiiMU.readfoo(); // Calculate internal variables, nothing is returned
// Do something with the variables
int foo = WiiMU.fox + WiiMU.foy;

In both cases, you need to properly declarer your class. Returning your data via a global variables is just a disaster waiting to happen and you shouldn't start learning such horrible coding practices.

For more details, get out your favourite book about C++ and reread the chapter about classes and about functions. It should be chapter 4 or 5.

Korman

Maybe something like this: ? [CODE UNTESTED]

class WiiMU
{
public:
  typedef float data;
  typedef void (*cb_filter)(data*,data*,data*,data*,data*,data*);
  
  WiiMU(cb_filter newFilter){ 
    filter = newFilter; 
    x = y = z = yaw = pitch = roll = 0;
  }

  void read(){
    //readData();
    
    //apply the filter
    filter(&x,&y,&z,&yaw,&pitch,&roll);
  }

protected:
  cb_filter filter;
  data x, y, z, yaw, pitch, roll;
};

void myFilter(WiiMU::data* x, WiiMU::data* y, WiiMU::data* z, WiiMU::data* yaw, WiiMU::data* pitch, WiiMU::data* roll) {
  //do what ever with the datas
  //REMEMBER, if you change the value here, they will change in the library as well!
}

WiiMU wiiMU(myFilter);

void setup() {
  //
}

void loop() {
  wiiMU.read();
}

If you want pre-read / post-read filters you can easily extend to support that as well.