faster sparki

Salut,

being fed up with the discussion about the slow sparki I applied an interim fix to sparki.ccp and sparki.h. Both can be found in the libraries\sparki subdirectory, either in the SprakiIDE directory tree or in the Arduino sketchbook directory tree.
I added the following variables to sparki.h (right at the start of the class definition, MOW are my initials):

[code]public:
SparkiClass();

/// MOW - additions
int M_maxSpeedLeft; // maximum motor speed for the left motor, allowing a motor specific setting if needed
int M_maxSpeedRight; // maximum motor speed for the right motor, allowing a motor specific setting if needed
int M_maxSpeedGripper; // maximum motor speed for the gripper motor
int M_turnSpeedLeft; // speed for left turn in percent of max speed
int M_turnSpeedRight; // speed for right turn in percent of max speed
int M_pctSpeedLeft; // speed of motor in percent of max speed
int M_pctSpeedRight; // speed of motor in percent of max speed
int M_pctSpeedGripper; // speed of motor in percent of max speed
/// MOW - additions END
[/code]

in sparki.cpp (staring at line 311) I changed the calls to the motorRotate to map the percent to the max values ( I commented out the original code and marked all changes with my initials):

void SparkiClass::moveRight()
{
  // MOW
  //  motorRotate(MOTOR_LEFT, DIR_CCW, 100);
  //  motorRotate(MOTOR_RIGHT, DIR_CCW, 100);
  motorRotate(MOTOR_LEFT, DIR_CCW, map(M_turnSpeedLeft,0,100,0,M_maxSpeedLeft));
  motorRotate(MOTOR_RIGHT, DIR_CCW, map(M_turnSpeedRight,0,100,0,M_maxSpeedRight));
  // MOW END
}

void SparkiClass::moveLeft(float deg)
{
  float turn = 21.388888*deg;
  moveLeft();
  delay(long(turn));
  moveStop();
}

void SparkiClass::moveLeft()
{
  // MOW
  //  motorRotate(MOTOR_LEFT, DIR_CW, 100);
  //  motorRotate(MOTOR_RIGHT, DIR_CW, 100);
  motorRotate(MOTOR_LEFT, DIR_CW, map(M_turnSpeedLeft,0,100,0,M_maxSpeedLeft));
  motorRotate(MOTOR_RIGHT, DIR_CW, map(M_turnSpeedRight,0,100,0,M_maxSpeedRight));
  // MOW END
}

void SparkiClass::moveForward(float cm)
{
  float run = 222.222222*cm;
  moveForward();
  delay(run);
  moveStop();
}

void SparkiClass::moveForward()
{
  // MOW
  //  motorRotate(MOTOR_LEFT, DIR_CCW, 100);
  //  motorRotate(MOTOR_RIGHT, DIR_CW, 100);
  motorRotate(MOTOR_LEFT, DIR_CCW, map(M_pctSpeedLeft,0,100,0,M_maxSpeedLeft));
  motorRotate(MOTOR_RIGHT, DIR_CW, map(M_pctSpeedRight,0,100,0,M_maxSpeedRight));
  // MOW END
}

void SparkiClass::moveBackward(float cm)
{
  float run = 222.222222*cm;
  moveBackward();
  delay(run);
  moveStop();
}

void SparkiClass::moveBackward()
{
  // MOW
  //  motorRotate(MOTOR_LEFT, DIR_CW, 100);
  //  motorRotate(MOTOR_RIGHT, DIR_CCW, 100);
  motorRotate(MOTOR_LEFT, DIR_CW, map(M_pctSpeedLeft,0,100,0,M_maxSpeedLeft));
  motorRotate(MOTOR_RIGHT, DIR_CCW, map(M_pctSpeedRight,0,100,0,M_maxSpeedRight));
  // MOW END
}

void SparkiClass::moveStop()
{
  motorStop(MOTOR_LEFT);
  motorStop(MOTOR_RIGHT);
}

void SparkiClass::gripperOpen()
{
  // MOW
  // motorRotate(MOTOR_GRIPPER, DIR_CCW, 100);
  motorRotate(MOTOR_GRIPPER, DIR_CCW, map(M_pctSpeedGripper,0,100,0,M_maxSpeedGripper));
  // MOW END
}
void SparkiClass::gripperClose()
{
  // MOW
  // motorRotate(MOTOR_GRIPPER, DIR_CW, 100);
  motorRotate(MOTOR_GRIPPER, DIR_CW, map(M_pctSpeedGripper,0,100,0,M_maxSpeedGripper));
  // MOW END
}

With this, I just have to declare my preferred speed values in the setup() routine of the sketch, here using the wall_avoidance code:

[code]#include <Sparki.h> // include the sparki library

