1

so basically I am using 4 servo motors for our robotic arm and 1 l298n motor driver for 2 motor drive for base of the arm. when I individually tested robotic arm and controlled 4 servo motors, it worked perfectly fine, at the same time when i tested linear motion with motor driver individually it also worked fine. But when I combined them only servos are working but l298n doesn't. when I comment out servos.attach() line, then again motor driver works. Pins are different. That's why even I couldn't figure out why am i getting this issue.

#include <WiFi.h>
#include <WebServer.h>
#include <Robojax_L298N_DC_motor.h>
#include<Servo.h>

WebServer server(80);
Servo myservo,shoulder,elbow,wrist;
int grippos=0,shoulderpos=0,elbowpos=0,wristpos=0;



// motor 1 settings
#define CHA 0
#define ENA 19 // this pin must be PWM enabled pin if Arduino board is used
#define IN1 18
#define IN2 5

// motor 2 settings
#define IN3 15
#define IN4 2
#define ENB 4// this pin must be PWM enabled pin if Arduino board is used
#define CHB 1

const int CCW = 2; // do not change
const int CW  = 1; // do not change

#define motor1 1 // do not change
#define motor2 2 // do not change

String prev="";

Robojax_L298N_DC_motor robot(IN1, IN2, ENA, CHA,  IN3, IN4, ENB, CHB);

void gripopen()
{
  Serial.println("open");
  grippos+=30;
  if(grippos>180)
  {
    grippos=180;
  }
  myservo.write(grippos);
  
}

void gripclose()
{
  grippos-=30;
  Serial.println("close");

  if(grippos<0)
  {
    grippos=0;
  }
  myservo.write(grippos);
  
}

void wristleft()
{
  wristpos-=30;
  Serial.println("wristleft");
  if(wristpos<0)
  {
    wristpos=0;
  }
  wrist.write(wristpos);
}

void wristright()
{
  Serial.println("wristright");
  wristpos+=30;
  if(wristpos>180)
  {
    wristpos=180;
  }
  wrist.write(wristpos);
}

void elbowup()
{
  Serial.println("elbow up");
  elbowpos+=15;
  if(elbowpos>180)
  {
    elbowpos=180;
  }
  elbow.write(elbowpos);
}

void elbowdown()
{
  Serial.println("elbow down");
  elbowpos-=15;
  if(elbowpos<0)
  {
    elbowpos=0;
  }
  elbow.write(elbowpos);
}

void shoulderup()
{
  Serial.println("shoulder up");
  shoulderpos+=15;
  if(shoulderpos>180)
  {
    shoulderpos=180;
  }
  shoulder.write(shoulderpos);
}

void shoulderdown()
{
  Serial.println("shoulder down");
  shoulderpos-=15;
  if(shoulderpos<0)
  {
    shoulderpos=180;
  }
  shoulder.write(shoulderpos);
}

void forward()
{
    Serial.println("forward");
    robot.rotate(motor1,100,CW);
    robot.rotate(motor2,100,CCW);
  
Serial.println("end reached");
  
}

void backward()
{
    Serial.println("backward");
        robot.rotate(motor1,100,CCW);
    robot.rotate(motor2,100,CW);
  Serial.println("end reached");
  
}

void left()
{
    Serial.println("left");
        robot.rotate(motor1,100,CW);
    robot.rotate(motor2,100,CW);
Serial.println("end reached");
  
}

void right()
{
    Serial.println("right");
      robot.rotate(motor1,100,CCW);
    robot.rotate(motor2,100,CCW);
Serial.println("end reached");
  
}

void stopbot()
{
  Serial.println("stopbot");
  robot.brake(1);
  robot.brake(2);
  prev="";
  Serial.println("end reached");
}

void reset()
{
  myservo.write(0);
  shoulder.write(0);
  elbow.write(0);
  wrist.write(0);
}

void handleRoot() {
  server.send(200, "text/plain", "Ready");
}

