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
Hello World!¶
This simple program will drive the m3pi forward, spin it left, reverse it and spin it right.
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.
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 }
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 :
| mbed | 3pi Expansion header |
|---|---|
| Ground | Ground |
| Vin | Vcc |
| p8 | nRST |
| p9 (from mbed) | RXD (to the 3pi) |
| p10 (to te 3pi) | TXD (from the 3pi) |