Zumo Simple Driving Program

These blog posts on the Zumo will start out very simple.  This is assuming the reader has little to no Arduino experience.  The goal of this post is simply to teach the reader how to write a program that will make the Zumo robot move forward for 1 second, pause, move backwards for 1 second, pause, then repeat.

When using the Zumo 32U4, you need to include the Zumo 32U4 library file.  This includes all of the basic commands to interface with the robot.  This is done by adding the line:

include <Zumo32U4.h>

to the program.  This should be the first line in the program (after any comments you might add).  The second line you will want to add is:

Zumo32U4Motors motors;

Zumo32U4Motors is what’s called a Class.  A Class is a way of defining an object.  We can discuss Classes later, but what you need to understand is that we need to create an object using the Class Zumo32U4Motors, and call it “motors.”  This is done using the line above.  From here on out, we can refer to our instance of the class simply as motors.

Now that we got that stuff out of the way, every Arduino program has to have two sections.  When you create a new program, the Arduino IDE will create these two sections for you.  It should look something like:

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
} 

These are actually both functions, one named “setup” and the other named “loop.”  Traditionally, functions return values, and the type of value that they return needs to be defined before the function name.  However, these functions do not return anything.  Therefore, their type is call void.  When the Arduino executes a program, it runs the setup function once, then runs the loop command over and over and over, until the power is turned off or the master reset is pressed.  When it runs a function, it executes everything between the { }.  In this case, there are only comments.  Whenever you have “//” it tells the compile to skip the rest of the line when compiling.  These are comments only for the users to read and the Zumo never sees them.

In the case of our simple program, we have nothing to set up, so just leave the setup function as it is.  Don’t delete it, as the compiler needs to have the function present, but you don’t need to type anything inside it.

For the loop function, you will need some delays and motor commands.  The first command we want is a 1 second delay.  This can be accomplished with the delay command.  This is a function that tells the robot not to do anything different for a certain number of milliseconds.  To delay for 1 second, you would need:

  delay(1000);

Note that the command requires a semicolon after it.  All commands other than compiler directives (like the include statement above) require a semicolon after them.

The next command that we will need is a motor command.  The command belongs to the class Zumo32U4Motors, but remember we created an object for this called motors.  When writing a command that is part of a class, we need to follow the format of classname.command().  The command is setSpeeds().  This command takes two arguments, namely, the left motor speed and the right motor speed.  Zumo speeds range from -400 to +400 and are always integer values.  To make the robot move forward at full speed, we would need:

  motors.setSpeeds(400, 400);

If we want to go forward for 1 second, we would need to follow this with another delay.  To stop the robot, you would then need:

  motors.setSpeeds(0, 0);

Going backwards is the same, but use speeds of -400.

Putting everything together, the program would be:

#include <Zumo32U4.h>
Zumo32U4Motors motors;

void setup() {
}

void loop() {
  delay(1000);
  motors.setSpeeds(400, 400);
  delay(1000);
  motors.setSpeeds(0, 0);
  delay(1000);
  motors.setSpeeds(-400, -400);
}  

You can modify this to do any sort of blind movement you would like.  However, keep in mind that everything is based on time, which makes it hard for precision navigation.  Comparing to the LEGO EV3, this would be equivalent to using unregulated motor blocks based on time.  If you want raw speed, it may work.  If you want precise navigation, not a good idea….  We can touch on better methods of driving and navigation in later posts.

The next post will be learning how to read the motor encoders.

This entry was posted in Robotics and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *