m3pi RPC
m3pi with Remote Procedure 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.
USB Serial RPC¶
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 mbed 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
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
Bluetooth Serial RPC¶
This is essentially the same as the USB Serial except we are now accepting the Serial connection from a RN42 Bluetooth module
Import program
00001 #include "mbed.h" 00002 #include "m3pi.h" 00003 #include "SerialRPCInterface.h" 00004 00005 // RN42 module defaults to 115,200 and is connected on p28,p27 00006 SerialRPCInterface Interface(p28, p27, 115200); 00007 00008 m3pi m3pi; 00009 00010 int main() { 00011 00012 m3pi.locate(0,1); 00013 m3pi.printf("Blutooth"); 00014 00015 // do nothing, just wait for RPC comands 00016 while(1) {} 00017 }
Pairing the m3pi¶
These instructions are how to pair Windows XP with the RN42 Bluetooth module.
- Switch on the m3pi that has a Bluetooth module (RN-42) fitted, it is now ready to be paired with the laptop.
- Open the “Bluetooth Devices” control box, usually found in the system tray:
- Run through the "Add device" process
- The last four digits of the device name are the last four digits of the MAC address in this case 0974
- When prompted, enter the passkey for the device, which is “1234”
- When the pairing is complete, two new COM ports will be created. COM45 will be used for our serial communication
Once the m3pi is paired, the mbed microcontroller needs to be programmed to accept commands over Bluetooth. Plug the mbed on the m3pi into your computer, then import, compile and download the "m3pi-BluetoothRPC" program above onto the m3pi. When you reset the mbed, the m3pi LCD screen should now read "m3pi Blutooth"
Bluetooth Applications¶
- Bluetooth Racing - A python app "BluetoothRacing.exe" that pairs with the m3pi's COM port, and enables the arrow keys to control the m3pi. As this uses RPC over a COM port, it would work over USB serial too, it's not as much fun though!