Adds ability to play a tune and to directly read sensor values

Dependents:   m3pi_MazeSolver m3pi_MazeSolverLVC mbeddedNets hiworld

Embed: (wiki syntax)

« Back to documentation index

m3pi Class Reference

m3pi Class Reference

m3pi control class More...

#include <m3pimaze.h>

Public Member Functions

 m3pi (PinName nrst, PinName tx, PinName rx)
 Create the m3pi object connected to the default pins.
void reset (void)
 Force a hardware reset of the 3pi.
void left_motor (float speed)
 Directly control the speed and direction of the left motor.
void right_motor (float speed)
 Directly control the speed and direction of the right motor.
void forward (float speed)
 Drive both motors forward as the same speed.
void backward (float speed)
 Drive both motors backward as the same speed.
void left (float speed)
 Drive left motor backwards and right motor forwards at the same speed to turn on the spot.
void right (float speed)
 Drive left motor forward and right motor backwards at the same speed to turn on the spot.
void stop (void)
 Stop both motors.
float pot_voltage (void)
 Read the voltage of the potentiometer on the 3pi.
float battery (void)
 Read the battery voltage on the 3pi.
float line_position (void)
 Read the position of the detected line.
char sensor_auto_calibrate (void)
 Calibrate the sensors.
void calibrate (void)
 Set calibration manually to the current settings.
void reset_calibration (void)
 Clear the current calibration settings.
void leds (int val)
 Write to the 8 LEDs.
void locate (int x, int y)
 Locate the cursor on the 8x2 LCD.
void cls (void)
 Clear the LCD.
int putc (int c)
 Send a character directly to the 3pi serial interface.
int getc ()
 Receive a character directly to the 3pi serial interface.
int print (char *text, int length)
 Send a string buffer to the 3pi serial interface.
int playtune (char *text, int length)
 Play a tune.
void readsensor (int *sensor)
 Read the calibrated value of a sensor.

Detailed Description

m3pi control class

Example:

 // Drive the m3pi forward, turn left, back, turn right, at half speed for half a second

   #include "mbed.h"
   #include "m3pimusic.h"

   m3pi pi(p8,p9,p10);

   int main() {

     wait(0.5);

     pi.forward(0.5);
     wait (0.5);
     pi.left(0.5);
     wait (0.5);
     pi.backward(0.5);
     wait (0.5);
     pi.right(0.5);
     wait (0.5);

     pi.stop();

 }

Definition at line 88 of file m3pimaze.h.


Constructor & Destructor Documentation

m3pi ( PinName  nrst,
PinName  tx,
PinName  rx 
)

Create the m3pi object connected to the default pins.

Parameters:
nrstGPIO pin used for reset. Default is p8
txSerial transmit pin. Default is p9
rxSerial receive pin. Default is p10

Definition at line 27 of file m3pimaze.cpp.


Member Function Documentation

void backward ( float  speed )

Drive both motors backward as the same speed.

Parameters:
speedA normalised number 0 - 1.0 represents the full range.

Definition at line 53 of file m3pimaze.cpp.

float battery ( void   )

Read the battery voltage on the 3pi.

Returns:
battery voltage as a float

Definition at line 92 of file m3pimaze.cpp.

void calibrate ( void   )

Set calibration manually to the current settings.

Definition at line 116 of file m3pimaze.cpp.

void cls ( void   )

Clear the LCD.

Definition at line 156 of file m3pimaze.cpp.

void forward ( float  speed )

Drive both motors forward as the same speed.

Parameters:
speedA normalised number 0 - 1.0 represents the full range.

Definition at line 48 of file m3pimaze.cpp.

int getc ( void   )

Receive a character directly to the 3pi serial interface.

Returns:
c The character received from the 3pi

Definition at line 194 of file m3pimaze.cpp.

void leds ( int  val )

Write to the 8 LEDs.

Parameters:
ledsAn 8 bit value to put on the LEDs

Definition at line 145 of file m3pimaze.cpp.

void left ( float  speed )

Drive left motor backwards and right motor forwards at the same speed to turn on the spot.

Parameters:
speedA normalised number 0 - 1.0 represents the full range.

Definition at line 58 of file m3pimaze.cpp.

void left_motor ( float  speed )

Directly control the speed and direction of the left motor.

Parameters:
speedA normalised number -1.0 - 1.0 represents the full range.

Definition at line 40 of file m3pimaze.cpp.

float line_position ( void   )

Read the position of the detected line.

Returns:
position as A normalised number -1.0 - 1.0 represents the full range.

Definition at line 100 of file m3pimaze.cpp.

void locate ( int  x,
int  y 
)

Locate the cursor on the 8x2 LCD.

Parameters:
xThe horizontal position, from 0 to 7
yThe vertical position, from 0 to 1

Definition at line 150 of file m3pimaze.cpp.

int playtune ( char *  text,
int  length 
)

Play a tune.

Definition at line 169 of file m3pimaze.cpp.

float pot_voltage ( void   )

Read the voltage of the potentiometer on the 3pi.

Returns:
voltage as a float

Definition at line 136 of file m3pimaze.cpp.

int print ( char *  text,
int  length 
)

Send a string buffer to the 3pi serial interface.

Parameters:
textA pointer to a char array
intThe character to send to the 3pi

Definition at line 160 of file m3pimaze.cpp.

int putc ( int  c )

Send a character directly to the 3pi serial interface.

Parameters:
cThe character to send to the 3pi

Definition at line 190 of file m3pimaze.cpp.

void readsensor ( int *  sensor )

Read the calibrated value of a sensor.

Parameters:
sensorPointer to array sensor [0-4]
valuesreturned in the range 0-1000 where 1000=completely dark

Definition at line 198 of file m3pimaze.cpp.

void reset ( void   )

Force a hardware reset of the 3pi.

Definition at line 33 of file m3pimaze.cpp.

void reset_calibration ( void   )

Clear the current calibration settings.

Definition at line 120 of file m3pimaze.cpp.

void right ( float  speed )

Drive left motor forward and right motor backwards at the same speed to turn on the spot.

Parameters:
speedA normalised number 0 - 1.0 represents the full range.

Definition at line 63 of file m3pimaze.cpp.

void right_motor ( float  speed )

Directly control the speed and direction of the right motor.

Parameters:
speedA normalised number -1.0 - 1.0 represents the full range.

Definition at line 44 of file m3pimaze.cpp.

char sensor_auto_calibrate ( void   )

Calibrate the sensors.

This turns the robot left then right, loking for a line

Definition at line 110 of file m3pimaze.cpp.

void stop ( void   )

Stop both motors.

Definition at line 68 of file m3pimaze.cpp.