Continuous Servo Issue

Hello everyone and thank you in advance!

I have a very simple task but am stumped by my servos. Basically, I just need to drive a robot and use ultrasonic sensors for obstacle avoidance. The robot is a tank bot and two continuous servos to turn the tracks.

Here is a link to the servos:
Amazon.com.

And here are the important snippets of code:
Main

#include "Sonar/Sonar.h"
#include "Control/Control.h"
#include "Control/Control.cpp"


#define LEFT_MOTOR        7
#define RIGHT_MOTOR       6


// Sonar (trig, echo)
Sonar front_sonar(SONAR_FRONT_TRIG, SONAR_FRONT_ECHO);
Sonar right_sonar(SONAR_RIGHT_TRIG, SONAR_RIGHT_ECHO);
Sonar left_sonar(SONAR_LEFT_TRIG, SONAR_LEFT_ECHO);


// Control (left servo pin, right servo pin)
Control controller(LEFT_MOTOR, RIGHT_MOTOR);


void setup() {
  // put your setup code here, to run once:
//   Serial.begin(9600);
}


void loop() {
// put your main code here, to run repeatedly:
  controller.Stop();
  delay(100);
}

Control.h

#pragma once

#include <Servo.h>

class Control {
  public:
    Control(int left_servo_pin, int right_servo_pin) {
      left_servo_.attach(left_servo_pin);
      right_servo_.attach(right_servo_pin);

      left_servo_.write(stop_);
      right_servo_.write(stop_);
    }

    void Stop();
    void ForwardFullSpd();
    void BackwardFullSpd();
    void TurnLeft();
    void TurnRight();

  // private:
    const int forward_spd_{0};
    const int backward_spd_{180};
    const int stop_{90};

    Servo left_servo_;
    Servo right_servo_;
};

Control.cpp

void Control::Stop() {
  right_servo_.write(stop_);
  left_servo_.write(stop_);
}
...

When I upload the code, the tracks move continuously even though my Control constructor immediately tells the tracks to stop. Furthermore, my loop function also commands the robot to stop. I looked at the servo to ensure that it follows the standard servo notations and sure enough:

The servo rotates at full speed CW or CCW. If programming with values used for normal 90° servos, a setting of 90 is 'no movement', a setting less than 45 is movement in the CW direction and a setting of more than 135 will be movement in the CCW direction.

In my debugging, I have narrowed down the cause of this problem to the left_servo_.attach() and right_servo_.attach(). As soon as I "attach" a motor, it starts going haywire. If I comment one of these attachments out, the corresponding motor stays in place while the attached motor turns. I even tried using analogWrite() to avoid attaching the pin but that also caused it to turn.
Please let me know if this is likely a hardware problem or if I made some sort of stupid mistake in my code.
Once again, thank you so much and I look forward to fixing this issue!

Have you tested the servos with a simple program to see if 90 is the correct value for stop? It's usually somewhere near that but often not exactly 90.

Also how are the servos powered? Odd behaviour is often caused by insufficient power, sometimes GNDs not connected. A schematic would be useful.

Steve

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.