Go Down

Topic: Strange issue while using the servo library in a private library (Read 722 times) previous topic - next topic

jajden

Hi, I had a sketch that worked great to control a robotic arm. I created a library to simplify the code but I have some really weird issues with it. As soon as I upload the sketch, the servos begin to behave in really weird ways, going to many random degrees and changing every seconds. I really have no idea of what the issue is. Here, I don't have anything in the loop an yet it continues behaving in strange ways
the arduino code:
Code: [Select]
#include <Bouger.h>
#include<NewPing.h>
#include<Servo.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar1(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN - 2, ECHO_PIN - 2, MAX_DISTANCE);
int dela = 20;
int lopa1 = 75;
int gal;
Bouger bouge;
int capteura,capteurb;
void setup() {
  Serial.begin(9600);
}



the cpp code:
Code: [Select]
#include "arduino.h"
#include "Bouger.h"
#include <Servo.h>


Bouger::Bouger()
{

  myservo0.attach(3);
  myservo1.attach(4);
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(7);
  myservo5.attach(8);
  myservo5.write(140);
}

void Bouger::allerposer() { // put the robotic arm in a special position
  delay(2000);
  boug(0, 90);
  boug(1, 90);
  boug(2, 0);
  boug(3, 90);
  boug(4, 180);
  delay(2000);
  boug(5, 85);
  while (1);
}
void Bouger::app(int lopa) // move the arm in a spacific way
{

  _lopa = lopa;
  _angle1 = 155 - _lopa;
  _angle2 = 2 * _lopa;
  _angle3 = 65 - _lopa;
  boug(1, _angle1);
  boug(2, _angle2);
  boug(3, _angle3);
  delay(350);

}

