Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m3pi.h
00001 #ifndef M3PI_H 00002 #define M3PI_H 00003 00004 #include "mbed.h" 00005 #include "platform.h" 00006 00007 #ifdef MBED_RPC 00008 #include "rpc.h" 00009 #endif 00010 00011 #define SEND_RAW_SENSOR_VALUES 0x86 00012 #define SEND_CALIBRATED_SENSOR_VALUES 0x87 00013 #define SEND_BATTERY_MILLIVOLTS 0xB1 00014 #define DO_PRINT 0xB8 00015 #define M1_FORWARD 0xC1 00016 #define M1_BACKWARD 0xC2 00017 #define M2_FORWARD 0xC5 00018 #define M2_BACKWARD 0xC6 00019 #define AUTO_CALIBRATE 0xBA 00020 00021 namespace physcom { 00022 00023 /** m3pi control class 00024 * 00025 */ 00026 class M3pi : public Stream { 00027 00028 private: 00029 float m1_speed; 00030 float m2_speed; 00031 00032 // Public functions 00033 public: 00034 00035 /** Create the m3pi object connected to the default pins 00036 * 00037 * @param nrst GPIO pin used for reset. Default is p23 00038 * @param tx Serial transmit pin. Default is p9 00039 * @param rx Serial receive pin. Default is p10 00040 */ 00041 M3pi(); 00042 00043 /** Create the m3pi object connected to specific pins 00044 * 00045 */ 00046 M3pi(PinName nrst, PinName tx, PinName rx); 00047 00048 /** Force a hardware reset of the 3pi 00049 */ 00050 void reset (void); 00051 00052 00053 /** This function will smoothly stop the motors of the robot by gradually 00054 * slowing down. 00055 * Contributed by Menno Vermeulen and Daniel Trujillo 00056 */ 00057 void stop(); 00058 00059 /** Activate motor 00060 * @param motor. Values: 0: Left; 1: Right 00061 * @param speed Float value representing the speed of the motor. Negative 00062 * values will turn the wheels in reverse. The possible values of this 00063 * parameter range between -1 and 1. The values are normalized, so speed=1 00064 * means the motor will activate at full (100%) power, while speed=0.2 would 00065 * activate the motor with 20% of its maximum power. 00066 * A good starting value can be speed=0.1. In most cases the speed value you 00067 * will use should be between -0.5 and 0.5. 00068 */ 00069 void activate_motor (int motor, float speed); 00070 00071 /** Reads the values of the active light sensors 00072 * @param sensors Array of 5 int elements that will be populated with 00073 * the sensor values 00074 */ 00075 void raw_sensors (int sensors[5]); 00076 00077 /** Reads the values of the active light sensors. The result values are 00078 * normalized in the range 0-1000 based on the calibration. The function 00079 * sensor_auto_calibrate() should be called once at the start of the program 00080 * before reading the calibrated values. 00081 * 00082 * @param sensors Array of 5 int elements that will be populated with 00083 * the sensor values 00084 */ 00085 void calibrated_sensors (int sensors[5]); 00086 00087 /** Read the battery voltage on the 3pi 00088 * @returns battery voltage as a float 00089 */ 00090 float battery(void); 00091 00092 00093 /** Calibrate the sensors. This turns the robot left then right, 00094 * looking for a line. Make sure that when this function is called, the 00095 * robot is positioned on the black line on white background. 00096 * The values of the calibration are storred internaly and used 00097 * when computing the normalized result values of calibrated_sensors(). 00098 */ 00099 char sensor_auto_calibrate(void); 00100 00101 private : 00102 00103 DigitalOut _nrst; 00104 Serial _ser; 00105 00106 virtual int _putc(int c); 00107 virtual int _getc(); 00108 00109 }; 00110 00111 00112 } // Namespace physcom 00113 00114 #endif
Generated on Sun Jul 17 2022 07:37:17 by
1.7.2