ESC behavior - Unpredictable response

To preface my post, I am developing a mobile robot which should travel predetermined distance and come to a halt at the target after traversing a predetermined path. I use a BLDC motor with ESC to drive the robot and the microcontroller, sensors, arduino code manage the actions. I use laser for target pointing.

The sequence of set up activities includes 1) esc calibration, 2) laser pointing to align the vehicle and 3) then flipping on a SPST switch to let the microcontroller know to accelerate the mobile robot.

I am experiencing an unpredictable behavior at the moment. After complete set up steps 1 and 2 above, which is about 2 mins, the vehicle doesn't start immediately after flipping the switch. Rather it takes about 8~10 seconds to start and then start moving.

I researched that this could be due to fail safe mechanism built into esc. So, I keep alternating between two relatively close Neutral PWM signals to prevent the robot and motor from entering into fail safe mode. However, this has not been reliable. It sometimes starts immediately when the switch is turned on after laser aiming, and at other times it takes 8~10 seconds to start. Could you please explain to me what could be going on here?

Thanks

If You provide code and schematics, all according to How to get the best out of this forum - Projects / General Guidance - Arduino Forum
replies could be given.

1 Like

You probably have a bad connection or maybe it is a problem in the code or maybe it is the power supply or maybe its ???? Without your code and an annotated schematic showing how you wired this the odds of getting a good answer are slim.

Sorry about that. Below is the code:


#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// ESC and Steering PWM settings
const int escPin = 10;          // Pin for ESC signal
const int startSwitchPin = 14;    // Start switch pin 
const int pwmFrequency = 50;    // 50 Hz for ESC and servo
const int minPulseWidth = 1000; // Min throttle (1ms)
const int neutralPulseWidth = 1500;
const int movePulseWidth = 1550;

// Setup function
void setup() {
  //Serial.begin(9600);

  // Initialize PWM for ESC 
  pinMode(escPin, OUTPUT);
  pinMode(startSwitchPin, INPUT_PULLUP); // Use internal pull-up resistor
  analogWriteFrequency(escPin, pwmFrequency);
  analogWriteResolution(16);

  // Initialize LCD
  lcd.begin();
  lcd.backlight();
  lcd.clear();

  // Non-blocking wait for ESC to arm
  while (!escArmed) {
  escArmStartTime = millis();
  // Calibrate ESC
  Serial.println("Calibrating ESC...");
  setPWM(escPin, maxPulseWidth);
  delay(2000);
  setPWM(escPin, minPulseWidth);
  delay(2000);
  setPWM(escPin, neutralPulseWidth);
  delay(2000);
  escArmed = true;
  lcd.clear();
  lcd.print("ESC Armed. Waiting for switch...");

  // Send minimum throttle signal periodically to keep ESC armed
    if (millis() - lastSignalTime >= 20) {
      setPWM(escPin, neutralPulseWidth); // Minimum Throttle
      delay(50);
      lastSignalTime = millis();
    }
  }
  // Non-blocking wait for the start switch, with a timeout
  switchWaitStartTime = millis();
  while (!switchActivated) {
    if (digitalRead(startSwitchPin) == LOW) { // Switch is pressed
      switchActivated = true;
      lcd.clear();
      lcd.print("Switch activated! Accelerate...");
    }

    // Continue sending minimum throttle signals to prevent esc from disarm and entering fail safe mode
    if (millis() - lastSignalTime >= 20) {

      lastSignalTime = millis();
      if (escneutralToggle == false){
        escneutralToggle = true;
        setPWM(escPin, neutralPulseWidth); // Minimum Throttle
        //delay(50);
      } else{
        escneutralToggle = false;
        setPWM(escPin, neutralPulseWidth + 1); // Minimum Throttle
        //delay(50);
      }
    }
  }
    setPWM(escPin, neutralPulseWidth);
    delay(2000);
  // Ramp up motor speed
  for (int pulseWidth = movePulseWidth; pulseWidth <= maxPulseWidth; pulseWidth += 20) {
    setPWM(escPin, pulseWidth);
    delay(200);
  }
}

// Function to generate PWM signals
void setPWM(int pin, int pulseWidth) {
  float dutyCycle = (pulseWidth * 65535) / (1000000 / pwmFrequency);
  analogWrite(pin, dutyCycle);
}


Was the above code snippet helpful?

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