void handleGet() {
  if (server.hasArg("data")) {
    String data = server.arg("data");
    if(data=="gripper_open")
    {
      gripopen();
    }
    else if(data=="reset")
    {
      Serial.println("Reset");
    }
    else if(data=="Stop")
    {
      stopbot();
    }
    else if(data=="Forward")
    {
     // forward();
      if(prev!=data)
      {
        forward();
        prev=data;
      }
    }
    else if(data=="Backward")
    {
     // backward();
      if(prev!=data)
      {
        backward();
        prev=data;
      }
    }
    else if(data=="Left")
    {
      //left();
      if(prev!=data)
      {
        left();
        prev=data;
      }
    }
    else if(data=="Right")
    {
      //right();
      if(prev!=data)
      {
        right();
        prev=data;
      }
    }
    else if(data=="gripper_close")
    {
      gripclose();
    }
    else if(data=="elbow_up")
    {
      elbowup();
    }
    else if(data=="elbow_down")
    {
      elbowdown();
    }
    else if(data=="shoulder_up")
    {
      shoulderup();
    }
    else if(data=="shoulder_down")
    {
      shoulderdown();
    }
    else if(data=="Wrist_left")
    {
      wristleft();
    }
    else if(data=="Wrist_right")
    {
      wristright();
    }
    Serial.println("Data: " + data);
   
  }
  server.send(200, "text/plain", "Data Received");
}

void handlePost() {
    if (server.hasArg("data")) {
    String data = server.arg("data");
    Serial.println("Data: " + data);
  }
 // server.send(200, "text/plain", "Data Received");
  server.send(200, "text/plain", "Processing Data");
}

void handleUpload() {
  HTTPUpload& upload = server.upload();
  if (upload.status == UPLOAD_FILE_START) {
    Serial.println("Receiving data:");
  } else if (upload.status == UPLOAD_FILE_WRITE) {
    Serial.write(upload.buf, upload.currentSize);
  } else if (upload.status == UPLOAD_FILE_END) {
    server.send(200, "text/plain", "Data: ");
  }
}

void setup() {
  Serial.begin(9600);
  WiFi.softAP("ESP32");
  myservo.attach(13);
  shoulder.attach(12);
  elbow.attach(14);
  wrist.attach(27);
  
  
  myservo.write(0);
  shoulder.write(0);
  elbow.write(0);
  wrist.write(0);

  // motordriver pins
  robot.begin();
  
  server.on("/", handleRoot);
  server.on("/get", HTTP_GET, handleGet);
  server.on("/pot", HTTP_POST,handlePost);
  server.begin();
}

void loop() {
  server.handleClient();
}

I am receiving correct data from the app. That also I had cross-checked. If anyone can help with this or guide me how can i solve this issue.

6
  • How do you power everything?
    – chrisl
    Jan 10, 2022 at 18:52
  • 12v lithium polymer battery 2200mAh. @chrisl Jan 11, 2022 at 2:03
  • And I guess you have connected the 12V from the battery directly to the L298N for powering the motor with 12V, am I right? I don't see an obvious error. Is the Serial output as expected? Have you checked the voltages (if they really stay as expected and not go down when running the motor)?
    – chrisl
    Jan 11, 2022 at 9:49
  • yes input voltage to l298n shows to be around 12v. also I have kept ground common with esp32. Serial monitor also gives correct output indicating that right function is being called. that's why I also don't understand where the issue lies. @chrisl Jan 11, 2022 at 10:06
  • This tutorial states that you cannot use the standard Servo library for an ESP32. Honestly I would think that this isn't a problem anymore, but you could try the library, that is mentioned there.
    – chrisl
    Jan 11, 2022 at 10:15

1 Answer 1

1

Hi I have the same problem here, and everything is pointing the issue could be related with the channels shared for PWM. In the Robojax lib there is control of channels want to assign for each motor, but in the ESP32Servo the channels are assigned automatically by the library in consecutive order (see ESP32PWM.cpp). I would try use different channel when calling Robojax that doesn't make conflict with the amount of Servo controlled, maybe could help. For instance in my case I have one servo and 2 DC motors controlled by LN98. So the servo is using channel 0 for PWM, the other two motors on Robojax I will try CHA= 2 and CHB = 3. Maybe this could help.

There are timers shared is explained on the ESP32PWM.cpp, line 259, but I believe the channels conflicts is what is causing this issue.

I will try and let you know. Thanks

I already finished, and for me it is working now. Just need to change CHA and CHB to different PWM channel number (0-15), but > than the amount of servo you have. thanks

1
  • actually even I felt the same and I was going to try that today. I guess it will work. Thank you bro Jan 16, 2022 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.