m5stack / M5StickC-Plus

M5StickCPlus Arduino Library
MIT License
367 stars 90 forks source link

Issue with ESP32 Analog Motor Control (using ledcwrite) #29

Closed PlastiBots closed 1 year ago

PlastiBots commented 2 years ago

I've been trying to get my M5StickC-Plus working with various motor controllers including TB6612FNG / MX1508 and have come across an issue. They work fine on the M5Stick-C Plus without the M5StickCPlus.h library (and related features) included, but it fails when enabling the M5 library. In both examples below, the code compiles/downloads fine, but the motor fails to drive in the latter example. I also did some testing using the MX1508 motor driver board and the same issue happened and can re-create this issue using other similar libraries. Also verified both motor controller boards are working. I suspect there is some conflict with setting up ledcwrite related features (e.g. channels? etc)? As there are no errors thrown during compile time, I can't debug the issue.

I've included 2 samples below using the TB6612FNG controller:

Sample 1 WORKS: No M5 library included and works fine and the motor drives per instruction:

#include <Arduino.h>
#include <TB6612_ESP32.h>
#define AIN1 26
#define AIN2 25
#define PWMA 0
#define STBY NULL   //note STBY connected directly to 5V pulled high (to save a pin)

int counter =0;
const int offsetA = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY,5000 ,8,1 );
// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.

void setup()
{
  Serial.begin(115200);
  gpio_pulldown_dis(GPIO_NUM_36);  //G36/25 share the same port, so set 36 as floating - https://github.com/m5stack/M5StickC-Plus/blob/master/README.md
  gpio_pullup_dis(GPIO_NUM_36);    // above

}

void loop()
{
  for(int i=0; i<200; i++)
  {
    motor1.drive(-200,5);   //speed + or -, duration in ms
    motor1.brake();
    delay(10);
    counter++;
    Serial.println(counter);
  } 
  for(int i=0; i<200; i++)
  {
    motor1.drive(200,5);   //speed + or -, duration in ms
    motor1.brake();
    delay(10);
    counter--;
    Serial.println(counter);
  } 
}

Sample 2 DOES NOT WORK: M5 library and a few features enabled. It compiles and downloads fine, but the motor does not drive.

#include <Arduino.h>
#include <TB6612_ESP32.h>
#include <M5StickCPlus.h>
#define AIN1 26
#define AIN2 25
#define PWMA 0
#define STBY NULL    //note STBY connected directly to 5V pulled high (to save a pin)

int counter =0;
const int offsetA = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY,5000 ,8,1 );

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.

void setup()
{
  Serial.begin(115200);
 M5.begin();
  gpio_pulldown_dis(GPIO_NUM_36);  //G36/25 share the same port, so set 36 as floating - https://github.com/m5stack/M5StickC-Plus/blob/master/README.md
  gpio_pullup_dis(GPIO_NUM_36);    // above

}

void loop()
{
  M5.update(); 

     //Use of the drive function which takes as arguements the speed
     //and optional duration.  A negative speed will cause it to go
     //backwards.  Speed can be from -255 to 255.  Also use of the
     //brake function which takes no arguements.

 if (M5.BtnA.wasPressed())
 {
  for(int i=0; i<200; i++)
  {
    motor1.drive(-200,5);   //speed + or -, duration in ms
    motor1.brake();
    delay(10);
    counter++;
    Serial.println(counter);
  } 
  for(int i=0; i<200; i++)
  {
    motor1.drive(200,5);   //speed + or -, duration in ms
    motor1.brake();
    delay(10);
    counter--;
    Serial.println(counter);
  } 

 }        
}
PlastiBots commented 2 years ago

After some testing, I figured it out. It seems that the M5StickC Plus library is making use of Channel 0 and 1. Once the motor is set on Channel 2, it works fine.

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY, 5000 , 8, 2);