void Bouger::boug(int f, int g) // slowly move any servo
{
  _f = f; // the servo number
  _g = g; // the direction the servo should go to
  Serial.print(_ar[g]);
  if (_f == 0) {
    if (_ar[0] > _g) {
  Serial.println(" 1");
      for (int _lo = _ar[0]; _lo > _g; _lo--) {
 Serial.println(_lo);
        myservo0.write(_lo);
        delay(_gb+100);
      }
    }
    if (_ar[0] < _g) {
  Serial.println(" 2");
      for (int _lo = _ar[0]; _lo < _g; _lo++) {
 Serial.println(_lo);
        myservo0.write(_lo);
        delay(_gb+100);
      }
    }
    _ar[0] = _g;
  }
  if (_f == 1) {
    if (_ar[1] > _g) {
      for (int _lo = _ar[1]; _lo > _g; _lo--) {
        myservo1.write(_lo);
        delay(_gb);
      }

    }
    if (_ar[1] < _g) {
      for (int _lo = _ar[1]; _lo < _g; _lo++) {
        myservo1.write(_lo);
        delay(_gb);
      }

    }
    _ar[1] = _g;
  }
  if (_f == 2) {
    if (_ar[2] > _g) {
      for (int _lo = _ar[2]; _lo > _g; _lo--) {
        myservo2.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[2] < _g) {
      for (int _lo = _ar[2]; _lo < _g; _lo++) {
        myservo2.write(_lo);
        delay(_gb);
      }
    }
    _ar[2] = _g;
  }
  if (_f == 3) {
    if (_ar[3] > _g) {
      for (int _lo = _ar[3]; _lo > _g; _lo--) {
        myservo3.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[3] < _g) {
      for (int _lo = _ar[3]; _lo < _g; _lo++) {
        myservo3.write(_lo);
        delay(_gb);
      }
    }
    _ar[3] = _g;
  }
  if (_f == 4) {
    if (_ar[4] > _g) {
      for (int _lo = _ar[4]; _lo > _g; _lo--) {
        myservo4.write(_lo);
        delay(_gb);
      }

    }
    if (_ar[4] < _g) {
      for (int _lo = _ar[4]; _lo < _g; _lo++) {
        myservo4.write(_lo);
        delay(_gb);
      }

    }
    _ar[4] = _g;
  }
  if (_f == 5) {
    if (_ar[5] > _g) {
      for (int _lo = _ar[5]; _lo > _g; _lo--) {
        myservo5.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[5] < _g) {
      for (int _lo = _ar[5]; _lo < _g; _lo++) {
        myservo5.write(_lo);
        delay(_gb);
      }
    }
    _ar[5] = _g;
  }

}


the .h code
Code: [Select]
#ifndef Bouger_h
#define Bouger_h
#include <Servo.h>
#include "Arduino.h"

class Bouger
{
  public:
    Bouger();
    void boug(int f, int g);
    void app(int lopa);
    void allerposer();
  private:
    int _f;
    int _g;
    int _lopa;
    Servo myservo0;
    Servo myservo1;
    Servo myservo2;
    Servo myservo3;
    Servo myservo4;
    Servo myservo5;
    int _ar[6] = {90, 90, 90, 90, 90, 90};
    int _retenir1[6] = {90, 90, 90, 90, 90, 90};
    int _retenir2[6] = {90, 90, 90, 90, 90, 90};
    int _retenir3[6] = {90, 90, 90, 90, 90, 90};
    int _retenir4[6] = {90, 90, 90, 90, 90, 90};
    int _retenir5[6] = {90, 90, 90, 90, 90, 90};
    int _retenir6[6] = {90, 90, 90, 90, 90, 90};
    int _angle1, _angle2, _angle3;
    int _gb = 0;
    int _pin[6] = {3, 4, 5, 6, 7, 8};
};

#endif

wildbill

Don't attach the servos in the constructor. You'll notice that many arduino classes have begin methods - serial is an example. There is an init routine called before your setup and loop functions. Init sets the hardware up for use. Constructors however are called before init, the hardware's not initialized and your attempts to attach are causing your funky results I suspect. Put all that code in a begin method and call it from setup.

jajden

I don't know a lot about classes and I don't really understand what you mean by 'constructor' and 'begin method'.
I tried this but it doesn't work:
.cpp
Code: [Select]
#include "arduino.h"
#include "Bouger.h"
#include <Servo.h>


Bouger::Bouger()
{

}

method Bouger::begin(){
    Servo myservo0;
    Servo myservo1;
    Servo myservo2;
    Servo myservo3;
    Servo myservo4;
    Servo myservo5;
  myservo0.attach(3);
  myservo1.attach(4);
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(7);
  myservo5.attach(8);
  myservo5.write(140);
}

void Bouger::allerposer() { // put the robotic arm in a special position
  delay(2000);
  boug(0, 90);
  boug(1, 90);
  boug(2, 0);
  boug(3, 90);
  boug(4, 180);
  delay(2000);
  boug(5, 85);
  while (1);
}
void Bouger::app(int lopa) // move the arm in a spacific way
{

  _lopa = lopa;
  _angle1 = 155 - _lopa;
  _angle2 = 2 * _lopa;
  _angle3 = 65 - _lopa;
  boug(1, _angle1);
  boug(2, _angle2);
  boug(3, _angle3);
  delay(350);

}

void Bouger::boug(int f, int g) // slowly move any servo
{
  _f = f; // the servo number
  _g = g; // the direction the servo should go to
  Serial.print(_ar[g]);
  if (_f == 0) {
    if (_ar[0] > _g) {
  Serial.println(" 1");
      for (int _lo = _ar[0]; _lo > _g; _lo--) {
Serial.println(_lo);
        myservo0.write(_lo);
        delay(_gb+100);
      }
    }
    if (_ar[0] < _g) {
  Serial.println(" 2");
      for (int _lo = _ar[0]; _lo < _g; _lo++) {
Serial.println(_lo);
        myservo0.write(_lo);
        delay(_gb+100);
      }
    }
    _ar[0] = _g;
  }
  if (_f == 1) {
    if (_ar[1] > _g) {
      for (int _lo = _ar[1]; _lo > _g; _lo--) {
        myservo1.write(_lo);
        delay(_gb);
      }

    }
    if (_ar[1] < _g) {
      for (int _lo = _ar[1]; _lo < _g; _lo++) {
        myservo1.write(_lo);
        delay(_gb);
      }

    }
    _ar[1] = _g;
  }
  if (_f == 2) {
    if (_ar[2] > _g) {
      for (int _lo = _ar[2]; _lo > _g; _lo--) {
        myservo2.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[2] < _g) {
      for (int _lo = _ar[2]; _lo < _g; _lo++) {
        myservo2.write(_lo);
        delay(_gb);
      }
    }
    _ar[2] = _g;
  }
  if (_f == 3) {
    if (_ar[3] > _g) {
      for (int _lo = _ar[3]; _lo > _g; _lo--) {
        myservo3.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[3] < _g) {
      for (int _lo = _ar[3]; _lo < _g; _lo++) {
        myservo3.write(_lo);
        delay(_gb);
      }
    }
    _ar[3] = _g;
  }
  if (_f == 4) {
    if (_ar[4] > _g) {
      for (int _lo = _ar[4]; _lo > _g; _lo--) {
        myservo4.write(_lo);
        delay(_gb);
      }

    }
    if (_ar[4] < _g) {
      for (int _lo = _ar[4]; _lo < _g; _lo++) {
        myservo4.write(_lo);
        delay(_gb);
      }

    }
    _ar[4] = _g;
  }
  if (_f == 5) {
    if (_ar[5] > _g) {
      for (int _lo = _ar[5]; _lo > _g; _lo--) {
        myservo5.write(_lo);
        delay(_gb);
      }
    }
    if (_ar[5] < _g) {
      for (int _lo = _ar[5]; _lo < _g; _lo++) {
        myservo5.write(_lo);
        delay(_gb);
      }
    }
    _ar[5] = _g;
  }

}

.h
Code: [Select]
#ifndef Bouger_h
#define Bouger_h
#include <Servo.h>
#include "Arduino.h"

class Bouger
{
  public:
    Bouger();
void begin();
    void boug(int f, int g);
    void app(int lopa);
    void allerposer();
  private:
    int _f;
    int _g;
    int _lopa;

    int _ar[6] = {90, 90, 90, 90, 90, 90};
    int _retenir1[6] = {90, 90, 90, 90, 90, 90};
    int _retenir2[6] = {90, 90, 90, 90, 90, 90};
    int _retenir3[6] = {90, 90, 90, 90, 90, 90};
    int _retenir4[6] = {90, 90, 90, 90, 90, 90};
    int _retenir5[6] = {90, 90, 90, 90, 90, 90};
    int _retenir6[6] = {90, 90, 90, 90, 90, 90};
    int _angle1, _angle2, _angle3;
    int _gb = 0;
    int _pin[6] = {3, 4, 5, 6, 7, 8};
};

#endif

arduino sketch:
Code: [Select]
#include <Bouger.h>
#include<NewPing.h>
#include<Servo.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar1(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN - 2, ECHO_PIN - 2, MAX_DISTANCE);
int dela = 20;
int lopa1 = 75;
int gal;
Bouger bouge;
int capteura,capteurb;
void setup() {
  Serial.begin(9600);
  bouge.begin();
}

void loop() {
}

Thank you so much for the time you spend helping me

wildbill

Leave the servo declarations as private members as you had them originally. Just do the attach in the begin method (function). As you have it now, they're local to begin and therefore go out of scope when it ends.

jajden

Thank you so much.
Now it works great!

Go Up