Let's Make Robots!

Arduino Esc: How to turn twitch into full speed spin!?

All Right! I finally have a code that does something, and a new esc! The brushless motor weakly twitches on all of the cycles, but am unable to figure out what I need to do to the code to turn this twitch into a full speed spin (increasing the angle does not help). The code is below:

#include <Servo.h>

#define MAXANGLE 160

typedef struct {
  int pinpwm;
  int range[2];
} ESCRecord;

ESCRecord escconf[4];
Servo escservos[4];
int angle = 0;

void config() {
  escconf[1] = (ESCRecord) {9, {836, 2400}};
}

void arm() {
  for(int esc = 0; esc < 4; esc++) {
    escservos[esc].attach(escconf[esc].pinpwm, \
                          escconf[esc].range[0], \
                          escconf[esc].range[1]);
    escservos[esc].write(0);
    delay(15);
  }
}

void cycle() {
  Serial.print("Cycle ESC Number ");
  for(int esc = 0; esc < 4; esc++) {
    Serial.print(esc);
    for(int angle = 0; angle <= MAXANGLE; angle++) {
      escservos[esc].write(angle);
      delay(15);
    }
    for(int angle = MAXANGLE; angle >= 0; angle--) {
      escservos[esc].write(angle);
      delay(15);
    }
  }
  Serial.println("");
}

void setup() {
  delay(1000);
  Serial.begin(9600);

  Serial.println("Wait for your input! [s] to init...");
  while(Serial.available() <= 0)
    delay(1000);
  int incomingByte = Serial.read();

  Serial.println("Start the system...");

  config();
  arm();
  delay(5000);

  if(incomingByte == 115) {
    Serial.println("Traverses all angles upward and downward."); 
    cycle();
    delay(1000);
  }
  Serial.println("Start loop...");
}

void setspeed(int angles[4]) {
  for(int esc = 0; esc < 4; esc++) {
    escservos[esc].write(angles[esc]);
    delay(120);
  }
}

void loop() {
  if(Serial.available() > 0) {
    int incomingByte = Serial.read();
    if(incomingByte == 43)      // sends the character '+'
      angle++;                  // increases the speed
    else if(incomingByte == 45) // sends the character '-'
      angle--;                  // decreases the speed
    else if(incomingByte == 48) // sends the character '0'
      angle = 0;                // set the speed to zero
   
    if(angle < 0)
      angle = 0;
    if(angle > MAXANGLE)
      angle = MAXANGLE;
     
    Serial.print("Set the angle to: ");
    Serial.println(angle);
  }
   
  int angles[4] = {angle, angle, angle, angle};
  setspeed(angles);
}

 

From: http://blog.mobileapes.com/2010/04/control-all-four-motors-from-arduino.html

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
drewtoby's picture

Or would a dc drill motor run off an 11v lipo work better for a mini sumo "spinner"? My contest is closing in so I may have to do that if we can't get it working soon.

bdk6's picture

I have never used one of these and the manual didn't give any detail, but I notice in your setspeed() function you have delay(120).  Servos, which this thing is designed to emulate, need updated values every 20 mS.  You can't update any faster than 120 mS with that delay.  I would try changing that to delay(18) and see if that helps.

My 1 1/2 cents worth

bdk6's picture

After thinking a minute I remembered the servo library uses a timer on Arduino, so it pulses automatically in the background.  My comment above is probably not relevant at all.

OddBot's picture

Without the instruction manual for the ESC ir will be hard to help you. When I built my Driftin Turbo Trike I had to similate the RC controls going through a simple safety start up sequence. To do this I needed to read the manual.

drewtoby's picture

I am using a 3 cell lipo as well. When it twitches it also beeps in 1 sec intervals, making me think that the led on my on/off switch may be stealing a lot power. Is that possible? It arms on cycle 12 on the code above

bdk6's picture

Page 2 of the manual has a section, Alert Tones, that says 1 second interval beeps means battery voltage is too low or high.  Since it seems you are using the right number of battery cells, my guess would be that when it tries to come on the voltage is sagging.  That is backed up by you saying the LED blinks.  The LED can't draw enough power to stop the motor.  It would burn up almost instantly.  My guess is you need a BFC ( Big F***ing Capacitor) across the power supply to stop the voltage droop.  Put the biggest cap you have across the power input and try that.

bdk6's picture

near the end of your posted code you have several lines like:

if(incomingByte == 43) // sends the character '+'

You can make it more readable by using the character directly like this:

if(incomingByte == '+') 

A character can act as an integer/byte by enclosing it in apostrophes (same key as quote mark " )