Go Down

Topic: Library constructor problem (Read 346 times) previous topic - next topic

AaronB

Hello, sorry for the unprofessional subject but I can't find a better one.. :D
I am trying to write an arduino library and have following problem:

I can initialize my class "DCVehicle" using 2 ways:
Using the constructor(int, AF_DCMotor* AF_DCMotor*) works without any problem but when I use the static method "createDCVehicle" my motors aren't running. I have no idea what the problem is and I tried a lot of different ways of using this method... (as constructor etc.) I can't figure out what triggers the problem.  :smiley-cry:

Sadly there is no way to handle errors (try / catch) and I tried to log using Serial: Everything looks as it should be.

Thanks in advance for your help.

My code: (I shortened it a little bit)

GridMotor.h:
Code: [Select]

class DCVehicle {
public:
DCVehicle(int velocity, AF_DCMotor *leftMotor, AF_DCMotor *rightMotor);

void setCompensator(float compensator);
float getCompensator();
void setVelocity(int velocity);
int getVelocity();
void setLeftMotorInverted(bool invertMotor);
bool isLeftMotorInverted();
void setRightMotorInverted(bool invertMotor);
bool isRightMotorInverted();
void move(int direction);
void moveFor(int direction, int forMilliseconds);
void turnRight();
void turnRightFor(int forMilliseconds);
void turnLeft();
void turnLeftFor(int forMilliseconds);
void stop();
int getSpeed();

private:
void setSpeed();
int getLeftState(int direction);
int getRightState(int direction);

AF_DCMotor *mL;
AF_DCMotor *mR;
float compensator;
int velocity;
bool mLInvert;
bool mRInvert;
};

DCVehicle createDCVehicle(int velocity, int leftMotor, int rightMotor, int frequency);


GridMotor.cpp:
Code: [Select]

//public

DCVehicle createDCVehicle(int velocity, int leftMotor, int rightMotor, int frequency) {
AF_DCMotor mL(leftMotor, frequency);
AF_DCMotor mR(rightMotor, frequency);

return DCVehicle(velocity, &mL, &mR);
}

DCVehicle::DCVehicle(int velocity, AF_DCMotor *leftMotor, AF_DCMotor *rightMotor) {
mL = leftMotor;
mR = rightMotor;
compensator = 0;
DCVehicle::velocity = velocity;
mLInvert = false;
mRInvert = false;

setSpeed();
}

void DCVehicle::setCompensator(float compensator) {
DCVehicle::compensator = (compensator < -1 ? -1 : (compensator > 1 ? 1 : compensator));
setSpeed();
}

float DCVehicle::getCompensator() {
return compensator;
}

void DCVehicle::setVelocity(int velocity) {
DCVehicle::velocity = (velocity < 0 ? 0 : (velocity > 255 ? 255 : velocity));
setSpeed();
}

int DCVehicle::getVelocity() {
return velocity;
}

void DCVehicle::setLeftMotorInverted(bool invertMotor) {
mLInvert = invertMotor;
}

bool DCVehicle::isLeftMotorInverted() {
return mLInvert;
}

void DCVehicle::setRightMotorInverted(bool invertMotor) {
mRInvert = invertMotor;
}

bool DCVehicle::isRightMotorInverted() {
return mRInvert;
}

void DCVehicle::move(int direction) {
mL->run(getLeftState(direction));
mR->run(getRightState(direction));
}

void DCVehicle::moveFor(int direction, int forMilliseconds) {
move(direction);
delay(forMilliseconds);
stop();
}

void DCVehicle::turnRight() {
mL->run(getLeftState(FORWARD));
mR->run(getRightState(BACKWARD));
}

void DCVehicle::turnRightFor(int forMilliseconds) {
turnRight();
delay(forMilliseconds);
stop();
}

void DCVehicle::turnLeft() {
mL->run(getLeftState(BACKWARD));
mR->run(getRightState(FORWARD));
}

void DCVehicle::turnLeftFor(int forMilliseconds) {
turnLeft();
delay(forMilliseconds);
stop();
}

void DCVehicle::stop() {
mL->run(RELEASE);
mR->run(RELEASE);
}

//private

void DCVehicle::setSpeed() {
mL->setSpeed(velocity - (compensator * velocity));
mR->setSpeed(velocity + (compensator * velocity));
}

int DCVehicle::getLeftState(int direction) {
if(direction == RELEASE || !mLInvert)
return direction;
return direction == FORWARD ? BACKWARD : FORWARD;
}

int DCVehicle::getRightState(int direction) {
if(direction == RELEASE || !mRInvert)
return direction;
return direction == FORWARD ? BACKWARD : FORWARD;
}


Arduino Sketch:
Code: [Select]

DCVehicle v = createDCVehicle(VELOCITY, MOTOR_L, MOTOR_R, FREQUENCY);

void setup() {
  Serial.begin(9600);
 
  pinMode(SENSOR_T, OUTPUT);
  pinMode(SENSOR_E, INPUT);
 
  v.setCompensator(COMPENSATOR);
 
  Serial.print("Comp.: ");
  Serial.println(v.getCompensator());
 
  Serial.print("Velocity: ");
  Serial.println(v.getVelocity());
 
  delay(2500);
}

void loop() {
  long cm = scanObstacle();
 
  if(cm <= OBSTACLE_DIST) {
    delay(TURN_PAUSE);
    v.turnRightFor(TURN_TIME);
    delay(TURN_PAUSE);
  } else
    v.moveFor(FORWARD, MOVE_TIME);
}

Delta_G

Code: [Select]

DCVehicle createDCVehicle(int velocity, int leftMotor, int rightMotor, int frequency) {
AF_DCMotor mL(leftMotor, frequency);
AF_DCMotor mR(rightMotor, frequency);

return DCVehicle(velocity, &mL, &mR);
}


You're creating two objects mL and mR.  Then you are passing pointers to them back to the calling function.  Then this function ends and mL and mR go out of scope.  So they cease to exist.  Now your calling code has two pointers that point to garbage memory. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

AaronB

Thank you, I was able to fix it with your reply :D

Go Up