Hi,
I'm quite new to arduino and c++ so i have a question. I'm trying to make a class that will be used to animate servos. I'm using a timer to triger an moveServos() function that is declared in the class. The problem is that I can't call it from the timer function. Can anybody help me and point me in the right direction? What am I doing wrong?
The code is here ServoTween.cpp:
#include <avr/interrupt.h>
#include <WProgram.h>
#include <Servo.h>
#include "ServoTween.h"
ISR(TIMER2_OVF_vect) {
//Move the servos
moveServos();
TCNT2=thisInstance.tcnt2;
}
ServoTween::ServoTween(){
Serial.begin(9600);
Serial.println("initialize()");
}
void ServoTween::moveServos()
//Here goes the code for moving the motors
}
And this is the header file ServoTween.h:
#include <WProgram.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "Servo.h"
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define MAX_SERVOS 48
#else // everything else
#define MAX_SERVOS 12
#endif
#define ANGLE 0
#define SPEED 1
#define DELAY 2
#define CURRENT_TWEEN 4
#define TIMER_CLOCK_FREQ 2000000.0 //2MHz for /8 prescale from 16MHz
#define RESOLUTION 65536
class ServoTween
{
public:
ServoTween();
unsigned int tcnt2;
void moveServos();
void setTimer();
//private:
int _trigerCount;
int _trigerCycle;
Servo _servoArray[12];
int _servoPins[12];
int _servoPosition[12];
int _servoCoordinates[12][16][3];
int _servoTweenArray[12][4];
int _servoCoordinatesCount[12];
int _minServoTime;
int _degreeSpeed;
int _speedResolution;
int _servoCount;
int _coordinatesCount;
};
Also when i put all the code in a sketch it works like a charm.
For which instance of the class do you want to call the moveServos() function in the ISR? This is the fundamental question that needs to be addressed.
If it doesn't matter, then you can make moveServos a static method. Doing so will prevent you from accessing the non-static methods/fields of the class, though.
The thing is that moveServos() is accessing the non-static methods/fields of the class. I instantiate the class in the arduino sketch with
#include <Servo.h>
#include "ServoTween.h"
ServoTween myServoTween(1000,8); //Create a new ServoTween(time_of_full_servo_rotation,speed_resolution)
/////////////////////////////////////////////////////////////////////
void setup() {
//Serial.begin(9600);
myServoTween.addServo(9); //Add a servo motor addServo(pin_number);
//Set the coordinates for a servoMotor
myServoTween.setCoordinates(0,10,2,1000); //setCoordinates(motor_number,angle,servo_speed,delay_after_finished);
myServoTween.setCoordinates(0,60,10,500); // 0 <= servo_speed <= speed_resolution
myServoTween.setCoordinates(0,130,0,1000);// delay_after_finished is in microseconds
}
void loop(){
//
}
in my previous post I omited the code that sets the coordinates because it works fine.
You can't call a class method (moveServos) without an instance of the class, unless the method is static. Since your method can not be static, you must define the instance of the class that the method is to be called for. It isn't rocket science.
Sorry for being a bit slow
but how can I do that? I tryed this:
ServoTween thisInstance();
ISR(TIMER2_OVF_vect) {
//Move the servos
//Serial.begin(9600);
//Serial.println("myglobalVar");
//Serial.println(thisInstance.myglobalVar);
thisInstance.moveServos();
//TCNT2=thisInstance.tcnt2;
}
ServoTween::ServoTween(){
Serial.begin(9600);
Serial.println("ServoTween()");
myglobalVar=15;
}
void ServoTween::moveServos()
{
//move the servos
}
and I get some strange results. I can then reference the the variables but I still can't call the function. Anyway thanks for the help PaulS.
I tryed this:
and I get some strange results.
Without knowing what results you got, strange or otherwise, it's hard to say what the problem is.
What is triggering the ISR? How often? Does the moveServo() function have time to complete before it is called again?