void setup()
{
sparki.M_maxSpeedLeft = 250;
sparki.M_maxSpeedRight = 250;
sparki.M_maxSpeedGripper = 250;
sparki.M_turnSpeedLeft = 100;
sparki.M_turnSpeedRight = 100;
sparki.M_pctSpeedLeft = 100;
sparki.M_pctSpeedRight = 100;
sparki.M_pctSpeedGripper = 100;
// position the Ultrasonic sensor
sparki.servo(SERVO_CENTER);
}

void loop()
{
sparki.RGB(RGB_GREEN); // turn the light green
int cm = sparki.ping(); // measures the distance with Sparki’s eyes
sparki.moveForward();
if(cm != -1) // make sure its not too close or too far
{
if(cm < 20) // if the distance measured is less than 10 centimeters
{
sparki.RGB(RGB_RED); // turn the light red
sparki.beep(); // beep!
sparki.moveBackward(10); // back up 10 centimeters
sparki.moveRight(30); // rotate right 30 degrees
delay(1000);
}
}
delay(100); // wait 0.1 seconds (100 milliseconds)
}[/code]

With this, my sparki starts to move nicely, using 6 AA batteries.
I will check the entire lib and once finished, I will post the complete lib with all my changes flagged with my initials on this topic here.

Ciao, Mathias

Nice. Just a bit more technical than my fix on the other post :blush: Your fix allows specifics for each sketch. Nice work.

Salut,

I do not claim the “copyright” on the fix - together we will get this robot to work!

Ciao, Mathias

Cool :smiley:

Thanks Mathias, we updated the code to include a speed fix. It looks like somehow we accidentally halved Sparki’s speed. 100 will be the new normal again. It will post in the next update shortly.

@roboalchemist: The angles were halved also right? Is that fix included? Thanks.

Right now the code uses a time delay, which explains why the angles suddenly became halved. With the speed doubled, angles go back to normal.

Couple of questions:

  1. How do we get your updates onto our computers when you post them? Will we have to re-download the IDE?
  2. When you say “100 will be the new normal”, what does that mean? Will Sparki by default go as fast as possible without damaging the hardware?
  3. Will there be a way to specify the speed via a function call?

[quote=“MathiasW”]
…With this, my sparki starts to move nicely, using 6 AA batteries…

Ciao, Mathias[/quote]

Mathias, how are you using 6 AA batteries in your Sparki?

Salut,

two thoughts in one word :sunglasses: : I use 4 AA batteries at 6 Volt. I also plan to test the Sparki with a 7.2 LiPo as replacing the batteries with rechargeable ones with not result in 6 V but 4.8 V (4x1.2 V of a AA akku). I am not a fan of batteries I cannot recharge. And with a LiPo Charger, I can charge the LiPo with a solar panel while I am fixing the library code :laughing:

Ciao, Mathias

Just a note, speed is entirely independent of battery level with Sparki. While it may change for regular motors, that’s just not how stepper motors work. The only way this would be the case is if Sparki is truly on its last legs already.

[quote=“vladarino”]1. How do we get your updates onto our computers when you post them? Will we have to re-download the IDE?
2. When you say “100 will be the new normal”, what does that mean? Will Sparki by default go as fast as possible without damaging the hardware?
3. Will there be a way to specify the speed via a function call?[/quote]

1.) Yup, just re-download and install
2.) Yes, it will go as fast as possible now by default.
3.) Not yet, but that’s a good idea. I’ll put it on the list for the next update.

Am I right, does the spark now run at the maximal possible speed?

Can it in principle eventually go faster, by accelerating from low to high? that way it doesn’t need to start the motor at the highest speed, Just curious!

Gerrit.

Until ArcBotics makes a new, official library release, you can check out my library enhancements at this post:

Along with other new features, you can set default speed for the wheels and gripper with expressions like

sparki.wheelSpeedPercentDefault = 80; sparki.gripperSpeedPercentDefault = 100;

Each function like driveForward(), turnLeft(), openGripper(), etc. also takes an optional speed percent parameter.

I my library, I currently have the default speed set to 80%. Setting that to 100% (through one of the above-mentioned mechanisms) will get you as much speed as the motors can handle. Don’t expect much; Sparki is not a race car. Note that setting the speed percentage to something larger than 100 will not accomplish anything; the library code clamps the percentage at 100.

I played around to see how fast the motors can turn. The stepper motors have inherent physical limitations about how quickly they can respond to changes in their control inputs. If the inputs change too quickly, the motors just buzz but don’t move.

Cranking up the battery voltage as someone suggested may get you a little more speed (assuming the library is modified accordingly), but at some point the battery voltage will decline to a point where it stops working again. Also, the stepper motors are rated at 5V, and I don’t know how much design margin they have to cope with higher voltages. I suspect the exercise may not be worth the time and risk, but if someone tries it, I’m interested in learning the results.

Finally, note that 0% speed does not mean stop. 0% is a somewhat arbitrarily chosen minimum speed.