Go Down

Topic: Servo Swings Far Left When using Custom Library (Read 578 times) previous topic - next topic

kelliott

So for a robotics program of mine I'm attempting to build a 180 degree sonar scanner and so I developed a scanner library for this purpose.  However, when I implement this library on a servo it causes the servo to one side of its boundaries and cease to work.  When I use the Sweep example though, it works fine.  If someone could help me and tell me what I'm doing wrong that'd be awesome.  The relevant code is below.  Let me know if you you guys need more.  Thank you!

The main sketch
Code: [Select]

#include <stdlib.h>
#include "MotorController.h"
#include "DiffDrive.h"
#include "Scanner.h"

void * operator new(size_t size)
{
  return malloc(size);
}

void operator delete(void * ptr)
{
  free(ptr);
}

//DiffDrive drive(5, 6, 7, 8);
Scanner scanner;
void setup(){
  delay(1000);
}

void loop(){
for(int pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    scanner.servo->write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(int pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    scanner.servo->write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}



The Scanner library.
Code: [Select]

/*
Keith Elliott
Created: 10/21/11
DESC: A library for controlling a rotating scanner with a servo and distance sensors
*/

#include "Scanner.h"

Scanner::Scanner(){
  //sonar = new USSensor();
  //sonar->init();
  servo = new Servo();
  servo->attach(10);
  //sweep = new Sweep();
}

Sweep Scanner::doSweep(int inc){
  servo->write(SERVO_MIN);
  delay(200);
  if (inc > 0){
    for (int i = SERVO_MIN; i <= SERVO_MAX; i+=inc){
      servo->write(i);
      delay(200);
      sweep->addNode(sonar->getDist(), i-CENT_OFF);
    }
  }
  else if (inc < 0){
    for (int i = SERVO_MAX; i >= SERVO_MIN; i+=inc){
      servo->write(i);
      delay(200);
      sweep->addNode(sonar->getDist(), i-CENT_OFF);
    }
  }
}

int Scanner::scan(int threshold, unsigned int timeout){
  unsigned long start = millis();
  int i;
  while (1){
    for (i = CENT_OFF; i <= SERVO_MAX; i+=30){
      if (sonar->getDist() < threshold)
        return i-CENT_OFF;
    }
    for (; i >= SERVO_MIN; i-=30){
      if (sonar->getDist() < threshold)
        return i-CENT_OFF;
    }
    for (; i <= CENT_OFF; i+=30){
      if (sonar->getDist() < threshold)
        return i-CENT_OFF;
    }
    if (millis() - start >= timeout*1000)
      return 0;
  }
}

zoomkat

Quote
If someone could help me and tell me what I'm doing wrong that'd be awesome.


I can't answer to your code, but servos act as you describe when they receive bad PPM position signals.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

kelliott

Yea and that was my first thought.  However, I've quadruple-checked my wiring and it all seems right.  I've also tested the example code, like I said above.  But as of right now it seems like my code and the example code should, for all intensive purposes, be the exact same with the exception of a library call.  So I have no idea why it's not working.

zoomkat

If the servo operates correctly using other servo code, then the only option is your code is bad or something that goes bump in the night is out to get you.  8)
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

kelliott

I'm starting to think there's something inherently wrong with my pointers and memory allocation.  My motor drivers don't work either :(

AWOL

Servos sometimes behave like that if you don't connect the grounds.
So I've been told   XD
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

It will be necessary to see the header file, too.

I don't think that new and delete are really necessary. The Scanner class can simply instantiate a Servo object, just like you do in a sketch.

kelliott

@ AWOL, yea that was what I thought, but I verified that they are connected with a multimeter.

@PaulS, my header file is below.  As for the new and delete, when I don't have get the error "undefined reference to `operator new(unsigned int)'" and when looked up, I was told to add the new and delete operators at the top.  My next step is going to be to replace all pointers with stack objects since I'm beginning to suspect that the problem is either pointers not being handled well, or that my robot has actually gained sentience and is just trying to mess with me.

Code: [Select]

/*
Keith Elliott
Created: 10/21/11
DESC: A library for controlling a rotating scanner with a servo and distance sensors
*/

#ifndef Scanner_h
#define Scanner_h

#include "Sweep.h"
#include "Servo.h"
#include "USSensor.h"
#include "WProgram.h"

#define CENT_OFF 90
#define SERVO_MAX 180
#define SERVO_MIN 0

class Scanner{
public:
  Scanner();
  Servo *servo;
  USSensor *sonar;
  Sweep *sweep;
  Sweep doSweep(int inc);
  int scan(int threshold, unsigned int timeout);
};

#endif

PaulS

Quote
As for the new and delete, when I don't have get the error "undefined reference to `operator new(unsigned int)'" and when looked up, I was told to add the new and delete operators at the top.

Because you tried to use them. I see no need for you to use them.

Code: [Select]
#include "Sweep.h"
What is a Sweep?

Code: [Select]
  Sweep doSweep(int inc);
Why is this function defined to return a Sweep object? If it really is supposed to return a Sweep object, why doesn't it?

I think you are making this class far more complex than it needs to be.

servo should be an object, not an instance.
sonar probably should be, too. Hard to say, since you haven't posted the header file.
Can't comment on sweep, since we have no idea what a sweep is.

Go Up