You are viewing an older revision! See the latest version

m3pi

Before you start

Before you run the hello world program, you must ensure that the Pololu 3pi robot has been reprogrammed to accept commands from the mbed Microcontroller. It is the combination of the mbed hardware and the new firmware on the 3pi that makes it an m3pi!

See Preparing your m3pi for more details

m3pi

Hello World!

This simple program will drive the m3pi forward, spin it left, reverse it and spin it right.

You Tube Clip

When rotating left or right at half speed, the m3pi spins at approximately 710 degrees per second, while forward and backward at half speed amounts to 470mm per second

Import program

00001 #include "mbed.h"
00002 #include "m3pi.h"
00003 
00004 m3pi m3pi;
00005 
00006 int main() {
00007 
00008     m3pi.locate(0,1);
00009     m3pi.printf("LO World");
00010 
00011     wait (2.0);
00012 
00013     m3pi.forward(0.5); // Forward half speed
00014     wait (0.5);        // wait half a second
00015     m3pi.left(0.5);    // Turn left at half speed
00016     wait (0.5);        // wait half a second
00017     m3pi.backward(0.5);// Backward at half speed 
00018     wait (0.5);        // wait half a second
00019     m3pi.right(0.5);   // Turn right at half speed
00020     wait (0.5);        // wait half a second
00021 
00022     m3pi.stop();       
00023 }

Line Following

This example program uses the reflective sensors under the front of the m3pi to detect a black line on a white background (or vice versa!) and control the motors to keep the line in the center of the sensors.

Note that this is a very (very) crude algorithm. Much more sophisticated control algorithms including PID can achieve far better line following performance.

You Tube Clip

Import program

00001 #include "mbed.h"
00002 #include "m3pi.h"
00003 
00004 m3pi m3pi;
00005 
00006 int main() {
00007 
00008     // Parameters that affect the performance
00009     float speed = 0.2;
00010     float correction = 0.1;   
00011     float threshold = 0.5;
00012 
00013     m3pi.locate(0,1);
00014     m3pi.printf("Line Flw");
00015 
00016     wait(2.0);
00017     
00018     m3pi.sensor_auto_calibrate();
00019     
00020     while (1) {
00021 
00022         // -1.0 is far left, 1.0 is far right, 0.0 in the middle
00023         float position_of_line = m3pi.line_position();
00024 
00025         // Line is more than the threshold to the right, slow the left motor
00026         if (position_of_line > threshold) {
00027             m3pi.right_motor(speed);
00028             m3pi.left_motor(speed-correction);
00029         }
00030 
00031         // Line is more than 50% to the left, slow the right motor
00032         else if (position_of_line < -threshold) {
00033             m3pi.left_motor(speed);
00034             m3pi.right_motor(speed-correction);
00035         }
00036 
00037         // Line is in the middle
00038         else {
00039             m3pi.forward(speed);
00040         }
00041     }
00042 }

m3pi with Remote Proceedure Call (RPC) Interface

The RPC interface is a very powerful way to remotely execute functions. Michael Walker did some excellent work on the RPC interface, and wrote the RPC Interface cookbook page.

The main initial interest for m3pi is to execute RPC calls over a serial interface. This could be directly from USB serial, or using any one of many serial-cable-replacement techniques, including Bluetooth and Zigbee.

Import program

00001 #include "mbed.h"
00002 #include "m3pi.h"
00003 #include "SerialRPCInterface.h"
00004 
00005 SerialRPCInterface Interface(USBTX, USBRX); // defaults to 9600
00006 m3pi m3pi;
00007 
00008 int main() {
00009 
00010     m3pi.locate(0,1);
00011     m3pi.printf("USB RPC");
00012 
00013     // do nothing, just wait for RPC comands over USB
00014     while (1) {}
00015 }

To keep things simple initially, the first experiment will be to use the mbde USB Serial port. If this is your first encounter with mbed, you can read more about the USB Serial port mbed provides on the Serial PC page in the handbook.

If you are using Windows, you will need to install a driver to have access to the mbed USB Serial port. You can find more information on the Windows Serial Configuration page.

To get this example working:

  • Build the USB RPC example, and program it into your mbed
  • If you are using Windows, run the Windows Serial Configuration
  • Open a serial terminal, if you are not sure which to use, see the Serial Terminals page in the handbook.
  • Connect to your mbeds Serial port using standard/default "9600 8-N-1" settings
  • Ensure that your serial terminal sends "CR+LF" on transmission
  • Start typing commands to control your m3pi

/media/uploads/chris/m3piusbrpcteraterm.png

Typing /m3pi into the terminal will display the list of commands possible. For reference they are:

A subset of the function in the m3pi class can be called over RPC, they are:

  • forward - both motors forward at the same speed (0.0 to 1.0)
  • backward - both motors backwards at the same speed (0.0 to 1.0)
  • left - Rotate, left motor backwards, right motor forwards (0.0 to 1.0)
  • right - Rotate, right motor backwards, left motor forwards (0.0 to 1.0)
  • left_motor - Direct control of direction and speed (-1.0 to 0.0)
  • right_motor - Direct control of direction and speed (-1.0 to 0.0)
  • stop - As it says :-)
  • battery - Read the battery voltage as a float
  • line_position - Where the line is detected, from left to right (-1.0 to 1.0)
  • sensor_auto_calibrate - Run the auto calibration routine

Hardware

There is very little connectivity needed needed to make the m3pi work, and it is all taken care of by the m3pi PCB. The basic connections are :

mbed3pi Expansion header
GroundGround
VinVcc
p8nRST
p9 (from mbed)RXD (to the 3pi)
p10 (to te 3pi)TXD (from the 3pi)

All wikipages